mirror of
https://github.com/goharbor/harbor.git
synced 2025-02-01 04:21:36 +01:00
Merge pull request #8425 from ywk253100/190726_acr
Fix #8319, got error when replicating image with Azure container registry
This commit is contained in:
commit
9e6b022ce1
@ -17,10 +17,20 @@ package models
|
|||||||
// Token represents the json returned by registry token service
|
// Token represents the json returned by registry token service
|
||||||
type Token struct {
|
type Token struct {
|
||||||
Token string `json:"token"`
|
Token string `json:"token"`
|
||||||
|
AccessToken string `json:"access_token"` // the token returned by azure container registry is called "access_token"
|
||||||
ExpiresIn int `json:"expires_in"`
|
ExpiresIn int `json:"expires_in"`
|
||||||
IssuedAt string `json:"issued_at"`
|
IssuedAt string `json:"issued_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetToken returns the content of the token
|
||||||
|
func (t *Token) GetToken() string {
|
||||||
|
token := t.Token
|
||||||
|
if len(token) == 0 {
|
||||||
|
token = t.AccessToken
|
||||||
|
}
|
||||||
|
return token
|
||||||
|
}
|
||||||
|
|
||||||
// ResourceActions ...
|
// ResourceActions ...
|
||||||
type ResourceActions struct {
|
type ResourceActions struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
package auth
|
package auth
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
@ -111,7 +112,12 @@ func (t *tokenAuthorizer) Modify(req *http.Request) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Header.Add(http.CanonicalHeaderKey("Authorization"), fmt.Sprintf("Bearer %s", token.Token))
|
tk := token.GetToken()
|
||||||
|
if len(tk) == 0 {
|
||||||
|
return errors.New("empty token content")
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Header.Add(http.CanonicalHeaderKey("Authorization"), fmt.Sprintf("Bearer %s", tk))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -30,7 +30,7 @@ const (
|
|||||||
service = "harbor-registry"
|
service = "harbor-registry"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetToken requests a token against the endpoint using credetial provided
|
// GetToken requests a token against the endpoint using credential provided
|
||||||
func GetToken(endpoint string, insecure bool, credential Credential,
|
func GetToken(endpoint string, insecure bool, credential Credential,
|
||||||
scopes []*token.ResourceActions) (*models.Token, error) {
|
scopes []*token.ResourceActions) (*models.Token, error) {
|
||||||
client := &http.Client{
|
client := &http.Client{
|
||||||
|
@ -1,18 +1,10 @@
|
|||||||
package azurecr
|
package azurecr
|
||||||
|
|
||||||
import (
|
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"
|
"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"
|
adp "github.com/goharbor/harbor/src/replication/adapter"
|
||||||
"github.com/goharbor/harbor/src/replication/adapter/native"
|
"github.com/goharbor/harbor/src/replication/adapter/native"
|
||||||
"github.com/goharbor/harbor/src/replication/model"
|
"github.com/goharbor/harbor/src/replication/model"
|
||||||
"github.com/goharbor/harbor/src/replication/util"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -24,27 +16,17 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func factory(registry *model.Registry) (adp.Adapter, error) {
|
func factory(registry *model.Registry) (adp.Adapter, error) {
|
||||||
if registry.Credential == nil || len(registry.Credential.AccessKey) == 0 ||
|
dockerRegistryAdapter, err := native.NewAdapter(registry)
|
||||||
len(registry.Credential.AccessSecret) == 0 {
|
|
||||||
return nil, fmt.Errorf("credential is necessary for registry %s", registry.URL)
|
|
||||||
}
|
|
||||||
|
|
||||||
authorizer := auth.NewBasicAuthCredential(registry.Credential.AccessKey,
|
|
||||||
registry.Credential.AccessSecret)
|
|
||||||
dockerRegistryAdapter, err := native.NewAdapterWithCustomizedAuthorizer(registry, authorizer)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &adapter{
|
return &adapter{
|
||||||
registry: registry,
|
|
||||||
Adapter: dockerRegistryAdapter,
|
Adapter: dockerRegistryAdapter,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type adapter struct {
|
type adapter struct {
|
||||||
*native.Adapter
|
*native.Adapter
|
||||||
registry *model.Registry
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure '*adapter' implements interface 'Adapter'.
|
// Ensure '*adapter' implements interface 'Adapter'.
|
||||||
@ -73,43 +55,3 @@ func (a *adapter) Info() (*model.RegistryInfo, error) {
|
|||||||
},
|
},
|
||||||
}, nil
|
}, 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
|
|
||||||
}
|
|
||||||
|
@ -1,128 +1,17 @@
|
|||||||
package azurecr
|
package azurecr
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
|
||||||
"net/http/httptest"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
|
|
||||||
"github.com/goharbor/harbor/src/common/utils/test"
|
|
||||||
adp "github.com/goharbor/harbor/src/replication/adapter"
|
|
||||||
"github.com/goharbor/harbor/src/replication/model"
|
"github.com/goharbor/harbor/src/replication/model"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func getMockAdapter(t *testing.T, hasCred, health bool) (*adapter, *httptest.Server) {
|
|
||||||
server := test.NewServer(
|
|
||||||
&test.RequestHandlerMapping{
|
|
||||||
Method: http.MethodGet,
|
|
||||||
Pattern: "/v2/_catalog",
|
|
||||||
Handler: func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
w.Write([]byte(`{"repositories": ["test1"]}`))
|
|
||||||
},
|
|
||||||
},
|
|
||||||
&test.RequestHandlerMapping{
|
|
||||||
Method: http.MethodGet,
|
|
||||||
Pattern: "/v2/{repo}/tags/list",
|
|
||||||
Handler: func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
w.Write([]byte(`{"name": "test1", "tags": ["latest"]}`))
|
|
||||||
},
|
|
||||||
},
|
|
||||||
&test.RequestHandlerMapping{
|
|
||||||
Method: http.MethodGet,
|
|
||||||
Pattern: "/v2/",
|
|
||||||
Handler: func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
fmt.Println(r.Method, r.URL)
|
|
||||||
if health {
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
} else {
|
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
&test.RequestHandlerMapping{
|
|
||||||
Method: http.MethodGet,
|
|
||||||
Pattern: "/",
|
|
||||||
Handler: func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
fmt.Println(r.Method, r.URL)
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
},
|
|
||||||
},
|
|
||||||
&test.RequestHandlerMapping{
|
|
||||||
Method: http.MethodPost,
|
|
||||||
Pattern: "/",
|
|
||||||
Handler: func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
fmt.Println(r.Method, r.URL)
|
|
||||||
if buf, e := ioutil.ReadAll(&io.LimitedReader{R: r.Body, N: 80}); e == nil {
|
|
||||||
fmt.Println("\t", string(buf))
|
|
||||||
}
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
},
|
|
||||||
},
|
|
||||||
)
|
|
||||||
registry := &model.Registry{
|
|
||||||
Type: model.RegistryTypeAzureAcr,
|
|
||||||
URL: server.URL,
|
|
||||||
}
|
|
||||||
if hasCred {
|
|
||||||
registry.Credential = &model.Credential{
|
|
||||||
AccessKey: "acr",
|
|
||||||
AccessSecret: "pwd",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
factory, err := adp.GetFactory(model.RegistryTypeAzureAcr)
|
|
||||||
assert.Nil(t, err)
|
|
||||||
assert.NotNil(t, factory)
|
|
||||||
a, err := factory(registry)
|
|
||||||
|
|
||||||
assert.Nil(t, err)
|
|
||||||
return a.(*adapter), server
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestInfo(t *testing.T) {
|
func TestInfo(t *testing.T) {
|
||||||
a, s := getMockAdapter(t, true, true)
|
a := &adapter{}
|
||||||
defer s.Close()
|
|
||||||
info, err := a.Info()
|
info, err := a.Info()
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.NotNil(t, info)
|
assert.NotNil(t, info)
|
||||||
assert.EqualValues(t, 1, len(info.SupportedResourceTypes))
|
assert.EqualValues(t, 1, len(info.SupportedResourceTypes))
|
||||||
assert.EqualValues(t, model.ResourceTypeImage, info.SupportedResourceTypes[0])
|
assert.EqualValues(t, model.ResourceTypeImage, info.SupportedResourceTypes[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHealthCheck(t *testing.T) {
|
|
||||||
a, s := getMockAdapter(t, true, false)
|
|
||||||
defer s.Close()
|
|
||||||
status, err := a.HealthCheck()
|
|
||||||
assert.Nil(t, err)
|
|
||||||
assert.NotNil(t, status)
|
|
||||||
assert.EqualValues(t, model.Unhealthy, status)
|
|
||||||
a, s = getMockAdapter(t, true, true)
|
|
||||||
defer s.Close()
|
|
||||||
status, err = a.HealthCheck()
|
|
||||||
assert.Nil(t, err)
|
|
||||||
assert.NotNil(t, status)
|
|
||||||
assert.EqualValues(t, model.Healthy, status)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPrepareForPush(t *testing.T) {
|
|
||||||
a, s := getMockAdapter(t, true, true)
|
|
||||||
defer s.Close()
|
|
||||||
resources := []*model.Resource{
|
|
||||||
{
|
|
||||||
Type: model.ResourceTypeImage,
|
|
||||||
Metadata: &model.ResourceMetadata{
|
|
||||||
Repository: &model.Repository{
|
|
||||||
Name: "busybox",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
err := a.PrepareForPush(resources)
|
|
||||||
assert.Nil(t, err)
|
|
||||||
}
|
|
||||||
|
@ -10,7 +10,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/goharbor/harbor/src/common/utils/log"
|
"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"
|
adp "github.com/goharbor/harbor/src/replication/adapter"
|
||||||
"github.com/goharbor/harbor/src/replication/adapter/native"
|
"github.com/goharbor/harbor/src/replication/adapter/native"
|
||||||
"github.com/goharbor/harbor/src/replication/model"
|
"github.com/goharbor/harbor/src/replication/model"
|
||||||
@ -35,25 +34,7 @@ func factory(registry *model.Registry) (adp.Adapter, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// if the registry.Credentail isn't specified, the credential here is nil
|
dockerRegistryAdapter, err := native.NewAdapter(registry)
|
||||||
// the client will request the token with no authentication
|
|
||||||
// this is needed for pulling images from public repositories
|
|
||||||
var credential auth.Credential
|
|
||||||
if registry.Credential != nil && len(registry.Credential.AccessSecret) != 0 {
|
|
||||||
credential = auth.NewBasicAuthCredential(
|
|
||||||
registry.Credential.AccessKey,
|
|
||||||
registry.Credential.AccessSecret)
|
|
||||||
}
|
|
||||||
authorizer := auth.NewStandardTokenAuthorizer(&http.Client{
|
|
||||||
Transport: util.GetHTTPTransport(registry.Insecure),
|
|
||||||
}, credential)
|
|
||||||
|
|
||||||
dockerRegistryAdapter, err := native.NewAdapterWithCustomizedAuthorizer(&model.Registry{
|
|
||||||
Name: registry.Name,
|
|
||||||
URL: registryURL, // specify the URL of Docker Hub registry service
|
|
||||||
Credential: registry.Credential,
|
|
||||||
Insecure: registry.Insecure,
|
|
||||||
}, authorizer)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -4,11 +4,10 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
|
|
||||||
adp "github.com/goharbor/harbor/src/replication/adapter"
|
adp "github.com/goharbor/harbor/src/replication/adapter"
|
||||||
"github.com/goharbor/harbor/src/replication/model"
|
"github.com/goharbor/harbor/src/replication/model"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -24,6 +23,7 @@ func getAdapter(t *testing.T) adp.Adapter {
|
|||||||
|
|
||||||
adapter, err := factory(&model.Registry{
|
adapter, err := factory(&model.Registry{
|
||||||
Type: model.RegistryTypeDockerHub,
|
Type: model.RegistryTypeDockerHub,
|
||||||
|
URL: baseURL,
|
||||||
Credential: &model.Credential{
|
Credential: &model.Credential{
|
||||||
AccessKey: testUser,
|
AccessKey: testUser,
|
||||||
AccessSecret: testPassword,
|
AccessSecret: testPassword,
|
||||||
|
@ -15,14 +15,10 @@
|
|||||||
package googlegcr
|
package googlegcr
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/goharbor/harbor/src/common/utils/log"
|
"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"
|
adp "github.com/goharbor/harbor/src/replication/adapter"
|
||||||
"github.com/goharbor/harbor/src/replication/adapter/native"
|
"github.com/goharbor/harbor/src/replication/adapter/native"
|
||||||
"github.com/goharbor/harbor/src/replication/model"
|
"github.com/goharbor/harbor/src/replication/model"
|
||||||
"github.com/goharbor/harbor/src/replication/util"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -36,17 +32,7 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func newAdapter(registry *model.Registry) (*adapter, error) {
|
func newAdapter(registry *model.Registry) (*adapter, error) {
|
||||||
var credential auth.Credential
|
dockerRegistryAdapter, err := native.NewAdapter(registry)
|
||||||
if registry.Credential != nil && len(registry.Credential.AccessSecret) != 0 {
|
|
||||||
credential = auth.NewBasicAuthCredential(
|
|
||||||
registry.Credential.AccessKey,
|
|
||||||
registry.Credential.AccessSecret)
|
|
||||||
}
|
|
||||||
authorizer := auth.NewStandardTokenAuthorizer(&http.Client{
|
|
||||||
Transport: util.GetHTTPTransport(registry.Insecure),
|
|
||||||
}, credential)
|
|
||||||
|
|
||||||
dockerRegistryAdapter, err := native.NewAdapterWithCustomizedAuthorizer(registry, authorizer)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -101,8 +87,3 @@ func (a adapter) HealthCheck() (model.HealthStatus, error) {
|
|||||||
}
|
}
|
||||||
return model.Healthy, nil
|
return model.Healthy, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// PrepareForPush nothing need to do.
|
|
||||||
func (a adapter) PrepareForPush(resources []*model.Resource) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user