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: use alternative ECR login logic #497

Draft
wants to merge 1 commit into
base: develop
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
39 changes: 26 additions & 13 deletions edge/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ 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/portainer/portainer/api/edge"
"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) (*edge.RegistryCredentials, error) {
if serverURL == "" || awsConfig == nil {
Expand All @@ -30,34 +32,45 @@ func DoAWSIAMRolesAnywhereAuthAndGetECRCredentials(serverURL string, awsConfig *
return nil, err
}

factory := api.DefaultClientFactory{}

cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithRegion(awsConfig.Region),
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(iamraCreds.AccessKeyId, iamraCreds.SecretAccessKey, iamraCreds.SessionToken)),
)
if err != nil {
log.Err(err).Msg("unable to build AWS client config")

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")
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]

return nil, ErrNoCredentials
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]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if the password has a :?


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

Expand Down