Skip to content

Commit bebf371

Browse files
authored
Merge pull request #21 from StarOfService/feature/assume-role
Provide possibility to assume AWS role (AWS STS)
2 parents 7a1fa84 + 4fe7e7e commit bebf371

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ Check your CloudFormation console once more and validate that your stack as well
289289

290290
Argument | Environment variable | Default value | Description
291291
---------|----------------------|---------------|------------
292+
assume-role | AWS_ASSUME_ROLE | | Assume AWS role when defined. Useful for stacks in another AWS account. Specify the full ARN, e.g. `arn:aws:iam::123456789:role/cloudformation-operator`
292293
capability | AWS_CAPABILITIES | | Enable specified capabilities for all stacks managed by the operator instance. Current parameter can be used multiple times. For example: `--capability CAPABILITY_NAMED_IAM --capability CAPABILITY_IAM`. Or with a line break when specifying as an environment variable: `AWS_CAPABILITIES=CAPABILITY_IAM$'\n'CAPABILITY_NAMED_IAM`
293294
debug | DEBUG | | Enable debug logging.
294295
dry-run | DRY_RUN | | If true, don't actually do anything.

cmd/cloudformation-operator/main.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@ import (
1313
sdkVersion "github.com/operator-framework/operator-sdk/version"
1414

1515
"github.com/aws/aws-sdk-go/aws"
16+
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
1617
"github.com/aws/aws-sdk-go/aws/session"
1718
"github.com/aws/aws-sdk-go/service/cloudformation"
19+
"github.com/aws/aws-sdk-go/service/cloudformation/cloudformationiface"
1820
)
1921

2022
var (
2123
namespace string
2224
region string
25+
assumeRole string
2326
tags = new(map[string]string)
2427
capabilities = []string{}
2528
dryRun bool
@@ -30,6 +33,7 @@ var (
3033
func init() {
3134
kingpin.Flag("namespace", "The Kubernetes namespace to watch").Default("default").Envar("WATCH_NAMESPACE").StringVar(&namespace)
3235
kingpin.Flag("region", "The AWS region to use").Envar("AWS_REGION").StringVar(&region)
36+
kingpin.Flag("assume-role", "Assume AWS role when defined. Useful for stacks in another AWS account. Specify the full ARN, e.g. `arn:aws:iam::123456789:role/cloudformation-operator`").Envar("AWS_ASSUME_ROLE").StringVar(&assumeRole)
3337
kingpin.Flag("capability", "The AWS CloudFormation capability to enable").Envar("AWS_CAPABILITIES").StringsVar(&capabilities)
3438
kingpin.Flag("dry-run", "If true, don't actually do anything.").Envar("DRY_RUN").BoolVar(&dryRun)
3539
kingpin.Flag("debug", "Enable debug logging.").Envar("DEBUG").BoolVar(&debug)
@@ -58,9 +62,21 @@ func main() {
5862

5963
printVersion()
6064

61-
client := cloudformation.New(session.New(), &aws.Config{
62-
Region: aws.String(region),
63-
})
65+
var client cloudformationiface.CloudFormationAPI
66+
sess := session.Must(session.NewSession())
67+
logrus.Info(assumeRole)
68+
if assumeRole != "" {
69+
logrus.Info("run assume")
70+
creds := stscreds.NewCredentials(sess, assumeRole)
71+
client = cloudformation.New(sess, &aws.Config{
72+
Credentials: creds,
73+
Region: aws.String(region),
74+
})
75+
} else {
76+
client = cloudformation.New(sess, &aws.Config{
77+
Region: aws.String(region),
78+
})
79+
}
6480

6581
sdk.Watch("cloudformation.linki.space/v1alpha1", "Stack", namespace, 0)
6682
sdk.Handle(stub.NewHandler(client, capabilities, *tags, dryRun))

0 commit comments

Comments
 (0)