From 000fe3d08496181e6f72de56cf3ab31900c4b55c Mon Sep 17 00:00:00 2001 From: chlins Date: Tue, 25 Jun 2019 12:40:15 +0800 Subject: [PATCH] refactor: adjust some logic in huawei adapter Signed-off-by: chlins --- .../adapter/huawei/huawei_adapter.go | 65 +++++++++---------- .../adapter/huawei/image_registry.go | 46 ++----------- 2 files changed, 35 insertions(+), 76 deletions(-) diff --git a/src/replication/adapter/huawei/huawei_adapter.go b/src/replication/adapter/huawei/huawei_adapter.go index 9c3dcb401..49a60cd73 100644 --- a/src/replication/adapter/huawei/huawei_adapter.go +++ b/src/replication/adapter/huawei/huawei_adapter.go @@ -1,8 +1,6 @@ package huawei import ( - "crypto/tls" - "encoding/base64" "encoding/json" "fmt" "io/ioutil" @@ -10,7 +8,10 @@ import ( "regexp" "strings" + common_http "github.com/goharbor/harbor/src/common/http" + "github.com/goharbor/harbor/src/common/http/modifier" "github.com/goharbor/harbor/src/common/utils/log" + "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" @@ -30,6 +31,7 @@ func init() { type adapter struct { *native.Adapter registry *model.Registry + client *common_http.Client } // Info gets info about Huawei SWR @@ -56,18 +58,8 @@ func (a *adapter) ListNamespaces(query *model.NamespaceQuery) ([]*model.Namespac } r.Header.Add("content-type", "application/json; charset=utf-8") - encodeAuth := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", a.registry.Credential.AccessKey, a.registry.Credential.AccessSecret))) - r.Header.Add("Authorization", "Basic "+encodeAuth) - client := &http.Client{} - if a.registry.Insecure == true { - client = &http.Client{ - Transport: &http.Transport{ - TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, - }, - } - } - resp, err := client.Do(r) + resp, err := a.client.Do(r) if err != nil { return namespaces, err } @@ -120,8 +112,11 @@ func (a *adapter) ConvertResourceMetadata(resourceMetadata *model.ResourceMetada func (a *adapter) PrepareForPush(resources []*model.Resource) error { namespaces := map[string]struct{}{} for _, resource := range resources { + var namespace string paths := strings.Split(resource.Metadata.Repository.Name, "/") - namespace := paths[0] + if len(paths) > 0 { + namespace = paths[0] + } ns, err := a.GetNamespace(namespace) if err != nil { return err @@ -133,9 +128,7 @@ func (a *adapter) PrepareForPush(resources []*model.Resource) error { } url := fmt.Sprintf("%s/dockyard/v2/namespaces", a.registry.URL) - client := &http.Client{ - Transport: util.GetHTTPTransport(a.registry.Insecure), - } + for namespace := range namespaces { namespacebyte, err := json.Marshal(struct { Namespace string `json:"namespace"` @@ -152,10 +145,8 @@ func (a *adapter) PrepareForPush(resources []*model.Resource) error { } r.Header.Add("content-type", "application/json; charset=utf-8") - encodeAuth := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", a.registry.Credential.AccessKey, a.registry.Credential.AccessSecret))) - r.Header.Add("Authorization", "Basic "+encodeAuth) - resp, err := client.Do(r) + resp, err := a.client.Do(r) if err != nil { return err } @@ -185,20 +176,8 @@ func (a *adapter) GetNamespace(namespaceStr string) (*model.Namespace, error) { } r.Header.Add("content-type", "application/json; charset=utf-8") - encodeAuth := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", a.registry.Credential.AccessKey, a.registry.Credential.AccessSecret))) - r.Header.Add("Authorization", "Basic "+encodeAuth) - var client *http.Client - if a.registry.Insecure == true { - client = &http.Client{ - Transport: &http.Transport{ - TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, - }, - } - } else { - client = &http.Client{} - } - resp, err := client.Do(r) + resp, err := a.client.Do(r) if err != nil { return namespace, err } @@ -237,9 +216,27 @@ func AdapterFactory(registry *model.Registry) (adp.Adapter, error) { if err != nil { return nil, err } + + var ( + modifiers []modifier.Modifier + authorizer modifier.Modifier + ) + if registry.Credential != nil { + authorizer = auth.NewBasicAuthCredential( + registry.Credential.AccessKey, + registry.Credential.AccessSecret) + modifiers = append(modifiers, authorizer) + } + return &adapter{ - registry: registry, Adapter: dockerRegistryAdapter, + registry: registry, + client: common_http.NewClient( + &http.Client{ + Transport: util.GetHTTPTransport(registry.Insecure), + }, + modifiers..., + ), }, nil } diff --git a/src/replication/adapter/huawei/image_registry.go b/src/replication/adapter/huawei/image_registry.go index b3636163e..3738ea2e0 100644 --- a/src/replication/adapter/huawei/image_registry.go +++ b/src/replication/adapter/huawei/image_registry.go @@ -1,8 +1,6 @@ package huawei import ( - "crypto/tls" - "encoding/base64" "encoding/json" "fmt" "io/ioutil" @@ -25,18 +23,8 @@ func (a *adapter) FetchImages(filters []*model.Filter) ([]*model.Resource, error } r.Header.Add("content-type", "application/json; charset=utf-8") - encodeAuth := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", a.registry.Credential.AccessKey, a.registry.Credential.AccessSecret))) - r.Header.Add("Authorization", "Basic "+encodeAuth) - client := &http.Client{} - if a.registry.Insecure == true { - client = &http.Client{ - Transport: &http.Transport{ - TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, - }, - } - } - resp, err := client.Do(r) + resp, err := a.client.Do(r) if err != nil { return resources, err } @@ -82,15 +70,7 @@ func (a *adapter) ManifestExist(repository, reference string) (exist bool, diges r.Header.Add("content-type", "application/json; charset=utf-8") r.Header.Add("Authorization", "Bearer "+token.Token) - client := &http.Client{} - if a.registry.Insecure == true { - client = &http.Client{ - Transport: &http.Transport{ - TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, - }, - } - } - resp, err := client.Do(r) + resp, err := a.client.Do(r) if err != nil { return exist, digest, err } @@ -133,15 +113,7 @@ func (a *adapter) DeleteManifest(repository, reference string) error { r.Header.Add("content-type", "application/json; charset=utf-8") r.Header.Add("Authorization", "Bearer "+token.Token) - client := &http.Client{} - if a.registry.Insecure == true { - client = &http.Client{ - Transport: &http.Transport{ - TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, - }, - } - } - resp, err := client.Do(r) + resp, err := a.client.Do(r) if err != nil { return err } @@ -220,18 +192,8 @@ func getJwtToken(a *adapter, repository string) (token jwtToken, err error) { } r.Header.Add("content-type", "application/json; charset=utf-8") - encodeAuth := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", a.registry.Credential.AccessKey, a.registry.Credential.AccessSecret))) - r.Header.Add("Authorization", "Basic "+encodeAuth) - client := &http.Client{} - if a.registry.Insecure == true { - client = &http.Client{ - Transport: &http.Transport{ - TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, - }, - } - } - resp, err := client.Do(r) + resp, err := a.client.Do(r) if err != nil { return token, err }