2019-01-11 14:59:35 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/x509"
|
2019-02-06 16:40:55 +00:00
|
|
|
"os"
|
2019-01-11 14:59:35 +00:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/identity"
|
2019-01-11 14:59:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
signCmd = &cobra.Command{
|
2019-02-06 16:40:55 +00:00
|
|
|
Use: "sign [signee identity-dir]",
|
2019-01-11 14:59:35 +00:00
|
|
|
Short: "Sign a CA and update corresponding CA and identity certificate chains",
|
2019-02-06 16:40:55 +00:00
|
|
|
Args: cobra.ExactArgs(1),
|
2019-01-11 14:59:35 +00:00
|
|
|
RunE: cmdSign,
|
|
|
|
}
|
|
|
|
|
|
|
|
signCfg struct {
|
2019-02-06 16:40:55 +00:00
|
|
|
SigneeCACfg identity.PeerCAConfig
|
|
|
|
SigneeIdentCfg identity.PeerConfig
|
2019-01-14 14:28:52 +00:00
|
|
|
// NB: defaults to same as CA
|
2019-01-11 14:59:35 +00:00
|
|
|
Signer identity.FullCAConfig
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func cmdSign(cmd *cobra.Command, args []string) error {
|
2019-02-06 16:40:55 +00:00
|
|
|
ca, err := signCfg.SigneeCACfg.Load()
|
2019-01-11 14:59:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-02-06 16:40:55 +00:00
|
|
|
var (
|
|
|
|
signeeIdentityExists bool
|
|
|
|
ident *identity.PeerIdentity
|
|
|
|
)
|
|
|
|
_, err = os.Stat(signCfg.SigneeIdentCfg.CertPath)
|
2019-01-11 14:59:35 +00:00
|
|
|
if err != nil {
|
2019-02-06 16:40:55 +00:00
|
|
|
signeeIdentityExists = !os.IsNotExist(err)
|
|
|
|
if signeeIdentityExists {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ident, err = signCfg.SigneeIdentCfg.Load()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-01-11 14:59:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
signer, err := signCfg.Signer.Load()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
restChain := []*x509.Certificate{signer.Cert}
|
|
|
|
|
|
|
|
// NB: backup ca and identity
|
2019-02-06 16:40:55 +00:00
|
|
|
err = signCfg.SigneeCACfg.SaveBackup(ca)
|
2019-01-11 14:59:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ca.Cert, err = signer.Sign(ca.Cert)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ca.RestChain = restChain
|
|
|
|
|
|
|
|
writeErrs := new(errs.Group)
|
2019-02-06 16:40:55 +00:00
|
|
|
err = signCfg.SigneeCACfg.Save(ca)
|
2019-01-11 14:59:35 +00:00
|
|
|
if err != nil {
|
|
|
|
writeErrs.Add(err)
|
|
|
|
}
|
|
|
|
|
2019-02-06 16:40:55 +00:00
|
|
|
if signeeIdentityExists {
|
|
|
|
err = signCfg.SigneeIdentCfg.SaveBackup(ident)
|
|
|
|
if err != nil {
|
|
|
|
writeErrs.Add(err)
|
|
|
|
}
|
|
|
|
ident.CA = ca.Cert
|
|
|
|
ident.RestChain = restChain
|
|
|
|
|
|
|
|
err = signCfg.SigneeIdentCfg.Save(ident)
|
|
|
|
if err != nil {
|
|
|
|
writeErrs.Add(err)
|
|
|
|
}
|
2019-01-11 14:59:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return writeErrs.Err()
|
|
|
|
}
|