Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion pkg/cmd/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func getAWSConfig(roleName, region string) (aws.Config, error) {
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithSharedCredentialsFiles([]string{credentialsPath}),
config.WithSharedConfigProfile(roleName),
config.WithDefaultRegion(region))
config.WithRegion(region))
if err != nil {
err = errors.Wrapf(err, "error retrieving AWS SSO credentials")
}
Expand Down
44 changes: 9 additions & 35 deletions pkg/cmd/ecr-login.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ package cmd

import (
"context"
"encoding/json"
"os"
"encoding/base64"
"strings"

utils "github.com/Ridecell/ridectl/pkg/utils"
"github.com/Ridecell/ridectl/pkg/exec"
"github.com/aws/aws-sdk-go-v2/service/ecr"
"github.com/pterm/pterm"

Expand Down Expand Up @@ -66,41 +66,15 @@ var ecrLoginCmd = &cobra.Command{
return errors.Wrapf(err, "error creating ECR auth token")
}

// Create docker creds using ECR Auth token output
ecrAuth := map[string]string{
"auth": *output.AuthorizationData[0].AuthorizationToken,
}

// Load existing ~/.docker/config.json file if exists, create if not present.
userHomeDir, err := os.UserHomeDir()
// Decode Auth Token and extract ECR auth password
decodedToken, err := base64.StdEncoding.DecodeString(*output.AuthorizationData[0].AuthorizationToken)
if err != nil {
pterm.Error.Printf("error getting user home directory: %v", err)
os.Exit(1)
}
dockerDir := userHomeDir + "/.docker"
utils.CreateDirIfNotPresent(dockerDir)

// Load existing ~/.docker/config.json file if exists
dockerConfig := map[string]interface{}{}
configData, _ := os.ReadFile(dockerDir + "/config.json")
if configData != nil {
_ = json.Unmarshal(configData, &dockerConfig)
return errors.Wrapf(err, "error decoding ECR auth token")
}
ecrAuth := strings.TrimPrefix(string(decodedToken), "AWS:")

// Add/Update docker creds
if _, ok := dockerConfig["auths"]; ok {
dockerConfig["auths"].(map[string]interface{})[*output.AuthorizationData[0].ProxyEndpoint] = ecrAuth
} else {
dockerConfig["auths"] = map[string]interface{}{
*output.AuthorizationData[0].ProxyEndpoint: ecrAuth,
}
}

byteValue, err := json.MarshalIndent(dockerConfig, "", " ")
if err != nil {
return err
}
err = os.WriteFile(dockerDir+"/config.json", byteValue, 0600)
dockerArgs := []string{"login", "--username", "AWS", *output.AuthorizationData[0].ProxyEndpoint, "--password", ecrAuth}
err = exec.ExecuteCommand("docker", dockerArgs, false)
if err == nil {
pterm.Success.Println("ECR login successful. NOTE: These credentials are only valid for 12 hours.")
}
Expand Down