Skip to content

Commit

Permalink
pkg/registry: improve logging, tests & (test) readability
Browse files Browse the repository at this point in the history
  • Loading branch information
Pwuts committed Jan 22, 2023
1 parent 905a38f commit 6d64f81
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 76 deletions.
6 changes: 3 additions & 3 deletions pkg/registry/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"strings"
Expand All @@ -26,7 +26,7 @@ func GetToken(container types.Container, registryAuth string) (string, error) {
}

URL := GetChallengeURL(normalizedRef)
logrus.WithField("URL", URL.String()).Debug("Building challenge URL")
logrus.WithField("URL", URL.String()).Debug("Built challenge URL")

var req *http.Request
if req, err = GetChallengeRequest(URL); err != nil {
Expand Down Expand Up @@ -99,7 +99,7 @@ func GetBearerHeader(challenge string, imageRef ref.Named, registryAuth string)
return "", err
}

body, _ := ioutil.ReadAll(authResponse.Body)
body, _ := io.ReadAll(authResponse.Body)
tokenResponse := &types.TokenResponse{}

err = json.Unmarshal(body, tokenResponse)
Expand Down
54 changes: 28 additions & 26 deletions pkg/registry/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package auth_test

import (
"fmt"
"github.com/containrrr/watchtower/internal/actions/mocks"
"github.com/containrrr/watchtower/pkg/registry/auth"
"net/url"
"os"
"strings"
"testing"
"time"

"github.com/containrrr/watchtower/internal/actions/mocks"
"github.com/containrrr/watchtower/pkg/registry/auth"
wtTypes "github.com/containrrr/watchtower/pkg/types"
ref "github.com/docker/distribution/reference"
. "github.com/onsi/ginkgo"
Expand Down Expand Up @@ -53,7 +53,7 @@ var _ = Describe("the auth module", func() {
mockCreated,
mockDigest)

When("getting an auth url", func() {
Describe("GetToken", func() {
It("should parse the token from the response",
SkipIfCredentialsEmpty(GHCRCredentials, func() {
creds := fmt.Sprintf("%s:%s", GHCRCredentials.Username, GHCRCredentials.Password)
Expand All @@ -62,64 +62,67 @@ var _ = Describe("the auth module", func() {
Expect(token).NotTo(Equal(""))
}),
)
})

Describe("GetAuthURL", func() {
It("should create a valid auth url object based on the challenge header supplied", func() {
input := `bearer realm="https://ghcr.io/token",service="ghcr.io",scope="repository:user/image:pull"`
challenge := `bearer realm="https://ghcr.io/token",service="ghcr.io",scope="repository:user/image:pull"`
imageRef, _ := ref.ParseNormalizedNamed("containrrr/watchtower")
expected := &url.URL{
Host: "ghcr.io",
Scheme: "https",
Path: "/token",
RawQuery: "scope=repository%3Acontainrrr%2Fwatchtower%3Apull&service=ghcr.io",
}
imageRef, _ := ref.ParseNormalizedNamed("containrrr/watchtower")
res, err := auth.GetAuthURL(input, imageRef)

URL, err := auth.GetAuthURL(challenge, imageRef)
Expect(err).NotTo(HaveOccurred())
Expect(res).To(Equal(expected))
Expect(URL).To(Equal(expected))
})
It("should create a valid auth url object based on the challenge header supplied", func() {
input := `bearer realm="https://ghcr.io/token"`
imageRef, _ := ref.ParseNormalizedNamed("containrrr/watchtower")
res, err := auth.GetAuthURL(input, imageRef)
Expect(err).To(HaveOccurred())
Expect(res).To(BeNil())

When("given an invalid challenge header", func() {
It("should return an error", func() {
challenge := `bearer realm="https://ghcr.io/token"`
imageRef, _ := ref.ParseNormalizedNamed("containrrr/watchtower")
URL, err := auth.GetAuthURL(challenge, imageRef)
Expect(err).To(HaveOccurred())
Expect(URL).To(BeNil())
})
})

When("deriving the auth scope from an image name", func() {
It("should prepend official dockerhub images with \"library/\"", func() {
Expect(getScopeFromImageAuthURL("registry")).To(Equal("library/registry"))
Expect(getScopeFromImageAuthURL("docker.io/registry")).To(Equal("library/registry"))
Expect(getScopeFromImageAuthURL("docker.io/registry")).To(Equal("library/registry"))
Expect(getScopeFromImageAuthURL("index.docker.io/registry")).To(Equal("library/registry"))
Expect(getScopeFromImageAuthURL("index.docker.io/registry")).To(Equal("library/registry"))

Expect(getScopeFromImageAuthURL("registry")).To(Equal("library/registry"))
Expect(getScopeFromImageAuthURL("registry")).To(Equal("library/registry"))
})
It("should not include vanity hosts\"", func() {
Expect(getScopeFromImageAuthURL("docker.io/containrrr/watchtower")).To(Equal("containrrr/watchtower"))
Expect(getScopeFromImageAuthURL("index.docker.io/containrrr/watchtower")).To(Equal("containrrr/watchtower"))
})
It("should not destroy three segment image names\"", func() {
Expect(getScopeFromImageAuthURL("piksel/containrrr/watchtower")).To(Equal("piksel/containrrr/watchtower"))
Expect(getScopeFromImageAuthURL("piksel/containrrr/watchtower")).To(Equal("piksel/containrrr/watchtower"))
Expect(getScopeFromImageAuthURL("ghcr.io/piksel/containrrr/watchtower")).To(Equal("piksel/containrrr/watchtower"))
})
It("should not add \"library/\" for one segment image names if they're not on dockerhub", func() {
It("should not prepend library/ to image names if they're not on dockerhub", func() {
Expect(getScopeFromImageAuthURL("ghcr.io/watchtower")).To(Equal("watchtower"))
// Expect(getScopeFromImageName("watchtower")).To(Equal("watchtower"))
Expect(getScopeFromImageAuthURL("ghcr.io/containrrr/watchtower")).To(Equal("containrrr/watchtower"))
})
})
})
When("getting a challenge url", func() {

Describe("GetChallengeURL", func() {
It("should create a valid challenge url object based on the image ref supplied", func() {
expected := url.URL{Host: "ghcr.io", Scheme: "https", Path: "/v2/"}
imageRef, _ := ref.ParseNormalizedNamed("ghcr.io/containrrr/watchtower:latest")
Expect(auth.GetChallengeURL(imageRef)).To(Equal(expected))
})
It("should assume docker hub if the image ref is not fully qualified", func() {
It("should assume Docker Hub for image refs with no explicit registry", func() {
expected := url.URL{Host: "index.docker.io", Scheme: "https", Path: "/v2/"}
imageRef, _ := ref.ParseNormalizedNamed("containrrr/watchtower:latest")
Expect(auth.GetChallengeURL(imageRef)).To(Equal(expected))
})
It("should use docker hub if the image ref specifies docker.io", func() {
It("should use index.docker.io if the image ref specifies docker.io", func() {
expected := url.URL{Host: "index.docker.io", Scheme: "https", Path: "/v2/"}
imageRef, _ := ref.ParseNormalizedNamed("docker.io/containrrr/watchtower:latest")
Expect(auth.GetChallengeURL(imageRef)).To(Equal(expected))
Expand All @@ -130,9 +133,8 @@ var _ = Describe("the auth module", func() {
var scopeImageRegexp = MatchRegexp("^repository:[a-z0-9]+(/[a-z0-9]+)*:pull$")

func getScopeFromImageAuthURL(imageName string) string {
challenge := `bearer realm="https://dummy.host/token",service="dummy.host",scope="repository:user/image:pull"`

normalizedRef, _ := ref.ParseNormalizedNamed(imageName)
challenge := `bearer realm="https://dummy.host/token",service="dummy.host",scope="repository:user/image:pull"`
URL, _ := auth.GetAuthURL(challenge, normalizedRef)

scope := URL.Query().Get("scope")
Expand Down
1 change: 1 addition & 0 deletions pkg/registry/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/docker/distribution/reference"
)

// domains for Docker Hub, the default registry
const (
DefaultRegistryDomain = "docker.io"
DefaultRegistryHost = "index.docker.io"
Expand Down
8 changes: 6 additions & 2 deletions pkg/registry/helpers/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,19 @@ var _ = Describe("the helpers", func() {
_, err := GetRegistryAddress("")
Expect(err).To(HaveOccurred())
})
It("should return index.docker.io if passed an image name with no explicit domain", func() {
It("should return index.docker.io for image refs with no explicit registry", func() {
Expect(GetRegistryAddress("watchtower")).To(Equal("index.docker.io"))
Expect(GetRegistryAddress("containrrr/watchtower")).To(Equal("index.docker.io"))
})
It("should return index.docker.io for image refs with docker.io domain", func() {
Expect(GetRegistryAddress("docker.io/watchtower")).To(Equal("index.docker.io"))
Expect(GetRegistryAddress("docker.io/containrrr/watchtower")).To(Equal("index.docker.io"))
})
It("should return the host if passed an image name containing a local host", func() {
Expect(GetRegistryAddress("henk:80/watchtower")).To(Equal("henk:80"))
Expect(GetRegistryAddress("localhost/watchtower")).To(Equal("localhost"))
})
It("should return the server name if passed a fully qualified image name", func() {
It("should return the server address if passed a fully qualified image name", func() {
Expect(GetRegistryAddress("github.com/containrrr/config")).To(Equal("github.com"))
})
})
Expand Down
13 changes: 4 additions & 9 deletions pkg/registry/manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ func BuildManifestURL(container types.Container) (string, error) {
return "", err
}
normalizedTaggedRef, isTagged := normalizedRef.(ref.NamedTagged)
if !isTagged {
return "", errors.New("Parsed container image ref has no tag: " + normalizedRef.String())
}
if !isTagged {
return "", errors.New("Parsed container image ref has no tag: " + normalizedRef.String())
}

host, _ := helpers.GetRegistryAddress(normalizedTaggedRef.Name())
img, tag := ExtractImageAndTag(normalizedTaggedRef)
img, tag := ref.Path(normalizedTaggedRef), normalizedTaggedRef.Tag()

logrus.WithFields(logrus.Fields{
"image": img,
Expand All @@ -43,8 +43,3 @@ func BuildManifestURL(container types.Container) (string, error) {
}
return url.String(), nil
}

// ExtractImageAndTag from image reference
func ExtractImageAndTag(namedTaggedRef ref.NamedTagged) (string, string) {
return ref.Path(namedTaggedRef), namedTaggedRef.Tag()
}
18 changes: 11 additions & 7 deletions pkg/registry/manifest/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,34 @@ func TestManifest(t *testing.T) {
var _ = Describe("the manifest module", func() {
Describe("BuildManifestURL", func() {
It("should return a valid url given a fully qualified image", func() {
expected := "https://ghcr.io/v2/containrrr/watchtower/manifests/latest"
imageRef := "ghcr.io/containrrr/watchtower:mytag"
expected := "https://ghcr.io/v2/containrrr/watchtower/manifests/mytag"

URL, err := buildMockContainerManifestURL("ghcr.io/containrrr/watchtower:latest")
URL, err := buildMockContainerManifestURL(imageRef)
Expect(err).NotTo(HaveOccurred())
Expect(URL).To(Equal(expected))
})
It("should assume Docker Hub for non-qualified images", func() {
It("should assume Docker Hub for image refs with no explicit registry", func() {
imageRef := "containrrr/watchtower:latest"
expected := "https://index.docker.io/v2/containrrr/watchtower/manifests/latest"

URL, err := buildMockContainerManifestURL("containrrr/watchtower:latest")
URL, err := buildMockContainerManifestURL(imageRef)
Expect(err).NotTo(HaveOccurred())
Expect(URL).To(Equal(expected))
})
It("should assume latest for image refs without an explicit tag", func() {
It("should assume latest for image refs with no explicit tag", func() {
imageRef := "containrrr/watchtower"
expected := "https://index.docker.io/v2/containrrr/watchtower/manifests/latest"

URL, err := buildMockContainerManifestURL("containrrr/watchtower")
URL, err := buildMockContainerManifestURL(imageRef)
Expect(err).NotTo(HaveOccurred())
Expect(URL).To(Equal(expected))
})
It("should not prepend library/ for single-part container names in registries other than Docker Hub", func() {
imageRef := "docker-registry.domain/imagename:latest"
expected := "https://docker-registry.domain/v2/imagename/manifests/latest"

URL, err := buildMockContainerManifestURL("docker-registry.domain/imagename:latest")
URL, err := buildMockContainerManifestURL(imageRef)
Expect(err).NotTo(HaveOccurred())
Expect(URL).To(Equal(expected))
})
Expand Down
8 changes: 4 additions & 4 deletions pkg/registry/trust.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
// loaded from environment variables or docker config
// as available in that order
func EncodedAuth(ref string) (string, error) {
auth, err := EncodedEnvAuth(ref)
auth, err := EncodedEnvAuth()
if err != nil {
auth, err = EncodedConfigAuth(ref)
}
Expand All @@ -28,15 +28,15 @@ func EncodedAuth(ref string) (string, error) {
// EncodedEnvAuth returns an encoded auth config for the given registry
// loaded from environment variables
// Returns an error if authentication environment variables have not been set
func EncodedEnvAuth(ref string) (string, error) {
func EncodedEnvAuth() (string, error) {
username := os.Getenv("REPO_USER")
password := os.Getenv("REPO_PASS")
if username != "" && password != "" {
auth := types.AuthConfig{
Username: username,
Password: password,
}
log.Debugf("Loaded auth credentials for user %s on registry %s", auth.Username, ref)
log.Debugf("Loaded auth credentials for registry user %s from environment", auth.Username)
log.Tracef("Using auth password %s", auth.Password)
return EncodeAuth(auth)
}
Expand All @@ -60,7 +60,7 @@ func EncodedConfigAuth(imageRef string) (string, error) {
}
configFile, err := cliconfig.Load(configDir)
if err != nil {
log.Errorf("Unable to find default config file %s", err)
log.Errorf("Unable to find default config file: %s", err)
return "", err
}
credStore := CredentialsStore(*configFile)
Expand Down
58 changes: 33 additions & 25 deletions pkg/registry/trust_test.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,49 @@
package registry

import (
"os"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"os"
)

var _ = Describe("Testing with Ginkgo", func() {
It("encoded env auth_ should return an error if repo envs are unset", func() {
_ = os.Unsetenv("REPO_USER")
_ = os.Unsetenv("REPO_PASS")

_, err := EncodedEnvAuth("")
Expect(err).To(HaveOccurred())
})
It("encoded env auth_ should return auth hash if repo envs are set", func() {
var err error
expectedHash := "eyJ1c2VybmFtZSI6ImNvbnRhaW5ycnItdXNlciIsInBhc3N3b3JkIjoiY29udGFpbnJyci1wYXNzIn0="
var _ = Describe("Registry credential helpers", func() {
Describe("EncodedAuth", func() {
It("should return repo credentials from env when set", func() {
var err error
expected := "eyJ1c2VybmFtZSI6ImNvbnRhaW5ycnItdXNlciIsInBhc3N3b3JkIjoiY29udGFpbnJyci1wYXNzIn0="

err = os.Setenv("REPO_USER", "containrrr-user")
Expect(err).NotTo(HaveOccurred())
err = os.Setenv("REPO_USER", "containrrr-user")
Expect(err).NotTo(HaveOccurred())

err = os.Setenv("REPO_PASS", "containrrr-pass")
Expect(err).NotTo(HaveOccurred())
err = os.Setenv("REPO_PASS", "containrrr-pass")
Expect(err).NotTo(HaveOccurred())

config, err := EncodedEnvAuth("")
Expect(config).To(Equal(expectedHash))
Expect(err).NotTo(HaveOccurred())
config, err := EncodedEnvAuth()
Expect(config).To(Equal(expected))
Expect(err).NotTo(HaveOccurred())
})
})
It("encoded config auth_ should return an error if file is not present", func() {
var err error

err = os.Setenv("DOCKER_CONFIG", "/dev/null/should-fail")
Expect(err).NotTo(HaveOccurred())
Describe("EncodedEnvAuth", func() {
It("should return an error if repo envs are unset", func() {
_ = os.Unsetenv("REPO_USER")
_ = os.Unsetenv("REPO_PASS")

_, err = EncodedConfigAuth("")
Expect(err).To(HaveOccurred())
_, err := EncodedEnvAuth()
Expect(err).To(HaveOccurred())
})
})

Describe("EncodedConfigAuth", func() {
It("should return an error if file is not present", func() {
var err error

err = os.Setenv("DOCKER_CONFIG", "/dev/null/should-fail")
Expect(err).NotTo(HaveOccurred())

_, err = EncodedConfigAuth("")
Expect(err).To(HaveOccurred())
})
})
})

0 comments on commit 6d64f81

Please sign in to comment.