-
Notifications
You must be signed in to change notification settings - Fork 402
/
sign.go
94 lines (78 loc) · 1.71 KB
/
sign.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"crypto/x509"
"os"
"github.com/spf13/cobra"
"github.com/zeebo/errs"
"storj.io/common/identity"
)
var (
signCmd = &cobra.Command{
Use: "sign [signee identity-dir]",
Short: "Sign a CA and update corresponding CA and identity certificate chains",
Args: cobra.ExactArgs(1),
RunE: cmdSign,
}
signCfg struct {
SigneeCACfg identity.PeerCAConfig
SigneeIdentCfg identity.PeerConfig
// NB: defaults to same as CA
Signer identity.FullCAConfig
}
)
func cmdSign(cmd *cobra.Command, args []string) error {
ca, err := signCfg.SigneeCACfg.Load()
if err != nil {
return err
}
var (
signeeIdentityExists bool
ident *identity.PeerIdentity
)
_, err = os.Stat(signCfg.SigneeIdentCfg.CertPath)
if err != nil {
signeeIdentityExists = !os.IsNotExist(err)
if signeeIdentityExists {
return err
}
ident, err = signCfg.SigneeIdentCfg.Load()
if err != nil {
return err
}
}
signer, err := signCfg.Signer.Load()
if err != nil {
return err
}
restChain := []*x509.Certificate{signer.Cert}
// NB: backup ca and identity
err = signCfg.SigneeCACfg.SaveBackup(ca)
if err != nil {
return err
}
ca.Cert, err = signer.Sign(ca.Cert)
if err != nil {
return err
}
ca.RestChain = restChain
writeErrs := new(errs.Group)
err = signCfg.SigneeCACfg.Save(ca)
if err != nil {
writeErrs.Add(err)
}
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)
}
}
return writeErrs.Err()
}