-
Notifications
You must be signed in to change notification settings - Fork 63
/
main.go
336 lines (307 loc) · 10.1 KB
/
main.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
/*
* Copyright 2018 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/sha1"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"github.com/Venafi/vcert/v4"
"github.com/Venafi/vcert/v4/pkg/certificate"
"github.com/Venafi/vcert/v4/pkg/endpoint"
"github.com/Venafi/vcert/v4/pkg/venafi/tpp"
"io/ioutil"
t "log"
"math/big"
"net"
"os"
"strings"
"time"
)
func main() {
if len(os.Args) != 2 || os.Args[1] == "" {
t.Fatalf("Usage: ./$0 common.name.venafi.example.com")
}
var commonName = os.Args[1]
//
// 0. Get client instance based on connection config
//
config := tppConfig
//config := cloudConfig
//config := mockConfig
c, err := vcert.NewClient(config)
if err != nil {
t.Fatalf("could not connect to endpoint: %s", err)
}
//
// 1.1. Compose request object
//
//Not all Venafi Cloud providers support IPAddress and EmailAddresses extensions.
var enrollReq = &certificate.Request{}
switch {
case config.ConnectorType == endpoint.ConnectorTypeTPP || config.ConnectorType == endpoint.ConnectorTypeFake:
enrollReq = &certificate.Request{
Subject: pkix.Name{
CommonName: commonName,
Organization: []string{"Venafi.com"},
OrganizationalUnit: []string{"Integration Team"},
Locality: []string{"Salt Lake"},
Province: []string{"Salt Lake"},
Country: []string{"US"},
},
DNSNames: []string{"www.client.venafi.example.com", "ww1.client.venafi.example.com"},
EmailAddresses: []string{"e1@venafi.example.com", "e2@venafi.example.com"},
IPAddresses: []net.IP{net.IPv4(127, 0, 0, 1), net.IPv4(127, 0, 0, 2)},
CsrOrigin: certificate.LocalGeneratedCSR,
KeyType: certificate.KeyTypeRSA,
KeyLength: 2048,
ChainOption: certificate.ChainOptionRootLast,
KeyPassword: "newPassw0rd!",
//Before setting custom field in request you need to configure custom field on TPP
CustomFields: []certificate.CustomField{
{Name: "custom", Value: "2019-12-10"},
},
}
case config.ConnectorType == endpoint.ConnectorTypeCloud:
enrollReq = &certificate.Request{
Subject: pkix.Name{
CommonName: commonName,
Organization: []string{"Venafi.com"},
OrganizationalUnit: []string{"Integration Team"},
Locality: []string{"Salt Lake"},
Province: []string{"Salt Lake"},
Country: []string{"US"},
},
DNSNames: []string{"www.client.venafi.example.com", "ww1.client.venafi.example.com"},
CsrOrigin: certificate.LocalGeneratedCSR,
KeyType: certificate.KeyTypeRSA,
KeyLength: 2048,
ChainOption: certificate.ChainOptionRootLast,
KeyPassword: "newPassw0rd!",
}
}
//
// 1.2. Generate private key and certificate request (CSR) based on request's options
//
err = c.GenerateRequest(nil, enrollReq)
if err != nil {
t.Fatalf("could not generate certificate request: %s", err)
}
//
// 1.3. Submit certificate request, get request ID as a response
//
requestID, err := c.RequestCertificate(enrollReq)
if err != nil {
t.Fatalf("could not submit certificate request: %s", err)
}
t.Printf("Successfully submitted certificate request. Will pickup certificate by ID %s", requestID)
//
// 1.4. Retrieve certificate using request ID obtained on previous step, get PEM collection as a response
//
pickupReq := &certificate.Request{
PickupID: requestID,
Timeout: 180 * time.Second,
}
pcc, err := c.RetrieveCertificate(pickupReq)
if err != nil {
t.Fatalf("could not retrieve certificate using requestId %s: %s", requestID, err)
}
//
// 1.5. (optional) Add certificate's private key to PEM collection
//
_ = pcc.AddPrivateKey(enrollReq.PrivateKey, []byte(enrollReq.KeyPassword))
t.Printf("Successfully picked up certificate for %s", commonName)
pp(pcc)
//
// 2.1. Compose renewal object
//
renewReq := &certificate.RenewalRequest{
// certificate is identified using DN
CertificateDN: requestID,
// ..or SHA1 Thumbprint
// Thumbprint: "",
//CertificateRequest: certificate.Request{}
}
//
// 2.2. Submit renewal request
//
newRequestID, err := c.RenewCertificate(renewReq)
if err != nil {
t.Fatalf("could not submit certificate renewal request: %s", err)
}
t.Printf("Successfully submitted certificate renewal request. Will pickup certificate by ID %s", newRequestID)
//
// 2.3. Retrieve certificate using request ID obtained on previous step, get PEM collection as a response
//
renewRetrieveReq := &certificate.Request{
PickupID: newRequestID,
Timeout: 180 * time.Second,
}
pcc2, err := c.RetrieveCertificate(renewRetrieveReq)
if err != nil {
t.Fatalf("could not retrieve certificate using requestId %s: %s", requestID, err)
}
t.Printf("Successfully retrieved renewed certificate for %s", commonName)
t.Printf("Old serial number %s", getSerial(pcc.Certificate))
t.Printf("New serial number %s", getSerial(pcc2.Certificate))
//
// 3.1. Compose revocation object
//
revokeReq := &certificate.RevocationRequest{
CertificateDN: requestID,
Reason: "key-compromise",
Comments: "revocation comment below",
Disable: false,
}
//
// 3.2. Submit revocation request (not supported in Venafi Cloud)
//
if config.ConnectorType != endpoint.ConnectorTypeCloud {
err = c.RevokeCertificate(revokeReq)
if err != nil {
t.Fatalf("could not submit certificate revocation request: %s", err)
}
t.Printf("Successfully submitted revocation request for %s", requestID)
}
//
// 4. Import certificate to another object of the same Zone
//
var importReq = &certificate.ImportRequest{}
switch {
case config.ConnectorType == endpoint.ConnectorTypeTPP || config.ConnectorType == endpoint.ConnectorTypeFake:
importObjectName := fmt.Sprintf("%s-imported", commonName)
importReq = &certificate.ImportRequest{
// if PolicyDN is empty, it is taken from cfg.Zone
ObjectName: importObjectName,
CertificateData: pcc.Certificate,
PrivateKeyData: pcc.PrivateKey,
Password: "newPassw0rd!",
Reconcile: false,
}
case config.ConnectorType == endpoint.ConnectorTypeCloud:
importObjectName := fmt.Sprintf("%s-imported", commonName)
importReq = &certificate.ImportRequest{
// if PolicyDN is empty, it is taken from cfg.Zone
ObjectName: importObjectName,
CertificateData: pcc.Certificate,
PrivateKeyData: "",
Reconcile: false,
}
}
importResp, err := c.ImportCertificate(importReq)
if err != nil {
t.Fatalf("could not import certificate: %s", err)
}
pp(importReq)
pp(importResp)
t.Printf("Successfully imported certificate to %s", importResp.CertificateDN)
//
// 5. Retrieve certificate & key from new object
//
var importedRetriveReq = &certificate.Request{}
switch {
case config.ConnectorType == endpoint.ConnectorTypeTPP || config.ConnectorType == endpoint.ConnectorTypeFake:
importedRetriveReq = &certificate.Request{
PickupID: importResp.CertificateDN,
Timeout: 180 * time.Second,
KeyPassword: "newPassw0rd!",
FetchPrivateKey: true,
}
case config.ConnectorType == endpoint.ConnectorTypeCloud:
//You can retrieve imported certificate by thumbprint or certificate Id.
thumbprint := calcThumbprint(pcc.Certificate)
importedRetriveReq = &certificate.Request{
Thumbprint: thumbprint,
//CertID: importResp.CertId,
Timeout: 180 * time.Second,
}
}
pcc3, err := c.RetrieveCertificate(importedRetriveReq)
if err != nil {
t.Fatalf("could not retrieve certificate using requestId %s: %s", requestID, err)
}
t.Printf("Successfully retrieved imported certificate from %s", importResp.CertificateDN)
pp(pcc3)
//
// 6. Get refresh token and refresh access token
//
if config.ConnectorType == endpoint.ConnectorTypeTPP {
var connectionTrustBundle *x509.CertPool
trustBundleFilePath := os.Getenv("TRUST_BUNDLE_PATH")
if trustBundleFilePath != "" {
buf, err := ioutil.ReadFile(trustBundleFilePath)
if err != nil {
panic(err)
}
connectionTrustBundle = x509.NewCertPool()
if !connectionTrustBundle.AppendCertsFromPEM(buf) {
panic("Failed to parse PEM trust bundle")
}
}
tppConnector, err := tpp.NewConnector(config.BaseUrl, "", false, connectionTrustBundle)
if err != nil {
t.Fatalf("could not create TPP connector: %s", err)
}
resp, err := tppConnector.GetRefreshToken(&endpoint.Authentication{
User: os.Getenv("TPP_USER"),
Password: os.Getenv("TPP_PASSWORD"),
Scope: "certificate:manage,revoke;", ClientId: "websdk"})
if err != nil {
panic(err)
}
fmt.Printf("Refresh token is %s", resp.Refresh_token)
auth := &endpoint.Authentication{RefreshToken: resp.Refresh_token, ClientId: "websdk"}
err = tppConnector.Authenticate(auth)
if err != nil {
t.Fatalf("err is not nil, err: %s", err)
}
}
//
// 7. Audit certificates list in zone
//
_l := 10
certList, err := c.ListCertificates(endpoint.Filter{Limit: &_l})
if err != nil {
t.Fatal(err)
}
fmt.Println("ID Common Name Expire")
for _, cert := range certList {
validTo := cert.ValidTo.String()
if cert.ValidTo.Before(time.Now()) {
validTo = fmt.Sprintf("\033[1;31m%s\033[0m", validTo)
}
fmt.Printf("%v %v %v\n", cert.ID, cert.CN, validTo)
}
}
func getSerial(crt string) *big.Int {
block, _ := pem.Decode([]byte(crt))
if block == nil || block.Type != "CERTIFICATE" {
t.Fatalf("could not get PEM certificate block")
}
newCert, err := x509.ParseCertificate([]byte(block.Bytes))
if err != nil {
t.Fatalf("could not parse x509 certificate: %s", err)
}
return newCert.SerialNumber
}
func calcThumbprint(cert string) string {
p, _ := pem.Decode([]byte(cert))
h := sha1.New()
h.Write(p.Bytes)
buf := h.Sum(nil)
return strings.ToUpper(fmt.Sprintf("%x", buf))
}