mirror of https://github.com/kubernetes/kops.git
make it possible to use assume role
This commit is contained in:
parent
68bf1870f9
commit
f31d8d5aa8
|
|
@ -19,6 +19,7 @@ package awsup
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
@ -33,6 +34,7 @@ import (
|
||||||
"github.com/aws/aws-sdk-go/aws"
|
"github.com/aws/aws-sdk-go/aws"
|
||||||
"github.com/aws/aws-sdk-go/aws/arn"
|
"github.com/aws/aws-sdk-go/aws/arn"
|
||||||
"github.com/aws/aws-sdk-go/aws/awserr"
|
"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/request"
|
||||||
"github.com/aws/aws-sdk-go/aws/session"
|
"github.com/aws/aws-sdk-go/aws/session"
|
||||||
"github.com/aws/aws-sdk-go/service/autoscaling"
|
"github.com/aws/aws-sdk-go/service/autoscaling"
|
||||||
|
|
@ -240,6 +242,13 @@ func ResetAWSCloudInstances() {
|
||||||
awsCloudInstances = make(map[string]AWSCloud)
|
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) {
|
func NewAWSCloud(region string, tags map[string]string) (AWSCloud, error) {
|
||||||
raw := awsCloudInstances[region]
|
raw := awsCloudInstances[region]
|
||||||
if raw == nil {
|
if raw == nil {
|
||||||
|
|
@ -254,11 +263,7 @@ func NewAWSCloud(region string, tags map[string]string) (AWSCloud, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
config := aws.NewConfig().WithRegion(region)
|
config := aws.NewConfig().WithRegion(region)
|
||||||
|
config = setConfig(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)
|
|
||||||
config = request.WithRetryer(config, newLoggingRetryer(ClientMaxRetries))
|
|
||||||
|
|
||||||
requestLogger := newRequestLogger(2)
|
requestLogger := newRequestLogger(2)
|
||||||
|
|
||||||
|
|
@ -269,6 +274,15 @@ func NewAWSCloud(region string, tags map[string]string) (AWSCloud, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c, err
|
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 = ec2.New(sess, config)
|
||||||
c.ec2.Handlers.Send.PushFront(requestLogger)
|
c.ec2.Handlers.Send.PushFront(requestLogger)
|
||||||
c.addHandlers(region, &c.ec2.Handlers)
|
c.addHandlers(region, &c.ec2.Handlers)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue