Skip to content

Commit 014d4f1

Browse files
danielforjpalermo
authored andcommitted
Add assume role support to BBL
Initial commit. Includes: BBL assuming the role while terraforming and the iaas interactions between bbl and aws. Missing: using the ops file found in bosh-deployment: `aws/cpi-assume-role-credentials.yml` to make the AWS CPI use the role. Unit tests/Integration tests.
1 parent 52ce0d0 commit 014d4f1

File tree

7 files changed

+24
-5
lines changed

7 files changed

+24
-5
lines changed

aws/client.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
awslib "github.com/aws/aws-sdk-go/aws"
1010
"github.com/aws/aws-sdk-go/aws/credentials"
11+
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
1112
"github.com/aws/aws-sdk-go/aws/session"
1213
awsec2 "github.com/aws/aws-sdk-go/service/ec2"
1314
awsroute53 "github.com/aws/aws-sdk-go/service/route53"
@@ -43,14 +44,19 @@ type Client struct {
4344
}
4445

4546
func NewClient(creds storage.AWS, logger logger) Client {
46-
config := &awslib.Config{
47-
Credentials: credentials.NewStaticCredentials(creds.AccessKeyID, creds.SecretAccessKey, ""),
48-
Region: awslib.String(creds.Region),
47+
config := awslib.NewConfig().
48+
WithCredentials(credentials.NewStaticCredentials(creds.AccessKeyID, creds.SecretAccessKey, "")).
49+
WithRegion(creds.Region)
50+
awsSession := session.Must(session.NewSession(config))
51+
52+
if creds.AssumeRoleArn != "" {
53+
stsCredentials := stscreds.NewCredentials(awsSession, creds.AssumeRoleArn)
54+
awsSession = session.Must(session.NewSession(awslib.NewConfig().WithCredentials(stsCredentials).WithRegion(creds.Region)))
4955
}
5056

5157
return Client{
52-
ec2Client: awsec2.New(session.Must(session.NewSession(config))),
53-
route53Client: awsroute53.New(session.Must(session.NewSession(config))),
58+
ec2Client: awsec2.New(awsSession),
59+
route53Client: awsroute53.New(awsSession),
5460
logger: logger,
5561
}
5662
}

config/global_flags.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type GlobalFlags struct {
1313
AWSAccessKeyID string `long:"aws-access-key-id" env:"BBL_AWS_ACCESS_KEY_ID"`
1414
AWSSecretAccessKey string `long:"aws-secret-access-key" env:"BBL_AWS_SECRET_ACCESS_KEY"`
1515
AWSRegion string `long:"aws-region" env:"BBL_AWS_REGION"`
16+
AWSAssumeRole string `long:"aws-assume-role" env:"BBL_AWS_ASSUME_ROLE"`
1617

1718
AzureClientID string `long:"azure-client-id" env:"BBL_AZURE_CLIENT_ID"`
1819
AzureClientSecret string `long:"azure-client-secret" env:"BBL_AZURE_CLIENT_SECRET"`

config/load_state_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ var _ = Describe("LoadState", func() {
193193
Expect(flags.EnvID).To(Equal("some-name"))
194194
Expect(flags.AWSAccessKeyID).To(Equal("some-aws-access-key"))
195195
Expect(flags.AWSSecretAccessKey).To(Equal("some-aws-secret-access-key"))
196+
Expect(flags.AWSAssumeRole).To(Equal("some-aws-assume-role"))
196197
})
197198
})
198199
})

config/merger.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ func (m Merger) updateVSphereState(globalFlags GlobalFlags, state storage.State)
116116
func (m Merger) updateAWSState(globalFlags GlobalFlags, state storage.State) (storage.State, error) {
117117
copyFlagToState(globalFlags.AWSAccessKeyID, &state.AWS.AccessKeyID)
118118
copyFlagToState(globalFlags.AWSSecretAccessKey, &state.AWS.SecretAccessKey)
119+
copyFlagToState(globalFlags.AWSAssumeRole, &state.AWS.AssumeRoleArn)
119120

120121
if globalFlags.AWSRegion != "" {
121122
if state.AWS.Region != "" && globalFlags.AWSRegion != state.AWS.Region {

storage/aws.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ package storage
33
type AWS struct {
44
AccessKeyID string `json:"-"`
55
SecretAccessKey string `json:"-"`
6+
AssumeRoleArn string `json:"assumeRole,omitempty"`
67
Region string `json:"region,omitempty"`
78
}

terraform/aws/input_generator.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,6 @@ func (i InputGenerator) Credentials(state storage.State) map[string]string {
6666
return map[string]string{
6767
"access_key": state.AWS.AccessKeyID,
6868
"secret_key": state.AWS.SecretAccessKey,
69+
"role_arn": state.AWS.AssumeRoleArn,
6970
}
7071
}

terraform/aws/templates/base.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ provider "aws" {
1515
access_key = "${var.access_key}"
1616
secret_key = "${var.secret_key}"
1717
region = "${var.region}"
18+
assume_role {
19+
role_arn = "${var.role_arn}"
20+
}
1821
}
1922

2023
variable "access_key" {
@@ -29,6 +32,11 @@ variable "region" {
2932
type = string
3033
}
3134

35+
variable "role_arn" {
36+
type = string
37+
default = ""
38+
}
39+
3240
variable "bosh_inbound_cidr" {
3341
default = "0.0.0.0/0"
3442
}

0 commit comments

Comments
 (0)