2022-10-10 07:05:55 -05:00
|
|
|
//go:build search
|
|
|
|
// +build search
|
2020-10-14 16:47:20 -05:00
|
|
|
|
2020-06-16 20:52:40 -05:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2020-07-06 17:44:32 -05:00
|
|
|
"bytes"
|
2020-06-16 20:52:40 -05:00
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2022-09-23 11:24:01 -05:00
|
|
|
"fmt"
|
2022-09-02 07:56:02 -05:00
|
|
|
"io"
|
2020-06-16 20:52:40 -05:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2022-01-19 10:57:10 -05:00
|
|
|
"strconv"
|
2022-11-15 12:06:25 -05:00
|
|
|
"strings"
|
2020-06-16 20:52:40 -05:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2022-11-15 12:06:25 -05:00
|
|
|
notreg "github.com/notaryproject/notation-go/registry"
|
2023-02-13 13:43:52 -05:00
|
|
|
ispec "github.com/opencontainers/image-spec/specs-go/v1"
|
2023-04-27 11:35:10 -05:00
|
|
|
"github.com/sigstore/cosign/v2/pkg/oci/remote"
|
2022-11-15 12:06:25 -05:00
|
|
|
|
2021-12-03 22:50:58 -05:00
|
|
|
zotErrors "zotregistry.io/zot/errors"
|
2022-12-22 13:19:42 -05:00
|
|
|
"zotregistry.io/zot/pkg/common"
|
2020-06-16 20:52:40 -05:00
|
|
|
)
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
var (
|
|
|
|
httpClientsMap = make(map[string]*http.Client) //nolint: gochecknoglobals
|
|
|
|
httpClientLock sync.Mutex //nolint: gochecknoglobals
|
|
|
|
)
|
2020-06-16 20:52:40 -05:00
|
|
|
|
2021-07-06 11:50:46 -05:00
|
|
|
const (
|
|
|
|
httpTimeout = 5 * time.Minute
|
|
|
|
certsPath = "/etc/containers/certs.d"
|
|
|
|
homeCertsDir = ".config/containers/certs.d"
|
|
|
|
clientCertFilename = "client.cert"
|
|
|
|
clientKeyFilename = "client.key"
|
|
|
|
caCertFilename = "ca.crt"
|
|
|
|
)
|
2020-06-16 20:52:40 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
func makeGETRequest(ctx context.Context, url, username, password string,
|
2022-09-23 11:24:01 -05:00
|
|
|
verifyTLS bool, debug bool, resultsPtr interface{}, configWriter io.Writer,
|
2022-03-21 12:37:23 -05:00
|
|
|
) (http.Header, error) {
|
2022-10-05 05:21:14 -05:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
2020-06-16 20:52:40 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.SetBasicAuth(username, password)
|
|
|
|
|
2022-09-23 11:24:01 -05:00
|
|
|
return doHTTPRequest(req, verifyTLS, debug, resultsPtr, configWriter)
|
2020-07-06 17:44:32 -05:00
|
|
|
}
|
|
|
|
|
2023-02-27 14:23:18 -05:00
|
|
|
func makeHEADRequest(ctx context.Context, url, username, password string, verifyTLS bool,
|
|
|
|
debug bool,
|
|
|
|
) (http.Header, error) {
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodHead, url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.SetBasicAuth(username, password)
|
|
|
|
|
|
|
|
return doHTTPRequest(req, verifyTLS, debug, nil, io.Discard)
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
func makeGraphQLRequest(ctx context.Context, url, query, username,
|
2022-09-23 11:24:01 -05:00
|
|
|
password string, verifyTLS bool, debug bool, resultsPtr interface{}, configWriter io.Writer,
|
2022-03-21 12:37:23 -05:00
|
|
|
) error {
|
2022-10-05 05:21:14 -05:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, bytes.NewBufferString(query))
|
2020-07-06 17:44:32 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
q := req.URL.Query()
|
|
|
|
q.Add("query", query)
|
|
|
|
|
|
|
|
req.URL.RawQuery = q.Encode()
|
|
|
|
|
|
|
|
req.SetBasicAuth(username, password)
|
|
|
|
req.Header.Add("Content-Type", "application/json")
|
|
|
|
|
2022-09-23 11:24:01 -05:00
|
|
|
_, err = doHTTPRequest(req, verifyTLS, debug, resultsPtr, configWriter)
|
2020-07-06 17:44:32 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-23 11:24:01 -05:00
|
|
|
func doHTTPRequest(req *http.Request, verifyTLS bool, debug bool,
|
|
|
|
resultsPtr interface{}, configWriter io.Writer,
|
|
|
|
) (http.Header, error) {
|
2021-07-06 11:50:46 -05:00
|
|
|
var httpClient *http.Client
|
|
|
|
|
2022-12-22 13:19:42 -05:00
|
|
|
var err error
|
|
|
|
|
2021-07-06 11:50:46 -05:00
|
|
|
host := req.Host
|
|
|
|
|
|
|
|
httpClientLock.Lock()
|
|
|
|
|
|
|
|
if httpClientsMap[host] == nil {
|
2022-12-22 13:19:42 -05:00
|
|
|
httpClient, err = common.CreateHTTPClient(verifyTLS, host, "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-06 11:50:46 -05:00
|
|
|
|
|
|
|
httpClientsMap[host] = httpClient
|
|
|
|
} else {
|
|
|
|
httpClient = httpClientsMap[host]
|
2020-07-17 14:42:22 -05:00
|
|
|
}
|
|
|
|
|
2021-07-06 11:50:46 -05:00
|
|
|
httpClientLock.Unlock()
|
|
|
|
|
2022-09-23 11:24:01 -05:00
|
|
|
if debug {
|
|
|
|
fmt.Fprintln(configWriter, "[debug] ", req.Method, " ", req.URL, "[request header] ", req.Header)
|
|
|
|
}
|
|
|
|
|
2020-06-16 20:52:40 -05:00
|
|
|
resp, err := httpClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-09-23 11:24:01 -05:00
|
|
|
if debug {
|
|
|
|
fmt.Fprintln(configWriter, "[debug] ", req.Method, req.URL, "[status] ",
|
|
|
|
resp.StatusCode, " ", "[respoonse header] ", resp.Header)
|
|
|
|
}
|
|
|
|
|
2020-06-16 20:52:40 -05:00
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
if resp.StatusCode == http.StatusUnauthorized {
|
|
|
|
return nil, zotErrors.ErrUnauthorizedAccess
|
|
|
|
}
|
|
|
|
|
2022-09-02 07:56:02 -05:00
|
|
|
bodyBytes, _ := io.ReadAll(resp.Body)
|
2020-06-16 20:52:40 -05:00
|
|
|
|
|
|
|
return nil, errors.New(string(bodyBytes)) //nolint: goerr113
|
|
|
|
}
|
|
|
|
|
2023-02-27 14:23:18 -05:00
|
|
|
if resultsPtr == nil {
|
|
|
|
return resp.Header, nil
|
|
|
|
}
|
|
|
|
|
2020-06-16 20:52:40 -05:00
|
|
|
if err := json.NewDecoder(resp.Body).Decode(resultsPtr); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.Header, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func isURL(str string) bool {
|
|
|
|
u, err := url.Parse(str)
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2020-06-16 20:52:40 -05:00
|
|
|
return err == nil && u.Scheme != "" && u.Host != ""
|
|
|
|
} // from https://stackoverflow.com/a/55551215
|
|
|
|
|
|
|
|
type requestsPool struct {
|
2023-02-27 14:23:18 -05:00
|
|
|
jobs chan *httpJob
|
2021-12-13 14:23:31 -05:00
|
|
|
done chan struct{}
|
|
|
|
wtgrp *sync.WaitGroup
|
|
|
|
outputCh chan stringResult
|
2020-06-16 20:52:40 -05:00
|
|
|
}
|
|
|
|
|
2023-02-27 14:23:18 -05:00
|
|
|
type httpJob struct {
|
|
|
|
url string
|
|
|
|
username string
|
|
|
|
password string
|
|
|
|
imageName string
|
|
|
|
tagName string
|
|
|
|
config searchConfig
|
2020-06-16 20:52:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const rateLimiterBuffer = 5000
|
|
|
|
|
2022-03-21 12:37:23 -05:00
|
|
|
func newSmoothRateLimiter(wtgrp *sync.WaitGroup, opch chan stringResult) *requestsPool {
|
2023-02-27 14:23:18 -05:00
|
|
|
ch := make(chan *httpJob, rateLimiterBuffer)
|
2020-06-16 20:52:40 -05:00
|
|
|
|
|
|
|
return &requestsPool{
|
2021-12-13 14:23:31 -05:00
|
|
|
jobs: ch,
|
|
|
|
done: make(chan struct{}),
|
|
|
|
wtgrp: wtgrp,
|
|
|
|
outputCh: opch,
|
2020-06-16 20:52:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// block every "rateLimit" time duration.
|
|
|
|
const rateLimit = 100 * time.Millisecond
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
func (p *requestsPool) startRateLimiter(ctx context.Context) {
|
|
|
|
p.wtgrp.Done()
|
2020-06-16 20:52:40 -05:00
|
|
|
|
|
|
|
throttle := time.NewTicker(rateLimit).C
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case job := <-p.jobs:
|
2021-12-13 14:23:31 -05:00
|
|
|
go p.doJob(ctx, job)
|
2020-06-16 20:52:40 -05:00
|
|
|
case <-p.done:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
<-throttle
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-27 14:23:18 -05:00
|
|
|
func (p *requestsPool) doJob(ctx context.Context, job *httpJob) {
|
2021-12-13 14:23:31 -05:00
|
|
|
defer p.wtgrp.Done()
|
2020-06-16 20:52:40 -05:00
|
|
|
|
2023-02-27 14:23:18 -05:00
|
|
|
// Check manifest media type
|
|
|
|
header, err := makeHEADRequest(ctx, job.url, job.username, job.password, *job.config.verifyTLS,
|
|
|
|
*job.config.debug)
|
2020-06-16 20:52:40 -05:00
|
|
|
if err != nil {
|
2021-12-13 14:23:31 -05:00
|
|
|
if isContextDone(ctx) {
|
2020-06-16 20:52:40 -05:00
|
|
|
return
|
|
|
|
}
|
2020-07-06 17:44:32 -05:00
|
|
|
p.outputCh <- stringResult{"", err}
|
2020-06-16 20:52:40 -05:00
|
|
|
}
|
|
|
|
|
2023-02-27 14:23:18 -05:00
|
|
|
switch header.Get("Content-Type") {
|
|
|
|
case ispec.MediaTypeImageManifest:
|
|
|
|
image, err := fetchImageManifestStruct(ctx, job)
|
|
|
|
if err != nil {
|
|
|
|
if isContextDone(ctx) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.outputCh <- stringResult{"", err}
|
2021-05-28 11:27:17 -05:00
|
|
|
|
2023-02-27 14:23:18 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
platformStr := getPlatformStr(image.Manifests[0].Platform)
|
2020-06-16 20:52:40 -05:00
|
|
|
|
2023-02-27 14:23:18 -05:00
|
|
|
str, err := image.string(*job.config.outputFormat, len(job.imageName), len(job.tagName), len(platformStr))
|
|
|
|
if err != nil {
|
|
|
|
if isContextDone(ctx) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.outputCh <- stringResult{"", err}
|
2021-05-28 11:27:17 -05:00
|
|
|
|
2023-02-27 14:23:18 -05:00
|
|
|
return
|
|
|
|
}
|
2021-05-28 11:27:17 -05:00
|
|
|
|
2023-02-27 14:23:18 -05:00
|
|
|
if isContextDone(ctx) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p.outputCh <- stringResult{str, nil}
|
|
|
|
case ispec.MediaTypeImageIndex:
|
|
|
|
image, err := fetchImageIndexStruct(ctx, job)
|
|
|
|
if err != nil {
|
|
|
|
if isContextDone(ctx) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.outputCh <- stringResult{"", err}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
platformStr := getPlatformStr(image.Manifests[0].Platform)
|
|
|
|
|
|
|
|
str, err := image.string(*job.config.outputFormat, len(job.imageName), len(job.tagName), len(platformStr))
|
|
|
|
if err != nil {
|
|
|
|
if isContextDone(ctx) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.outputCh <- stringResult{"", err}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if isContextDone(ctx) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p.outputCh <- stringResult{str, nil}
|
|
|
|
default:
|
|
|
|
return
|
2020-06-16 20:52:40 -05:00
|
|
|
}
|
2023-02-27 14:23:18 -05:00
|
|
|
}
|
2020-06-16 20:52:40 -05:00
|
|
|
|
2023-02-27 14:23:18 -05:00
|
|
|
func fetchImageIndexStruct(ctx context.Context, job *httpJob) (*imageStruct, error) {
|
|
|
|
var indexContent ispec.Index
|
2022-11-04 12:27:34 -05:00
|
|
|
|
2023-02-27 14:23:18 -05:00
|
|
|
header, err := makeGETRequest(ctx, job.url, job.username, job.password,
|
|
|
|
*job.config.verifyTLS, *job.config.debug, &indexContent, job.config.resultWriter)
|
2022-11-04 12:27:34 -05:00
|
|
|
if err != nil {
|
2023-02-27 14:23:18 -05:00
|
|
|
if isContextDone(ctx) {
|
|
|
|
return nil, context.Canceled
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, err
|
2022-11-04 12:27:34 -05:00
|
|
|
}
|
|
|
|
|
2023-02-27 14:23:18 -05:00
|
|
|
indexDigest := header.Get("docker-content-digest")
|
2022-11-15 12:06:25 -05:00
|
|
|
|
2023-02-27 14:23:18 -05:00
|
|
|
indexSize, err := strconv.ParseInt(header.Get("Content-Length"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-11-15 12:06:25 -05:00
|
|
|
}
|
|
|
|
|
2023-02-27 14:23:18 -05:00
|
|
|
imageSize := indexSize
|
|
|
|
|
|
|
|
manifestList := make([]manifestStruct, 0, len(indexContent.Manifests))
|
2022-11-15 12:06:25 -05:00
|
|
|
|
2023-02-27 14:23:18 -05:00
|
|
|
for _, manifestDescriptor := range indexContent.Manifests {
|
|
|
|
manifest, err := fetchManifestStruct(ctx, job.imageName, manifestDescriptor.Digest.String(),
|
|
|
|
job.config, job.username, job.password)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
imageSize += int64(atoiWithDefault(manifest.Size, 0))
|
|
|
|
|
|
|
|
if manifestDescriptor.Platform != nil {
|
|
|
|
manifest.Platform = platform{
|
|
|
|
Os: manifestDescriptor.Platform.OS,
|
|
|
|
Arch: manifestDescriptor.Platform.Architecture,
|
|
|
|
Variant: manifestDescriptor.Platform.Variant,
|
2022-11-15 12:06:25 -05:00
|
|
|
}
|
|
|
|
}
|
2023-02-27 14:23:18 -05:00
|
|
|
|
|
|
|
manifestList = append(manifestList, manifest)
|
2022-11-15 12:06:25 -05:00
|
|
|
}
|
|
|
|
|
2023-02-27 14:23:18 -05:00
|
|
|
isIndexSigned := isCosignSigned(ctx, job.imageName, indexDigest, job.config, job.username, job.password) ||
|
|
|
|
isNotationSigned(ctx, job.imageName, indexDigest, job.config, job.username, job.password)
|
|
|
|
|
|
|
|
return &imageStruct{
|
|
|
|
RepoName: job.imageName,
|
|
|
|
Tag: job.tagName,
|
2023-03-21 12:16:00 -05:00
|
|
|
Digest: indexDigest,
|
|
|
|
MediaType: ispec.MediaTypeImageIndex,
|
|
|
|
Manifests: manifestList,
|
2023-02-27 14:23:18 -05:00
|
|
|
Size: strconv.FormatInt(imageSize, 10),
|
|
|
|
IsSigned: isIndexSigned,
|
2023-03-21 12:16:00 -05:00
|
|
|
verbose: *job.config.verbose,
|
2023-02-27 14:23:18 -05:00
|
|
|
}, nil
|
|
|
|
}
|
2022-11-04 12:27:34 -05:00
|
|
|
|
2023-02-27 14:23:18 -05:00
|
|
|
func atoiWithDefault(size string, defaultVal int) int {
|
|
|
|
val, err := strconv.Atoi(size)
|
|
|
|
if err != nil {
|
|
|
|
return defaultVal
|
|
|
|
}
|
2020-06-16 20:52:40 -05:00
|
|
|
|
2023-02-27 14:23:18 -05:00
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
|
|
|
func fetchImageManifestStruct(ctx context.Context, job *httpJob) (*imageStruct, error) {
|
|
|
|
manifest, err := fetchManifestStruct(ctx, job.imageName, job.tagName, job.config, job.username, job.password)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &imageStruct{
|
2023-03-21 12:16:00 -05:00
|
|
|
RepoName: job.imageName,
|
|
|
|
Tag: job.tagName,
|
|
|
|
Digest: manifest.Digest,
|
|
|
|
MediaType: ispec.MediaTypeImageManifest,
|
2023-02-27 14:23:18 -05:00
|
|
|
Manifests: []manifestStruct{
|
|
|
|
manifest,
|
|
|
|
},
|
2023-03-21 12:16:00 -05:00
|
|
|
Size: manifest.Size,
|
|
|
|
IsSigned: manifest.IsSigned,
|
|
|
|
verbose: *job.config.verbose,
|
2023-02-27 14:23:18 -05:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func fetchManifestStruct(ctx context.Context, repo, manifestReference string, searchConf searchConfig,
|
|
|
|
username, password string,
|
|
|
|
) (manifestStruct, error) {
|
|
|
|
manifestResp := ispec.Manifest{}
|
|
|
|
|
|
|
|
URL := fmt.Sprintf("%s/v2/%s/manifests/%s",
|
|
|
|
*searchConf.servURL, repo, manifestReference)
|
|
|
|
|
|
|
|
header, err := makeGETRequest(ctx, URL, username, password,
|
|
|
|
*searchConf.verifyTLS, *searchConf.debug, &manifestResp, searchConf.resultWriter)
|
2020-06-16 20:52:40 -05:00
|
|
|
if err != nil {
|
2021-12-13 14:23:31 -05:00
|
|
|
if isContextDone(ctx) {
|
2023-02-27 14:23:18 -05:00
|
|
|
return manifestStruct{}, context.Canceled
|
2020-06-16 20:52:40 -05:00
|
|
|
}
|
|
|
|
|
2023-02-27 14:23:18 -05:00
|
|
|
return manifestStruct{}, err
|
2020-06-16 20:52:40 -05:00
|
|
|
}
|
|
|
|
|
2023-02-27 14:23:18 -05:00
|
|
|
manifestDigest := header.Get("docker-content-digest")
|
|
|
|
configDigest := manifestResp.Config.Digest.String()
|
|
|
|
|
|
|
|
configContent, err := fetchConfig(ctx, repo, configDigest, searchConf, username, password)
|
|
|
|
if err != nil {
|
|
|
|
if isContextDone(ctx) {
|
|
|
|
return manifestStruct{}, context.Canceled
|
|
|
|
}
|
|
|
|
|
|
|
|
return manifestStruct{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
opSys := ""
|
|
|
|
arch := ""
|
|
|
|
variant := ""
|
|
|
|
|
|
|
|
if manifestResp.Config.Platform != nil {
|
|
|
|
opSys = manifestResp.Config.Platform.OS
|
|
|
|
arch = manifestResp.Config.Platform.Architecture
|
|
|
|
variant = manifestResp.Config.Platform.Variant
|
|
|
|
}
|
|
|
|
|
|
|
|
if opSys == "" {
|
|
|
|
opSys = configContent.OS
|
|
|
|
}
|
|
|
|
|
|
|
|
if arch == "" {
|
|
|
|
arch = configContent.Architecture
|
|
|
|
}
|
|
|
|
|
|
|
|
if variant == "" {
|
|
|
|
variant = configContent.Variant
|
|
|
|
}
|
|
|
|
|
|
|
|
manifestSize, err := strconv.ParseInt(header.Get("Content-Length"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return manifestStruct{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var imageSize int64
|
|
|
|
|
|
|
|
imageSize += manifestResp.Config.Size
|
|
|
|
imageSize += manifestSize
|
|
|
|
|
|
|
|
layers := []layer{}
|
|
|
|
|
|
|
|
for _, entry := range manifestResp.Layers {
|
|
|
|
imageSize += entry.Size
|
|
|
|
|
|
|
|
layers = append(
|
|
|
|
layers,
|
|
|
|
layer{
|
|
|
|
Size: entry.Size,
|
|
|
|
Digest: entry.Digest.String(),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
isSigned := isCosignSigned(ctx, repo, manifestDigest, searchConf, username, password) ||
|
|
|
|
isNotationSigned(ctx, repo, manifestDigest, searchConf, username, password)
|
|
|
|
|
|
|
|
return manifestStruct{
|
|
|
|
ConfigDigest: configDigest,
|
|
|
|
Digest: manifestDigest,
|
|
|
|
Layers: layers,
|
|
|
|
Platform: platform{Os: opSys, Arch: arch, Variant: variant},
|
|
|
|
Size: strconv.FormatInt(imageSize, 10),
|
|
|
|
IsSigned: isSigned,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func fetchConfig(ctx context.Context, repo, configDigest string, searchConf searchConfig,
|
|
|
|
username, password string,
|
|
|
|
) (ispec.Image, error) {
|
|
|
|
configContent := ispec.Image{}
|
|
|
|
|
|
|
|
URL := fmt.Sprintf("%s/v2/%s/blobs/%s",
|
|
|
|
*searchConf.servURL, repo, configDigest)
|
|
|
|
|
|
|
|
_, err := makeGETRequest(ctx, URL, username, password,
|
|
|
|
*searchConf.verifyTLS, *searchConf.debug, &configContent, searchConf.resultWriter)
|
|
|
|
if err != nil {
|
|
|
|
if isContextDone(ctx) {
|
|
|
|
return ispec.Image{}, context.Canceled
|
|
|
|
}
|
|
|
|
|
|
|
|
return ispec.Image{}, err
|
2020-06-16 20:52:40 -05:00
|
|
|
}
|
|
|
|
|
2023-02-27 14:23:18 -05:00
|
|
|
return configContent, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func isNotationSigned(ctx context.Context, repo, digestStr string, searchConf searchConfig,
|
|
|
|
username, password string,
|
|
|
|
) bool {
|
|
|
|
var referrers ispec.Index
|
|
|
|
|
|
|
|
URL := fmt.Sprintf("%s/v2/%s/referrers/%s?artifactType=%s",
|
|
|
|
*searchConf.servURL, repo, digestStr, notreg.ArtifactTypeNotation)
|
|
|
|
|
|
|
|
_, err := makeGETRequest(ctx, URL, username, password,
|
|
|
|
*searchConf.verifyTLS, *searchConf.debug, &referrers, searchConf.resultWriter)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-05-10 12:15:33 -05:00
|
|
|
if len(referrers.Manifests) > 0 {
|
|
|
|
return true
|
2023-02-27 14:23:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func isCosignSigned(ctx context.Context, repo, digestStr string, searchConf searchConfig,
|
|
|
|
username, password string,
|
|
|
|
) bool {
|
|
|
|
var result interface{}
|
|
|
|
cosignTag := strings.Replace(digestStr, ":", "-", 1) + "." + remote.SignatureTagSuffix
|
|
|
|
|
|
|
|
URL := fmt.Sprintf("%s/v2/%s/manifests/%s", *searchConf.servURL, repo, cosignTag)
|
|
|
|
|
|
|
|
_, err := makeGETRequest(ctx, URL, username, password, *searchConf.verifyTLS,
|
|
|
|
*searchConf.debug, &result, searchConf.resultWriter)
|
|
|
|
|
|
|
|
return err == nil
|
2020-06-16 20:52:40 -05:00
|
|
|
}
|
|
|
|
|
2023-02-27 14:23:18 -05:00
|
|
|
func (p *requestsPool) submitJob(job *httpJob) {
|
2020-06-16 20:52:40 -05:00
|
|
|
p.jobs <- job
|
|
|
|
}
|