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

docker/reference: add IsFullIdentifier #1476

Merged
merged 1 commit into from Feb 24, 2022

Conversation

vrothberg
Copy link
Member

containers/common/libimage is in need to check whether a given input
looks like a full image ID (i.e., a 64-byte hex string). Since
compiling regexes has a negative impact on init- and run-time, let's
expose a function to check whether a given string matches the already
compiled regex in docker/reference.

Signed-off-by: Valentin Rothberg vrothberg@redhat.com

@mtrmac PTAL
I want it for optimizing the fixes for containers/podman#12761.

Copy link
Collaborator

@mtrmac mtrmac left a comment

Choose a reason for hiding this comment

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

I realize I’m probably being a pest, still… what’s the full story here?

(Conceptually, I’d expect a “parse input of $syntax” function that is the ~authoritative implementation of $syntax; not an API of “try parsing the input as $syntax1; if it failed, try $syntax2; if it failed, try $syntax3”. This new function could well be a part of the first “authoritative parser” implementation, it just raises my suspicion :) )

Specifically WRT containers/podman#12761 , compare

} else if img, err := store.Image(possibleID); err == nil && img != nil && len(possibleID) >= minimumTruncatedIDLength && strings.HasPrefix(img.ID, possibleID) {
and
if id == "" && len(ref) >= minimumTruncatedIDLength && !strings.ContainsAny(ref, "@:") {
enforcing a minimumTruncatedIDLength (OTOH the contents check is fairly weak, and using a stricter regex might be valuable). That’s, AFAICT, not the case in lookupImageInLocalStorage, where store.Image() accepts an unambiguous prefix of any length.

I’m also not sure how detecting a full identifier changes handling of short identifiers, perhaps that’s only tangentially related?

Is this just a locally-scoped change to modify the one

                if strings.HasPrefix(name, "sha256:") || (len(name) == 64 && !strings.ContainsAny(name, "/.:@")) {

check in Runtime.Pull? Consider ParseAnyReference — that would work fine for the ID syntaxes, OTOH the Named returned would not be usable, so that’s probably not better.

(Completely unrelated; The HasPrefix(name, "sha256:") check seems to be always false.)


Implementation looks OK, of course. From a maintenance POV, I’d prefer adding it to a separate file (regexp-additions.go)? so that the correspondence with distribution/distribution/reference is clear and easier to maintain — OTOH the test is really much better when integrated into the existing one, and placing the test and the function differently is not all that attractive.

@mtrmac mtrmac changed the title docker/referece: add IsFullIdentifier docker/reference: add IsFullIdentifier Feb 23, 2022
@vrothberg
Copy link
Member Author

I realize I’m probably being a pest, still… what’s the full story here?

🤣

I’m also not sure how detecting a full identifier changes handling of short identifiers, perhaps that’s only tangentially related?

I want it for an optimization. containers/podman#12761 happens because we're doing a lookupImageInLocalStorage() with the input before doing the short-name expansion. I want to keep looking up an image with it's full ID as fast as possible (i.e., immediately) without it being subject to short-name expansion with N look ups. In order to do that, I need check whether the input is a 64-byte hex string.

Here's the PR for c/common that should make my intention clearer: containers/common#939

@vrothberg
Copy link
Member Author

There's another issue with tagging that I noticed:
podman tag $image $64-byte-hex does not error out but should -.-

@mtrmac
Copy link
Collaborator

mtrmac commented Feb 23, 2022

containers/podman#12761 happens because we're doing a lookupImageInLocalStorage() with the input before doing the short-name expansion. I want to keep looking up an image with it's full ID as fast as possible (i.e., immediately) without it being subject to short-name expansion with N look ups. In order to do that, I need check whether the input is a 64-byte hex string.

OK, that’s actually getting us closer to a consistent parser — where the 64-byte hex strings are only ever interpreted one way. Nice.

LGTM; consider moving it to a separate file, but that’s quite a weak preference.

@vrothberg
Copy link
Member Author

LGTM; consider moving it to a separate file, but that’s quite a weak preference.

Thanks! Done ✔️

@mtrmac
Copy link
Collaborator

mtrmac commented Feb 23, 2022

Looks like CI is frequently broken now…

@vrothberg
Copy link
Member Author

Looks like CI is frequently broken now…

--- FAIL: TestReadKeyring (0.00s)
keyring_test.go:185: expected to read 1 userkeyring, but get 2

I saw this one a number of times this week.

@mtrmac
Copy link
Collaborator

mtrmac commented Feb 23, 2022

Looks like CI is frequently broken now…

#1477 , for the record.

@vrothberg
Copy link
Member Author

green

@TomSweeneyRedHat
Copy link
Member

@vrothberg you may need to rebase
changes LGTM otherwise

containers/common/libimage is in need to check whether a given input
looks like a full image ID (i.e., a 64-byte hex string).  Since
compiling regexes has a negative impact on init- and run-time, let's
expose a function to check whether a given string matches the already
compiled regex in docker/reference.

Signed-off-by: Valentin Rothberg <vrothberg@redhat.com>
@vrothberg vrothberg merged commit 1045fb7 into containers:main Feb 24, 2022
@vrothberg vrothberg deleted the full-id-regex branch February 24, 2022 10:01
vrothberg added a commit to vrothberg/common that referenced this pull request Feb 24, 2022
Mainly to pull in changes from the following PR which I will use in
another commit: containers/image#1476

Signed-off-by: Valentin Rothberg <vrothberg@redhat.com>
vrothberg added a commit to vrothberg/common that referenced this pull request Feb 24, 2022
Mainly to pull in changes from the following PR which I will use in
another commit: containers/image#1476

Signed-off-by: Valentin Rothberg <vrothberg@redhat.com>
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.

None yet

3 participants