Skip to content

Commit

Permalink
make it possible to use assume role
Browse files Browse the repository at this point in the history
  • Loading branch information
zetaab committed Mar 16, 2023
1 parent b03544d commit 027748f
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions upup/pkg/fi/cloudup/awsup/aws_cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package awsup
import (
"context"
"fmt"
"os"
"strconv"
"strings"
"sync"
Expand All @@ -33,6 +34,7 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/autoscaling"
Expand Down Expand Up @@ -236,6 +238,13 @@ func (c *awsCloudImplementation) Region() string {

var awsCloudInstances map[string]AWSCloud = make(map[string]AWSCloud)

func setConfig(config *aws.Config) *aws.Config {
// This avoids a confusing error message when we fail to get credentials
// e.g. https://github.com/kubernetes/kops/issues/605
config = config.WithCredentialsChainVerboseErrors(true)
return request.WithRetryer(config, newLoggingRetryer(ClientMaxRetries))
}

func NewAWSCloud(region string, tags map[string]string) (AWSCloud, error) {
raw := awsCloudInstances[region]
if raw == nil {
Expand All @@ -250,11 +259,7 @@ func NewAWSCloud(region string, tags map[string]string) (AWSCloud, error) {
}

config := aws.NewConfig().WithRegion(region)

// This avoids a confusing error message when we fail to get credentials
// e.g. https://github.com/kubernetes/kops/issues/605
config = config.WithCredentialsChainVerboseErrors(true)
config = request.WithRetryer(config, newLoggingRetryer(ClientMaxRetries))
config = setConfig(config)

requestLogger := newRequestLogger(2)

Expand All @@ -265,6 +270,15 @@ func NewAWSCloud(region string, tags map[string]string) (AWSCloud, error) {
if err != nil {
return c, err
}

// assumes the role before executing commands
roleARN := os.Getenv("AWS_ASSUME_ROLE_ARN")
if roleARN != "" {
creds := stscreds.NewCredentials(sess, roleARN)
config = &aws.Config{Credentials: creds}
config = setConfig(config).WithRegion(region)
}

c.ec2 = ec2.New(sess, config)
c.ec2.Handlers.Send.PushFront(requestLogger)
c.addHandlers(region, &c.ec2.Handlers)
Expand Down

0 comments on commit 027748f

Please sign in to comment.