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

Make it possible to use assume role #15234

Merged
merged 2 commits into from
May 9, 2023
Merged
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
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 @@ -240,6 +242,13 @@ func ResetAWSCloudInstances() {
awsCloudInstances = 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 @@ -254,11 +263,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 @@ -269,6 +274,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("KOPS_AWS_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
Loading