2022-04-27 01:00:20 -05:00
|
|
|
//go:build search || ui_base
|
|
|
|
// +build search ui_base
|
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"
|
2020-07-17 14:42:22 -05:00
|
|
|
"crypto/tls"
|
2021-07-06 11:50:46 -05:00
|
|
|
"crypto/x509"
|
2020-06-16 20:52:40 -05:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2021-07-06 11:50:46 -05:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2020-06-16 20:52:40 -05:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2021-12-03 22:50:58 -05:00
|
|
|
zotErrors "zotregistry.io/zot/errors"
|
|
|
|
"zotregistry.io/zot/pkg/storage"
|
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-07-06 11:50:46 -05:00
|
|
|
func createHTTPClient(verifyTLS bool, host string) *http.Client {
|
2022-03-21 12:37:23 -05:00
|
|
|
htr := http.DefaultTransport.(*http.Transport).Clone() //nolint: forcetypeassert
|
2020-07-17 14:42:22 -05:00
|
|
|
if !verifyTLS {
|
2021-12-13 14:23:31 -05:00
|
|
|
htr.TLSClientConfig = &tls.Config{InsecureSkipVerify: true} //nolint: gosec
|
2021-07-06 11:50:46 -05:00
|
|
|
|
|
|
|
return &http.Client{
|
|
|
|
Timeout: httpTimeout,
|
2021-12-13 14:23:31 -05:00
|
|
|
Transport: htr,
|
2021-07-06 11:50:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a copy of the system cert pool
|
|
|
|
caCertPool, _ := x509.SystemCertPool()
|
|
|
|
|
|
|
|
tlsConfig := loadPerHostCerts(caCertPool, host)
|
|
|
|
if tlsConfig == nil {
|
2021-12-13 14:23:31 -05:00
|
|
|
tlsConfig = &tls.Config{RootCAs: caCertPool, MinVersion: tls.VersionTLS12}
|
2020-07-17 14:42:22 -05:00
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
htr = &http.Transport{TLSClientConfig: tlsConfig}
|
2021-07-06 11:50:46 -05:00
|
|
|
|
2020-06-16 20:52:40 -05:00
|
|
|
return &http.Client{
|
2020-07-17 14:42:22 -05:00
|
|
|
Timeout: httpTimeout,
|
2021-12-13 14:23:31 -05:00
|
|
|
Transport: htr,
|
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-03-21 12:37:23 -05:00
|
|
|
verifyTLS bool, resultsPtr interface{},
|
|
|
|
) (http.Header, error) {
|
2021-12-13 14:23:31 -05:00
|
|
|
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
|
2020-06-16 20:52:40 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.SetBasicAuth(username, password)
|
|
|
|
|
2020-07-06 17:44:32 -05:00
|
|
|
return doHTTPRequest(req, verifyTLS, resultsPtr)
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
func makeGraphQLRequest(ctx context.Context, url, query, username,
|
2022-03-21 12:37:23 -05:00
|
|
|
password string, verifyTLS bool, resultsPtr interface{},
|
|
|
|
) error {
|
2021-12-13 14:23:31 -05:00
|
|
|
req, err := http.NewRequestWithContext(ctx, "GET", 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")
|
|
|
|
|
|
|
|
_, err = doHTTPRequest(req, verifyTLS, resultsPtr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func doHTTPRequest(req *http.Request, verifyTLS bool, resultsPtr interface{}) (http.Header, error) {
|
2021-07-06 11:50:46 -05:00
|
|
|
var httpClient *http.Client
|
|
|
|
|
|
|
|
host := req.Host
|
|
|
|
|
|
|
|
httpClientLock.Lock()
|
|
|
|
|
|
|
|
if httpClientsMap[host] == nil {
|
|
|
|
httpClient = createHTTPClient(verifyTLS, host)
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
2020-06-16 20:52:40 -05:00
|
|
|
resp, err := httpClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
if resp.StatusCode == http.StatusUnauthorized {
|
|
|
|
return nil, zotErrors.ErrUnauthorizedAccess
|
|
|
|
}
|
|
|
|
|
|
|
|
bodyBytes, _ := ioutil.ReadAll(resp.Body)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-07-06 11:50:46 -05:00
|
|
|
func loadPerHostCerts(caCertPool *x509.CertPool, host string) *tls.Config {
|
|
|
|
// Check if the /home/user/.config/containers/certs.d/$IP:$PORT dir exists
|
|
|
|
home := os.Getenv("HOME")
|
|
|
|
clientCertsDir := filepath.Join(home, homeCertsDir, host)
|
|
|
|
|
2021-11-10 09:31:03 -05:00
|
|
|
if storage.DirExists(clientCertsDir) {
|
2021-07-06 11:50:46 -05:00
|
|
|
tlsConfig, err := getTLSConfig(clientCertsDir, caCertPool)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
return tlsConfig
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the /etc/containers/certs.d/$IP:$PORT dir exists
|
|
|
|
clientCertsDir = filepath.Join(certsPath, host)
|
2021-11-10 09:31:03 -05:00
|
|
|
if storage.DirExists(clientCertsDir) {
|
2021-07-06 11:50:46 -05:00
|
|
|
tlsConfig, err := getTLSConfig(clientCertsDir, caCertPool)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
return tlsConfig
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getTLSConfig(certsPath string, caCertPool *x509.CertPool) (*tls.Config, error) {
|
|
|
|
clientCert := filepath.Join(certsPath, clientCertFilename)
|
|
|
|
clientKey := filepath.Join(certsPath, clientKeyFilename)
|
|
|
|
caCertFile := filepath.Join(certsPath, caCertFilename)
|
|
|
|
|
|
|
|
cert, err := tls.LoadX509KeyPair(clientCert, clientKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
caCert, err := ioutil.ReadFile(caCertFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
caCertPool.AppendCertsFromPEM(caCert)
|
|
|
|
|
|
|
|
return &tls.Config{
|
|
|
|
Certificates: []tls.Certificate{cert},
|
|
|
|
RootCAs: caCertPool,
|
2021-12-13 14:23:31 -05:00
|
|
|
MinVersion: tls.VersionTLS12,
|
2021-07-06 11:50:46 -05:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-06-16 20:52:40 -05:00
|
|
|
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,
|
|
|
|
*job.config.verifyTLS, &job.manifestResp)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
digest := header.Get("docker-content-digest")
|
|
|
|
digest = strings.TrimPrefix(digest, "sha256:")
|
|
|
|
|
2021-05-28 11:27:17 -05:00
|
|
|
configDigest := job.manifestResp.Config.Digest
|
|
|
|
configDigest = strings.TrimPrefix(configDigest, "sha256:")
|
|
|
|
|
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,
|
|
|
|
Digest: strings.TrimPrefix(entry.Digest, "sha256:"),
|
|
|
|
},
|
|
|
|
)
|
2020-06-16 20:52:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
image := &imageStruct{}
|
2021-05-28 11:27:17 -05:00
|
|
|
image.verbose = *job.config.verbose
|
2020-06-16 20:52:40 -05:00
|
|
|
image.Name = job.imageName
|
|
|
|
image.Tags = []tags{
|
|
|
|
{
|
2021-05-28 11:27:17 -05:00
|
|
|
Name: job.tagName,
|
|
|
|
Digest: digest,
|
|
|
|
Size: size,
|
|
|
|
ConfigDigest: configDigest,
|
|
|
|
Layers: layers,
|
2020-06-16 20:52:40 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-07-17 14:42:22 -05:00
|
|
|
str, err := image.string(*job.config.outputFormat)
|
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
|
|
|
|
}
|