mirror of
https://github.com/containrrr/watchtower.git
synced 2026-02-23 07:34:06 +01:00
feat: pass context when fetching digests
This commit is contained in:
parent
b71eb2dec7
commit
9220b51665
5 changed files with 40 additions and 33 deletions
|
|
@ -1,35 +1,37 @@
|
|||
package digest
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/containrrr/watchtower/internal/meta"
|
||||
"github.com/containrrr/watchtower/pkg/registry/auth"
|
||||
"github.com/containrrr/watchtower/pkg/registry/manifest"
|
||||
"github.com/containrrr/watchtower/pkg/types"
|
||||
"github.com/sirupsen/logrus"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// ContentDigestHeader is the key for the key-value pair containing the digest header
|
||||
const ContentDigestHeader = "Docker-Content-Digest"
|
||||
|
||||
// CompareDigest ...
|
||||
func CompareDigest(container types.Container, registryAuth string) (bool, error) {
|
||||
func CompareDigest(ctx context.Context, container types.Container, registryAuth string) (bool, error) {
|
||||
if !container.HasImageInfo() {
|
||||
return false, errors.New("container image info missing")
|
||||
}
|
||||
|
||||
|
||||
var digest string
|
||||
|
||||
registryAuth = TransformAuth(registryAuth)
|
||||
token, err := auth.GetToken(container, registryAuth)
|
||||
token, err := auth.GetToken(ctx, container, registryAuth)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
|
@ -39,7 +41,7 @@ func CompareDigest(container types.Container, registryAuth string) (bool, error)
|
|||
return false, err
|
||||
}
|
||||
|
||||
if digest, err = GetDigest(digestURL, token); err != nil {
|
||||
if digest, err = GetDigest(ctx, digestURL, token); err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
|
|
@ -74,7 +76,7 @@ func TransformAuth(registryAuth string) string {
|
|||
}
|
||||
|
||||
// GetDigest from registry using a HEAD request to prevent rate limiting
|
||||
func GetDigest(url string, token string) (string, error) {
|
||||
func GetDigest(ctx context.Context, url string, token string) (string, error) {
|
||||
tr := &http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
DialContext: (&net.Dialer{
|
||||
|
|
@ -90,7 +92,7 @@ func GetDigest(url string, token string) (string, error) {
|
|||
}
|
||||
client := &http.Client{Transport: tr}
|
||||
|
||||
req, _ := http.NewRequest("HEAD", url, nil)
|
||||
req, _ := http.NewRequestWithContext(ctx, "HEAD", url, nil)
|
||||
req.Header.Set("User-Agent", meta.UserAgent)
|
||||
|
||||
if token != "" {
|
||||
|
|
|
|||
|
|
@ -2,20 +2,21 @@ package digest_test
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/containrrr/watchtower/internal/actions/mocks"
|
||||
"github.com/containrrr/watchtower/pkg/registry/digest"
|
||||
wtTypes "github.com/containrrr/watchtower/pkg/types"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
"github.com/onsi/gomega/ghttp"
|
||||
"net/http"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func TestDigest(t *testing.T) {
|
||||
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(GinkgoT(), "Digest Suite")
|
||||
}
|
||||
|
|
@ -29,6 +30,7 @@ var (
|
|||
Username: os.Getenv("CI_INTEGRATION_TEST_REGISTRY_GH_USERNAME"),
|
||||
Password: os.Getenv("CI_INTEGRATION_TEST_REGISTRY_GH_PASSWORD"),
|
||||
}
|
||||
ctx = context.Background()
|
||||
)
|
||||
|
||||
func SkipIfCredentialsEmpty(credentials *wtTypes.RegistryCredentials, fn func()) func() {
|
||||
|
|
@ -65,7 +67,7 @@ var _ = Describe("Digests", func() {
|
|||
It("should return true if digests match",
|
||||
SkipIfCredentialsEmpty(GHCRCredentials, func() {
|
||||
creds := fmt.Sprintf("%s:%s", GHCRCredentials.Username, GHCRCredentials.Password)
|
||||
matches, err := digest.CompareDigest(mockContainer, creds)
|
||||
matches, err := digest.CompareDigest(ctx, mockContainer, creds)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(matches).To(Equal(true))
|
||||
}),
|
||||
|
|
@ -78,7 +80,7 @@ var _ = Describe("Digests", func() {
|
|||
|
||||
})
|
||||
It("should return an error when container contains no image info", func() {
|
||||
matches, err := digest.CompareDigest(mockContainerNoImage, `user:pass`)
|
||||
matches, err := digest.CompareDigest(ctx, mockContainerNoImage, `user:pass`)
|
||||
Expect(err).To(HaveOccurred())
|
||||
Expect(matches).To(Equal(false))
|
||||
})
|
||||
|
|
@ -116,7 +118,7 @@ var _ = Describe("Digests", func() {
|
|||
}),
|
||||
),
|
||||
)
|
||||
dig, err := digest.GetDigest(server.URL(), "token")
|
||||
dig, err := digest.GetDigest(ctx, server.URL(), "token")
|
||||
Expect(server.ReceivedRequests()).Should(HaveLen(1))
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(dig).To(Equal(mockDigest))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue