mirror of
https://github.com/goharbor/harbor.git
synced 2024-11-23 10:45:45 +01:00
Merge pull request #7380 from cd1989/anonymous-dockerhub
Support anonymous access to dockerhub
This commit is contained in:
commit
d9a2b2b79a
@ -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
|
||||
|
@ -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)
|
||||
|
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user