Skip to content

Commit

Permalink
Make duration configurable
Browse files Browse the repository at this point in the history
Signed-off-by: Tamal Saha <[email protected]>
  • Loading branch information
tamalsaha committed Feb 29, 2024
1 parent cd5c010 commit 1b8d807
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
14 changes: 11 additions & 3 deletions cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ import (
"math"
"math/big"
"net"
"path"
"os"
"path"
"strings"
"time"
)
Expand All @@ -49,6 +49,14 @@ type Config struct {
Organization []string
AltNames AltNames
Usages []x509.ExtKeyUsage
Duration time.Duration
}

func (cfg Config) GetDuration() time.Duration {
if cfg.Duration > 0 {
return cfg.Duration
}
return duration365d
}

// AltNames contains the domain names and IP addresses that will be added
Expand All @@ -74,7 +82,7 @@ func NewSelfSignedCACert(cfg Config, key crypto.Signer) (*x509.Certificate, erro
Organization: cfg.Organization,
},
NotBefore: now.UTC(),
NotAfter: now.Add(duration365d * 10).UTC(),
NotAfter: now.Add(cfg.GetDuration() * 10).UTC(),
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
BasicConstraintsValid: true,
IsCA: true,
Expand Down Expand Up @@ -109,7 +117,7 @@ func NewSignedCert(cfg Config, key crypto.Signer, caCert *x509.Certificate, caKe
IPAddresses: cfg.AltNames.IPs,
SerialNumber: serial,
NotBefore: caCert.NotBefore,
NotAfter: time.Now().Add(duration365d).UTC(),
NotAfter: time.Now().Add(cfg.GetDuration()).UTC(),
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
ExtKeyUsage: cfg.Usages,
}
Expand Down
37 changes: 35 additions & 2 deletions certstore/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"path"
"strings"
"time"

"github.com/pkg/errors"
"gomodules.xyz/blobfs"
Expand Down Expand Up @@ -41,12 +42,19 @@ type CertStore struct {
organization []string
prefix string
ca string
duration time.Duration
caKey *rsa.PrivateKey
caCert *x509.Certificate
}

func New(fs blobfs.Interface, dir string, organization ...string) (*CertStore, error) {
return &CertStore{fs: fs, dir: dir, ca: "ca", organization: append([]string(nil), organization...)}, nil
func New(fs blobfs.Interface, dir string, duration time.Duration, organization ...string) *CertStore {
return &CertStore{
fs: fs,
dir: dir,
ca: "ca",
duration: duration,
organization: append([]string(nil), organization...),
}
}

func (s *CertStore) InitCA(prefix ...string) error {
Expand Down Expand Up @@ -138,6 +146,7 @@ func (s *CertStore) createCAFromKey(key *rsa.PrivateKey) error {
DNSNames: []string{s.ca},
IPs: []net.IP{net.ParseIP("127.0.0.1")},
},
Duration: s.duration,
}
crt, err := cert.NewSelfSignedCACert(cfg, key)
if err != nil {
Expand Down Expand Up @@ -211,6 +220,7 @@ func (s *CertStore) NewServerCertPair(sans cert.AltNames) (*x509.Certificate, *r
Organization: s.organization,
AltNames: sans,
Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
Duration: s.duration,
}
key, err := cert.NewPrivateKey()
if err != nil {
Expand All @@ -231,6 +241,27 @@ func (s *CertStore) NewServerCertPairBytes(sans cert.AltNames) ([]byte, []byte,
return cert.EncodeCertPEM(crt), cert.EncodePrivateKeyPEM(key), nil
}

func (cs *CertStore) GetServerCertPair(name string, sans cert.AltNames) (*x509.Certificate, *rsa.PrivateKey, error) {
crt, key, err := cs.Read(name)
if err != nil || time.Until(crt.NotAfter) < 10*time.Minute {
crt, key, err := cs.NewServerCertPair(sans)
if err != nil {
return nil, nil, err
}
err = cs.Write(name, crt, key)
return crt, key, err
}
return crt, key, nil
}

func (s *CertStore) GetServerCertPairBytes(name string, sans cert.AltNames) ([]byte, []byte, error) {
crt, key, err := s.GetServerCertPair(name, sans)
if err != nil {
return nil, nil, err
}
return cert.EncodeCertPEM(crt), cert.EncodePrivateKeyPEM(key), nil
}

// NewPeerCertPair is used to create cert pair that can serve as both server and client.
// This is used to issue peer certificates for etcd.
func (s *CertStore) NewPeerCertPair(sans cert.AltNames) (*x509.Certificate, *rsa.PrivateKey, error) {
Expand All @@ -239,6 +270,7 @@ func (s *CertStore) NewPeerCertPair(sans cert.AltNames) (*x509.Certificate, *rsa
Organization: s.organization,
AltNames: sans,
Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth},
Duration: s.duration,
}
key, err := cert.NewPrivateKey()
if err != nil {
Expand All @@ -265,6 +297,7 @@ func (s *CertStore) NewClientCertPair(sans cert.AltNames, organization ...string
Organization: organization,
AltNames: sans,
Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
Duration: s.duration,
}
key, err := cert.NewPrivateKey()
if err != nil {
Expand Down

0 comments on commit 1b8d807

Please sign in to comment.