diff --git a/src/replication/adapter/dockerhub/adapter.go b/src/replication/adapter/dockerhub/adapter.go index 7c6a55ddd..2b1068b4f 100644 --- a/src/replication/adapter/dockerhub/adapter.go +++ b/src/replication/adapter/dockerhub/adapter.go @@ -17,6 +17,7 @@ import ( func init() { if err := adp.RegisterFactory(model.RegistryTypeDockerHub, func(registry *model.Registry) (adp.Adapter, error) { + registry.URL = baseURL client, err := NewClient(registry) if err != nil { return nil, err diff --git a/src/replication/adapter/dockerhub/client.go b/src/replication/adapter/dockerhub/client.go index 26563a802..11fbd65a9 100644 --- a/src/replication/adapter/dockerhub/client.go +++ b/src/replication/adapter/dockerhub/client.go @@ -25,13 +25,19 @@ func NewClient(registry *model.Registry) (*Client, error) { client := &Client{ host: registry.URL, 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. + client.credential = LoginCredential{ + User: registry.Credential.AccessKey, + Password: registry.Credential.AccessSecret, + } err := client.refreshToken() if err != nil { return nil, fmt.Errorf("login to dockerhub error: %v", err) diff --git a/src/replication/adapter/image_registry.go b/src/replication/adapter/image_registry.go index 9d8484d65..cfd909335 100644 --- a/src/replication/adapter/image_registry.go +++ b/src/replication/adapter/image_registry.go @@ -68,7 +68,7 @@ func NewDefaultImageRegistry(registry *model.Registry) (*DefaultImageRegistry, e UserAgent: UserAgentReplication, }, } - if registry.Credential != nil { + if registry.Credential != nil && len(registry.Credential.AccessSecret) != 0 { var cred modifier.Modifier if registry.Credential.Type == model.CredentialTypeSecret { 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 func (d *DefaultImageRegistry) HealthCheck() (model.HealthStatus, 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() } else { err = d.Ping()