generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 40
/
vault.go
349 lines (305 loc) · 10.4 KB
/
vault.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
package connector
import (
"context"
"github.com/harness/harness-go-sdk/harness/nextgen"
"github.com/harness/terraform-provider-harness/helpers"
"github.com/harness/terraform-provider-harness/internal/utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func ResourceConnectorVault() *schema.Resource {
resource := &schema.Resource{
Description: "Resource for creating a HashiCorp Vault Secret Manager connector.",
ReadContext: resourceConnectorVaultRead,
CreateContext: resourceConnectorVaultCreateOrUpdate,
UpdateContext: resourceConnectorVaultCreateOrUpdate,
DeleteContext: resourceConnectorDelete,
Importer: helpers.MultiLevelResourceImporter,
Schema: map[string]*schema.Schema{
"auth_token": {
Description: "Authentication token for Vault.",
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"base_path": {
Description: "Location of the Vault directory where the secret will be stored.",
Type: schema.TypeString,
Optional: true,
},
"vault_url": {
Description: "URL of the HashiCorp Vault.",
Type: schema.TypeString,
Required: true,
},
"is_read_only": {
Description: "Read only or not.",
Type: schema.TypeBool,
Optional: true,
},
"renewal_interval_minutes": {
Description: "The time interval for the token renewal.",
Type: schema.TypeInt,
Required: true,
},
"secret_engine_manually_configured": {
Description: "Manually entered Secret Engine.",
Type: schema.TypeBool,
Optional: true,
},
"secret_engine_name": {
Description: "Name of the Secret Engine.",
Type: schema.TypeString,
Optional: true,
},
"app_role_id": {
Description: "ID of App Role.",
Type: schema.TypeString,
Optional: true,
},
"secret_id": {
Description: "ID of the Secret.",
Type: schema.TypeString,
Optional: true,
},
"is_default": {
Description: "Is default or not.",
Type: schema.TypeBool,
Optional: true,
},
"secret_engine_version": {
Description: "Version of Secret Engine.",
Type: schema.TypeInt,
Optional: true,
},
"delegate_selectors": {
Description: "List of Delegate Selectors that belong to the same Delegate and are used to connect to the Secret Manager.",
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"namespace": {
Description: "Vault namespace where the Secret will be created.",
Type: schema.TypeString,
Optional: true,
},
"use_k8s_auth": {
Description: "Boolean value to indicate if K8s Auth is used for authentication.",
Type: schema.TypeBool,
Optional: true,
},
"sink_path": {
Description: "The location from which the authentication token should be read.",
Type: schema.TypeString,
Optional: true,
},
"use_vault_agent": {
Description: "Boolean value to indicate if Vault Agent is used for authentication.",
Type: schema.TypeBool,
Optional: true,
},
"use_aws_iam": {
Description: "Boolean value to indicate if AWS IAM is used for authentication.",
Type: schema.TypeBool,
Optional: true,
},
"aws_region": {
Description: "AWS region where the AWS IAM authentication will happen.",
Type: schema.TypeString,
Optional: true,
},
"vault_aws_iam_role": {
Description: "The Vault role defined to bind to aws iam account/role being accessed.",
Type: schema.TypeString,
Optional: true,
},
"xvault_aws_iam_server_id": {
Description: "The AWS IAM Header Server ID that has been configured for this AWS IAM instance.",
Type: schema.TypeString,
Optional: true,
},
"vault_k8s_auth_role": {
Description: "The role where K8s Auth will happen.",
Type: schema.TypeString,
Optional: true,
},
"service_account_token_path": {
Description: "The Service Account token path in the K8s pod where the token is mounted.",
Type: schema.TypeString,
Optional: true,
},
"k8s_auth_endpoint": {
Description: "The path where Kubernetes Auth is enabled in Vault.",
Type: schema.TypeString,
Optional: true,
},
"renew_app_role_token": {
Description: "Boolean value to indicate if AppRole token renewal is enabled or not.",
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
"access_type": {
Description: "Access type.",
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"APP_ROLE", "TOKEN", "VAULT_AGENT", "AWS_IAM", "K8s_AUTH"}, false),
},
"default": {
Description: "Is default or not.",
Type: schema.TypeBool,
Optional: true,
},
"read_only": {
Description: "Read only.",
Type: schema.TypeBool,
Optional: true,
},
},
}
helpers.SetMultiLevelResourceSchema(resource.Schema)
return resource
}
func resourceConnectorVaultRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn, err := resourceConnectorReadBase(ctx, d, meta, nextgen.ConnectorTypes.Vault)
if err != nil {
return err
}
if conn == nil {
return nil
}
if err := readConnectorVault(d, conn); err != nil {
return diag.FromErr(err)
}
return nil
}
func resourceConnectorVaultCreateOrUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := buildConnectorVault(d)
newConn, err := resourceConnectorCreateOrUpdateBase(ctx, d, meta, conn)
if err != nil {
return err
}
if err := readConnectorVault(d, newConn); err != nil {
return diag.FromErr(err)
}
return nil
}
func buildConnectorVault(d *schema.ResourceData) *nextgen.ConnectorInfo {
connector := &nextgen.ConnectorInfo{
Type_: nextgen.ConnectorTypes.Vault,
Vault: &nextgen.VaultConnector{},
}
if attr, ok := d.GetOk("auth_token"); ok {
connector.Vault.AuthToken = attr.(string)
}
if attr, ok := d.GetOk("base_path"); ok {
connector.Vault.BasePath = attr.(string)
}
if attr, ok := d.GetOk("vault_url"); ok {
connector.Vault.VaultUrl = attr.(string)
}
if attr, ok := d.GetOk("is_read_only"); ok {
connector.Vault.IsReadOnly = attr.(bool)
}
if attr, ok := d.GetOk("renewal_interval_minutes"); ok {
connector.Vault.RenewalIntervalMinutes = int64(attr.(int))
}
if attr, ok := d.GetOk("secret_engine_manually_configured"); ok {
connector.Vault.SecretEngineManuallyConfigured = attr.(bool)
}
if attr, ok := d.GetOk("secret_engine_name"); ok {
connector.Vault.SecretEngineName = attr.(string)
}
if attr, ok := d.GetOk("app_role_id"); ok {
connector.Vault.AppRoleId = attr.(string)
}
if attr, ok := d.GetOk("secret_id"); ok {
connector.Vault.SecretId = attr.(string)
}
if attr, ok := d.GetOk("is_default"); ok {
connector.Vault.IsDefault = attr.(bool)
}
if attr, ok := d.GetOk("secret_engine_version"); ok {
connector.Vault.SecretEngineVersion = int32(attr.(int))
}
if attr, ok := d.GetOk("delegate_selectors"); ok {
connector.Vault.DelegateSelectors = utils.InterfaceSliceToStringSlice(attr.(*schema.Set).List())
}
if attr, ok := d.GetOk("namespace"); ok {
connector.Vault.Namespace = attr.(string)
}
if attr, ok := d.GetOk("sink_path"); ok {
connector.Vault.SinkPath = attr.(string)
}
if attr, ok := d.GetOk("use_vault_agent"); ok {
connector.Vault.UseVaultAgent = attr.(bool)
}
if attr, ok := d.GetOk("use_aws_iam"); ok {
connector.Vault.UseAwsIam = attr.(bool)
}
if attr, ok := d.GetOk("aws_region"); ok {
connector.Vault.AwsRegion = attr.(string)
}
if attr, ok := d.GetOk("vault_aws_iam_role"); ok {
connector.Vault.VaultAwsIamRole = attr.(string)
}
if attr, ok := d.GetOk("xvault_aws_iam_server_id"); ok {
connector.Vault.XvaultAwsIamServerId = attr.(string)
}
if attr, ok := d.GetOk("use_k8s_auth"); ok {
connector.Vault.UseK8sAuth = attr.(bool)
}
if attr, ok := d.GetOk("vault_k8s_auth_role"); ok {
connector.Vault.VaultK8sAuthRole = attr.(string)
}
if attr, ok := d.GetOk("service_account_token_path"); ok {
connector.Vault.ServiceAccountTokenPath = attr.(string)
}
if attr, ok := d.GetOk("k8s_auth_endpoint"); ok {
connector.Vault.K8sAuthEndpoint = attr.(string)
}
if attr, ok := d.GetOk("renew_app_role_token"); ok {
connector.Vault.RenewAppRoleToken = attr.(bool)
}
if attr, ok := d.GetOk("access_type"); ok {
connector.Vault.AccessType = attr.(string)
}
if attr, ok := d.GetOk("default"); ok {
connector.Vault.Default_ = attr.(bool)
}
if attr, ok := d.GetOk("read_only"); ok {
connector.Vault.ReadOnly = attr.(bool)
}
return connector
}
func readConnectorVault(d *schema.ResourceData, connector *nextgen.ConnectorInfo) error {
d.Set("auth_token", connector.Vault.AuthToken)
d.Set("base_path", connector.Vault.BasePath)
d.Set("vault_url", connector.Vault.VaultUrl)
d.Set("is_read_only", connector.Vault.IsReadOnly)
d.Set("renewal_interval_minutes", connector.Vault.RenewalIntervalMinutes)
d.Set("secret_engine_manually_configured", connector.Vault.SecretEngineManuallyConfigured)
d.Set("secret_engine_name", connector.Vault.SecretEngineName)
d.Set("app_role_id", connector.Vault.AppRoleId)
d.Set("secret_id", connector.Vault.SecretId)
d.Set("is_default", connector.Vault.IsDefault)
d.Set("secret_engine_version", connector.Vault.SecretEngineVersion)
d.Set("delegate_selectors", connector.Vault.DelegateSelectors)
d.Set("namespace", connector.Vault.Namespace)
d.Set("sink_path", connector.Vault.SinkPath)
d.Set("use_vault_agent", connector.Vault.UseVaultAgent)
d.Set("use_aws_iam", connector.Vault.UseAwsIam)
d.Set("aws_region", connector.Vault.AwsRegion)
d.Set("vault_aws_iam_role", connector.Vault.VaultAwsIamRole)
d.Set("xvault_aws_iam_server_id", connector.Vault.XvaultAwsIamServerId)
d.Set("use_k8s_auth", connector.Vault.UseK8sAuth)
d.Set("vault_k8s_auth_role", connector.Vault.VaultK8sAuthRole)
d.Set("service_account_token_path", connector.Vault.ServiceAccountTokenPath)
d.Set("k8s_auth_endpoint", connector.Vault.K8sAuthEndpoint)
d.Set("renew_app_role_token", connector.Vault.RenewAppRoleToken)
d.Set("access_type", connector.Vault.AccessType)
d.Set("default", connector.Vault.Default_)
d.Set("read_only", connector.Vault.ReadOnly)
return nil
}