Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add imgpkg-session-id to http headers #600

Merged
merged 1 commit into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pkg/imgpkg/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"crypto/tls"
"crypto/x509"
"fmt"
"math/rand"
"net/http"
"os"
"regexp"
Expand Down Expand Up @@ -42,6 +43,8 @@ type Opts struct {

EnvironFunc func() []string
ActiveKeychains []auth.IAASKeychain

SessionID string
}

// DeepCopy the options to a new struct
Expand Down Expand Up @@ -194,6 +197,12 @@ func NewSimpleRegistryWithTransport(opts Opts, rTripper http.RoundTripper) (*Sim
baseRoundTripper = transport.NewLogger(rTripper)
}

sessionID := opts.SessionID
if sessionID == "" {
sessionID = fmt.Sprint(rand.Intn(9999999999))
}
baseRoundTripper = NewImgpkgRoundTripper(baseRoundTripper, sessionID)

// Wrap the transport in something that can retry network flakes.
baseRoundTripper = transport.NewRetry(baseRoundTripper, transport.WithRetryBackoff(retryBackoff))

Expand Down
43 changes: 43 additions & 0 deletions pkg/imgpkg/registry/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,49 @@ func TestRegistry_Digest(t *testing.T) {
})
}

func TestRegistry_TransportHeaders(t *testing.T) {
t.Run("when doing request to registry, imgpkg sends the header imgpkg-session-id", func(t *testing.T) {
expectedDigest := "sha256:477c34d98f9e090a4441cf82d2f1f03e64c8eb730e8c1ef39a8595e685d4df65"
server := createServer(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Docker-Content-Digest", expectedDigest)
require.Equal(t, "673062197574995717", r.Header.Get("imgpkg-session-id"))
})
defer server.Close()
u, err := url.Parse(server.URL)
require.NoError(t, err)

subject, err := registry.NewSimpleRegistry(registry.Opts{SessionID: "673062197574995717"})
require.NoError(t, err)

imgRef, err := name.ParseReference(fmt.Sprintf("%s/repo:latest", u.Host))
require.NoError(t, err)
_, err = subject.Digest(imgRef)
require.NoError(t, err)
})

t.Run("when doing 2 request to registry, the value in the header imgpkg-session-id does not change", func(t *testing.T) {
expectedDigest := "sha256:477c34d98f9e090a4441cf82d2f1f03e64c8eb730e8c1ef39a8595e685d4df65"
server := createServer(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Docker-Content-Digest", expectedDigest)
require.Equal(t, "673062197574995717", r.Header.Get("imgpkg-session-id"))
})
defer server.Close()
u, err := url.Parse(server.URL)
require.NoError(t, err)

subject, err := registry.NewSimpleRegistry(registry.Opts{SessionID: "673062197574995717"})
require.NoError(t, err)

imgRef, err := name.ParseReference(fmt.Sprintf("%s/repo:latest", u.Host))
require.NoError(t, err)
_, err = subject.Digest(imgRef)
require.NoError(t, err)

_, err = subject.Digest(imgRef)
require.NoError(t, err)
})
}

func createServer(handler func(w http.ResponseWriter, r *http.Request)) *httptest.Server {
response := []byte("doesn't matter")
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
20 changes: 20 additions & 0 deletions pkg/imgpkg/registry/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,23 @@ func (n NoopRoundTripperStorage) CreateRoundTripper(_ regname.Registry, _ authn.
func (n NoopRoundTripperStorage) BaseRoundTripper() http.RoundTripper {
return nil
}

// NewImgpkgRoundTripper creates a RoundTripper that will add headers to the request
func NewImgpkgRoundTripper(parent http.RoundTripper, sessionID string) *ImgpkgRoundTripper {
return &ImgpkgRoundTripper{
parent: parent,
sessionID: sessionID,
}
}

// ImgpkgRoundTripper RoundTripper that adds headers to request
type ImgpkgRoundTripper struct {
parent http.RoundTripper
sessionID string
}

// RoundTrip changes the request to add headers and calls the parent RoundTrip
func (i *ImgpkgRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Add("imgpkg-session-id", i.sessionID)
return i.parent.RoundTrip(req)
}
Loading