Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --assume-role-arn option #538

Merged
merged 8 commits into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ Flags:
--ext-str=KEY=VALUE;... external string values for Jsonnet
--ext-code=KEY=VALUE;... external code values for Jsonnet
--config="ecspresso.yml" config file
--assume-role-arn="" the ARN of the role to assume
--option=OPTION

Commands:
Expand Down
11 changes: 6 additions & 5 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import (
)

type CLIOptions struct {
Envfile []string `help:"environment files"`
Debug bool `help:"enable debug log"`
ExtStr map[string]string `help:"external string values for Jsonnet"`
ExtCode map[string]string `help:"external code values for Jsonnet"`
Config string `help:"config file" default:"ecspresso.yml"`
Envfile []string `help:"environment files"`
Debug bool `help:"enable debug log"`
ExtStr map[string]string `help:"external string values for Jsonnet"`
ExtCode map[string]string `help:"external code values for Jsonnet"`
Config string `help:"config file" default:"ecspresso.yml"`
AssumeRoleARN string `help:"the ARN of the role to assume" default:""`

Option *Option

Expand Down
3 changes: 3 additions & 0 deletions cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var cliTests = []struct {
"--ext-str", "s2=v2",
"--ext-code", "c1=123",
"--ext-code", "c2=1+2",
"--assume-role-arn", "arn:aws:iam::123456789012:role/exampleRole",
},
sub: "status",
option: &ecspresso.Option{
Expand All @@ -33,6 +34,7 @@ var cliTests = []struct {
ExtStr: map[string]string{"s1": "v1", "s2": "v2"},
ExtCode: map[string]string{"c1": "123", "c2": "1+2"},
InitOption: nil,
AssumeRoleARN: "arn:aws:iam::123456789012:role/exampleRole",
},
subOption: &ecspresso.StatusOption{
Events: ptr(10),
Expand All @@ -57,6 +59,7 @@ var cliTests = []struct {
ExtStr: map[string]string{},
ExtCode: map[string]string{},
InitOption: nil,
AssumeRoleARN: "",
},
subOption: &ecspresso.StatusOption{
Events: ptr(100),
Expand Down
1 change: 1 addition & 0 deletions cliv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func ParseCLIv2(args []string) (string, *CLIOptions, func(), error) {
Debug: opts.Debug,
ExtStr: opts.ExtStr,
ExtCode: opts.ExtCode,
AssumeRoleARN: opts.AssumeRoleARN,
}
if opts.Option.ExtStr == nil {
opts.Option.ExtStr = map[string]string{}
Expand Down
12 changes: 12 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (

"github.com/aws/aws-sdk-go-v2/aws"
awsConfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials/stscreds"
"github.com/aws/aws-sdk-go-v2/service/sts"
"github.com/google/go-jsonnet"
goVersion "github.com/hashicorp/go-version"
"github.com/kayac/ecspresso/v2/appspec"
Expand Down Expand Up @@ -163,6 +165,16 @@ func (c *Config) Restrict(ctx context.Context) error {
return nil
}

func (c *Config) AssumeRole(assumeRoleARN string) {
if assumeRoleARN == "" {
return
}
Log("[INFO] assume role: %s", assumeRoleARN)
stsClient := sts.NewFromConfig(c.awsv2Config)
assumeRoleProvider := stscreds.NewAssumeRoleProvider(stsClient, assumeRoleARN)
c.awsv2Config.Credentials = aws.NewCredentialsCache(assumeRoleProvider)
}

func (c *Config) setupPlugins(ctx context.Context) error {
for _, p := range c.Plugins {
if err := p.Setup(ctx, c); err != nil {
Expand Down
2 changes: 2 additions & 0 deletions ecspresso.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ func New(ctx context.Context, opt *Option) (*App, error) {
return nil, fmt.Errorf("failed to load config file %s: %w", opt.ConfigFilePath, err)
}
}
conf.AssumeRole(opt.AssumeRoleARN)

logger := newLogger()
if opt.Debug {
Expand Down Expand Up @@ -181,6 +182,7 @@ type Option struct {
Debug bool
ExtStr map[string]string
ExtCode map[string]string
AssumeRoleARN string
}

func (opt *Option) resolveConfigFilePath() (path string) {
Expand Down