forked from LiorAlafiArmo/registryx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
165 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package registryclients | ||
|
||
import ( | ||
"context" | ||
"github.com/Masterminds/semver/v3" | ||
"github.com/armosec/registryx/common" | ||
"github.com/armosec/registryx/interfaces" | ||
"github.com/google/go-containerregistry/pkg/authn" | ||
"github.com/google/go-containerregistry/pkg/v1/remote" | ||
"slices" | ||
"sort" | ||
"strings" | ||
) | ||
|
||
const ( | ||
latestTag = "latest" | ||
) | ||
|
||
func getAllRepositories(ctx context.Context, registry interfaces.IRegistry) ([]string, error) { | ||
var repos, pageRepos []string | ||
var nextPage *common.PaginationOption | ||
var err error | ||
|
||
firstPage := common.MakePagination(registry.GetMaxPageSize()) | ||
catalogOpts := common.CatalogOption{} | ||
|
||
for pageRepos, nextPage, err = registry.Catalog(ctx, firstPage, catalogOpts, authn.FromConfig(*registry.GetAuth())); ; pageRepos, nextPage, err = registry.Catalog(ctx, *nextPage, catalogOpts, authn.FromConfig(*registry.GetAuth())) { | ||
if err != nil { | ||
return nil, err | ||
} | ||
if len(pageRepos) == 0 { | ||
break | ||
} | ||
repos = append(repos, pageRepos...) | ||
|
||
if nextPage == nil || nextPage.Cursor == "" { | ||
break | ||
} | ||
} | ||
return repos, nil | ||
} | ||
|
||
func getImageLatestTag(repo string, registry interfaces.IRegistry) (string, error) { | ||
firstPage := common.MakePagination(1000) | ||
var tags []string | ||
withAuth := remote.WithAuth(authn.FromConfig(*registry.GetAuth())) | ||
if latestTags, err := registry.GetLatestTags(repo, 1, withAuth); err == nil { | ||
for _, tag := range latestTags { | ||
if strings.HasSuffix(tag, ".sig") { | ||
continue | ||
} | ||
tagsForDigest := strings.Split(tag, ",") | ||
return tagsForDigest[0], nil | ||
} | ||
} else { | ||
for tagsPage, nextPage, err := registry.List(repo, firstPage, withAuth); ; tagsPage, nextPage, err = registry.List(repo, *nextPage, withAuth) { | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if slices.Contains(tagsPage, latestTag) { | ||
return latestTag, nil | ||
} | ||
|
||
tags = append(tags, tagsPage...) | ||
|
||
if nextPage == nil { | ||
break | ||
} | ||
} | ||
return getLatestTag(tags), nil | ||
} | ||
return "", nil | ||
} | ||
|
||
func getLatestTag(tags []string) string { | ||
var versions []*semver.Version | ||
for _, tag := range tags { | ||
version, err := semver.NewVersion(tag) | ||
if err == nil { | ||
versions = append(versions, version) | ||
} | ||
} | ||
sort.Sort(sort.Reverse(semver.Collection(versions))) | ||
return versions[0].String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package registryclients | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/armosec/armoapi-go/armotypes" | ||
"github.com/armosec/registryx/common" | ||
"github.com/armosec/registryx/registries/harbor" | ||
dockerregistry "github.com/docker/docker/api/types/registry" | ||
"github.com/google/go-containerregistry/pkg/authn" | ||
"github.com/google/go-containerregistry/pkg/name" | ||
) | ||
|
||
type HarborRegistryClient struct { | ||
Registry *armotypes.HarborImageRegistry | ||
} | ||
|
||
func (h *HarborRegistryClient) GetAllRepositories(ctx context.Context) ([]string, error) { | ||
registry, err := name.NewRegistry(h.Registry.InstanceURL) | ||
if err != nil { | ||
return nil, err | ||
} | ||
iRegistry, err := harbor.NewHarborRegistry(&authn.AuthConfig{Username: h.Registry.Username, Password: h.Registry.Password}, ®istry, &common.RegistryOptions{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return getAllRepositories(ctx, iRegistry) | ||
} | ||
|
||
func (h *HarborRegistryClient) GetImagesToScan(_ context.Context) (map[string]string, error) { | ||
registry, err := name.NewRegistry(h.Registry.InstanceURL) | ||
if err != nil { | ||
return nil, err | ||
} | ||
iRegistry, err := harbor.NewHarborRegistry(&authn.AuthConfig{Username: h.Registry.Username, Password: h.Registry.Password}, ®istry, &common.RegistryOptions{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
images := make(map[string]string, len(h.Registry.Repositories)) | ||
for _, repository := range h.Registry.Repositories { | ||
tag, err := getImageLatestTag(repository, iRegistry) | ||
if err != nil { | ||
return nil, err | ||
} else if tag == "" { | ||
return nil, fmt.Errorf("failed to find latest tag for repository %s", repository) | ||
} | ||
images[fmt.Sprintf("%s/%s", h.Registry.InstanceURL, repository)] = tag | ||
} | ||
return images, nil | ||
} | ||
|
||
func (h *HarborRegistryClient) GetDockerAuth() *dockerregistry.AuthConfig { | ||
return &dockerregistry.AuthConfig{ | ||
Username: h.Registry.Username, | ||
Password: h.Registry.Password, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters