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"
|
|
|
|
"github.com/sigstore/cosign/pkg/oci/remote"
|
|
|
|
|
2021-12-03 22:50:58 -05:00
|
|
|
zotErrors "zotregistry.io/zot/errors"
|
2022-11-15 12:06:25 -05:00
|
|
|
"zotregistry.io/zot/pkg/api"
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2021-12-13 14:23:31 -05:00
|
|
|
jobs chan *manifestJob
|
|
|
|
done chan struct{}
|
|
|
|
wtgrp *sync.WaitGroup
|
|
|
|
outputCh chan stringResult
|
2020-06-16 20:52:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type manifestJob struct {
|
|
|
|
url string
|
|
|
|
username string
|
|
|
|
password string
|
|
|
|
imageName string
|
|
|
|
tagName string
|
2020-07-17 14:42:22 -05:00
|
|
|
config searchConfig
|
2020-06-16 20:52:40 -05:00
|
|
|
manifestResp manifestResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
const rateLimiterBuffer = 5000
|
|
|
|
|
2022-03-21 12:37:23 -05:00
|
|
|
func newSmoothRateLimiter(wtgrp *sync.WaitGroup, opch chan stringResult) *requestsPool {
|
2020-06-16 20:52:40 -05:00
|
|
|
ch := make(chan *manifestJob, rateLimiterBuffer)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
func (p *requestsPool) doJob(ctx context.Context, job *manifestJob) {
|
|
|
|
defer p.wtgrp.Done()
|
2020-06-16 20:52:40 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
header, err := makeGETRequest(ctx, job.url, job.username, job.password,
|
2022-09-23 11:24:01 -05:00
|
|
|
*job.config.verifyTLS, *job.config.debug, &job.manifestResp, job.config.resultWriter)
|
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
|
|
|
}
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
digestStr := header.Get("docker-content-digest")
|
2021-05-28 11:27:17 -05:00
|
|
|
configDigest := job.manifestResp.Config.Digest
|
|
|
|
|
2020-06-16 20:52:40 -05:00
|
|
|
var size uint64
|
|
|
|
|
2021-05-28 11:27:17 -05:00
|
|
|
layers := []layer{}
|
|
|
|
|
|
|
|
for _, entry := range job.manifestResp.Layers {
|
|
|
|
size += entry.Size
|
|
|
|
|
|
|
|
layers = append(
|
|
|
|
layers,
|
|
|
|
layer{
|
|
|
|
Size: entry.Size,
|
2022-10-22 15:46:13 -05:00
|
|
|
Digest: entry.Digest,
|
2021-05-28 11:27:17 -05:00
|
|
|
},
|
|
|
|
)
|
2020-06-16 20:52:40 -05:00
|
|
|
}
|
|
|
|
|
2022-11-04 12:27:34 -05:00
|
|
|
size += uint64(job.manifestResp.Config.Size)
|
|
|
|
|
|
|
|
manifestSize, err := strconv.Atoi(header.Get("Content-Length"))
|
|
|
|
if err != nil {
|
|
|
|
p.outputCh <- stringResult{"", err}
|
|
|
|
}
|
|
|
|
|
2022-11-15 12:06:25 -05:00
|
|
|
isSigned := false
|
|
|
|
cosignTag := strings.Replace(digestStr, ":", "-", 1) + "." + remote.SignatureTagSuffix
|
|
|
|
|
|
|
|
_, err = makeGETRequest(ctx, *job.config.servURL+"/v2/"+job.imageName+
|
|
|
|
"/manifests/"+cosignTag, job.username, job.password,
|
|
|
|
*job.config.verifyTLS, *job.config.debug, &job.manifestResp, job.config.resultWriter)
|
|
|
|
if err == nil {
|
|
|
|
isSigned = true
|
|
|
|
}
|
|
|
|
|
|
|
|
var referrers api.ReferenceList
|
|
|
|
|
|
|
|
if !isSigned {
|
|
|
|
_, err = makeGETRequest(ctx, fmt.Sprintf("%s/oras/artifacts/v1/%s/manifests/%s/referrers?artifactType=%s",
|
|
|
|
*job.config.servURL, job.imageName, digestStr, notreg.ArtifactTypeNotation), job.username, job.password,
|
|
|
|
*job.config.verifyTLS, *job.config.debug, &referrers, job.config.resultWriter)
|
|
|
|
if err == nil {
|
|
|
|
for _, reference := range referrers.References {
|
|
|
|
if reference.ArtifactType == notreg.ArtifactTypeNotation {
|
|
|
|
isSigned = true
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-04 12:27:34 -05:00
|
|
|
size += uint64(manifestSize)
|
|
|
|
|
2020-06-16 20:52:40 -05:00
|
|
|
image := &imageStruct{}
|
2021-05-28 11:27:17 -05:00
|
|
|
image.verbose = *job.config.verbose
|
2022-01-19 10:57:10 -05:00
|
|
|
image.RepoName = job.imageName
|
|
|
|
image.Tag = job.tagName
|
2022-10-22 15:46:13 -05:00
|
|
|
image.Digest = digestStr
|
2022-01-19 10:57:10 -05:00
|
|
|
image.Size = strconv.Itoa(int(size))
|
|
|
|
image.ConfigDigest = configDigest
|
|
|
|
image.Layers = layers
|
2022-11-15 12:06:25 -05:00
|
|
|
image.IsSigned = isSigned
|
2020-06-16 20:52:40 -05:00
|
|
|
|
2022-10-11 10:56:03 -05:00
|
|
|
str, err := image.string(*job.config.outputFormat, len(job.imageName), len(job.tagName))
|
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
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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{str, nil}
|
2020-06-16 20:52:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *requestsPool) submitJob(job *manifestJob) {
|
|
|
|
p.jobs <- job
|
|
|
|
}
|