generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 40
/
aws_secret_manager.go
254 lines (224 loc) · 9.03 KB
/
aws_secret_manager.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
package connector
import (
"context"
"fmt"
"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"
)
func ResourceConnectorAwsSM() *schema.Resource {
resource := &schema.Resource{
Description: "Resource for creating an AWS Secret Manager connector.",
ReadContext: resourceConnectorAwsSMRead,
CreateContext: resourceConnectorAwsSMCreateOrUpdate,
UpdateContext: resourceConnectorAwsSMCreateOrUpdate,
DeleteContext: resourceConnectorDelete,
Importer: helpers.MultiLevelResourceImporter,
Schema: map[string]*schema.Schema{
"secret_name_prefix": {
Description: "A prefix to be added to all secrets.",
Type: schema.TypeString,
Optional: true,
},
"region": {
Description: "The AWS region where the AWS Secret Manager is.",
Type: schema.TypeString,
Required: true,
},
"delegate_selectors": {
Description: "Connect using only the delegates which have these tags.",
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"credentials": {
Description: "The credentials to use for connecting to aws.",
Type: schema.TypeList,
MaxItems: 1,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"manual": {
Description: "Specify the AWS key and secret used for authenticating.",
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
ConflictsWith: []string{"credentials.0.assume_role", "credentials.0.inherit_from_delegate"},
AtLeastOneOf: []string{"credentials.0.manual", "credentials.0.assume_role", "credentials.0.inherit_from_delegate"},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"access_key_ref": {
Description: "The reference to the Harness secret containing the AWS access key.",
Type: schema.TypeString,
Required: true,
},
"secret_key_ref": {
Description: "The reference to the Harness secret containing the AWS secret key.",
Type: schema.TypeString,
Required: true,
},
},
},
},
"inherit_from_delegate": {
Description: "Inherit the credentials from from the delegate.",
Type: schema.TypeBool,
Optional: true,
ConflictsWith: []string{"credentials.0.manual", "credentials.0.assume_role"},
AtLeastOneOf: []string{"credentials.0.manual", "credentials.0.assume_role", "credentials.0.inherit_from_delegate"},
RequiredWith: []string{"delegate_selectors"},
},
"assume_role": {
Description: "Connect using STS assume role.",
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
ConflictsWith: []string{"credentials.0.manual", "credentials.0.inherit_from_delegate"},
AtLeastOneOf: []string{"credentials.0.manual", "credentials.0.assume_role", "credentials.0.inherit_from_delegate"},
RequiredWith: []string{"delegate_selectors"},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"role_arn": {
Description: "The ARN of the role to assume.",
Type: schema.TypeString,
Required: true,
},
"external_id": {
Description: "If the administrator of the account to which the role belongs provided you with an external ID, then enter that value.",
Type: schema.TypeString,
Optional: true,
},
"duration": {
Description: "The duration, in seconds, of the role session. The value can range from 900 seconds (15 minutes) to 3600 seconds (1 hour). By default, the value is set to 3600 seconds. An expiration can also be specified in the client request body. The minimum value is 1 hour.",
Type: schema.TypeInt,
Required: true,
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
value := v.(int)
if value < 900 || value > 3600 {
errors = append(errors, fmt.Errorf("%q must be between 900 and 3600", k))
}
return
},
},
},
},
},
},
},
},
},
}
helpers.SetMultiLevelResourceSchema(resource.Schema)
return resource
}
func resourceConnectorAwsSMRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn, err := resourceConnectorReadBase(ctx, d, meta, nextgen.ConnectorTypes.AwsSecretManager)
if err != nil {
return err
}
if err := readConnectorAwsSM(d, conn); err != nil {
return diag.FromErr(err)
}
return nil
}
func resourceConnectorAwsSMCreateOrUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := buildConnectorAwsSM(d)
newConn, err := resourceConnectorCreateOrUpdateBase(ctx, d, meta, conn)
if err != nil {
return err
}
if err := readConnectorAwsSM(d, newConn); err != nil {
return diag.FromErr(err)
}
return nil
}
func buildConnectorAwsSM(d *schema.ResourceData) *nextgen.ConnectorInfo {
connector := &nextgen.ConnectorInfo{
Type_: nextgen.ConnectorTypes.AwsSecretManager,
AwsSecretManager: &nextgen.AwsSecretManager{},
}
if attr, ok := d.GetOk("secret_name_prefix"); ok {
connector.AwsSecretManager.SecretNamePrefix = attr.(string)
}
if attr, ok := d.GetOk("region"); ok {
connector.AwsSecretManager.Region = attr.(string)
}
if attr, ok := d.GetOk("delegate_selectors"); ok {
connector.AwsSecretManager.DelegateSelectors = utils.InterfaceSliceToStringSlice(attr.(*schema.Set).List())
}
if attr, ok := d.GetOk("credentials"); ok {
config := attr.([]interface{})[0].(map[string]interface{})
connector.AwsSecretManager.Credential = &nextgen.AwsSecretManagerCredential{}
if attr, ok := config["inherit_from_delegate"]; ok && attr.(bool) {
connector.AwsSecretManager.Credential.Type_ = nextgen.AwsSecretManagerAuthTypes.AssumeIAMRole
connector.AwsSecretManager.Credential.AssumeIamRole = &nextgen.AwsSmCredentialSpecAssumeIam{}
}
if attr := config["manual"].([]interface{}); len(attr) > 0 {
config := attr[0].(map[string]interface{})
connector.AwsSecretManager.Credential.Type_ = nextgen.AwsSecretManagerAuthTypes.ManualConfig
connector.AwsSecretManager.Credential.ManualConfig = &nextgen.AwsSmCredentialSpecManualConfig{}
if attr, ok := config["access_key_ref"]; ok {
connector.AwsSecretManager.Credential.ManualConfig.AccessKey = attr.(string)
}
if attr, ok := config["secret_key_ref"]; ok {
connector.AwsSecretManager.Credential.ManualConfig.SecretKey = attr.(string)
}
}
if attr := config["assume_role"].([]interface{}); len(attr) > 0 {
config := attr[0].(map[string]interface{})
connector.AwsSecretManager.Credential.Type_ = nextgen.AwsSecretManagerAuthTypes.AssumeSTSRole
connector.AwsSecretManager.Credential.AssumeStsRole = &nextgen.AwsSmCredentialSpecAssumeSts{}
if attr, ok := config["role_arn"]; ok {
connector.AwsSecretManager.Credential.AssumeStsRole.RoleArn = attr.(string)
}
if attr, ok := config["external_id"]; ok {
connector.AwsSecretManager.Credential.AssumeStsRole.ExternalId = attr.(string)
}
if attr, ok := config["duration"]; ok {
connector.AwsSecretManager.Credential.AssumeStsRole.AssumeStsRoleDuration = int32(attr.(int))
}
}
}
return connector
}
func readConnectorAwsSM(d *schema.ResourceData, connector *nextgen.ConnectorInfo) error {
d.Set("secret_name_prefix", connector.AwsSecretManager.SecretNamePrefix)
d.Set("region", connector.AwsSecretManager.Region)
d.Set("delegate_selectors", connector.AwsSecretManager.DelegateSelectors)
switch connector.AwsSecretManager.Credential.Type_ {
case nextgen.AwsSecretManagerAuthTypes.AssumeIAMRole:
d.Set("credentials", []interface{}{
map[string]interface{}{
"inherit_from_delegate": true,
},
})
case nextgen.AwsSecretManagerAuthTypes.ManualConfig:
d.Set("credentials", []interface{}{
map[string]interface{}{
"manual": []interface{}{
map[string]interface{}{
"access_key_ref": connector.AwsSecretManager.Credential.ManualConfig.AccessKey,
"secret_key_ref": connector.AwsSecretManager.Credential.ManualConfig.SecretKey,
},
},
},
})
case nextgen.AwsSecretManagerAuthTypes.AssumeSTSRole:
d.Set("credentials", []interface{}{
map[string]interface{}{
"assume_role": []interface{}{
map[string]interface{}{
"role_arn": connector.AwsSecretManager.Credential.AssumeStsRole.RoleArn,
"external_id": connector.AwsSecretManager.Credential.AssumeStsRole.ExternalId,
"duration": connector.AwsSecretManager.Credential.AssumeStsRole.AssumeStsRoleDuration,
},
},
},
})
default:
return fmt.Errorf("unsupported aws kms auth type: %s", connector.AwsSecretManager.Credential.Type_)
}
return nil
}