-
Notifications
You must be signed in to change notification settings - Fork 547
/
resource_aws_secret_backend_role.go
331 lines (296 loc) · 10.1 KB
/
resource_aws_secret_backend_role.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package vault
import (
"fmt"
"log"
"strconv"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-vault/internal/consts"
"github.com/hashicorp/terraform-provider-vault/internal/provider"
"github.com/hashicorp/terraform-provider-vault/util"
)
func awsSecretBackendRoleResource(name string) *schema.Resource {
return &schema.Resource{
Create: awsSecretBackendRoleWrite,
Read: provider.ReadWrapper(awsSecretBackendRoleRead),
Update: awsSecretBackendRoleWrite,
Delete: awsSecretBackendRoleDelete,
Exists: awsSecretBackendRoleExists,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Unique name for the role.",
},
"backend": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The path of the AWS Secret Backend the role belongs to.",
},
"policy_arns": {
Type: schema.TypeSet,
Optional: true,
Description: "ARN for an existing IAM policy the role should use.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"policy_document": {
Type: schema.TypeString,
Optional: true,
Description: "IAM policy the role should use in JSON format.",
ValidateFunc: ValidateDataJSONFunc(name),
DiffSuppressFunc: util.JsonDiffSuppress,
},
"credential_type": {
Type: schema.TypeString,
Required: true,
Description: "Role credential type.",
},
"role_arns": {
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
ForceNew: true,
Description: "ARNs of AWS roles allowed to be assumed. Only valid when credential_type is 'assumed_role'",
},
"iam_groups": {
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
Description: "A list of IAM group names. IAM users generated against this vault role will be added to these IAM Groups. For a credential type of assumed_role or federation_token, the policies sent to the corresponding AWS call (sts:AssumeRole or sts:GetFederation) will be the policies from each group in iam_groups combined with the policy_document and policy_arns parameters.",
},
"iam_tags": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "A map of strings representing key/value pairs used as tags for any IAM user created by this role.",
},
consts.FieldSessionTags: {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: fmt.Sprintf(`Session tags to be set for assume role creds created.`),
},
consts.FieldExternalID: {
Type: schema.TypeString,
Optional: true,
Description: "External ID to set for assume role creds.",
},
"default_sts_ttl": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: "The default TTL in seconds for STS credentials. When a TTL is not specified when STS credentials are requested, and a default TTL is specified on the role, then this default TTL will be used. Valid only when credential_type is one of assumed_role or federation_token.",
},
"max_sts_ttl": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: "The max allowed TTL in seconds for STS credentials (credentials TTL are capped to max_sts_ttl). Valid only when credential_type is one of assumed_role or federation_token.",
},
"permissions_boundary_arn": {
Type: schema.TypeString,
Optional: true,
Description: "The ARN of the AWS Permissions Boundary to attach to IAM users created in the role. Valid only when credential_type is iam_user. If not specified, then no permissions boundary policy will be attached.",
},
"user_path": {
Type: schema.TypeString,
Optional: true,
Description: "The path for the user name. Valid only when credential_type is iam_user. Default is /",
},
},
}
}
func awsSecretBackendRoleWrite(d *schema.ResourceData, meta interface{}) error {
client, e := provider.GetClient(d, meta)
if e != nil {
return e
}
backend := d.Get("backend").(string)
name := d.Get("name").(string)
policyARNsIfc, ok := d.GetOk("policy_arns")
var policyARNs []interface{}
if ok {
policyARNs = policyARNsIfc.(*schema.Set).List()
}
policyDocument := d.Get("policy_document")
roleARNs := d.Get("role_arns").(*schema.Set).List()
iamGroups := d.Get("iam_groups").(*schema.Set).List()
iamTags := d.Get("iam_tags")
sessionTags := d.Get(consts.FieldSessionTags)
externalID := d.Get(consts.FieldExternalID)
if policyDocument == "" && len(policyARNs) == 0 && len(roleARNs) == 0 && len(iamGroups) == 0 {
return fmt.Errorf("at least one of: `policy_document`, `policy_arns`, `role_arns` or `iam_groups` must be set")
}
credentialType := d.Get("credential_type").(string)
userPath := d.Get("user_path").(string)
permissionBoundaryArn := d.Get("permissions_boundary_arn").(string)
data := map[string]interface{}{
"credential_type": credentialType,
}
if d.HasChange("permissions_boundary_arn") {
if credentialType == "iam_user" {
data["permissions_boundary_arn"] = permissionBoundaryArn
} else {
return fmt.Errorf("permissions_boundary_arn is only valid when credential_type is iam_user")
}
}
if d.HasChange("policy_document") {
data["policy_document"] = policyDocument
}
if d.HasChange("policy_arns") {
data["policy_arns"] = policyARNs
}
if d.HasChange("role_arns") {
data["role_arns"] = roleARNs
}
if d.HasChange("iam_groups") {
data["iam_groups"] = iamGroups
}
if d.HasChange("iam_tags") {
data["iam_tags"] = iamTags
}
if d.HasChange(consts.FieldSessionTags) {
data[consts.FieldSessionTags] = sessionTags
}
if d.HasChange(consts.FieldExternalID) {
data[consts.FieldExternalID] = externalID
}
if d.HasChange("user_path") {
if credentialType == "iam_user" {
data["user_path"] = userPath
} else {
return fmt.Errorf("user_path is only valid when credential_type is iam_user")
}
}
defaultStsTTL, defaultStsTTLOk := d.GetOk("default_sts_ttl")
maxStsTTL, maxStsTTLOk := d.GetOk("max_sts_ttl")
if credentialType == "assumed_role" || credentialType == "federation_token" {
if defaultStsTTLOk {
data["default_sts_ttl"] = strconv.Itoa(defaultStsTTL.(int))
}
if maxStsTTLOk {
data["max_sts_ttl"] = strconv.Itoa(maxStsTTL.(int))
}
} else {
if defaultStsTTLOk {
return fmt.Errorf("default_sts_ttl is only valid when credential_type is assumed_role or federation_token")
}
if maxStsTTLOk {
return fmt.Errorf("max_sts_ttl is only valid when credential_type is assumed_role or federation_token")
}
}
log.Printf("[DEBUG] Creating role %q on AWS backend %q", name, backend)
_, err := client.Logical().Write(backend+"/roles/"+name, data)
if err != nil {
return fmt.Errorf("error creating role %q for backend %q: %s", name, backend, err)
}
log.Printf("[DEBUG] Created role %q on AWS backend %q", name, backend)
d.SetId(backend + "/roles/" + name)
return awsSecretBackendRoleRead(d, meta)
}
func awsSecretBackendRoleRead(d *schema.ResourceData, meta interface{}) error {
client, e := provider.GetClient(d, meta)
if e != nil {
return e
}
path := d.Id()
pathPieces := strings.Split(path, "/")
if len(pathPieces) < 3 || pathPieces[len(pathPieces)-2] != "roles" {
return fmt.Errorf("invalid id %q; must be {backend}/roles/{name}", path)
}
log.Printf("[DEBUG] Reading role from %q", path)
secret, err := client.Logical().Read(path)
if err != nil {
return fmt.Errorf("error reading role %q: %s", path, err)
}
log.Printf("[DEBUG] Read role from %q", path)
if secret == nil {
log.Printf("[WARN] Role %q not found, removing from state", path)
d.SetId("")
return nil
}
if _, ok := d.GetOk("policy_document"); ok {
d.Set("policy_document", secret.Data["policy_document"])
} else if v, ok := secret.Data["policy_document"]; ok {
d.Set("policy_document", v)
}
if _, ok := d.GetOk("policy_arns"); ok {
d.Set("policy_arns", secret.Data["policy_arns"])
} else if v, ok := secret.Data["policy_arns"]; ok {
d.Set("policy_arns", v)
}
d.Set("credential_type", secret.Data["credential_type"])
d.Set("role_arns", secret.Data["role_arns"])
if v, ok := secret.Data["default_sts_ttl"]; ok {
d.Set("default_sts_ttl", v)
}
if v, ok := secret.Data["max_sts_ttl"]; ok {
d.Set("max_sts_ttl", v)
}
if v, ok := secret.Data["iam_groups"]; ok {
d.Set("iam_groups", v)
}
if v, ok := secret.Data["iam_tags"]; ok {
d.Set("iam_tags", v)
}
if v, ok := secret.Data[consts.FieldSessionTags]; ok {
d.Set(consts.FieldSessionTags, v)
}
if v, ok := secret.Data[consts.FieldExternalID]; ok {
d.Set(consts.FieldExternalID, v)
}
if v, ok := secret.Data["permissions_boundary_arn"]; ok {
d.Set("permissions_boundary_arn", v)
}
if v, ok := secret.Data["user_path"]; ok {
d.Set("user_path", v)
}
d.Set("backend", strings.Join(pathPieces[:len(pathPieces)-2], "/"))
d.Set("name", pathPieces[len(pathPieces)-1])
return nil
}
func awsSecretBackendRoleDelete(d *schema.ResourceData, meta interface{}) error {
client, e := provider.GetClient(d, meta)
if e != nil {
return e
}
path := d.Id()
log.Printf("[DEBUG] Deleting role %q", path)
_, err := client.Logical().Delete(path)
if err != nil {
return fmt.Errorf("error deleting role %q: %s", path, err)
}
log.Printf("[DEBUG] Deleted role %q", path)
return nil
}
func awsSecretBackendRoleExists(d *schema.ResourceData, meta interface{}) (bool, error) {
client, e := provider.GetClient(d, meta)
if e != nil {
return false, e
}
path := d.Id()
log.Printf("[DEBUG] Checking if %q exists", path)
secret, err := client.Logical().Read(path)
if err != nil {
return true, fmt.Errorf("error checking if %q exists: %s", path, err)
}
log.Printf("[DEBUG] Checked if %q exists", path)
return secret != nil, nil
}