Enabled auth and support http registries for OCI

Signed-off-by: Andrew Block <andy.block@gmail.com>
pull/10527/head
Andrew Block 3 years ago committed by Scott Rigby
parent 4c8a3faaa2
commit 291c17fcc5
No known key found for this signature in database
GPG Key ID: C7C6FBB5B91C1155

@ -17,6 +17,7 @@ limitations under the License.
package registry // import "helm.sh/helm/v3/internal/experimental/registry"
import (
"context"
"encoding/json"
"fmt"
"io"
@ -34,7 +35,7 @@ import (
"oras.land/oras-go/pkg/content"
"oras.land/oras-go/pkg/oras"
"oras.land/oras-go/pkg/registry"
registrremote "oras.land/oras-go/pkg/registry/remote"
registryremote "oras.land/oras-go/pkg/registry/remote"
registryauth "oras.land/oras-go/pkg/registry/remote/auth"
"helm.sh/helm/v3/internal/version"
@ -100,6 +101,23 @@ func NewClient(options ...ClientOption) (*Client, error) {
"User-Agent": {version.GetUserAgent()},
},
Cache: registryauth.DefaultCache,
Credential: func(ctx context.Context, reg string) (registryauth.Credential, error) {
dockerClient, ok := client.authorizer.(*dockerauth.Client)
if !ok {
return registryauth.EmptyCredential, errors.New("unable to obtain docker client")
}
username, password, err := dockerClient.Credential(reg)
if err != nil {
return registryauth.EmptyCredential, errors.New("unable to retrieve credentials")
}
return registryauth.Credential{
Username: username,
Password: password,
}, nil
},
}
}
@ -555,21 +573,33 @@ func PushOptStrictMode(strictMode bool) PushOption {
}
}
// Tags provides an all semver compliant tags for a given repository
// Tags provides a sorted list all semver compliant tags for a given repository
func (c *Client) Tags(ref string) ([]string, error) {
parsedReference, err := registry.ParseReference(ref)
if err != nil {
return nil, err
}
repository := registrremote.Repository{
repository := registryremote.Repository{
Reference: parsedReference,
Client: c.registryAuthorizer,
}
registryTags, err := registry.Tags(ctx(c.out, c.debug), &repository)
if err != nil {
return nil, err
var registryTags []string
for {
registryTags, err = registry.Tags(ctx(c.out, c.debug), &repository)
if err != nil {
// Fallback to http based request
if !repository.PlainHTTP && strings.Contains(err.Error(), "server gave HTTP response") {
repository.PlainHTTP = true
continue
}
return nil, err
}
break
}
var tagVersions []*semver.Version

@ -294,7 +294,23 @@ func (suite *RegistryClientTestSuite) Test_2_Pull() {
suite.Equal(provData, result.Prov.Data)
}
func (suite *RegistryClientTestSuite) Test_3_Logout() {
func (suite *RegistryClientTestSuite) Test_3_Tags() {
// Load test chart (to build ref pushed in previous test)
chartData, err := ioutil.ReadFile("../../../pkg/downloader/testdata/local-subchart-0.1.0.tgz")
suite.Nil(err, "no error loading test chart")
meta, err := extractChartMeta(chartData)
suite.Nil(err, "no error extracting chart meta")
ref := fmt.Sprintf("%s/testrepo/%s", suite.DockerRegistryHost, meta.Name)
// Query for tags and validate length
tags, err := suite.RegistryClient.Tags(ref)
suite.Nil(err, "no error retrieving tags")
suite.Equal(1, len(tags))
}
func (suite *RegistryClientTestSuite) Test_4_Logout() {
err := suite.RegistryClient.Logout("this-host-aint-real:5000")
suite.NotNil(err, "error logging out of registry that has no entry")
@ -302,7 +318,7 @@ func (suite *RegistryClientTestSuite) Test_3_Logout() {
suite.Nil(err, "no error logging out of registry")
}
func (suite *RegistryClientTestSuite) Test_4_ManInTheMiddle() {
func (suite *RegistryClientTestSuite) Test_5_ManInTheMiddle() {
ref := fmt.Sprintf("%s/testrepo/supposedlysafechart:9.9.9", suite.CompromisedRegistryHost)
// returns content that does not match the expected digest

Loading…
Cancel
Save