-
Notifications
You must be signed in to change notification settings - Fork 102
/
resource_artifactory_certificate.go
284 lines (241 loc) · 6.79 KB
/
resource_artifactory_certificate.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
package artifactory
import (
"context"
"crypto/sha256"
"crypto/x509"
"encoding/hex"
"encoding/pem"
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/go-resty/resty/v2"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const endpoint = "artifactory/api/system/security/certificates/"
// CertificateDetails this type doesn't even exist in the new go client. In fact, the whole API call doesn't
type CertificateDetails struct {
CertificateAlias string `json:"certificateAlias,omitempty"`
IssuedTo string `json:"issuedTo,omitempty"`
IssuedBy string `json:"issuedby,omitempty"`
IssuedOn string `json:"issuedOn,omitempty"`
ValidUntil string `json:"validUntil,omitempty"`
FingerPrint string `json:"fingerPrint,omitempty"`
}
func resourceArtifactoryCertificate() *schema.Resource {
return &schema.Resource{
Create: resourceCertificateCreate,
Read: resourceCertificateRead,
Update: resourceCertificateUpdate,
Delete: resourceCertificateDelete,
Exists: resourceCertificateExists,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"alias": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"content": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
ValidateFunc: validation.All(
validation.StringIsNotEmpty,
func(value interface{}, key string) ([]string, []error) {
_, err := extractCertificate(value.(string))
if err != nil {
return nil, []error{err}
}
return nil, nil
},
),
},
"file": {
Type: schema.TypeString,
Sensitive: true,
Optional: true,
ExactlyOneOf: []string{"content", "file"},
ValidateFunc: func(value interface{}, key string) ([]string, []error) {
var errors []error
if _, err := os.Stat(value.(string)); err != nil {
return nil, append(errors, err)
}
data, err := ioutil.ReadFile(value.(string))
if err != nil {
return nil, append(errors, err)
}
_, err = extractCertificate(string(data))
if err != nil {
return nil, append(errors, err)
}
return nil, nil
},
},
"fingerprint": {
Type: schema.TypeString,
Computed: true,
},
"issued_by": {
Type: schema.TypeString,
Computed: true,
},
"issued_on": {
Type: schema.TypeString,
Computed: true,
},
"issued_to": {
Type: schema.TypeString,
Computed: true,
},
"valid_until": {
Type: schema.TypeString,
Computed: true,
},
},
CustomizeDiff: calculateFingerprint,
}
}
func calculateFingerprint(_ context.Context, d *schema.ResourceDiff, _ interface{}) error {
content, err := getContentFromDiff(d)
fingerprint, err := calculateFingerPrint(content)
if err != nil {
return err
}
if d.Get("fingerprint").(string) != fingerprint {
if err = d.SetNewComputed("fingerprint"); err != nil {
fmt.Println(err)
return err
}
}
return nil
}
func formatFingerPrint(f []byte) string {
buf := make([]byte, 0, 3*len(f))
x := buf[1*len(f) : 3*len(f)]
hex.Encode(x, f)
for i := 0; i < len(x); i += 2 {
buf = append(buf, x[i], x[i+1], ':')
}
return strings.ToUpper(string(buf[:len(buf)-1]))
}
func extractCertificate(pemData string) (*x509.Certificate, error) {
block, rest := pem.Decode([]byte(pemData))
for block != nil {
if block.Type != "CERTIFICATE" {
block, rest = pem.Decode(rest)
continue
}
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, err
}
return cert, nil
}
return nil, fmt.Errorf("no certificate in PEM data")
}
func calculateFingerPrint(pemData string) (string, error) {
cert, err := extractCertificate(pemData)
if err != nil {
return "", err
}
fingerprint := sha256.Sum256(cert.Raw)
return formatFingerPrint(fingerprint[:]), nil
}
func findCertificate(alias string, m interface{}) (*CertificateDetails, error) {
c := m.(*resty.Client)
certificates := new([]CertificateDetails)
_, err := c.R().SetResult(certificates).Get(endpoint)
if err != nil {
return nil, err
}
// No way other than to loop through each certificate
for _, cert := range *certificates {
if cert.CertificateAlias == alias {
return &cert, nil
}
}
return nil, nil
}
func resourceCertificateCreate(d *schema.ResourceData, m interface{}) error {
d.SetId(d.Get("alias").(string))
return resourceCertificateUpdate(d, m)
}
func resourceCertificateRead(d *schema.ResourceData, m interface{}) error {
cert, err := findCertificate(d.Id(), m)
if err != nil {
return err
}
if cert != nil {
setValue := mkLens(d)
setValue("alias", (*cert).CertificateAlias)
setValue("fingerprint", (*cert).FingerPrint)
setValue("issued_by", (*cert).IssuedBy)
setValue("issued_on", (*cert).IssuedOn)
setValue("issued_to", (*cert).IssuedTo)
errors := setValue("valid_until", (*cert).ValidUntil)
if errors != nil && len(errors) > 0 {
return fmt.Errorf("failed to pack certificate %q", errors)
}
return nil
}
d.SetId("")
return nil
}
func getContentFromDiff(d *schema.ResourceDiff) (string, error) {
content, contentExists := d.GetOkExists("content")
file, fileExists := d.GetOkExists("file")
if contentExists == fileExists {
return "", fmt.Errorf("you must define 'content' as the contents of the pem file, OR set 'file' to the path of your pem file ")
}
if contentExists {
return content.(string), nil
}
if fileExists {
data, err := ioutil.ReadFile(file.(string))
if err != nil {
return "", err
}
return string(data), nil
}
return "", fmt.Errorf("mmm, couldn't get content or file. You need either a content or a file")
}
func getContentFromData(d *schema.ResourceData) (string, error) {
if content, ok := d.GetOkExists("content"); ok {
return content.(string), nil
}
if file, ok := d.GetOkExists("file"); ok {
data, err := ioutil.ReadFile(file.(string))
if err != nil {
return "", err
}
return string(data), nil
}
return "", fmt.Errorf("mmm, couldn't get content or file. You need either a content or a file")
}
func resourceCertificateUpdate(d *schema.ResourceData, m interface{}) error {
content, err := getContentFromData(d)
if err != nil {
return err
}
_, err = m.(*resty.Client).R().SetBody(content).SetHeader("content-type", "text/plain").Post(endpoint + d.Id())
if err != nil {
return err
}
return resourceCertificateRead(d, m)
}
func resourceCertificateDelete(d *schema.ResourceData, m interface{}) error {
_, err := m.(*resty.Client).R().Delete(endpoint + d.Id())
if err != nil {
return err
}
d.SetId("")
return nil
}
func resourceCertificateExists(d *schema.ResourceData, m interface{}) (bool, error) {
cert, err := findCertificate(d.Id(), m)
return err == nil && cert != nil, err
}