-
Notifications
You must be signed in to change notification settings - Fork 402
/
certificate_authority.go
220 lines (183 loc) · 5.5 KB
/
certificate_authority.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"crypto/x509"
"fmt"
"os"
"path/filepath"
"github.com/spf13/cobra"
"storj.io/common/cfgstruct"
"storj.io/common/identity"
"storj.io/common/peertls/extensions"
"storj.io/common/process"
"storj.io/storj/private/revocation"
)
var (
caCmd = &cobra.Command{
Use: "certificate-authority",
Short: "Manage certificate authorities",
Annotations: map[string]string{"type": "setup"},
}
newCACmd = &cobra.Command{
Use: "create",
Short: "Create a new certificate authority",
RunE: cmdNewCA,
Annotations: map[string]string{"type": "setup"},
}
getIDCmd = &cobra.Command{
Use: "id",
Short: "Get the id of a CA",
RunE: cmdGetID,
Annotations: map[string]string{"type": "setup"},
}
caExtCmd = &cobra.Command{
Use: "extensions [service]",
Short: "Prints the extensions attached to the identity CA certificate",
RunE: cmdCAExtensions,
Args: cobra.MaximumNArgs(1),
Annotations: map[string]string{"type": "setup"},
}
revokeCACmd = &cobra.Command{
Use: "revoke",
Short: "Revoke the identity's CA certificate (creates backup)",
RunE: cmdRevokeCA,
Annotations: map[string]string{"type": "setup"},
}
revokePeerCACmd = &cobra.Command{
Use: "revoke-peer [service] [revoked cert path]",
Short: "Revoke a peer identity's CA certificate and add to local revocation database",
Args: cobra.MaximumNArgs(2),
RunE: cmdRevokePeerCA,
Annotations: map[string]string{"type": "setup"},
}
newCACfg struct {
CA identity.CASetupConfig
}
getIDCfg struct {
CA identity.PeerCAConfig
}
caExtCfg struct {
CA identity.FullCAConfig
}
revokeCACfg struct {
CA identity.FullCAConfig
// TODO: add "broadcast" option to send revocation to network nodes
}
revokePeerCACfg struct {
CA identity.FullCAConfig
PeerCA identity.PeerCAConfig
RevocationDBURL string
}
)
func init() {
// NB: init functions are executed in lexicographical order of filename
identityDirParam := cfgstruct.FindIdentityDirParam()
if identityDirParam != "" {
defaultIdentityDir = identityDirParam
}
confDirParam := cfgstruct.FindConfigDirParam()
if confDirParam != "" {
defaultConfigDir = confDirParam
}
rootCmd.PersistentFlags().StringVar(&configDir, "config-dir", defaultConfigDir, "service config directory")
rootCmd.PersistentFlags().StringVar(&identityDir, "identity-dir", defaultIdentityDir, "root directory for identity output")
rootCmd.AddCommand(caCmd)
caCmd.AddCommand(newCACmd)
caCmd.AddCommand(getIDCmd)
caCmd.AddCommand(caExtCmd)
caCmd.AddCommand(revokeCACmd)
caCmd.AddCommand(revokePeerCACmd)
process.Bind(newCACmd, &newCACfg, defaults, cfgstruct.IdentityDir(defaultIdentityDir))
process.Bind(getIDCmd, &getIDCfg, defaults, cfgstruct.IdentityDir(defaultIdentityDir))
process.Bind(caExtCmd, &caExtCfg, defaults, cfgstruct.IdentityDir(defaultIdentityDir))
process.Bind(revokeCACmd, &revokeCACfg, defaults, cfgstruct.IdentityDir(defaultIdentityDir))
process.Bind(revokePeerCACmd, &revokePeerCACfg, defaults, cfgstruct.ConfDir(defaultConfigDir), cfgstruct.IdentityDir(defaultIdentityDir))
}
func cmdNewCA(cmd *cobra.Command, args []string) error {
ctx, _ := process.Ctx(cmd)
_, err := newCACfg.CA.Create(ctx, os.Stdout)
return err
}
func cmdGetID(cmd *cobra.Command, args []string) (err error) {
p, err := getIDCfg.CA.Load()
if err != nil {
return err
}
fmt.Printf("base58-check node ID:\t%s\n", p.ID)
fmt.Printf("hex node ID:\t\t%x\n", p.ID)
fmt.Printf("node ID bytes:\t\t%v\n", p.ID[:])
difficulty, err := p.ID.Difficulty()
if err == nil {
fmt.Printf("difficulty:\t\t%d\n", difficulty)
}
return nil
}
func cmdRevokeCA(cmd *cobra.Command, args []string) (err error) {
ca, err := revokeCACfg.CA.Load()
if err != nil {
return err
}
// NB: backup original cert
if err := revokeCACfg.CA.SaveBackup(ca); err != nil {
return err
}
if err := ca.Revoke(); err != nil {
return err
}
updateCfg := identity.FullCAConfig{
CertPath: revokeCACfg.CA.CertPath,
}
if err := updateCfg.Save(ca); err != nil {
return err
}
return nil
}
func cmdRevokePeerCA(cmd *cobra.Command, args []string) (err error) {
ctx, _ := process.Ctx(cmd)
if len(args) > 0 {
revokePeerCACfg.CA = identity.FullCAConfig{
CertPath: filepath.Join(identityDir, args[0], "ca.cert"),
KeyPath: filepath.Join(identityDir, args[0], "ca.key"),
}
revokePeerCACfg.RevocationDBURL = "bolt://" + filepath.Join(configDir, args[0], "revocations.db")
}
if len(args) > 1 {
revokePeerCACfg.PeerCA = identity.PeerCAConfig{
CertPath: args[1],
}
}
ca, err := revokePeerCACfg.CA.Load()
if err != nil {
return err
}
peerCA, err := revokePeerCACfg.PeerCA.Load()
if err != nil {
return err
}
ext, err := extensions.NewRevocationExt(ca.Key, peerCA.Cert)
if err != nil {
return err
}
revDB, err := revocation.OpenDB(ctx, revokePeerCACfg.RevocationDBURL)
if err != nil {
return err
}
if err = revDB.Put(ctx, []*x509.Certificate{ca.Cert, peerCA.Cert}, ext); err != nil {
return err
}
return nil
}
func cmdCAExtensions(cmd *cobra.Command, args []string) (err error) {
if len(args) > 0 {
caExtCfg.CA = identity.FullCAConfig{
CertPath: filepath.Join(identityDir, args[0], "ca.cert"),
KeyPath: filepath.Join(identityDir, args[0], "ca.key"),
}
}
ca, err := caExtCfg.CA.Load()
if err != nil {
return err
}
return printExtensions(ca.Cert.Raw, ca.Cert.Extensions)
}