Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Factor out the doing of the registry challenge
Browse files Browse the repository at this point in the history
This mainly just to keep method length a little more managable.
  • Loading branch information
squaremo committed Jan 24, 2019
1 parent 3457681 commit 45275ad
Showing 1 changed file with 43 additions and 36 deletions.
79 changes: 43 additions & 36 deletions registry/client_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,48 +44,16 @@ func (t *logging) RoundTrip(req *http.Request) (*http.Response, error) {
return res, err
}

func (f *RemoteClientFactory) ClientFor(repo image.CanonicalName, creds Credentials) (Client, error) {
insecure := false
for _, h := range f.InsecureHosts {
if repo.Domain == h {
insecure = true
break
}
}

tlsConfig := &tls.Config{
InsecureSkipVerify: insecure,
}
// Since we construct one of these per scan, be fairly ruthless
// about throttling the number, and closing of, idle connections.
baseTx := &http.Transport{
TLSClientConfig: tlsConfig,
MaxIdleConns: 10,
IdleConnTimeout: 10 * time.Second,
Proxy: http.ProxyFromEnvironment,
}
tx := f.Limiters.RoundTripper(baseTx, repo.Domain)
if f.Trace {
tx = &logging{f.Logger, tx}
}

f.mu.Lock()
if f.challengeManager == nil {
f.challengeManager = challenge.NewSimpleManager()
}
manager := f.challengeManager
f.mu.Unlock()

func (f *RemoteClientFactory) doChallenge(manager challenge.Manager, tx http.RoundTripper, domain string, insecureOK bool) (*url.URL, error) {
registryURL := url.URL{
Scheme: "https",
Host: repo.Domain,
Host: domain,
Path: "/v2/",
}

// Before we know how to authorise, need to establish which
// authorisation challenges the host will send. See if we've been
// here before.
attemptInsecureFallback := insecure
attemptChallenge:
cs, err := manager.GetChallenges(registryURL)
if err != nil {
Expand All @@ -104,9 +72,9 @@ attemptChallenge:
Transport: tx,
}).Do(req)
if err != nil {
if attemptInsecureFallback {
if insecureOK {
registryURL.Scheme = "http"
attemptInsecureFallback = false
insecureOK = false
goto attemptChallenge
}
return nil, err
Expand All @@ -117,6 +85,45 @@ attemptChallenge:
}
registryURL = *res.Request.URL // <- the URL after any redirection
}
return &registryURL, nil
}

func (f *RemoteClientFactory) ClientFor(repo image.CanonicalName, creds Credentials) (Client, error) {
insecure := false
for _, h := range f.InsecureHosts {
if repo.Domain == h {
insecure = true
break
}
}

tlsConfig := &tls.Config{
InsecureSkipVerify: insecure,
}
// Since we construct one of these per scan, be fairly ruthless
// about throttling the number, and closing of, idle connections.
baseTx := &http.Transport{
TLSClientConfig: tlsConfig,
MaxIdleConns: 10,
IdleConnTimeout: 10 * time.Second,
Proxy: http.ProxyFromEnvironment,
}
tx := f.Limiters.RoundTripper(baseTx, repo.Domain)
if f.Trace {
tx = &logging{f.Logger, tx}
}

f.mu.Lock()
if f.challengeManager == nil {
f.challengeManager = challenge.NewSimpleManager()
}
manager := f.challengeManager
f.mu.Unlock()

registryURL, err := f.doChallenge(manager, tx, repo.Domain, insecure)
if err != nil {
return nil, err
}

cred := creds.credsFor(repo.Domain)
if f.Trace {
Expand Down

0 comments on commit 45275ad

Please sign in to comment.