mirror of
https://github.com/containrrr/watchtower.git
synced 2025-12-16 23:20:12 +01:00
remove redundant context and ignore coverage output
This commit is contained in:
parent
75c62a90e4
commit
870f49bdc9
6 changed files with 22 additions and 25 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -5,3 +5,4 @@ dist
|
||||||
.idea
|
.idea
|
||||||
.DS_Store
|
.DS_Store
|
||||||
/site
|
/site
|
||||||
|
coverage.out
|
||||||
|
|
|
||||||
|
|
@ -294,7 +294,7 @@ func (client dockerClient) PullImage(ctx context.Context, container Container) e
|
||||||
|
|
||||||
log.WithFields(fields).Debugf("Checking if pull is needed")
|
log.WithFields(fields).Debugf("Checking if pull is needed")
|
||||||
|
|
||||||
if match, err := digest.CompareDigest(ctx, container, opts.RegistryAuth); err != nil {
|
if match, err := digest.CompareDigest(container, opts.RegistryAuth); err != nil {
|
||||||
log.Info("Could not do a head request, falling back to regulara pull.")
|
log.Info("Could not do a head request, falling back to regulara pull.")
|
||||||
log.Debugf("Reason: %s", err.Error())
|
log.Debugf("Reason: %s", err.Error())
|
||||||
} else if match {
|
} else if match {
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
package auth
|
package auth
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
@ -11,7 +10,7 @@ import (
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
url2 "net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -19,17 +18,17 @@ import (
|
||||||
const ChallengeHeader = "WWW-Authenticate"
|
const ChallengeHeader = "WWW-Authenticate"
|
||||||
|
|
||||||
// GetToken fetches a token for the registry hosting the provided image
|
// GetToken fetches a token for the registry hosting the provided image
|
||||||
func GetToken(ctx context.Context, container types.Container, registryAuth string) (string, error) {
|
func GetToken(container types.Container, registryAuth string) (string, error) {
|
||||||
var err error
|
var err error
|
||||||
|
var URL url.URL
|
||||||
|
|
||||||
var url url2.URL
|
if URL, err = GetChallengeURL(container.ImageName()); err != nil {
|
||||||
if url, err = GetChallengeURL(container.ImageName()); err != nil {
|
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
logrus.WithField("url", url.String()).Debug("Building challenge URL")
|
logrus.WithField("URL", URL.String()).Debug("Building challenge URL")
|
||||||
|
|
||||||
var req *http.Request
|
var req *http.Request
|
||||||
if req, err = GetChallengeRequest(url); err != nil {
|
if req, err = GetChallengeRequest(URL); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -55,15 +54,15 @@ func GetToken(ctx context.Context, container types.Container, registryAuth strin
|
||||||
return fmt.Sprintf("Basic %s", registryAuth), nil
|
return fmt.Sprintf("Basic %s", registryAuth), nil
|
||||||
}
|
}
|
||||||
if strings.HasPrefix(challenge, "bearer") {
|
if strings.HasPrefix(challenge, "bearer") {
|
||||||
return GetBearerHeader(ctx, challenge, container.ImageName(), err, registryAuth)
|
return GetBearerHeader(challenge, container.ImageName(), err, registryAuth)
|
||||||
}
|
}
|
||||||
|
|
||||||
return "", errors.New("unsupported challenge type from registry")
|
return "", errors.New("unsupported challenge type from registry")
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetChallengeRequest creates a request for getting challenge instructions
|
// GetChallengeRequest creates a request for getting challenge instructions
|
||||||
func GetChallengeRequest(url url2.URL) (*http.Request, error) {
|
func GetChallengeRequest(URL url.URL) (*http.Request, error) {
|
||||||
req, err := http.NewRequest("GET", url.String(), nil)
|
req, err := http.NewRequest("GET", URL.String(), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
@ -73,7 +72,7 @@ func GetChallengeRequest(url url2.URL) (*http.Request, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetBearerHeader tries to fetch a bearer token from the registry based on the challenge instructions
|
// GetBearerHeader tries to fetch a bearer token from the registry based on the challenge instructions
|
||||||
func GetBearerHeader(ctx context.Context, challenge string, img string, err error, registryAuth string) (string, error) {
|
func GetBearerHeader(challenge string, img string, err error, registryAuth string) (string, error) {
|
||||||
client := http.Client{}
|
client := http.Client{}
|
||||||
if strings.Contains(img, ":") {
|
if strings.Contains(img, ":") {
|
||||||
img = strings.Split(img, ":")[0]
|
img = strings.Split(img, ":")[0]
|
||||||
|
|
@ -113,7 +112,7 @@ func GetBearerHeader(ctx context.Context, challenge string, img string, err erro
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAuthURL from the instructions in the challenge
|
// GetAuthURL from the instructions in the challenge
|
||||||
func GetAuthURL(challenge string, img string) (*url2.URL, error) {
|
func GetAuthURL(challenge string, img string) (*url.URL, error) {
|
||||||
loweredChallenge := strings.ToLower(challenge)
|
loweredChallenge := strings.ToLower(challenge)
|
||||||
raw := strings.TrimPrefix(loweredChallenge, "bearer")
|
raw := strings.TrimPrefix(loweredChallenge, "bearer")
|
||||||
|
|
||||||
|
|
@ -136,7 +135,7 @@ func GetAuthURL(challenge string, img string) (*url2.URL, error) {
|
||||||
return nil, fmt.Errorf("challenge header did not include all values needed to construct an auth url")
|
return nil, fmt.Errorf("challenge header did not include all values needed to construct an auth url")
|
||||||
}
|
}
|
||||||
|
|
||||||
authURL, _ := url2.Parse(fmt.Sprintf("%s", values["realm"]))
|
authURL, _ := url.Parse(fmt.Sprintf("%s", values["realm"]))
|
||||||
q := authURL.Query()
|
q := authURL.Query()
|
||||||
q.Add("service", values["service"])
|
q.Add("service", values["service"])
|
||||||
scopeImage := strings.TrimPrefix(img, values["service"])
|
scopeImage := strings.TrimPrefix(img, values["service"])
|
||||||
|
|
@ -152,18 +151,18 @@ func GetAuthURL(challenge string, img string) (*url2.URL, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetChallengeURL creates a URL object based on the image info
|
// GetChallengeURL creates a URL object based on the image info
|
||||||
func GetChallengeURL(img string) (url2.URL, error) {
|
func GetChallengeURL(img string) (url.URL, error) {
|
||||||
|
|
||||||
normalizedNamed, _ := reference.ParseNormalizedNamed(img)
|
normalizedNamed, _ := reference.ParseNormalizedNamed(img)
|
||||||
host, err := helpers.NormalizeRegistry(normalizedNamed.String())
|
host, err := helpers.NormalizeRegistry(normalizedNamed.String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return url2.URL{}, err
|
return url.URL{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
url := url2.URL{
|
URL := url.URL{
|
||||||
Scheme: "https",
|
Scheme: "https",
|
||||||
Host: host,
|
Host: host,
|
||||||
Path: "/v2/",
|
Path: "/v2/",
|
||||||
}
|
}
|
||||||
return url, nil
|
return URL, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
package auth_test
|
package auth_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/containrrr/watchtower/internal/actions/mocks"
|
"github.com/containrrr/watchtower/internal/actions/mocks"
|
||||||
"github.com/containrrr/watchtower/pkg/registry/auth"
|
"github.com/containrrr/watchtower/pkg/registry/auth"
|
||||||
|
|
@ -56,7 +55,7 @@ var _ = Describe("the auth module", func() {
|
||||||
It("should parse the token from the response",
|
It("should parse the token from the response",
|
||||||
SkipIfCredentialsEmpty(GHCRCredentials, func() {
|
SkipIfCredentialsEmpty(GHCRCredentials, func() {
|
||||||
creds := fmt.Sprintf("%s:%s", GHCRCredentials.Username, GHCRCredentials.Password)
|
creds := fmt.Sprintf("%s:%s", GHCRCredentials.Username, GHCRCredentials.Password)
|
||||||
token, err := auth.GetToken(context.Background(), mockContainer, creds)
|
token, err := auth.GetToken(mockContainer, creds)
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
Expect(token).NotTo(Equal(""))
|
Expect(token).NotTo(Equal(""))
|
||||||
}),
|
}),
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
package digest
|
package digest
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
|
@ -19,11 +18,11 @@ import (
|
||||||
const ContentDigestHeader = "Docker-Content-Digest"
|
const ContentDigestHeader = "Docker-Content-Digest"
|
||||||
|
|
||||||
// CompareDigest ...
|
// CompareDigest ...
|
||||||
func CompareDigest(ctx context.Context, container types.Container, registryAuth string) (bool, error) {
|
func CompareDigest(container types.Container, registryAuth string) (bool, error) {
|
||||||
var digest string
|
var digest string
|
||||||
|
|
||||||
registryAuth = TransformAuth(registryAuth)
|
registryAuth = TransformAuth(registryAuth)
|
||||||
token, err := auth.GetToken(ctx, container, registryAuth)
|
token, err := auth.GetToken(container, registryAuth)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
package digest_test
|
package digest_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/containrrr/watchtower/internal/actions/mocks"
|
"github.com/containrrr/watchtower/internal/actions/mocks"
|
||||||
"github.com/containrrr/watchtower/pkg/registry/digest"
|
"github.com/containrrr/watchtower/pkg/registry/digest"
|
||||||
|
|
@ -60,7 +59,7 @@ var _ = Describe("Digests", func() {
|
||||||
It("should return true if digests match",
|
It("should return true if digests match",
|
||||||
SkipIfCredentialsEmpty(GHCRCredentials, func() {
|
SkipIfCredentialsEmpty(GHCRCredentials, func() {
|
||||||
creds := fmt.Sprintf("%s:%s", GHCRCredentials.Username, GHCRCredentials.Password)
|
creds := fmt.Sprintf("%s:%s", GHCRCredentials.Username, GHCRCredentials.Password)
|
||||||
matches, err := digest.CompareDigest(context.Background(), mockContainer, creds)
|
matches, err := digest.CompareDigest(mockContainer, creds)
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
Expect(matches).To(Equal(true))
|
Expect(matches).To(Equal(true))
|
||||||
}),
|
}),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue