diff --git a/src/jobservice/job/impl/replication/replication.go b/src/jobservice/job/impl/replication/replication.go index 49dc320e0..8fde31e3e 100644 --- a/src/jobservice/job/impl/replication/replication.go +++ b/src/jobservice/job/impl/replication/replication.go @@ -38,6 +38,8 @@ import ( _ "github.com/goharbor/harbor/src/replication/adapter/googlegcr" // register the AwsEcr adapter _ "github.com/goharbor/harbor/src/replication/adapter/awsecr" + // register the AzureAcr adapter + _ "github.com/goharbor/harbor/src/replication/adapter/azurecr" ) // Replication implements the job interface diff --git a/src/replication/adapter/azurecr/adapter.go b/src/replication/adapter/azurecr/adapter.go new file mode 100644 index 000000000..6e77f5770 --- /dev/null +++ b/src/replication/adapter/azurecr/adapter.go @@ -0,0 +1,118 @@ +package azurecr + +import ( + "fmt" + "net/http" + + "github.com/goharbor/harbor/src/common/http/modifier" + common_http_auth "github.com/goharbor/harbor/src/common/http/modifier/auth" + "github.com/goharbor/harbor/src/common/utils/log" + registry_pkg "github.com/goharbor/harbor/src/common/utils/registry" + "github.com/goharbor/harbor/src/common/utils/registry/auth" + adp "github.com/goharbor/harbor/src/replication/adapter" + "github.com/goharbor/harbor/src/replication/adapter/native" + "github.com/goharbor/harbor/src/replication/model" + "github.com/goharbor/harbor/src/replication/util" +) + +func init() { + if err := adp.RegisterFactory(model.RegistryTypeAzureAcr, factory); err != nil { + log.Errorf("Register adapter factory for %s error: %v", model.RegistryTypeAzureAcr, err) + return + } + log.Infof("Factory for adapter %s registered", model.RegistryTypeAzureAcr) +} + +func factory(registry *model.Registry) (adp.Adapter, error) { + client, err := getClient(registry) + if err != nil { + return nil, err + } + + reg, err := native.NewWithClient(registry, client) + if err != nil { + return nil, err + } + + return &adapter{ + registry: registry, + Native: reg, + }, nil +} + +type adapter struct { + *native.Native + registry *model.Registry +} + +// Ensure '*adapter' implements interface 'Adapter'. +var _ adp.Adapter = (*adapter)(nil) + +// Info returns information of the registry +func (a *adapter) Info() (*model.RegistryInfo, error) { + return &model.RegistryInfo{ + Type: model.RegistryTypeAzureAcr, + SupportedResourceTypes: []model.ResourceType{ + model.ResourceTypeImage, + }, + SupportedResourceFilters: []*model.FilterStyle{ + { + Type: model.FilterTypeName, + Style: model.FilterStyleTypeText, + }, + { + Type: model.FilterTypeTag, + Style: model.FilterStyleTypeText, + }, + }, + SupportedTriggers: []model.TriggerType{ + model.TriggerTypeManual, + model.TriggerTypeScheduled, + }, + }, nil +} + +// PrepareForPush no preparation needed for Azure container registry +func (a *adapter) PrepareForPush(resources []*model.Resource) error { + return nil +} + +// HealthCheck checks health status of a registry +func (a adapter) HealthCheck() (model.HealthStatus, error) { + err := a.PingGet() + if err != nil { + return model.Unhealthy, nil + } + + return model.Healthy, nil +} + +func getClient(registry *model.Registry) (*http.Client, error) { + if registry.Credential == nil || + len(registry.Credential.AccessKey) == 0 || len(registry.Credential.AccessSecret) == 0 { + return nil, fmt.Errorf("no credential to ping registry %s", registry.URL) + } + + var cred modifier.Modifier + if registry.Credential.Type == model.CredentialTypeSecret { + cred = common_http_auth.NewSecretAuthorizer(registry.Credential.AccessSecret) + } else { + cred = auth.NewBasicAuthCredential( + registry.Credential.AccessKey, + registry.Credential.AccessSecret) + } + + transport := util.GetHTTPTransport(registry.Insecure) + modifiers := []modifier.Modifier{ + &auth.UserAgentModifier{ + UserAgent: adp.UserAgentReplication, + }, + cred, + } + + client := &http.Client{ + Transport: registry_pkg.NewTransport(transport, modifiers...), + } + + return client, nil +} diff --git a/src/replication/adapter/azurecr/adapter_test.go b/src/replication/adapter/azurecr/adapter_test.go new file mode 100644 index 000000000..6a6df8166 --- /dev/null +++ b/src/replication/adapter/azurecr/adapter_test.go @@ -0,0 +1 @@ +package azurecr diff --git a/src/replication/adapter/image_registry.go b/src/replication/adapter/image_registry.go index 3249168d8..02bb10266 100644 --- a/src/replication/adapter/image_registry.go +++ b/src/replication/adapter/image_registry.go @@ -114,6 +114,21 @@ type DefaultImageRegistry struct { clients map[string]*registry_pkg.Repository } +// NewDefaultRegistryWithClient returns an instance of DefaultImageRegistry +func NewDefaultRegistryWithClient(registry *model.Registry, client *http.Client) (*DefaultImageRegistry, error) { + reg, err := registry_pkg.NewRegistry(registry.URL, client) + if err != nil { + return nil, err + } + + return &DefaultImageRegistry{ + Registry: reg, + client: client, + registry: registry, + clients: map[string]*registry_pkg.Repository{}, + }, nil +} + // NewDefaultImageRegistry returns an instance of DefaultImageRegistry func NewDefaultImageRegistry(registry *model.Registry) (*DefaultImageRegistry, error) { var authorizer modifier.Modifier diff --git a/src/replication/adapter/native/adapter.go b/src/replication/adapter/native/adapter.go index 79a3212bd..2793d4d4f 100644 --- a/src/replication/adapter/native/adapter.go +++ b/src/replication/adapter/native/adapter.go @@ -15,6 +15,8 @@ package native import ( + "net/http" + "github.com/goharbor/harbor/src/common/utils/log" adp "github.com/goharbor/harbor/src/replication/adapter" "github.com/goharbor/harbor/src/replication/model" @@ -30,25 +32,39 @@ func init() { log.Infof("the factory for adapter %s registered", model.RegistryTypeDockerRegistry) } -func newAdapter(registry *model.Registry) (*native, error) { +func newAdapter(registry *model.Registry) (*Native, error) { reg, err := adp.NewDefaultImageRegistry(registry) if err != nil { return nil, err } - return &native{ + return &Native{ registry: registry, DefaultImageRegistry: reg, }, nil } -type native struct { +// NewWithClient ... +func NewWithClient(registry *model.Registry, client *http.Client) (*Native, error) { + reg, err := adp.NewDefaultRegistryWithClient(registry, client) + if err != nil { + return nil, err + } + return &Native{ + registry: registry, + DefaultImageRegistry: reg, + }, nil +} + +// Native is adapter to native docker registry +type Native struct { *adp.DefaultImageRegistry registry *model.Registry } -var _ adp.Adapter = native{} +var _ adp.Adapter = Native{} -func (native) Info() (info *model.RegistryInfo, err error) { +// Info ... +func (Native) Info() (info *model.RegistryInfo, err error) { return &model.RegistryInfo{ Type: model.RegistryTypeDockerRegistry, SupportedResourceTypes: []model.ResourceType{ @@ -72,4 +88,4 @@ func (native) Info() (info *model.RegistryInfo, err error) { } // PrepareForPush nothing need to do. -func (native) PrepareForPush([]*model.Resource) error { return nil } +func (Native) PrepareForPush([]*model.Resource) error { return nil } diff --git a/src/replication/adapter/native/adapter_test.go b/src/replication/adapter/native/adapter_test.go index 0c6ff74ff..e51675bf9 100644 --- a/src/replication/adapter/native/adapter_test.go +++ b/src/replication/adapter/native/adapter_test.go @@ -33,7 +33,7 @@ func Test_newAdapter(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got, err := newAdapter(tt.registry) + got, err := NewAdapter(tt.registry) if tt.wantErr { assert.NotNil(t, err) assert.Nil(t, got) @@ -48,7 +48,7 @@ func Test_newAdapter(t *testing.T) { func Test_native_Info(t *testing.T) { var registry = &model.Registry{URL: "abc"} var reg, _ = adp.NewDefaultImageRegistry(registry) - var adapter = native{ + var adapter = Native{ DefaultImageRegistry: reg, registry: registry, } @@ -67,7 +67,7 @@ func Test_native_Info(t *testing.T) { func Test_native_PrepareForPush(t *testing.T) { var registry = &model.Registry{URL: "abc"} var reg, _ = adp.NewDefaultImageRegistry(registry) - var adapter = native{ + var adapter = Native{ DefaultImageRegistry: reg, registry: registry, } diff --git a/src/replication/adapter/native/image_registry.go b/src/replication/adapter/native/image_registry.go index d279b6ede..9f3f387be 100644 --- a/src/replication/adapter/native/image_registry.go +++ b/src/replication/adapter/native/image_registry.go @@ -20,9 +20,10 @@ import ( "github.com/goharbor/harbor/src/replication/util" ) -var _ adp.ImageRegistry = native{} +var _ adp.ImageRegistry = Native{} -func (n native) FetchImages(filters []*model.Filter) ([]*model.Resource, error) { +// FetchImages ... +func (n Native) FetchImages(filters []*model.Filter) ([]*model.Resource, error) { nameFilterPattern := "" tagFilterPattern := "" for _, filter := range filters { @@ -62,7 +63,7 @@ func (n native) FetchImages(filters []*model.Filter) ([]*model.Resource, error) return resources, nil } -func (n native) filterRepositories(pattern string) ([]string, error) { +func (n Native) filterRepositories(pattern string) ([]string, error) { // if the pattern is a specific repository name, just returns the parsed repositories // and will check the existence later when filtering the tags if repositories, ok := util.IsSpecificPath(pattern); ok { @@ -90,7 +91,7 @@ func (n native) filterRepositories(pattern string) ([]string, error) { return result, nil } -func (n native) filterTags(repository, pattern string) ([]string, error) { +func (n Native) filterTags(repository, pattern string) ([]string, error) { tags, err := n.ListTag(repository) if err != nil { return nil, err diff --git a/src/replication/adapter/native/image_registry_test.go b/src/replication/adapter/native/image_registry_test.go index 841004a20..abd2baba7 100644 --- a/src/replication/adapter/native/image_registry_test.go +++ b/src/replication/adapter/native/image_registry_test.go @@ -72,7 +72,7 @@ func Test_native_FetchImages(t *testing.T) { var reg, err = adp.NewDefaultImageRegistry(registry) assert.NotNil(t, reg) assert.Nil(t, err) - var adapter = native{ + var adapter = Native{ DefaultImageRegistry: reg, registry: registry, } diff --git a/src/replication/model/registry.go b/src/replication/model/registry.go index 39445cea4..4f2459fe9 100644 --- a/src/replication/model/registry.go +++ b/src/replication/model/registry.go @@ -28,6 +28,7 @@ const ( RegistryTypeHuawei RegistryType = "huawei-SWR" RegistryTypeGoogleGcr RegistryType = "google-gcr" RegistryTypeAwsEcr RegistryType = "aws-ecr" + RegistryTypeAzureAcr RegistryType = "azure-acr" FilterStyleTypeText = "input" FilterStyleTypeRadio = "radio" diff --git a/src/replication/replication.go b/src/replication/replication.go index ad7767c24..de17d0a1f 100644 --- a/src/replication/replication.go +++ b/src/replication/replication.go @@ -39,6 +39,8 @@ import ( _ "github.com/goharbor/harbor/src/replication/adapter/googlegcr" // register the AwsEcr adapter _ "github.com/goharbor/harbor/src/replication/adapter/awsecr" + // register the AzureAcr adapter + _ "github.com/goharbor/harbor/src/replication/adapter/azurecr" ) var (