Skip to content

Commit 8f430b4

Browse files
bfladmildwonkey
authored andcommitted
backend/s3: Updates for Terraform v0.13.0 (#25134)
* deps: Update github.com/hashicorp/aws-sdk-go-base@v0.5.0 Updated via: ``` $ go get github.com/hashicorp/aws-sdk-go-base@v0.5.0 $ go mod tidy $ go mod vendor ``` * backend/s3: Updates for Terraform v0.13.0 Reference: #13410 Reference: #18774 Reference: #19482 Reference: #20062 Reference: #20599 Reference: #22103 Reference: #22161 Reference: #22601 Reference: #22992 Reference: #24252 Reference: #24253 Reference: #24480 Reference: #25056 Changes: ``` NOTES * backend/s3: Deprecated `lock_table`, `skip_get_ec2_platforms`, `skip_requesting_account_id` arguments have been removed * backend/s3: Credential ordering has changed from static, environment, shared credentials, EC2 metadata, default AWS Go SDK (shared configuration, web identity, ECS, EC2 Metadata) to static, environment, shared credentials, default AWS Go SDK (shared configuration, web identity, ECS, EC2 Metadata) * The `AWS_METADATA_TIMEOUT` environment variable no longer has any effect as we now depend on the default AWS Go SDK EC2 Metadata client timeout of one second with two retries ENHANCEMENTS * backend/s3: Always enable shared configuration file support (no longer require `AWS_SDK_LOAD_CONFIG` environment variable) * backend/s3: Automatically expand `~` prefix for home directories in `shared_credentials_file` argument * backend/s3: Add `assume_role_duration_seconds`, `assume_role_policy_arns`, `assume_role_tags`, and `assume_role_transitive_tag_keys` arguments BUG FIXES * backend/s3: Ensure configured profile is used * backend/s3: Ensure configured STS endpoint is used during AssumeRole API calls * backend/s3: Prefer AWS shared configuration over EC2 metadata credentials * backend/s3: Prefer ECS credentials over EC2 metadata credentials * backend/s3: Remove hardcoded AWS Provider messaging ``` Output from acceptance testing: ``` --- PASS: TestBackend (16.32s) --- PASS: TestBackendConfig (0.58s) --- PASS: TestBackendConfig_AssumeRole (0.02s) --- PASS: TestBackendConfig_conflictingEncryptionSchema (0.00s) --- PASS: TestBackendConfig_invalidKey (0.00s) --- PASS: TestBackendConfig_invalidSSECustomerKeyEncoding (0.00s) --- PASS: TestBackendConfig_invalidSSECustomerKeyLength (0.00s) --- PASS: TestBackendExtraPaths (13.21s) --- PASS: TestBackendLocked (28.98s) --- PASS: TestBackendPrefixInWorkspace (5.65s) --- PASS: TestBackendSSECustomerKey (17.60s) --- PASS: TestBackend_impl (0.00s) --- PASS: TestForceUnlock (17.50s) --- PASS: TestKeyEnv (50.25s) --- PASS: TestRemoteClient (4.78s) --- PASS: TestRemoteClientLocks (16.85s) --- PASS: TestRemoteClient_clientMD5 (12.08s) --- PASS: TestRemoteClient_impl (0.00s) --- PASS: TestRemoteClient_stateChecksum (17.92s) ```
1 parent 4bf88fc commit 8f430b4

File tree

32 files changed

+2117
-848
lines changed

32 files changed

+2117
-848
lines changed

backend/remote-state/s3/backend.go

Lines changed: 88 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func New() backend.Backend {
4444
"region": {
4545
Type: schema.TypeString,
4646
Required: true,
47-
Description: "The region of the S3 bucket.",
47+
Description: "AWS region of the S3 Bucket and DynamoDB Table (if used).",
4848
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
4949
"AWS_REGION",
5050
"AWS_DEFAULT_REGION",
@@ -114,14 +114,6 @@ func New() backend.Backend {
114114
Default: "",
115115
},
116116

117-
"lock_table": {
118-
Type: schema.TypeString,
119-
Optional: true,
120-
Description: "DynamoDB table for state locking",
121-
Default: "",
122-
Deprecated: "please use the dynamodb_table attribute",
123-
},
124-
125117
"dynamodb_table": {
126118
Type: schema.TypeString,
127119
Optional: true,
@@ -157,29 +149,13 @@ func New() backend.Backend {
157149
Default: false,
158150
},
159151

160-
"skip_get_ec2_platforms": {
161-
Type: schema.TypeBool,
162-
Optional: true,
163-
Description: "Skip getting the supported EC2 platforms.",
164-
Default: false,
165-
Deprecated: "The S3 Backend does not require EC2 functionality and this attribute is no longer used.",
166-
},
167-
168152
"skip_region_validation": {
169153
Type: schema.TypeBool,
170154
Optional: true,
171155
Description: "Skip static validation of region name.",
172156
Default: false,
173157
},
174158

175-
"skip_requesting_account_id": {
176-
Type: schema.TypeBool,
177-
Optional: true,
178-
Description: "Skip requesting the account ID.",
179-
Default: false,
180-
Deprecated: "The S3 Backend no longer automatically looks up the AWS Account ID and this attribute is no longer used.",
181-
},
182-
183159
"skip_metadata_api_check": {
184160
Type: schema.TypeBool,
185161
Optional: true,
@@ -223,13 +199,40 @@ func New() backend.Backend {
223199
Default: "",
224200
},
225201

202+
"assume_role_duration_seconds": {
203+
Type: schema.TypeInt,
204+
Optional: true,
205+
Description: "Seconds to restrict the assume role session duration.",
206+
},
207+
226208
"assume_role_policy": {
227209
Type: schema.TypeString,
228210
Optional: true,
229-
Description: "The permissions applied when assuming a role.",
211+
Description: "IAM Policy JSON describing further restricting permissions for the IAM Role being assumed.",
230212
Default: "",
231213
},
232214

215+
"assume_role_policy_arns": {
216+
Type: schema.TypeSet,
217+
Optional: true,
218+
Description: "Amazon Resource Names (ARNs) of IAM Policies describing further restricting permissions for the IAM Role being assumed.",
219+
Elem: &schema.Schema{Type: schema.TypeString},
220+
},
221+
222+
"assume_role_tags": {
223+
Type: schema.TypeMap,
224+
Optional: true,
225+
Description: "Assume role session tags.",
226+
Elem: &schema.Schema{Type: schema.TypeString},
227+
},
228+
229+
"assume_role_transitive_tag_keys": {
230+
Type: schema.TypeSet,
231+
Optional: true,
232+
Description: "Assume role session tag keys to pass to any subsequent sessions.",
233+
Elem: &schema.Schema{Type: schema.TypeString},
234+
},
235+
233236
"workspace_key_prefix": {
234237
Type: schema.TypeString,
235238
Optional: true,
@@ -302,6 +305,7 @@ func (b *Backend) configure(ctx context.Context) error {
302305
b.workspaceKeyPrefix = data.Get("workspace_key_prefix").(string)
303306
b.serverSideEncryption = data.Get("encrypt").(bool)
304307
b.kmsKeyID = data.Get("kms_key_id").(string)
308+
b.ddbTable = data.Get("dynamodb_table").(string)
305309

306310
customerKeyString := data.Get("sse_customer_key").(string)
307311
if customerKeyString != "" {
@@ -316,39 +320,74 @@ func (b *Backend) configure(ctx context.Context) error {
316320
}
317321
}
318322

319-
b.ddbTable = data.Get("dynamodb_table").(string)
320-
if b.ddbTable == "" {
321-
// try the deprecated field
322-
b.ddbTable = data.Get("lock_table").(string)
323-
}
324-
325323
cfg := &awsbase.Config{
326-
AccessKey: data.Get("access_key").(string),
327-
AssumeRoleARN: data.Get("role_arn").(string),
328-
AssumeRoleExternalID: data.Get("external_id").(string),
329-
AssumeRolePolicy: data.Get("assume_role_policy").(string),
330-
AssumeRoleSessionName: data.Get("session_name").(string),
331-
CredsFilename: data.Get("shared_credentials_file").(string),
332-
DebugLogging: logging.IsDebugOrHigher(),
333-
IamEndpoint: data.Get("iam_endpoint").(string),
334-
MaxRetries: data.Get("max_retries").(int),
335-
Profile: data.Get("profile").(string),
336-
Region: data.Get("region").(string),
337-
SecretKey: data.Get("secret_key").(string),
338-
SkipCredsValidation: data.Get("skip_credentials_validation").(bool),
339-
SkipMetadataApiCheck: data.Get("skip_metadata_api_check").(bool),
340-
StsEndpoint: data.Get("sts_endpoint").(string),
341-
Token: data.Get("token").(string),
324+
AccessKey: data.Get("access_key").(string),
325+
AssumeRoleARN: data.Get("role_arn").(string),
326+
AssumeRoleDurationSeconds: data.Get("assume_role_duration_seconds").(int),
327+
AssumeRoleExternalID: data.Get("external_id").(string),
328+
AssumeRolePolicy: data.Get("assume_role_policy").(string),
329+
AssumeRoleSessionName: data.Get("session_name").(string),
330+
CallerDocumentationURL: "https://www.terraform.io/docs/backends/types/s3.html",
331+
CallerName: "S3 Backend",
332+
CredsFilename: data.Get("shared_credentials_file").(string),
333+
DebugLogging: logging.IsDebugOrHigher(),
334+
IamEndpoint: data.Get("iam_endpoint").(string),
335+
MaxRetries: data.Get("max_retries").(int),
336+
Profile: data.Get("profile").(string),
337+
Region: data.Get("region").(string),
338+
SecretKey: data.Get("secret_key").(string),
339+
SkipCredsValidation: data.Get("skip_credentials_validation").(bool),
340+
SkipMetadataApiCheck: data.Get("skip_metadata_api_check").(bool),
341+
StsEndpoint: data.Get("sts_endpoint").(string),
342+
Token: data.Get("token").(string),
342343
UserAgentProducts: []*awsbase.UserAgentProduct{
343344
{Name: "APN", Version: "1.0"},
344345
{Name: "HashiCorp", Version: "1.0"},
345346
{Name: "Terraform", Version: version.String()},
346347
},
347348
}
348349

350+
if policyARNSet := data.Get("assume_role_policy_arns").(*schema.Set); policyARNSet.Len() > 0 {
351+
for _, policyARNRaw := range policyARNSet.List() {
352+
policyARN, ok := policyARNRaw.(string)
353+
354+
if !ok {
355+
continue
356+
}
357+
358+
cfg.AssumeRolePolicyARNs = append(cfg.AssumeRolePolicyARNs, policyARN)
359+
}
360+
}
361+
362+
if tagMap := data.Get("assume_role_tags").(map[string]interface{}); len(tagMap) > 0 {
363+
cfg.AssumeRoleTags = make(map[string]string)
364+
365+
for k, vRaw := range tagMap {
366+
v, ok := vRaw.(string)
367+
368+
if !ok {
369+
continue
370+
}
371+
372+
cfg.AssumeRoleTags[k] = v
373+
}
374+
}
375+
376+
if transitiveTagKeySet := data.Get("assume_role_transitive_tag_keys").(*schema.Set); transitiveTagKeySet.Len() > 0 {
377+
for _, transitiveTagKeyRaw := range transitiveTagKeySet.List() {
378+
transitiveTagKey, ok := transitiveTagKeyRaw.(string)
379+
380+
if !ok {
381+
continue
382+
}
383+
384+
cfg.AssumeRoleTransitiveTagKeys = append(cfg.AssumeRoleTransitiveTagKeys, transitiveTagKey)
385+
}
386+
}
387+
349388
sess, err := awsbase.GetSession(cfg)
350389
if err != nil {
351-
return err
390+
return fmt.Errorf("error configuring S3 Backend: %w", err)
352391
}
353392

354393
b.dynClient = dynamodb.New(sess.Copy(&aws.Config{

0 commit comments

Comments
 (0)