Skip to content

Commit

Permalink
Fix bugs in assume role support (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
shubham149 authored Jul 12, 2022
1 parent f1cb9b2 commit 56b0e6a
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions cmd/kaniko-ecr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,16 +333,15 @@ func createDockerConfig(dockerUsername, dockerPassword, accessKey, secretKey,
dockerConfig.SetAuth(docker.RegistryV1, dockerUsername, dockerPassword)
}

if accessKey == "" && assumeRole != "" {
if assumeRole != "" {
var err error
accessKey, secretKey, err = getAssumeRoleCreds(region, assumeRole, externalId, "")
username, password, registry, err := getAssumeRoleCreds(region, assumeRole, externalId, "")
if err != nil {
return nil, err
}
}

// only setup auth when pushing or credentials are defined
if !noPush || accessKey != "" {
dockerConfig.SetAuth(registry, username, password)
} else if !noPush || accessKey != "" {
// only setup auth when pushing or credentials are defined
if registry == "" {
return nil, fmt.Errorf("registry must be specified")
}
Expand Down Expand Up @@ -447,10 +446,10 @@ func uploadRepositoryPolicy(region, repo, registry, repositoryPolicy string) (er
return err
}

func getAssumeRoleCreds(region, roleArn, externalId, roleSessionName string) (string, string, error) {
func getAssumeRoleCreds(region, roleArn, externalId, roleSessionName string) (string, string, string, error) {
sess, err := session.NewSession(&awsv1.Config{Region: &region})
if err != nil {
return "", "", errors.Wrap(err, "failed to create aws session")
return "", "", "", errors.Wrap(err, "failed to create aws session")
}

svc := ecrv1.New(sess, &awsv1.Config{
Expand All @@ -461,11 +460,11 @@ func getAssumeRoleCreds(region, roleArn, externalId, roleSessionName string) (st
}),
})

username, password, _, err := getAuthInfo(svc)
username, password, registry, err := getAuthInfo(svc)
if err != nil {
return "", "", errors.Wrap(err, "failed to get ECR auth")
return "", "", "", errors.Wrap(err, "failed to get ECR auth")
}
return username, password, nil
return username, password, registry, nil
}

func getAuthInfo(svc *ecrv1.ECR) (username, password, registry string, err error) {
Expand Down

0 comments on commit 56b0e6a

Please sign in to comment.