From 7e6ba573cfc1e48dde13f2768472d7a3c8717c42 Mon Sep 17 00:00:00 2001 From: pohanhuangtw Date: Mon, 18 Nov 2024 04:19:58 +0000 Subject: [PATCH] [NVSHAS-9507] Fix JFrog fetch tag issue. --- controller/scan/jfrog.go | 24 +++++++++++++++++++++++- share/scan/registry/tags.go | 14 +++++++++----- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/controller/scan/jfrog.go b/controller/scan/jfrog.go index 31283478e..b38284c91 100644 --- a/controller/scan/jfrog.go +++ b/controller/scan/jfrog.go @@ -216,6 +216,23 @@ func getSubdomainFromRepo(repo string) (string, string) { return "", "" } +// Only be called by in jfrog, since the api is not working for it, create it as a callback function. +func (r *jfrog) GetArtifactoryTags(repositoryStr string, rc *scanUtils.RegClient) ([]string, error) { + tags := make([]string, 0) + var err error + parts := strings.Split(repositoryStr, "/") + if len(parts) != 2 { + log.WithFields(log.Fields{"repository": repositoryStr}).Error("Invalid input format") + return tags, err + } + + key := parts[0] + repository := parts[1] + + url := r.url("/artifactory/api/docker/%s/v2/%s/tags/list", key, repository) + return rc.FetchTagsPaginated(url, repositoryStr) +} + func (r *jfrog) GetTagList(domain, repo, tag string) ([]string, error) { smd.scanLog.Debug() @@ -232,7 +249,12 @@ func (r *jfrog) GetTagList(domain, repo, tag string) ([]string, error) { } repo = subRepo } - return rc.Tags(repo) + + if tags, err := rc.Tags(repo); len(tags) > 0 { + return tags, err + } + + return r.GetArtifactoryTags(repo, rc) } func (r *jfrog) GetAllImages() (map[share.CLUSImage][]string, error) { diff --git a/share/scan/registry/tags.go b/share/scan/registry/tags.go index 34d27c0b4..fc88c0caa 100644 --- a/share/scan/registry/tags.go +++ b/share/scan/registry/tags.go @@ -8,16 +8,14 @@ type tagsResponse struct { Tags []string `json:"tags"` } -func (r *Registry) Tags(repository string) ([]string, error) { - url := r.url("/v2/%s/tags/list", repository) +func (r *Registry) FetchTagsPaginated(url, repositoryStr string) ([]string, error) { tags := make([]string, 0) - var response tagsResponse var err error r.Client.SetTimeout(longTimeout) for { - log.WithFields(log.Fields{"url": url, "repository": repository}).Debug() + log.WithFields(log.Fields{"url": url, "repository": repositoryStr}).Debug() url, err = r.getPaginatedJson(url, &response) switch err { case ErrNoMorePages: @@ -27,7 +25,13 @@ func (r *Registry) Tags(repository string) ([]string, error) { tags = append(tags, response.Tags...) continue default: - return tags, nil + log.WithFields(log.Fields{"error": err, "url": url}).Error("Failed to fetch tags") + return tags, err } } } + +func (r *Registry) Tags(repository string) ([]string, error) { + url := r.url("/v2/%s/tags/list", repository) + return r.FetchTagsPaginated(url, repository) +}