mirror of
https://github.com/project-zot/zot.git
synced 2026-06-18 05:28:07 +08:00
e072aa09e2
This commit modernizes code across multiple packages by: - Using Go 1.18+ features (slices.IndexFunc, strings.Cut) - Pre-allocating slices and maps with known capacity - Consolidating defensive checks and improving code clarity - Fixing test data and build tag issues CLI client improvements: - Pre-allocate slices in search functions and service methods - Replace strings.Split with strings.Cut for username:password parsing - Use range-based iteration instead of manual index loops Search extension optimizations: - Cache sort functions in pagination modules - Pre-allocate page buffers and maps - Consolidate defensive checks in filterBaseImages/filterDerivedImages - Fix image bas and derived logic allowing out of sequence layers for base images - Fix image pagination reporting images groupped by repos when sorted by update time - Remove duplicate resolver_test.go file Monitoring extension: - Replace manual loops with slices.IndexFunc - Pre-allocate bucketsFloat2String map Sync extension: - Pre-allocate slice in parseRegistryURLs Test utilities: - Fix build tags in oci_layout.go Signed-off-by: Andrei Aaron <andreifdaaron@gmail.com>
133 lines
2.4 KiB
Go
133 lines
2.4 KiB
Go
//go:build sync
|
|
|
|
package sync
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/url"
|
|
"os"
|
|
"path"
|
|
"strings"
|
|
|
|
"github.com/opencontainers/go-digest"
|
|
ispec "github.com/opencontainers/image-spec/specs-go/v1"
|
|
|
|
syncconf "zotregistry.dev/zot/v2/pkg/extensions/config/sync"
|
|
)
|
|
|
|
// Get sync.FileCredentials from file.
|
|
func getFileCredentials(filepath string) (syncconf.CredentialsFile, error) {
|
|
credsFile, err := os.ReadFile(filepath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var creds syncconf.CredentialsFile
|
|
|
|
err = json.Unmarshal(credsFile, &creds)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return creds, nil
|
|
}
|
|
|
|
// parse a reference, return its digest and if it's valid.
|
|
func parseReference(reference string) (digest.Digest, bool) {
|
|
var ok bool
|
|
|
|
d, err := digest.Parse(reference)
|
|
if err == nil {
|
|
ok = true
|
|
}
|
|
|
|
return d, ok
|
|
}
|
|
|
|
// Given a list of registry string URLs parse them and return *url.URLs slice.
|
|
func parseRegistryURLs(rawURLs []string) ([]*url.URL, error) {
|
|
urls := make([]*url.URL, 0, len(rawURLs))
|
|
|
|
for _, rawURL := range rawURLs {
|
|
u, err := url.Parse(rawURL)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
urls = append(urls, u)
|
|
}
|
|
|
|
return urls, nil
|
|
}
|
|
|
|
func GetDescriptorReference(desc ispec.Descriptor) string {
|
|
v, ok := desc.Annotations[ispec.AnnotationRefName]
|
|
if ok {
|
|
return v
|
|
}
|
|
|
|
return desc.Digest.String()
|
|
}
|
|
|
|
func StripRegistryTransport(url string) string {
|
|
return strings.Replace(strings.Replace(url, "http://", "", 1), "https://", "", 1)
|
|
}
|
|
|
|
func getCertificates(certDir string) (string, string, string, error) {
|
|
var clientCert string
|
|
|
|
var clientKey string
|
|
|
|
var regCert string
|
|
|
|
files, err := os.ReadDir(certDir)
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
return "", "", "", nil
|
|
}
|
|
|
|
return "", "", "", err
|
|
}
|
|
|
|
for _, file := range files {
|
|
if file.IsDir() {
|
|
continue
|
|
}
|
|
|
|
if strings.HasSuffix(file.Name(), ".cert") {
|
|
certPath := path.Join(certDir, file.Name())
|
|
|
|
buf, err := os.ReadFile(certPath)
|
|
if err != nil {
|
|
return "", "", "", err
|
|
}
|
|
|
|
clientCert = string(buf)
|
|
}
|
|
|
|
if strings.HasSuffix(file.Name(), ".key") {
|
|
certPath := path.Join(certDir, file.Name())
|
|
|
|
buf, err := os.ReadFile(certPath)
|
|
if err != nil {
|
|
return "", "", "", err
|
|
}
|
|
|
|
clientKey = string(buf)
|
|
}
|
|
|
|
if strings.HasSuffix(file.Name(), ".crt") {
|
|
certPath := path.Join(certDir, file.Name())
|
|
|
|
buf, err := os.ReadFile(certPath)
|
|
if err != nil {
|
|
return "", "", "", err
|
|
}
|
|
|
|
regCert = string(buf)
|
|
}
|
|
}
|
|
|
|
return clientCert, clientKey, regCert, nil
|
|
}
|