generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 40
/
ssh_credential.go
544 lines (449 loc) · 15 KB
/
ssh_credential.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
package secrets
import (
"context"
"errors"
"github.com/harness/harness-go-sdk/harness/cd/graphql"
"github.com/harness/terraform-provider-harness/internal"
"github.com/harness/terraform-provider-harness/internal/service/cd/usagescope"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
var validAuthenticationMethods = []string{
"ssh_authentication",
"kerberos_authentication",
}
var validSSHAuthenticationTypes = []string{
"ssh_authentication.0.inline_ssh",
"ssh_authentication.0.ssh_key_file",
"ssh_authentication.0.server_password",
}
var validTGTGenerationMethods = []string{
"kerberos_authentication.0.tgt_generation_method.0.kerberos_password_id",
"kerberos_authentication.0.tgt_generation_method.0.key_tab_file_path",
}
func ResourceSSHCredential() *schema.Resource {
return &schema.Resource{
Description: "Resource for creating an encrypted text secret",
CreateContext: resourceSSHCredentialCreateOrUpdate,
ReadContext: resourceSSHCredentialRead,
UpdateContext: resourceSSHCredentialCreateOrUpdate,
DeleteContext: resourceSSHCredentialDelete,
Schema: map[string]*schema.Schema{
"id": {
Description: "Id of the encrypted text secret",
Type: schema.TypeString,
Computed: true,
},
"name": {
Description: "Name of the encrypted text secret",
Type: schema.TypeString,
Required: true,
},
"ssh_authentication": {
Description: "Authentication method for SSH. Cannot be used if kerberos_authentication is specified. Only one of `inline_ssh`, `server_password`, or `ssh_key_file` should be set",
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
ExactlyOneOf: validAuthenticationMethods,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"port": {
Description: "The port to connect to",
Type: schema.TypeInt,
Required: true,
},
"username": {
Description: "The username to use when connecting to ssh",
Type: schema.TypeString,
Required: true,
},
"inline_ssh": {
Description: "Inline SSH authentication configuration. Only ond of `passphrase_secret_id` or `ssh_key_file_id` should be used",
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
ExactlyOneOf: validSSHAuthenticationTypes,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"passphrase_secret_id": {
Description: "The id of the encrypted secret to use",
Type: schema.TypeString,
Optional: true,
},
"ssh_key_file_id": {
Description: "The id of the secret containing the SSH key",
Type: schema.TypeString,
Required: true,
},
},
},
},
"ssh_key_file": {
Description: "Use ssh key file for authentication",
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
ExactlyOneOf: validSSHAuthenticationTypes,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"path": {
Description: "The path to the key file on the delegate",
Type: schema.TypeString,
Required: true,
},
"passphrase_secret_id": {
Description: "The id of the secret containing the password to use for the ssh key",
Type: schema.TypeString,
Optional: true,
},
},
},
},
"server_password": {
Description: "Server password authentication configuration",
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
ExactlyOneOf: validSSHAuthenticationTypes,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"password_secret_id": {
Description: "The id of the encrypted secret",
Type: schema.TypeString,
Required: true,
},
},
},
},
},
},
},
"kerberos_authentication": {
Description: "Kerberos authentication for SSH. Cannot be used if ssh_authentication is specified",
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
ExactlyOneOf: validAuthenticationMethods,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"port": {
Description: "Port to use for Kerberos authentication",
Type: schema.TypeInt,
Required: true,
ForceNew: true,
},
"principal": {
Description: "Name of the principal for authentication",
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"realm": {
Description: "Realm associated with the Kerberos authentication",
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"tgt_generation_method": {
Description: "TGT generation method",
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"kerberos_password_id": {
Description: "The id of the encrypted text secret",
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ExactlyOneOf: validTGTGenerationMethods,
},
"key_tab_file_path": {
Description: "The path to the key tab file",
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ExactlyOneOf: validTGTGenerationMethods,
},
},
},
},
},
},
},
"usage_scope": usagescope.Schema(),
},
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
}
}
func resourceSSHCredentialCreateOrUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*internal.Session)
var input *graphql.SSHCredential
var err error
if d.IsNewResource() {
input = &graphql.SSHCredential{}
} else {
if input, err = c.CDClient.SecretClient.GetSSHCredentialById(d.Id()); err != nil {
return diag.FromErr(err)
} else if input == nil {
d.SetId("")
d.MarkNewResource()
return nil
}
}
input.Name = d.Get("name").(string)
if err := expandAuthenticationScheme(d, input); err != nil {
return diag.FromErr(err)
}
usageScope, err := usagescope.ExpandUsageScope(d.Get("usage_scope").(*schema.Set).List())
if err != nil {
return diag.FromErr(err)
}
input.UsageScope = usageScope
cred, err := c.CDClient.SecretClient.CreateSSHCredential(input)
if err != nil {
return diag.FromErr(err)
}
return readSSHCredential(d, cred)
}
func resourceSSHCredentialRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*internal.Session)
credId := d.Get("id").(string)
cred, err := c.CDClient.SecretClient.GetSSHCredentialById(credId)
if err != nil {
return diag.FromErr(err)
}
if cred == nil {
d.SetId("")
d.MarkNewResource()
return nil
}
return readSSHCredential(d, cred)
}
func readSSHCredential(d *schema.ResourceData, secret *graphql.SSHCredential) diag.Diagnostics {
d.SetId(secret.Id)
d.Set("name", secret.Name)
if sshAuth := flattenSSHAuthentication(secret.SSHAuthentication); len(sshAuth) > 0 {
d.Set("ssh_authentication", sshAuth)
}
if kerberosAuth := flattenKerberosAuthentication(secret.KerberosAuthentication); len(kerberosAuth) > 0 {
d.Set("kerberos_authentication", kerberosAuth)
}
if usageScope := usagescope.FlattenUsageScope(secret.UsageScope); len(usageScope) > 0 {
d.Set("usage_scope", usageScope)
}
return nil
}
func resourceSSHCredentialDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*internal.Session)
err := c.CDClient.SecretClient.DeleteSecret(d.Get("id").(string), graphql.SecretTypes.SSHCredential)
if err != nil {
return diag.FromErr(err)
}
return nil
}
func flattenKerberosAuthentication(cred *graphql.KerberosAuthentication) []interface{} {
response := make([]interface{}, 0)
if cred == nil {
return response
}
data := map[string]interface{}{}
data["port"] = cred.Port
data["principal"] = cred.Principal
data["realm"] = cred.Realm
data["tgt_generation_method"] = flattenTGTGenerationMethod(cred.TGTGenerationMethod)
return response
}
func flattenTGTGenerationMethod(tgt *graphql.TGTGenerationMethod) []interface{} {
response := make([]interface{}, 0)
if tgt == nil {
return response
}
data := map[string]interface{}{}
if tgt.KeyTabFile != nil {
data["key_tab_file_path"] = tgt.KeyTabFile.FilePath
}
if tgt.KerberosPassword != nil {
data["kerberos_password_id"] = tgt.KerberosPassword.PasswordSecretId
}
return response
}
func flattenSSHAuthentication(cred *graphql.SSHAuthentication) []interface{} {
response := make([]interface{}, 0)
if cred == nil {
return response
}
data := map[string]interface{}{}
data["port"] = cred.Port
data["username"] = cred.Username
if cred.SSHAuthenticationMethod != nil {
switch cred.SSHAuthenticationMethod.SSHCredentialType {
case graphql.SSHCredentialTypes.Password:
data["server_password"] = flattenSSHServerPasswordConfig(cred.SSHAuthenticationMethod.ServerPassword)
case graphql.SSHCredentialTypes.SSHKeyFilePath:
data["ssh_key_file"] = flattenSSHKeyFileConfig(cred.SSHAuthenticationMethod.SSHKeyFile)
case graphql.SSHCredentialTypes.SSHKey:
data["inline_ssh"] = flattenInlineSSHConfig(cred.SSHAuthenticationMethod.InlineSSHKey)
}
}
return append(response, data)
}
func flattenInlineSSHConfig(cred *graphql.InlineSSHKey) []interface{} {
response := make([]interface{}, 0)
if cred == nil {
return response
}
data := map[string]interface{}{}
data["passphrase_secret_id"] = cred.PassphraseSecretId
data["ssh_key_file_id"] = cred.SSHKeySecretFileId
return append(response, data)
}
func flattenSSHKeyFileConfig(cred *graphql.SSHKeyFile) []interface{} {
response := make([]interface{}, 0)
if cred == nil {
return response
}
data := map[string]interface{}{}
data["path"] = cred.Path
data["passphrase_secret_id"] = cred.PassphraseSecretId
return append(response, data)
}
func flattenSSHServerPasswordConfig(cred *graphql.SSHPassword) []interface{} {
response := make([]interface{}, 0)
if cred == nil {
return response
}
data := map[string]interface{}{}
data["password_secret_id"] = cred.PasswordSecretId
return append(response, data)
}
func expandAuthenticationScheme(d *schema.ResourceData, cred *graphql.SSHCredential) error {
if err := expandSSHAuthentication(d.Get("ssh_authentication").([]interface{}), cred); err != nil {
return err
}
if err := expandKerberosAuthentication(d.Get("kerberos_authentication").([]interface{}), cred); err != nil {
return err
}
return nil
}
func expandSSHAuthentication(d []interface{}, cred *graphql.SSHCredential) error {
if len(d) <= 0 {
cred.SSHAuthentication = nil
return nil
}
data := d[0].(map[string]interface{})
auth := &graphql.SSHAuthentication{
SSHAuthenticationMethod: &graphql.SSHAuthenticationMethod{},
}
if attr, ok := data["port"]; ok {
auth.Port = attr.(int)
}
if attr, ok := data["username"]; ok && attr != "" {
auth.Username = attr.(string)
}
expandInlineSSHConfig(data["inline_ssh"].([]interface{}), auth)
expandSSHKeyFileConfig(data["ssh_key_file"].([]interface{}), auth)
expandServerPasswordConfig(data["server_password"].([]interface{}), auth)
cred.AuthenticationScheme = graphql.SSHAuthenticationSchemes.SSH
cred.SSHAuthentication = auth
return nil
}
func expandServerPasswordConfig(d []interface{}, auth *graphql.SSHAuthentication) {
if len(d) <= 0 {
auth.SSHAuthenticationMethod.ServerPassword = nil
return
}
serverPasswordConfig := &graphql.SSHPassword{}
data := d[0].(map[string]interface{})
if attr, ok := data["password_secret_id"]; ok && attr != "" {
serverPasswordConfig.PasswordSecretId = attr.(string)
}
auth.SSHAuthenticationMethod.SSHCredentialType = graphql.SSHCredentialTypes.Password
auth.SSHAuthenticationMethod.ServerPassword = serverPasswordConfig
}
func expandSSHKeyFileConfig(d []interface{}, auth *graphql.SSHAuthentication) {
if len(d) <= 0 {
auth.SSHAuthenticationMethod.SSHKeyFile = nil
return
}
sshKeyConfig := &graphql.SSHKeyFile{}
data := d[0].(map[string]interface{})
if attr, ok := data["passphrase_secret_id"]; ok && attr != "" {
sshKeyConfig.PassphraseSecretId = attr.(string)
}
if attr, ok := data["path"]; ok && attr != "" {
sshKeyConfig.Path = attr.(string)
}
auth.SSHAuthenticationMethod.SSHCredentialType = graphql.SSHCredentialTypes.SSHKeyFilePath
auth.SSHAuthenticationMethod.SSHKeyFile = sshKeyConfig
}
func expandInlineSSHConfig(d []interface{}, auth *graphql.SSHAuthentication) {
if len(d) <= 0 {
auth.SSHAuthenticationMethod.InlineSSHKey = nil
return
}
inlineConfig := &graphql.InlineSSHKey{}
data := d[0].(map[string]interface{})
if attr, ok := data["passphrase_secret_id"]; ok && attr != "" {
inlineConfig.PassphraseSecretId = attr.(string)
}
if attr, ok := data["ssh_key_file_id"]; ok && attr != "" {
inlineConfig.SSHKeySecretFileId = attr.(string)
}
auth.SSHAuthenticationMethod.SSHCredentialType = graphql.SSHCredentialTypes.SSHKey
auth.SSHAuthenticationMethod.InlineSSHKey = inlineConfig
}
func expandKerberosAuthentication(d []interface{}, cred *graphql.SSHCredential) error {
if len(d) <= 0 {
cred.KerberosAuthentication = nil
return nil
}
data := d[0].(map[string]interface{})
auth := &graphql.KerberosAuthentication{}
if attr, ok := data["port"]; ok {
auth.Port = attr.(int)
}
if attr, ok := data["principal"]; ok && attr != "" {
auth.Principal = attr.(string)
}
if attr, ok := data["realm"]; ok && attr != "" {
auth.Realm = attr.(string)
}
expandTGTGenerationMethod(data["tgt_generation_method"].([]interface{}), auth)
if auth.TGTGenerationMethod != nil {
if auth.TGTGenerationMethod.KerberosPassword != nil && auth.TGTGenerationMethod.KeyTabFile != nil {
return errors.New("must set only one of `kerberos_password_id` or `key_tab_file_path` for tgt_generation_method")
}
}
cred.AuthenticationScheme = graphql.SSHAuthenticationSchemes.Kerberos
cred.KerberosAuthentication = auth
return nil
}
func expandTGTGenerationMethod(d []interface{}, auth *graphql.KerberosAuthentication) {
if len(d) <= 0 {
auth.TGTGenerationMethod = nil
return
}
details := &graphql.TGTGenerationMethod{}
data := d[0].(map[string]interface{})
if attr, ok := data["kerberos_password_id"]; ok && attr != "" {
details.TGTGenerationUsing = graphql.TGTGenerationUsingOptions.Password
details.KerberosPassword = &graphql.KerberosPassword{
PasswordSecretId: attr.(string),
}
}
if attr, ok := data["key_tab_file_path"]; ok && attr != "" {
details.TGTGenerationUsing = graphql.TGTGenerationUsingOptions.KeyTabFile
details.KeyTabFile = &graphql.KeyTabFile{
FilePath: attr.(string),
}
}
}