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

fix verify ECR images in another region. #660

Merged
merged 1 commit into from
Jan 13, 2024
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 tests/ci/ecs-task-def.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
},
{
"essential": true,
"image": "debian:buster-slim",
"image": "{{ must_env `AWS_ACCOUNT_ID` }}.dkr.ecr.us-east-1.amazonaws.com/bash:latest",
"logConfiguration": {
"logDriver": "awslogs",
"options": {
Expand Down
39 changes: 28 additions & 11 deletions verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ type verifier struct {
cwl *cloudwatchlogs.Client
ssm *ssm.Client
secretsmanager *secretsmanager.Client
ecr *ecr.Client
ecr map[string]*ecr.Client
s3 *s3.Client
opt *VerifyOption
isAssumed bool
execCfg *aws.Config
}

func (v *verifier) IsAssumed() bool {
Expand All @@ -48,11 +49,25 @@ func newVerifier(execCfg, appCfg *aws.Config, opt *VerifyOption) *verifier {
cwl: cloudwatchlogs.NewFromConfig(*execCfg),
ssm: ssm.NewFromConfig(*execCfg),
secretsmanager: secretsmanager.NewFromConfig(*execCfg),
ecr: ecr.NewFromConfig(*execCfg),
s3: s3.NewFromConfig(*appCfg),
opt: opt,
isAssumed: execCfg != appCfg,
ecr: map[string]*ecr.Client{
execCfg.Region: ecr.NewFromConfig(*execCfg),
},
s3: s3.NewFromConfig(*appCfg),
opt: opt,
isAssumed: execCfg != appCfg,
execCfg: execCfg,
}
}

func (v *verifier) ecrClient(region string) *ecr.Client {
if c, ok := v.ecr[region]; ok {
return c
}
cfg := v.execCfg.Copy()
cfg.Region = region
client := ecr.NewFromConfig(cfg)
v.ecr[region] = client
return client
}

func (v *verifier) existsSecretValue(ctx context.Context, from string) error {
Expand Down Expand Up @@ -378,12 +393,12 @@ func (d *App) verifyTaskDefinition(ctx context.Context) error {
}

var (
ecrImageURLRegex = regexp.MustCompile(`dkr\.ecr\..+.amazonaws\.com/.*`)
// e.g. {aws_account_id}.dkr.ecr.{region}.amazonaws.com/amazonlinux:latest
ecrImageURLRegex = regexp.MustCompile(`^([0-9]+)\.dkr\.ecr\.([0-9a-zA-Z-]+)\.amazonaws\.com/.*`)
)

func (d *App) verifyECRImage(ctx context.Context, image string) error {
d.Log("[DEBUG] VERIFY ECR Image")
out, err := d.verifier.ecr.GetAuthorizationToken(
func (d *App) verifyECRImage(ctx context.Context, image, region string) error {
out, err := d.verifier.ecrClient(region).GetAuthorizationToken(
ctx,
&ecr.GetAuthorizationTokenInput{},
)
Expand Down Expand Up @@ -494,9 +509,11 @@ func (d *App) verifyImage(ctx context.Context, image string) error {
if image == "" {
return errors.New("image is not defined")
}
if ecrImageURLRegex.MatchString(image) {
return d.verifyECRImage(ctx, image)
if m := ecrImageURLRegex.FindStringSubmatch(image); len(m) == 3 {
d.Log("[DEBUG] VERIFY ECR Image %s in region %s", image, m[2])
return d.verifyECRImage(ctx, image, m[2]) // m[1] is aws account id, m[2] is region
}
d.Log("[DEBUG] VERIFY Registry Image %s", image)
return d.verifyRegistryImage(ctx, image, "", "")
}

Expand Down