-
Notifications
You must be signed in to change notification settings - Fork 299
/
Copy pathtools_cert.go
208 lines (176 loc) · 5.69 KB
/
tools_cert.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
// Copyright 2020 Nokia
// Licensed under the BSD 3-Clause License.
// SPDX-License-Identifier: BSD-3-Clause
package cmd
import (
"fmt"
"os"
"path/filepath"
"time"
"github.com/charmbracelet/log"
"github.com/spf13/cobra"
"github.com/srl-labs/containerlab/cert"
"github.com/srl-labs/containerlab/types"
"github.com/srl-labs/containerlab/utils"
)
var (
commonName string
country string
locality string
organization string
organizationUnit string
expiry string
path string
caNamePrefix string
certNamePrefix string
certHosts []string
caCertPath string
caKeyPath string
keySize int
)
func init() {
toolsCmd.AddCommand(certCmd)
certCmd.AddCommand(CACmd)
certCmd.AddCommand(signCertCmd)
CACmd.AddCommand(CACreateCmd)
CACreateCmd.Flags().StringVarP(&commonName, "cn", "", "containerlab.dev", "Common Name")
CACreateCmd.Flags().StringVarP(&country, "country", "c", "Internet", "Country")
CACreateCmd.Flags().StringVarP(&locality, "locality", "l", "Server", "Location")
CACreateCmd.Flags().StringVarP(&organization, "organization", "o", "Containerlab", "Organization")
CACreateCmd.Flags().StringVarP(&organizationUnit, "ou", "", "Containerlab Tools", "Organization Unit")
CACreateCmd.Flags().StringVarP(&expiry, "expiry", "e", "87600h", "certificate validity period")
CACreateCmd.Flags().StringVarP(&path, "path", "p", "",
"path to write certificate and key to. Default is current working directory")
CACreateCmd.Flags().StringVarP(&caNamePrefix, "name", "n", "ca", "certificate/key filename prefix")
signCertCmd.Flags().StringSliceVarP(&certHosts, "hosts", "", []string{},
"comma separate list of hosts of a certificate")
signCertCmd.Flags().StringVarP(&commonName, "cn", "", "containerlab.dev", "Common Name")
signCertCmd.Flags().StringVarP(&caCertPath, "ca-cert", "", "", "Path to CA certificate")
signCertCmd.Flags().StringVarP(&caKeyPath, "ca-key", "", "", "Path to CA private key")
signCertCmd.Flags().StringVarP(&country, "country", "c", "Internet", "Country")
signCertCmd.Flags().StringVarP(&locality, "locality", "l", "Server", "Location")
signCertCmd.Flags().StringVarP(&organization, "organization", "o", "Containerlab", "Organization")
signCertCmd.Flags().StringVarP(&organizationUnit, "ou", "", "Containerlab Tools", "Organization Unit")
signCertCmd.Flags().StringVarP(&path, "path", "p", "",
"path to write certificate and key to. Default is current working directory")
signCertCmd.Flags().StringVarP(&certNamePrefix, "name", "n", "cert", "certificate/key filename prefix")
signCertCmd.Flags().IntVarP(&keySize, "key-size", "", 2048, "private key size")
}
var certCmd = &cobra.Command{
Use: "cert",
Short: "TLS certificate operations",
}
var CACmd = &cobra.Command{
Use: "ca",
Short: "certificate authority operations",
}
var CACreateCmd = &cobra.Command{
Use: "create",
Short: "create ca certificate and key",
RunE: createCA,
}
var signCertCmd = &cobra.Command{
Use: "sign",
Short: "create and sign certificate",
RunE: signCert,
}
// createCA creates a new CA certificate and key and writes them to the specified path.
func createCA(_ *cobra.Command, _ []string) error {
var err error
if path == "" {
path, err = os.Getwd()
if err != nil {
return err
}
}
log.Infof("Certificate attributes: CN=%s, C=%s, L=%s, O=%s, OU=%s, Validity period=%s",
commonName, country, locality, organization, organizationUnit, expiry)
ca := cert.NewCA()
expDuration, err := time.ParseDuration(expiry)
if err != nil {
return fmt.Errorf("failed parsing expiry %s", expiry)
}
csrInput := &cert.CACSRInput{
CommonName: commonName,
Country: country,
Locality: locality,
Organization: organization,
OrganizationUnit: organizationUnit,
Expiry: expDuration,
KeySize: keySize,
}
caCert, err := ca.GenerateCACert(csrInput)
if err != nil {
return err
}
utils.CreateDirectory(path, 0777) // skipcq: GSC-G302
err = caCert.Write(
filepath.Join(path, caNamePrefix+types.CertFileSuffix),
filepath.Join(path, caNamePrefix+types.KeyFileSuffix),
"",
)
if err != nil {
return err
}
return nil
}
// signCert creates node certificate and sign it with CA.
func signCert(_ *cobra.Command, _ []string) error {
var err error
if path == "" {
path, err = os.Getwd()
if err != nil {
return err
}
}
ca := cert.NewCA()
var caCert *cert.Certificate
log.Debugf("CA cert path: %q", caCertPath)
if caCertPath != "" {
// TODO: we might also honor the External CA env vars here
caCert, err = cert.NewCertificateFromFile(caCertPath, caKeyPath, "")
if err != nil {
return err
}
}
// set loaded certificate to a CA and initialize a signer
err = ca.SetCACert(caCert)
if err != nil {
return err
}
log.Info("Creating and signing certificate",
"Hosts", certHosts,
"CN", commonName,
"C", country,
"L", locality,
"O", organization,
"OU", organizationUnit,
)
expDuration, err := time.ParseDuration(expiry)
if err != nil {
return fmt.Errorf("failed parsing expiry %s", expiry)
}
nodeCert, err := ca.GenerateAndSignNodeCert(
&cert.NodeCSRInput{
Hosts: certHosts,
CommonName: commonName,
Country: country,
Locality: locality,
Organization: organization,
OrganizationUnit: organizationUnit,
Expiry: expDuration,
KeySize: keySize,
})
if err != nil {
return err
}
utils.CreateDirectory(path, 0777) // skipcq: GSC-G302
err = nodeCert.Write(
filepath.Join(path, certNamePrefix+types.CertFileSuffix),
filepath.Join(path, certNamePrefix+types.KeyFileSuffix),
filepath.Join(path, certNamePrefix+types.CSRFileSuffix))
if err != nil {
return err
}
return nil
}