api/server/backend/build: sanitizeRepoAndTags() check for digest

The reference.ParseNormalizedNamed() utility already returns a Named
reference, but we're interested in wether the digest has a digest, so
check for that.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-11-14 19:15:55 +01:00
parent 8bb58153e7
commit 4769809555
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C

View file

@ -21,14 +21,10 @@ func tagImages(ic ImageComponent, stdout io.Writer, imageID image.ID, repoAndTag
}
// sanitizeRepoAndTags parses the raw "t" parameter received from the client
// to a slice of repoAndTag.
// It also validates each repoName and tag.
func sanitizeRepoAndTags(names []string) ([]reference.Named, error) {
var (
repoAndTags []reference.Named
// This map is used for deduplicating the "-t" parameter.
uniqNames = make(map[string]struct{})
)
// to a slice of repoAndTag. It removes duplicates, and validates each name
// to not contain a digest.
func sanitizeRepoAndTags(names []string) (repoAndTags []reference.Named, err error) {
uniqNames := map[string]struct{}{}
for _, repo := range names {
if repo == "" {
continue
@ -39,14 +35,12 @@ func sanitizeRepoAndTags(names []string) ([]reference.Named, error) {
return nil, err
}
if _, isCanonical := ref.(reference.Canonical); isCanonical {
if _, ok := ref.(reference.Digested); ok {
return nil, errors.New("build tag cannot contain a digest")
}
ref = reference.TagNameOnly(ref)
nameWithTag := ref.String()
if _, exists := uniqNames[nameWithTag]; !exists {
uniqNames[nameWithTag] = struct{}{}
repoAndTags = append(repoAndTags, ref)