Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ziti/cmd/pki/pki_create_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func (o *PKICreateClientOptions) Run() error {
}

// Concat the newly-created client cert with the intermediate cert to create a client.chain.pem file
if err := o.Flags.PKI.Chain(signer, req); err != nil {
if err := o.Flags.PKI.Chain(signer, req, o.Flags.AllowOverwrite); err != nil {
return errors.Wrap(err, "unable to generate cert chain")
}

Expand Down
2 changes: 1 addition & 1 deletion ziti/cmd/pki/pki_create_intermediate.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func (o *PKICreateIntermediateOptions) Run() error {
}

// Concat the newly-created intermediate cert with the signing cert to create an intermediate.chain.pem file
if err := o.Flags.PKI.Chain(signer, req); err != nil {
if err := o.Flags.PKI.Chain(signer, req, false); err != nil {
return errors.Wrap(err, "unable to generate cert chain")
}

Expand Down
2 changes: 1 addition & 1 deletion ziti/cmd/pki/pki_create_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func (o *PKICreateServerOptions) Run() error {
}

// Concat the newly-created server cert with the intermediate cert to create a server.chain.pem file
if err := o.Flags.PKI.Chain(signer, req); err != nil {
if err := o.Flags.PKI.Chain(signer, req, o.Flags.AllowOverwrite); err != nil {
return errors.Wrap(err, "unable to generate cert chain")
}

Expand Down
6 changes: 3 additions & 3 deletions ziti/pki/pki/pki.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,13 @@ func publicKeyFromPrivate(key crypto.PrivateKey) (crypto.PublicKey, error) {
return nil, errors.New("unsupported key type")
}

// Chain will...
func (e *ZitiPKI) Chain(signer *certificate.Bundle, req *Request) error {
// Chain concats a signing cert and a newly signed certificate and stores the chained PEM.
func (e *ZitiPKI) Chain(signer *certificate.Bundle, req *Request, allowOverwrite bool) error {
destCA := signer.Name
if req.Template.IsCA {
destCA = req.Name
}
if err := e.Store.Chain(signer.Name, destCA, req.Name); err != nil {
if err := e.Store.Chain(signer.Name, destCA, req.Name, allowOverwrite); err != nil {
return fmt.Errorf("failed saving generated chain: %v", err)
}
return nil
Expand Down
23 changes: 13 additions & 10 deletions ziti/pki/store/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,14 @@ func (l *Local) path(caName, name string) (key string, cert string) {

// Exists checks if a certificate or private key already exist on the local
// filesystem for a given name.
// Exists returns true when a complete bundle (both key and cert) exists.
// A key without a cert (e.g., created separately via "ziti pki create key")
// is not a complete bundle and should not block certificate creation.
func (l *Local) Exists(caName, name string) bool {
privPath, certPath := l.path(caName, name)
if _, err := os.Stat(privPath); err == nil {
return true
}
if _, err := os.Stat(certPath); err == nil {
return true
}
return false
_, keyErr := os.Stat(privPath)
_, certErr := os.Stat(certPath)
return keyErr == nil && certErr == nil
}

// Fetch fetches the private key and certificate for a given name signed by caName.
Expand Down Expand Up @@ -135,10 +134,14 @@ func (l *Local) Add(caName, name string, isCa bool, key, cert []byte, allowOverw
}

// Chain concats an intermediate cert and a newly signed certificate bundle and adds the chained cert to the store.
func (l *Local) Chain(caName, destCaName, name string) error {
// Chain concats an intermediate cert and a newly signed certificate bundle and adds the chained cert to the store.
func (l *Local) Chain(caName, destCaName, name string, allowOverwrite bool) error {
chainName := name + ".chain.pem"
if l.Exists(destCaName, chainName) {
return fmt.Errorf("a bundle already exists for the name %v within CA %v", chainName, destCaName)
if !allowOverwrite {
chainPath := filepath.Join(l.Root, destCaName, "certs", chainName)
if _, err := os.Stat(chainPath); err == nil {
return fmt.Errorf("a bundle already exists for the name %v within CA %v", chainName, destCaName)
}
}
if err := l.writeChainBundle(caName, destCaName, name, chainName); err != nil {
return fmt.Errorf("failed writing chain %v to the local filesystem: %v", chainName, err)
Expand Down
3 changes: 2 additions & 1 deletion ziti/pki/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ type Store interface {
// The signing CA name.
// The destination CA name.
// The certificate bundle name.
// Flag indicating if existing chain can be overwritten.
//
// Returns an error if it failed to store the bundle.
Chain(string, string, string) error
Chain(string, string, string, bool) error

// AddCSR adds a CSR to the store.
//
Expand Down
Loading