-
Notifications
You must be signed in to change notification settings - Fork 60
/
cmdHelper.go
386 lines (339 loc) · 11.2 KB
/
cmdHelper.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
* Copyright 2020-2024 Venafi, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"crypto/tls"
"crypto/x509"
"encoding/pem"
"fmt"
"net"
"net/http"
"net/url"
"os"
"strings"
"time"
"github.com/urfave/cli/v2"
"golang.org/x/crypto/pkcs12"
"github.com/Venafi/vcert/v5"
"github.com/Venafi/vcert/v5/pkg/certificate"
"github.com/Venafi/vcert/v5/pkg/endpoint"
"github.com/Venafi/vcert/v5/pkg/venafi"
"github.com/Venafi/vcert/v5/pkg/venafi/cloud"
"github.com/Venafi/vcert/v5/pkg/venafi/firefly"
"github.com/Venafi/vcert/v5/pkg/venafi/tpp"
)
var tlsConfig tls.Config
func runBeforeCommand(c *cli.Context) error {
//TODO: move all flag validations here
flags.orgUnits = c.StringSlice("ou")
flags.dnsSans = c.StringSlice("san-dns")
flags.emailSans = c.StringSlice("san-email")
flags.upnSans = c.StringSlice("san-upn")
flags.customFields = c.StringSlice("field")
flags.sshCertExtension = c.StringSlice("extension")
flags.sshCertPrincipal = c.StringSlice("principal")
flags.sshCertSourceAddrs = c.StringSlice("source-address")
flags.sshCertDestAddrs = c.StringSlice("destination-address")
noDuplicatedFlags := []string{"instance", "tls-address", "app-info"}
for _, f := range noDuplicatedFlags {
if len(c.StringSlice(f)) > 1 {
return fmt.Errorf("flag %s can not be duplicated", f)
} else if len(c.StringSlice(f)) == 1 {
switch f {
case "instance":
flags.instance = c.StringSlice(f)[0]
case "tls-address":
flags.tlsAddress = c.StringSlice(f)[0]
case "app-info":
flags.appInfo = c.StringSlice(f)[0]
}
}
}
for _, stringIP := range c.StringSlice("san-ip") {
ip := net.ParseIP(stringIP)
flags.ipSans = append(flags.ipSans, ip)
}
for _, stringURI := range c.StringSlice("san-uri") {
uri, _ := url.Parse(stringURI)
flags.uriSans = append(flags.uriSans, uri)
}
if flags.platformString != "" {
flags.platform = venafi.GetPlatformType(flags.platformString)
}
if flags.platform == venafi.Firefly {
if flags.scope != "" {
//The separator in scope flag is ";" but Firefly use " " as separator
flags.scope = strings.ReplaceAll(flags.scope, ";", " ")
}
if flags.csrOption == "" {
flags.csrOption = "service"
}
}
return nil
}
func setTLSConfig() error {
//Set RenegotiateFreelyAsClient in case of we're communicating with MTLS TPP server with only user\password
if flags.apiKey == "" {
tlsConfig.Renegotiation = tls.RenegotiateFreelyAsClient
}
if flags.insecure {
// We are ignoring the following from the linter, since from:
// golangci-lint version 1.51.2 built from 3e8facb4 on 2023-02-19T21:43:54Z
// it started failing due to error in this version of the linter
// #nosec G402: Look for bad TLS connection settings
tlsConfig.InsecureSkipVerify = true
}
if flags.clientP12 != "" {
// Load client PKCS#12 archive
p12, err := os.ReadFile(flags.clientP12)
if err != nil {
return fmt.Errorf("Error reading PKCS#12 archive file: %s", err)
}
blocks, err := pkcs12.ToPEM(p12, flags.clientP12PW)
if err != nil {
return fmt.Errorf("Error converting PKCS#12 archive file to PEM blocks: %s", err)
}
var pemData []byte
for _, b := range blocks {
pemData = append(pemData, pem.EncodeToMemory(b)...)
}
// Construct TLS certificate from PEM data
cert, err := tls.X509KeyPair(pemData, pemData)
if err != nil {
return fmt.Errorf("Error reading PEM data to build X.509 certificate: %s", err)
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(pemData)
// Setup HTTPS client
tlsConfig.Certificates = []tls.Certificate{cert}
tlsConfig.RootCAs = caCertPool
// nolint:staticcheck
tlsConfig.BuildNameToCertificate()
}
//Setting TLS configuration
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tlsConfig
return nil
}
func getTppCredentials(tppConnector *tpp.Connector, cfg *vcert.Config, clientP12 bool) error {
//TODO: quick workaround to suppress logs when output is in JSON.
if flags.credFormat != "json" {
logf("Getting credentials...")
}
if cfg.Credentials.RefreshToken != "" {
resp, err := tppConnector.RefreshAccessToken(&endpoint.Authentication{
RefreshToken: cfg.Credentials.RefreshToken,
ClientId: flags.clientId,
Scope: flags.scope,
})
if err != nil {
return err
}
if flags.credFormat == "json" {
if err := outputJSON(resp); err != nil {
return err
}
} else {
tm := time.Unix(int64(resp.Expires), 0).UTC().Format(time.RFC3339)
fmt.Println("access_token: ", resp.Access_token)
fmt.Println("access_token_expires: ", tm)
fmt.Println("refresh_token: ", resp.Refresh_token)
fmt.Println("refresh_until: ", time.Unix(int64(resp.Refresh_until), 0).UTC().Format(time.RFC3339))
}
} else if cfg.Credentials.User != "" && cfg.Credentials.Password != "" {
auth := &endpoint.Authentication{
User: cfg.Credentials.User,
Password: cfg.Credentials.Password,
Scope: flags.scope,
ClientId: flags.clientId}
if flags.sshCred {
auth.Scope = "ssh:manage"
} else if flags.pmCred {
auth.Scope = "certificate:manage,revoke;configuration:manage"
}
resp, err := tppConnector.GetRefreshToken(auth)
if err != nil {
return err
}
if flags.credFormat == "json" {
if err := outputJSON(resp); err != nil {
return err
}
} else {
tm := time.Unix(int64(resp.Expires), 0).UTC().Format(time.RFC3339)
fmt.Println("access_token: ", resp.Access_token)
fmt.Println("access_token_expires: ", tm)
if resp.Refresh_token != "" {
fmt.Println("refresh_token: ", resp.Refresh_token)
fmt.Println("refresh_until: ", time.Unix(int64(resp.Refresh_until), 0).UTC().Format(time.RFC3339))
}
}
} else if clientP12 {
resp, err := tppConnector.GetRefreshToken(&endpoint.Authentication{
ClientPKCS12: clientP12,
Scope: flags.scope,
ClientId: flags.clientId})
if err != nil {
return err
}
if flags.credFormat == "json" {
if err := outputJSON(resp); err != nil {
return err
}
} else {
tm := time.Unix(int64(resp.Expires), 0).UTC().Format(time.RFC3339)
fmt.Println("access_token: ", resp.Access_token)
fmt.Println("access_token_expires: ", tm)
if resp.Refresh_token != "" {
fmt.Println("refresh_token: ", resp.Refresh_token)
fmt.Println("refresh_until: ", time.Unix(int64(resp.Refresh_until), 0).UTC().Format(time.RFC3339))
}
}
} else {
return fmt.Errorf("failed to determine credentials set")
}
return nil
}
func getVaaSCredentials(vaasConnector *cloud.Connector, cfg *vcert.Config) error {
//TODO: quick workaround to suppress logs when output is in JSON.
if flags.credFormat != "json" {
logf("Getting credentials...")
}
// Register new account to VaaS
if cfg.Credentials.User != "" {
statusCode, userDetails, err := vaasConnector.CreateAPIUserAccount(cfg.Credentials.User, cfg.Credentials.Password)
if err != nil {
return fmt.Errorf("failed to create a User Account/rotate API Key in VaaS: %s", err)
}
apiKey := userDetails.APIKey
if flags.credFormat == "json" {
return outputJSON(apiKey)
} else {
var headerMessage string
if statusCode == http.StatusCreated {
headerMessage = "the user account was created successfully. To complete the registration please review your email account and follow the link."
} else if statusCode == http.StatusAccepted {
headerMessage = "the user account already exists therefore the API Key was rotated. To complete the activation of the rotated API Key," +
" please review your email account and follow the link."
} else { // only is expected that the status code returned is 201 or 202
return fmt.Errorf("unexpected http status code when the useraccount is tried to be created or api key rotated: %d", statusCode)
}
fmt.Println(headerMessage)
fmt.Println("api_key: ", apiKey.Key)
fmt.Println("api_key_expires: ", apiKey.ValidityEndDateString)
}
// Request access token
} else if cfg.Credentials.ExternalJWT != "" && cfg.Credentials.TokenURL != "" {
// Request access token from VaaS service account
tokenResponse, err := vaasConnector.GetAccessToken(cfg.Credentials)
if err != nil {
return fmt.Errorf("failed to request access token from VCP: %w", err)
}
if flags.credFormat == "json" {
return outputJSON(tokenResponse)
} else {
validityPeriod := time.Duration(tokenResponse.ExpiresIn) * time.Second
expirationDate := time.Now().Add(validityPeriod)
t := expirationDate.UTC().Format(time.RFC3339)
fmt.Println("access_token: ", tokenResponse.AccessToken)
fmt.Println("expires_in: ", t)
}
} else {
return fmt.Errorf("failed to determine credentials set")
}
return nil
}
func getFireflyCredentials(fireflyConnector *firefly.Connector, cfg *vcert.Config) error {
//TODO: quick workaround to suppress logs when output is in JSON.
if flags.credFormat != "json" {
logf("Getting credentials...")
}
token, err := fireflyConnector.Authorize(cfg.Credentials)
if err != nil {
return err
}
if flags.credFormat == "json" {
if err := outputJSON(token); err != nil {
return err
}
} else {
fmt.Println("access_token: ", token.AccessToken)
fmt.Println("refresh_token: ", token.RefreshToken)
fmt.Println("token_type: ", token.TokenType)
fmt.Println("access_token_expires: ", token.Expiry)
}
return nil
}
func generateCsrForCommandGenCsr(cf *commandFlags, privateKeyPass []byte) (privateKey []byte, csr []byte, err error) {
certReq := &certificate.Request{}
if cf.keyType != nil {
certReq.KeyType = *cf.keyType
}
certReq.KeyLength = cf.keySize
if cf.keyCurve != certificate.EllipticCurveNotSet {
certReq.KeyCurve = cf.keyCurve
}
err = certReq.GeneratePrivateKey()
if err != nil {
return
}
var pBlock *pem.Block
if len(privateKeyPass) == 0 {
pBlock, err = certificate.GetPrivateKeyPEMBock(certReq.PrivateKey)
if err != nil {
return
}
privateKey = pem.EncodeToMemory(pBlock)
} else {
pBlock, err = certificate.GetEncryptedPrivateKeyPEMBock(certReq.PrivateKey, privateKeyPass)
if err != nil {
return
}
privateKey = pem.EncodeToMemory(pBlock)
}
certReq = fillCertificateRequest(certReq, cf)
err = certReq.GenerateCSR()
if err != nil {
return
}
err = certReq.GeneratePrivateKey()
if err != nil {
return
}
csr = certReq.GetCSR()
return
}
func writeOutKeyAndCsr(commandName string, cf *commandFlags, key []byte, csr []byte) (err error) {
pcc := &certificate.PEMCollection{}
pcc.CSR = string(csr[:])
pcc.PrivateKey = string(key[:])
result := &Result{
Pcc: pcc,
PickupId: "",
Config: &Config{
Command: commandName,
Format: cf.csrFormat,
ChainOption: certificate.ChainOptionFromString(cf.chainOption),
AllFile: cf.file,
KeyFile: cf.keyFile,
CSRFile: cf.csrFile,
ChainFile: "",
PickupIdFile: "",
KeyPassword: cf.keyPassword,
},
}
err = result.Flush()
return
}