mirror of
https://github.com/goharbor/harbor.git
synced 2024-11-27 12:46:03 +01:00
Support anonymous access to dockerhub
Signed-off-by: cd1989 <chende@caicloud.io>
This commit is contained in:
parent
3e41ebb723
commit
21c7554644
@ -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
|
||||||
|
@ -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)
|
||||||
|
@ -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()
|
||||||
|
Loading…
Reference in New Issue
Block a user