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

feat: update AWS auth logic to ECR #498

Draft
wants to merge 1 commit into
base: release/2.18
Choose a base branch
from
Draft
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
40 changes: 28 additions & 12 deletions edge/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,27 @@ package aws

import (
"context"
"encoding/base64"
"errors"
"strings"

"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/ecr"
iamra "github.com/aws/rolesanywhere-credential-helper/aws_signing_helper"
"github.com/awslabs/amazon-ecr-credential-helper/ecr-login/api"
"github.com/portainer/agent"
"github.com/rs/zerolog/log"
)

var ErrNoCredentials = errors.New("No credentials found")
var ErrNoCredentials = errors.New("no credentials found")

func DoAWSIAMRolesAnywhereAuthAndGetECRCredentials(serverURL string, awsConfig *agent.AWSConfig) (*agent.RegistryCredentials, error) {
if serverURL == "" || awsConfig == nil {
log.Info().
Str("server_url", serverURL).
Str("aws configuration region", awsConfig.Region).
Msg("incomplete information when using local AWS config for credential lookup")

return nil, errors.New("invalid ecr configuration")
}

Expand All @@ -28,8 +31,6 @@ func DoAWSIAMRolesAnywhereAuthAndGetECRCredentials(serverURL string, awsConfig *
return nil, err
}

factory := api.DefaultClientFactory{}

cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithRegion(awsConfig.Region),
Expand All @@ -40,20 +41,35 @@ func DoAWSIAMRolesAnywhereAuthAndGetECRCredentials(serverURL string, awsConfig *
return nil, err
}

client := factory.NewClient(cfg)
client := ecr.NewFromConfig(cfg)

creds, err := client.GetCredentials(serverURL)
output, err := client.GetAuthorizationToken(context.TODO(), &ecr.GetAuthorizationTokenInput{})
if err != nil {
// This might not be an ECR registry
// Therefore we deliberately not return an error here so that the upstream logic can fallback to other credential providers
log.Warn().Str("server_url", serverURL).Err(err).Msg("unable to retrieve credentials from server")
return nil, ErrNoCredentials
log.Err(err).Msg("unable to get ECR authorization token")
return nil, err
}

if len(output.AuthorizationData) == 0 {
log.Err(err).Msg("unable to find ECR authorization token associated with the AWS account")
return nil, errors.New("no ECR authorization token associated with AWS account")
}

data := output.AuthorizationData[0]

token, err := base64.StdEncoding.DecodeString(*data.AuthorizationToken)
if err != nil {
log.Err(err).Msg("unable to decode ECR authorization token")
return nil, err
}

tokenParts := strings.Split(string(token), ":")
username := tokenParts[0]
password := tokenParts[1]

return &agent.RegistryCredentials{
ServerURL: serverURL,
Username: creds.Username,
Secret: creds.Password,
Username: username,
Secret: password,
}, nil
}

Expand Down