Support anonymous access to dockerhub

Signed-off-by: cd1989 <chende@caicloud.io>
This commit is contained in:
cd1989 2019-04-14 16:31:24 +08:00
parent 3e41ebb723
commit 21c7554644
3 changed files with 14 additions and 6 deletions

View File

@ -17,6 +17,7 @@ import (
func init() { func init() {
if err := adp.RegisterFactory(model.RegistryTypeDockerHub, func(registry *model.Registry) (adp.Adapter, error) { if err := adp.RegisterFactory(model.RegistryTypeDockerHub, func(registry *model.Registry) (adp.Adapter, error) {
registry.URL = baseURL
client, err := NewClient(registry) client, err := NewClient(registry)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -25,13 +25,19 @@ func NewClient(registry *model.Registry) (*Client, error) {
client := &Client{ client := &Client{
host: registry.URL, host: registry.URL,
client: http.DefaultClient, client: http.DefaultClient,
credential: LoginCredential{ }
User: registry.Credential.AccessKey,
Password: registry.Credential.AccessSecret, // For anonymous access, no need to refresh token.
}, if registry.Credential == nil ||
(len(registry.Credential.AccessKey) == 0 && len(registry.Credential.AccessSecret) == 0) {
return client, nil
} }
// Login to DockerHub to get access token, default expire date is 30d. // Login to DockerHub to get access token, default expire date is 30d.
client.credential = LoginCredential{
User: registry.Credential.AccessKey,
Password: registry.Credential.AccessSecret,
}
err := client.refreshToken() err := client.refreshToken()
if err != nil { if err != nil {
return nil, fmt.Errorf("login to dockerhub error: %v", err) return nil, fmt.Errorf("login to dockerhub error: %v", err)

View File

@ -68,7 +68,7 @@ func NewDefaultImageRegistry(registry *model.Registry) (*DefaultImageRegistry, e
UserAgent: UserAgentReplication, UserAgent: UserAgentReplication,
}, },
} }
if registry.Credential != nil { if registry.Credential != nil && len(registry.Credential.AccessSecret) != 0 {
var cred modifier.Modifier var cred modifier.Modifier
if registry.Credential.Type == model.CredentialTypeSecret { if registry.Credential.Type == model.CredentialTypeSecret {
cred = common_http_auth.NewSecretAuthorizer(registry.Credential.AccessSecret) cred = common_http_auth.NewSecretAuthorizer(registry.Credential.AccessSecret)
@ -135,7 +135,8 @@ func (d *DefaultImageRegistry) create(repository string) (*registry_pkg.Reposito
// HealthCheck checks health status of a registry // HealthCheck checks health status of a registry
func (d *DefaultImageRegistry) HealthCheck() (model.HealthStatus, error) { func (d *DefaultImageRegistry) HealthCheck() (model.HealthStatus, error) {
var err error var err error
if d.registry.Credential == nil { if d.registry.Credential == nil ||
(len(d.registry.Credential.AccessKey) == 0 && len(d.registry.Credential.AccessSecret) == 0) {
err = d.PingSimple() err = d.PingSimple()
} else { } else {
err = d.Ping() err = d.Ping()