mirror of
https://github.com/containrrr/watchtower.git
synced 2026-03-05 04:10:17 +01:00
add some more tests and debug logs
This commit is contained in:
parent
2b68874087
commit
0c704ce02b
9 changed files with 727 additions and 39 deletions
|
|
@ -87,7 +87,7 @@ func GetBearerToken(ctx context.Context, challenge string, img string, err error
|
|||
return "", err
|
||||
}
|
||||
|
||||
if credentials.Username != "" && credentials.Password != "" {
|
||||
if credentials != nil && credentials.Username != "" && credentials.Password != "" {
|
||||
log.WithField("credentials", credentials).Debug("Found credentials. Adding basic auth.")
|
||||
r.SetBasicAuth(credentials.Username, credentials.Password)
|
||||
} else {
|
||||
|
|
@ -124,15 +124,21 @@ func GetAuthURL(challenge string, img string) (*url2.URL, error) {
|
|||
val := strings.Trim(kv[1], "\"")
|
||||
values[key] = val
|
||||
}
|
||||
|
||||
if values["realm"] == "" || values["service"] == "" || values["scope"] == "" {
|
||||
return nil, fmt.Errorf("challenge header did not include all values needed to construct an auth url")
|
||||
if values["realm"] == "" || values["service"] == "" {
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"realm": values["realm"],
|
||||
"service": values["service"],
|
||||
}).Debug("Checking challenge header content")
|
||||
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"]))
|
||||
q := authURL.Query()
|
||||
q.Add("service", values["service"])
|
||||
scopeImage := strings.TrimPrefix(img, values["service"])
|
||||
if !strings.Contains(scopeImage, "/") {
|
||||
scopeImage = "library/" + scopeImage
|
||||
}
|
||||
scope := fmt.Sprintf("repository:%s:pull", scopeImage)
|
||||
q.Add("scope", scope)
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,12 @@
|
|||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/containrrr/watchtower/pkg/logger"
|
||||
wtTypes "github.com/containrrr/watchtower/pkg/types"
|
||||
"github.com/docker/docker/api/types"
|
||||
"net/url"
|
||||
"os"
|
||||
"testing"
|
||||
. "github.com/onsi/gomega"
|
||||
. "github.com/onsi/ginkgo"
|
||||
|
|
@ -11,9 +16,46 @@ func TestAuth(t *testing.T) {
|
|||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "Registry Auth Suite")
|
||||
}
|
||||
func SkipIfCredentialsEmpty(credentials *wtTypes.RegistryCredentials, fn func()) func() {
|
||||
if credentials.Username == "" {
|
||||
return func() {
|
||||
Skip("Username missing. Skipping integration test")
|
||||
}
|
||||
} else if credentials.Password == "" {
|
||||
return func() {
|
||||
Skip("Password missing. Skipping integration test")
|
||||
}
|
||||
} else {
|
||||
return fn
|
||||
}
|
||||
}
|
||||
|
||||
var GHCRCredentials = &wtTypes.RegistryCredentials{
|
||||
Username: os.Getenv("CI_INTEGRATION_TEST_REGISTRY_GH_USERNAME"),
|
||||
Password: os.Getenv("CI_INTEGRATION_TEST_REGISTRY_GH_PASSWORD"),
|
||||
}
|
||||
|
||||
var _ = Describe("the auth module", func() {
|
||||
var ctx = logger.AddDebugLogger(context.Background())
|
||||
var ghImage = types.ImageInspect{
|
||||
ID: "sha256:6972c414f322dfa40324df3c503d4b217ccdec6d576e408ed10437f508f4181b",
|
||||
RepoTags: []string{
|
||||
"ghcr.io/k6io/operator:latest",
|
||||
},
|
||||
RepoDigests: []string{
|
||||
"ghcr.io/k6io/operator@sha256:d68e1e532088964195ad3a0a71526bc2f11a78de0def85629beb75e2265f0547",
|
||||
},
|
||||
}
|
||||
|
||||
When("getting an auth url", func() {
|
||||
It("should parse the token from the response",
|
||||
SkipIfCredentialsEmpty(GHCRCredentials, func() {
|
||||
token, err := GetToken(ctx, ghImage, GHCRCredentials)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(token).NotTo(Equal(""))
|
||||
}),
|
||||
)
|
||||
|
||||
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"`
|
||||
expected := &url.URL{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue