Skip to content

Commit

Permalink
Merge pull request #440 from drone-plugins/CI-12566
Browse files Browse the repository at this point in the history
Fixed 'error getting ECR auth: WebIdentityErr: unable to read file at…' issue
  • Loading branch information
Ompragash authored May 20, 2024
2 parents 11015f0 + 49e9dde commit 292ebe0
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions cmd/drone-ecr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func main() {
assumeRole = getenv("PLUGIN_ASSUME_ROLE")
externalId = getenv("PLUGIN_EXTERNAL_ID")
scanOnPush = parseBoolOrDefault(false, getenv("PLUGIN_SCAN_ON_PUSH"))
idToken = os.Getenv("PLUGIN_OIDC_TOKEN_ID")
idToken = os.Getenv("PLUGIN_OIDC_TOKEN_ID")
)

// set the region
Expand Down Expand Up @@ -218,9 +218,24 @@ func getECRClient(sess *session.Session, role string, externalId string, idToken
if role == "" {
return ecr.New(sess)
}
// Use STS AssumeRoleWithWebIdentity when idToken is provided

if idToken != "" {
creds := stscreds.NewWebIdentityCredentials(sess, role, "", idToken)
tempFile, err := os.CreateTemp("/tmp", "idToken-*.jwt")
if err != nil {
log.Fatalf("Failed to create temporary file: %v", err)
}
defer tempFile.Close()

if err := os.Chmod(tempFile.Name(), 0600); err != nil {
log.Fatalf("Failed to set file permissions: %v", err)
}

if _, err := tempFile.WriteString(idToken); err != nil {
log.Fatalf("Failed to write ID token to temporary file: %v", err)
}

// Create credentials using the path to the ID token file
creds := stscreds.NewWebIdentityCredentials(sess, role, "", tempFile.Name())
return ecr.New(sess, &aws.Config{Credentials: creds})
} else if externalId != "" {
return ecr.New(sess, &aws.Config{
Expand Down

0 comments on commit 292ebe0

Please sign in to comment.