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: made ECR type checker flexible to accept the server w/o https #111

Merged
merged 6 commits into from
Aug 16, 2022

Conversation

devashish-patel
Copy link
Contributor

Resolves #110

@devashish-patel devashish-patel marked this pull request as ready for review August 9, 2022 21:51
@devashish-patel devashish-patel requested a review from a team as a code owner August 9, 2022 21:51
@devashish-patel devashish-patel self-assigned this Aug 9, 2022
Copy link
Contributor

@lbajolet-hashicorp lbajolet-hashicorp left a comment

Choose a reason for hiding this comment

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

I just have a comment on the current behaviour for the GetECRType with regards to invalid URIs, I feel we can do better, but it's probably outside the scope of this PR.

The code as proposed LGTM!

typ, _ = awsConfig.GetEcrType("google.com")
if typ != docker.Invalid {
msg := fmt.Sprintf("ECR type should be %v", docker.Invalid)
if typ != docker.Private {
Copy link
Contributor

Choose a reason for hiding this comment

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

I know the current code behaves like this now that we forcefully introduce a schema, and I'd imagine before the current change, if one specified https://google.com this would end-up in the docker.Private state, so this is probably out-of-scope for this PR, but is there any heuristic we can use to weed out easy invalids like this one?
Naively I'd think something like checking if the host is not the the shape of \.dkr\.ecr\.[^.]+\.amazonaws\.com we know it's not a private ECR URL?
Maybe I'm overthinking this, but I'd for sure prefer we reject more explicitely when the URI's not an ECR one, AFAICT there's basically two shapes for those so we can pinpoint them more accurately imho.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, before we introduced this code, we did not have any checks. We used to rely upon the AWS SDK. My thought process is to filter out ECR Public/Private URLs if we can otherwise let the AWS SDK handle the error raising as it used to be. And one more thing is that this part of the code is executed only for the AWS, so this is a minor edge case.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm in the same light that we can probably do a bit more than force the HTTPS schema. It looks to me that the check for the protocol is not necessary at all, and that we should support URLs both with and without the schema. Is my observation correct?

That said, it looks like you are using the GetEcrType to determine if an authorization token should be obtained for the Public ECR repository or for a Private repository. The difference being just the region. Is that correct?

If so I think we can probably have logic that says if login server host is public.ecr.aws then we should get a PublicAuthorization token, for everything else we should treat as private. That said it is not clear to me if a user can CNAME the public gallery URL to something custom to the user, which in that case could be an issue for the auto detection. If CNAMing is an option should there be a configuration argument public_ecr_gallery (bool) that if true would get a Public authorization token?

Lastly for the docker login command it looks like it does not matter if you pass it a login server with or without a protocol. So this should not be an issue unless I'm mistaken on the supported values.

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe I'm overthinking this, but I'd for sure prefer we reject more explicitely when the URI's not an ECR one, AFAICT there's basically two shapes for those so we can pinpoint them more accurately imho.

I don't you are over thinking here. Generally speaking we should try to validate for these values when possible. I don't know if it is as simple as checking for AWS owned domain if CNAMing is possible. Which is probably why the original code relied on the API to fail. This is not me saying we shouldn't it is more like we need to do research to make sure it does not run into the same issues as checking for a schema on the URL.

- Add an optional flag to identify whether to force push image to ECR
Public
- Rectify logic to identify if the image should be pushed Public/Private
- Simplify code by using the new flag to authenticate with AWS
- Add test cases
@devashish-patel devashish-patel requested review from lbajolet-hashicorp and nywilken and removed request for nywilken August 12, 2022 23:02
Copy link
Contributor

@lbajolet-hashicorp lbajolet-hashicorp left a comment

Choose a reason for hiding this comment

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

I have a few comments/nits on the code, the logic looks OK to me. I'll approve when they're addressed

// ECR. If the user sets this to `true` from the config, we will forcefully
// try to push to Public ECR otherwise set this from code based on the
// given LoginServer value
PublicEcrGallery bool `mapstructure:"public_ecr_gallery" required:"false"`
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd be maindful of the technical refs here since this comment is the effective user-facing documentation for the flag. I'd recommend we get more to-the-point with this, and leave out the refs to LoginServer here

Copy link
Contributor

Choose a reason for hiding this comment

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

OK, now that I looked at the implementation, I think I misunderstood this flag's use. I assumed that we removed all kinds of automatic detection with the introduction of this flag, but this is not the case, this flag bypasses the check.

Maybe we should rename it to aws_force_use_public_ecr? That'd make the intent clearer I think.
If we don't go the force route, for consistency I'd recommend prefixing the option name with aws_ still, it'd make it clear this setting is AWS-specific.

@@ -38,39 +37,30 @@ type AwsAccessConfig struct {
// communicate with AWS. Learn how to set
Copy link
Contributor

Choose a reason for hiding this comment

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

Out-of-scope but this caught my attention: the Learn how to set this. feels a bit brutal when we read the docs as it points to nothing, we should probably either remove that sentence, or make it point to a learning guide

"login_server": "https://public.ecr.aws/j9y7g6y8/dev_hc_pkr_dkr_test_1"
}
]
]
Copy link
Contributor

Choose a reason for hiding this comment

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

The indentation looks off here, is it automatically formatted by the renderer? Otherwise I'd recommend fixing this, it makes it hard to read.

@devashish-patel devashish-patel merged commit 0a103ca into main Aug 16, 2022
@devashish-patel devashish-patel deleted the dpatel/110-ecr-url-validation branch August 16, 2022 16:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ECR Login fails in Packer 1.8.3
3 participants