Skip to content

Commit 65a6c6a

Browse files
authored
Add support for assume_role access config (#207)
* Add support for `assume_role` access config * go generate ./...
1 parent 993fdc7 commit 65a6c6a

File tree

6 files changed

+3440
-3375
lines changed

6 files changed

+3440
-3375
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ The following attibutes are also available. These are optional and used in the s
7272

7373
- `access_key`
7474
- `secret_key`
75-
- `profile`
76-
- `token`
77-
- `mfa_code`
75+
- `assume_role`
7876
- `custom_endpoint_ec2`
79-
- `skip_region_validation`
77+
- `mfa_code`
78+
- `profile`
8079
- `skip_metadata_api_check`
80+
- `token`
8181

8282
### IAM Task or Instance Role
8383

access_config.go

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// @see https://github.com/hashicorp/packer/blob/v1.6.4/builder/amazon/common/access_config.go
1+
// @see https://github.com/hashicorp/packer-plugin-amazon/blob/v1.0.0/builder/common/access_config.go
22

33
package main
44

@@ -16,16 +16,32 @@ import (
1616
"github.com/hashicorp/go-cleanhttp"
1717
)
1818

19+
// AssumeRoleConfig lets users set configuration options for assuming a special
20+
// role when executing this plugin.
21+
type AssumeRoleConfig struct {
22+
AssumeRoleARN string `mapstructure:"role_arn" required:"false"`
23+
AssumeRoleDurationSeconds int `mapstructure:"duration_seconds" required:"false"`
24+
AssumeRoleExternalID string `mapstructure:"external_id" required:"false"`
25+
AssumeRolePolicy string `mapstructure:"policy" required:"false"`
26+
AssumeRolePolicyARNs []string `mapstructure:"policy_arns" required:"false"`
27+
AssumeRoleSessionName string `mapstructure:"session_name" required:"false"`
28+
AssumeRoleTags map[string]string `mapstructure:"tags" required:"false"`
29+
AssumeRoleTransitiveTagKeys []string `mapstructure:"transitive_tag_keys" required:"false"`
30+
}
31+
1932
// AccessConfig is for common configuration related to AWS access
2033
type AccessConfig struct {
21-
AccessKey string `mapstructure:"access_key"`
22-
SecretKey string `mapstructure:"secret_key"`
23-
ProfileName string `mapstructure:"profile"`
24-
Token string `mapstructure:"token"`
25-
MFACode string `mapstructure:"mfa_code"`
26-
CustomEndpointEc2 string `mapstructure:"custom_endpoint_ec2"`
27-
SkipValidation bool `mapstructure:"skip_region_validation"`
28-
SkipMetadataAPICheck bool `mapstructure:"skip_metadata_api_check"`
34+
AccessKey string `mapstructure:"access_key"`
35+
AssumeRole AssumeRoleConfig `mapstructure:"assume_role" required:"false"`
36+
CustomEndpointEc2 string `mapstructure:"custom_endpoint_ec2"`
37+
MFACode string `mapstructure:"mfa_code"`
38+
ProfileName string `mapstructure:"profile"`
39+
SecretKey string `mapstructure:"secret_key"`
40+
SkipMetadataAPICheck bool `mapstructure:"skip_metadata_api_check"`
41+
Token string `mapstructure:"token"`
42+
43+
// SkipValidation is not used, but it is still a valid option to keep backward compatibility.
44+
SkipValidation bool `mapstructure:"skip_region_validation"`
2945

3046
session *session.Session
3147
}
@@ -102,12 +118,20 @@ func (c *AccessConfig) Session() (*session.Session, error) {
102118
func (c *AccessConfig) GetCredentials(config *aws.Config) (*awsCredentials.Credentials, error) {
103119
// Reload values into the config used by the Packer-Terraform shared SDK
104120
awsbaseConfig := &awsbase.Config{
105-
AccessKey: c.AccessKey,
106-
DebugLogging: false,
107-
Profile: c.ProfileName,
108-
SecretKey: c.SecretKey,
109-
SkipMetadataApiCheck: c.SkipMetadataAPICheck,
110-
Token: c.Token,
121+
AccessKey: c.AccessKey,
122+
AssumeRoleARN: c.AssumeRole.AssumeRoleARN,
123+
AssumeRoleDurationSeconds: c.AssumeRole.AssumeRoleDurationSeconds,
124+
AssumeRoleExternalID: c.AssumeRole.AssumeRoleExternalID,
125+
AssumeRolePolicy: c.AssumeRole.AssumeRolePolicy,
126+
AssumeRolePolicyARNs: c.AssumeRole.AssumeRolePolicyARNs,
127+
AssumeRoleSessionName: c.AssumeRole.AssumeRoleSessionName,
128+
AssumeRoleTags: c.AssumeRole.AssumeRoleTags,
129+
AssumeRoleTransitiveTagKeys: c.AssumeRole.AssumeRoleTransitiveTagKeys,
130+
DebugLogging: false,
131+
Profile: c.ProfileName,
132+
SecretKey: c.SecretKey,
133+
SkipMetadataApiCheck: c.SkipMetadataAPICheck,
134+
Token: c.Token,
111135
}
112136

113137
return awsbase.GetCredentials(awsbaseConfig)

cleaner_mock.go

Lines changed: 25 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:generate mapstructure-to-hcl2 -type Config
1+
//go:generate mapstructure-to-hcl2 -type Config,AssumeRoleConfig
22

33
package main
44

config.hcl2spec.go

Lines changed: 65 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)