-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
provide custom RoundTripper to the blob.Downloader
- Loading branch information
Showing
6 changed files
with
220 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package keychain | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
|
||
"github.com/google/go-containerregistry/pkg/authn" | ||
) | ||
|
||
func NewHTTPClient(keychain authn.Keychain) *http.Client { | ||
return &http.Client{ | ||
Transport: &roundTripper{ | ||
keychain: keychain, | ||
inner: http.DefaultTransport, | ||
}, | ||
} | ||
} | ||
|
||
type roundTripper struct { | ||
keychain authn.Keychain | ||
inner http.RoundTripper | ||
} | ||
|
||
func (rt *roundTripper) RoundTrip(req *http.Request) (*http.Response, error) { | ||
if rt.keychain == nil { | ||
return rt.inner.RoundTrip(req) | ||
} | ||
|
||
authenticator, err := rt.keychain.Resolve(&urlResource{url: req.URL}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if authenticator == authn.Anonymous { | ||
return rt.inner.RoundTrip(req) | ||
} | ||
|
||
conf, err := authenticator.Authorization() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if conf.RegistryToken != "" { | ||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", conf.RegistryToken)) | ||
} else { | ||
req.SetBasicAuth(conf.Username, conf.Password) | ||
} | ||
|
||
return rt.inner.RoundTrip(req) | ||
} | ||
|
||
type urlResource struct { | ||
url *url.URL | ||
} | ||
|
||
func (r *urlResource) RegistryStr() string { | ||
return r.url.Hostname() | ||
} | ||
|
||
func (r *urlResource) String() string { | ||
return r.RegistryStr() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package keychain_test | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"os" | ||
|
||
"code.cloudfoundry.org/cnbapplifecycle/pkg/keychain" | ||
"github.com/google/go-containerregistry/pkg/authn" | ||
"github.com/jarcoal/httpmock" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("HTTP RoundTripper", func() { | ||
var ( | ||
creds authn.Keychain | ||
client *http.Client | ||
err error | ||
) | ||
|
||
BeforeEach(func() { | ||
creds, err = keychain.FromEnv() | ||
Expect(err).ToNot(HaveOccurred()) | ||
client = keychain.NewHTTPClient(creds) | ||
}) | ||
|
||
Describe("RoundTrip", func() { | ||
It("works when keychain is nil", func() { | ||
httpmock.RegisterResponder("GET", "https://test.io", func(r *http.Request) (*http.Response, error) { | ||
if a := r.Header.Get("Authorization"); a != "" { | ||
return httpmock.NewStringResponse(http.StatusBadRequest, fmt.Sprintf("found authorization header: %q", a)), nil | ||
} | ||
return httpmock.NewStringResponse(http.StatusOK, ""), nil | ||
}) | ||
|
||
client = keychain.NewHTTPClient(nil) | ||
|
||
res, err := client.Get("https://test.io") | ||
Expect(err).ToNot(HaveOccurred()) | ||
defer res.Body.Close() | ||
Expect(res.StatusCode).To(Equal(http.StatusOK)) | ||
}) | ||
|
||
It("works with the default keychain (anonymous)", func() { | ||
httpmock.RegisterResponder("GET", "https://test.io", func(r *http.Request) (*http.Response, error) { | ||
if a := r.Header.Get("Authorization"); a != "" { | ||
return httpmock.NewStringResponse(http.StatusBadRequest, fmt.Sprintf("found authorization header: %q", a)), nil | ||
} | ||
return httpmock.NewStringResponse(http.StatusOK, ""), nil | ||
}) | ||
res, err := client.Get("https://test.io") | ||
Expect(err).ToNot(HaveOccurred()) | ||
defer res.Body.Close() | ||
Expect(res.StatusCode).To(Equal(http.StatusOK)) | ||
}) | ||
|
||
Describe("with credentials", func() { | ||
BeforeEach(func() { | ||
Expect(os.Setenv(keychain.CnbCredentialsEnv, `{"bearer.test":{"token":"foo"},"basic.test":{"username":"foo","password":"bar"}}`)).To(Succeed()) | ||
creds, err = keychain.FromEnv() | ||
Expect(err).ToNot(HaveOccurred()) | ||
client = keychain.NewHTTPClient(creds) | ||
}) | ||
|
||
AfterEach(func() { | ||
Expect(os.Unsetenv(keychain.CnbCredentialsEnv)).To(Succeed()) | ||
}) | ||
|
||
It("sets Authorization header (token)", func() { | ||
httpmock.RegisterResponder("GET", "https://bearer.test", func(r *http.Request) (*http.Response, error) { | ||
authHeader := r.Header.Get("Authorization") | ||
if authHeader == "" { | ||
return httpmock.NewStringResponse(http.StatusBadRequest, "authorization header not found"), nil | ||
} | ||
|
||
return httpmock.NewStringResponse(http.StatusOK, authHeader), nil | ||
}) | ||
|
||
res, err := client.Get("https://bearer.test") | ||
Expect(err).ToNot(HaveOccurred()) | ||
defer res.Body.Close() | ||
Expect(res.StatusCode).To(Equal(http.StatusOK)) | ||
|
||
body := bytes.NewBuffer(nil) | ||
io.Copy(body, res.Body) | ||
Expect(body.String()).To(Equal("Bearer foo")) | ||
}) | ||
|
||
It("sets Authorization header (basic)", func() { | ||
httpmock.RegisterResponder("GET", "https://basic.test", func(r *http.Request) (*http.Response, error) { | ||
authHeader := r.Header.Get("Authorization") | ||
if authHeader == "" { | ||
return httpmock.NewStringResponse(http.StatusBadRequest, "authorization header not found"), nil | ||
} | ||
|
||
return httpmock.NewStringResponse(http.StatusOK, authHeader), nil | ||
}) | ||
|
||
res, err := client.Get("https://basic.test") | ||
Expect(err).ToNot(HaveOccurred()) | ||
defer res.Body.Close() | ||
Expect(res.StatusCode).To(Equal(http.StatusOK)) | ||
|
||
body := bytes.NewBuffer(nil) | ||
io.Copy(body, res.Body) | ||
Expect(body.String()).To(Equal("Basic Zm9vOmJhcg==")) | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters