Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
Signed-off-by: nyagamunene <[email protected]>
  • Loading branch information
nyagamunene committed Oct 14, 2024
1 parent 5f8e28c commit 5ee1a18
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 66 deletions.
18 changes: 1 addition & 17 deletions cmd/certs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
"gopkg.in/yaml.v2"
)

const (
Expand Down Expand Up @@ -101,7 +100,7 @@ func main() {
logger.Error(fmt.Sprintf("failed to load %s gRPC server configuration : %s", svcName, err))
}

config, err := LoadConfig(configFile)
config, err := certs.LoadConfig(configFile)
if err != nil {
logger.Error(fmt.Sprintf("failed to load CA config file : %s", err))
return
Expand Down Expand Up @@ -171,18 +170,3 @@ func initLogger(levelText string) (*slog.Logger, error) {

return slog.New(logHandler), nil
}

func LoadConfig(filename string) (*certs.Config, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()

var config certs.Config
decoder := yaml.NewDecoder(file)
if err := decoder.Decode(&config); err != nil {
return nil, err
}
return &config, nil
}
61 changes: 61 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0

package certs

import (
"net"
"os"

"gopkg.in/yaml.v2"
)

type CAConfig struct {
CommonName string `yaml:"common_name"`
Organization []string `yaml:"organization"`
OrganizationalUnit []string `yaml:"organizational_unit"`
Country []string `yaml:"country"`
Province []string `yaml:"province"`
Locality []string `yaml:"locality"`
StreetAddress []string `yaml:"street_address"`
PostalCode []string `yaml:"postal_code"`
DNSNames []string `yaml:"dns_names"`
IPAddresses []string `yaml:"ip_addresses"`
ValidityPeriod string `yaml:"validity_period"`
}

func LoadConfig(filename string) (*Config, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()

var config CAConfig
decoder := yaml.NewDecoder(file)
if err := decoder.Decode(&config); err != nil {
return nil, err
}
return &Config{
CommonName: config.CommonName,
Organization: config.Organization,
OrganizationalUnit: config.OrganizationalUnit,
Country: config.Country,
Province: config.Province,
Locality: config.Locality,
StreetAddress: config.StreetAddress,
PostalCode: config.PostalCode,
DNSNames: config.DNSNames,
IPAddresses: parseIPs(config.IPAddresses),
}, nil
}

func parseIPs(ipStrings []string) []net.IP {
var ips []net.IP
for _, ipString := range ipStrings {
if ip := net.ParseIP(ipString); ip != nil {
ips = append(ips, ip)
}
}
return ips
}
43 changes: 21 additions & 22 deletions docker/config.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
# Copyright (c) Abstract Machines
# SPDX-License-Identifier: Apache-2.0

ca:
common_name: "AbstractMachines_Selfsigned_ca"
organization:
- "AbstractMacines"
organizational_unit:
- "AbstractMachines_ca"
country:
- "France"
province:
- "Sirbea"
locality:
- "Sirbea"
street_address:
- "Sirbea"
postal_code:
- "Sirbea"
dns_names:
- "localhost"
ip_addresses:
- "192.168.100.4"
- "164.90.178.85"
validity_period: "8760h"
common_name: "AbstractMachines_Selfsigned_ca"
organization:
- "AbstractMacines"
organizational_unit:
- "AbstractMachines_ca"
country:
- "France"
province:
- "Sirbea"
locality:
- "Sirbea"
street_address:
- "Sirbea"
postal_code:
- "Sirbea"
dns_names:
- "localhost"
ip_addresses:
- "192.168.100.4"
- "164.90.178.85"
validity_period: "8760h"
38 changes: 11 additions & 27 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ type CA struct {
SerialNumber string
}

type CAConfig struct {
type Config struct {
CommonName string `yaml:"common_name"`
Organization []string `yaml:"organization"`
OrganizationalUnit []string `yaml:"organizational_unit"`
Expand All @@ -90,14 +90,10 @@ type CAConfig struct {
StreetAddress []string `yaml:"street_address"`
PostalCode []string `yaml:"postal_code"`
DNSNames []string `yaml:"dns_names"`
IPAddresses []string `yaml:"ip_addresses"`
IPAddresses []net.IP `yaml:"ip_addresses"`
ValidityPeriod string `yaml:"validity_period"`
}

type Config struct {
CA CAConfig `yaml:"ca"`
}

var (
serialNumberLimit = new(big.Int).Lsh(big.NewInt(1), 128)
ErrNotFound = errors.New("entity not found")
Expand Down Expand Up @@ -143,15 +139,13 @@ func NewService(ctx context.Context, repo Repository, config *Config) (Service,
}

// check if root ca should be rotated
rotateRoot := svc.shouldRotateCA(RootCA)
if rotateRoot {
if svc.shouldRotateCA(RootCA) {
if err := svc.rotateCA(ctx, RootCA, config); err != nil {
return &svc, err
}
}

rotateIntermediate := svc.shouldRotateCA(IntermediateCA)
if rotateIntermediate {
if svc.shouldRotateCA(IntermediateCA) {
if err := svc.rotateCA(ctx, IntermediateCA, config); err != nil {
return &svc, err
}
Expand Down Expand Up @@ -471,7 +465,7 @@ func (s *service) GetSigningCA(ctx context.Context, token string) (Certificate,
return cert, nil
}

func (s *service) generateRootCA(ctx context.Context, config CAConfig) (*CA, error) {
func (s *service) generateRootCA(ctx context.Context, config Config) (*CA, error) {
rootKey, err := rsa.GenerateKey(rand.Reader, PrivateKeyBytes)
if err != nil {
return nil, err
Expand Down Expand Up @@ -508,7 +502,7 @@ func (s *service) generateRootCA(ctx context.Context, config CAConfig) (*CA, err
BasicConstraintsValid: true,
IsCA: true,
DNSNames: config.DNSNames,
IPAddresses: parseIPs(config.IPAddresses),
IPAddresses: config.IPAddresses,
}

certBytes, err := x509.CreateCertificate(rand.Reader, certTemplate, certTemplate, &rootKey.PublicKey, rootKey)
Expand Down Expand Up @@ -547,7 +541,7 @@ func (s *service) saveCA(ctx context.Context, cert *x509.Certificate, privateKey
return nil
}

func (s *service) createIntermediateCA(ctx context.Context, rootCA *CA, config CAConfig) (*CA, error) {
func (s *service) createIntermediateCA(ctx context.Context, rootCA *CA, config Config) (*CA, error) {
intermediateKey, err := rsa.GenerateKey(rand.Reader, PrivateKeyBytes)
if err != nil {
return nil, err
Expand Down Expand Up @@ -584,7 +578,7 @@ func (s *service) createIntermediateCA(ctx context.Context, rootCA *CA, config C
BasicConstraintsValid: true,
IsCA: true,
DNSNames: config.DNSNames,
IPAddresses: parseIPs(config.IPAddresses),
IPAddresses: config.IPAddresses,
}

certBytes, err := x509.CreateCertificate(rand.Reader, &template, rootCA.Certificate, &intermediateKey.PublicKey, rootCA.PrivateKey)
Expand Down Expand Up @@ -653,12 +647,12 @@ func (s *service) rotateCA(ctx context.Context, ctype CertType, config *Config)
return err
}
}
newRootCA, err := s.generateRootCA(ctx, config.CA)
newRootCA, err := s.generateRootCA(ctx, *config)
if err != nil {
return err
}
s.rootCA = newRootCA
newIntermediateCA, err := s.createIntermediateCA(ctx, newRootCA, config.CA)
newIntermediateCA, err := s.createIntermediateCA(ctx, newRootCA, *config)
if err != nil {
return err
}
Expand All @@ -674,7 +668,7 @@ func (s *service) rotateCA(ctx context.Context, ctype CertType, config *Config)
return err
}
}
newIntermediateCA, err := s.createIntermediateCA(ctx, s.rootCA, config.CA)
newIntermediateCA, err := s.createIntermediateCA(ctx, s.rootCA, *config)
if err != nil {
return err
}
Expand Down Expand Up @@ -774,13 +768,3 @@ func (s *service) loadCACerts(ctx context.Context) error {
}
return nil
}

func parseIPs(ipStrings []string) []net.IP {
var ips []net.IP
for _, ipString := range ipStrings {
if ip := net.ParseIP(ipString); ip != nil {
ips = append(ips, ip)
}
}
return ips
}

0 comments on commit 5ee1a18

Please sign in to comment.