This repository has been archived by the owner on Dec 24, 2023. It is now read-only.
forked from hashicorp/terraform-provider-vault
/
resource_aws_secret_backend_role.go
192 lines (170 loc) · 5.55 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
package vault
import (
"fmt"
"log"
"strings"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/vault/api"
"github.com/terraform-providers/terraform-provider-vault/util"
)
func awsSecretBackendRoleResource() *schema.Resource {
return &schema.Resource{
Create: awsSecretBackendRoleWrite,
Read: 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.TypeList,
Optional: true,
ConflictsWith: []string{"policy", "policy_arn"},
Description: "ARN for an existing IAM policy the role should use.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"policy_arn": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"policy_document", "policy", "policy_arns"},
Description: "ARN for an existing IAM policy the role should use.",
Deprecated: `Use "policy_arns".`,
},
"policy_document": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"policy_arn", "policy"},
Description: "IAM policy the role should use in JSON format.",
DiffSuppressFunc: util.JsonDiffSuppress,
},
"policy": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"policy_arns", "policy_arn", "policy_document"},
Description: "IAM policy the role should use in JSON format.",
DiffSuppressFunc: util.JsonDiffSuppress,
Deprecated: `Use "policy_document".`,
},
"credential_type": {
Type: schema.TypeString,
Required: true,
Description: "Role credential type.",
},
},
}
}
func awsSecretBackendRoleWrite(d *schema.ResourceData, meta interface{}) error {
client := meta.(*api.Client)
backend := d.Get("backend").(string)
name := d.Get("name").(string)
policyARNsIfc, ok := d.GetOk("policy_arns")
var policyARNs []string
if !ok {
policyARN := d.Get("policy_arn").(string)
if policyARN != "" {
policyARNs = append(policyARNs, policyARN)
}
}
for _, arnIfc := range policyARNsIfc.([]interface{}) {
policyARNs = append(policyARNs, arnIfc.(string))
}
policy, ok := d.GetOk("policy_document")
if !ok {
policy = d.Get("policy")
}
if policy == "" && len(policyARNs) == 0 {
return fmt.Errorf("either policy or policy_arn must be set.")
}
data := map[string]interface{}{
"credential_type": d.Get("credential_type").(string),
}
if policy != "" {
data["policy_document"] = policy
}
if len(policyARNs) != 0 {
data["policy_arns"] = policyARNs
}
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 := meta.(*api.Client)
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 _, ok := d.GetOk("policy"); ok {
d.Set("policy", 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 _, ok := d.GetOk("policy_arn"); ok {
d.Set("policy_arn", 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("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 := meta.(*api.Client)
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 := meta.(*api.Client)
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
}