-
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.
Add credential consumption for protected buildpacks
Co-authored-by: Nicolas Bender <[email protected]>
- Loading branch information
1 parent
2a38840
commit 9283ee5
Showing
8 changed files
with
388 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package keychain | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"os" | ||
|
||
"github.com/google/go-containerregistry/pkg/authn" | ||
) | ||
|
||
const CnbCredentialsEnv = "CNB_REGISTRY_CREDS" | ||
|
||
type auth struct { | ||
Username string `json:"username,omitempty"` | ||
Password string `json:"password,omitempty"` | ||
Token string `json:"token,omitempty"` | ||
} | ||
|
||
func (a auth) config() (authn.AuthConfig, error) { | ||
if !((a.Username != "" && a.Password != "" && a.Token == "") || (a.Token != "" && a.Username == "" && a.Password == "")) { | ||
return authn.AuthConfig{}, errors.New("invalid credential combination") | ||
} | ||
|
||
if a.Token != "" { | ||
return authn.AuthConfig{ | ||
RegistryToken: a.Token, | ||
}, nil | ||
} | ||
|
||
return authn.AuthConfig{ | ||
Username: a.Username, | ||
Password: a.Password, | ||
}, nil | ||
} | ||
|
||
type envKeyChain struct { | ||
credentials map[string]auth | ||
} | ||
|
||
func FromEnv() (authn.Keychain, error) { | ||
value, ok := os.LookupEnv(CnbCredentialsEnv) | ||
if !ok { | ||
return authn.DefaultKeychain, nil | ||
} | ||
|
||
e := &envKeyChain{} | ||
if err := json.Unmarshal([]byte(value), &e.credentials); err != nil { | ||
return nil, err | ||
} | ||
|
||
return authn.NewMultiKeychain(e, authn.DefaultKeychain), nil | ||
} | ||
|
||
func (e *envKeyChain) Resolve(resource authn.Resource) (authn.Authenticator, error) { | ||
creds, ok := e.credentials[resource.RegistryStr()] | ||
if !ok { | ||
return authn.Anonymous, nil | ||
} | ||
|
||
config, err := creds.config() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return authn.FromConfig(config), nil | ||
} |
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,28 @@ | ||
package keychain_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/jarcoal/httpmock" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = BeforeSuite(func() { | ||
// block all HTTP requests | ||
httpmock.Activate() | ||
}) | ||
|
||
var _ = BeforeEach(func() { | ||
// remove any mocks | ||
httpmock.Reset() | ||
}) | ||
|
||
var _ = AfterSuite(func() { | ||
httpmock.DeactivateAndReset() | ||
}) | ||
|
||
func TestKeychain(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Keychain Suite") | ||
} |
Oops, something went wrong.