harbor/utils/registry/registry.go

158 lines
3.7 KiB
Go
Raw Normal View History

2016-04-13 08:43:17 +02:00
/*
Copyright (c) 2016 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package registry
import (
2016-04-15 07:17:32 +02:00
"encoding/json"
2016-04-13 08:43:17 +02:00
"fmt"
"io/ioutil"
"net/http"
"net/url"
2016-04-27 11:59:43 +02:00
"strings"
2016-04-13 08:43:17 +02:00
2016-04-27 12:03:56 +02:00
"github.com/vmware/harbor/utils/log"
2016-04-27 11:59:43 +02:00
"github.com/vmware/harbor/utils/registry/auth"
"github.com/vmware/harbor/utils/registry/errors"
2016-04-13 08:43:17 +02:00
)
2016-04-20 07:06:19 +02:00
// Registry holds information of a registry entity
2016-04-13 08:43:17 +02:00
type Registry struct {
Endpoint *url.URL
client *http.Client
}
2016-04-27 11:59:43 +02:00
// NewRegistry returns an instance of registry
func NewRegistry(endpoint string, client *http.Client) (*Registry, error) {
endpoint = strings.TrimRight(endpoint, "/")
2016-04-13 08:43:17 +02:00
u, err := url.Parse(endpoint)
if err != nil {
return nil, err
}
2016-04-27 11:59:43 +02:00
registry := &Registry{
2016-04-13 08:43:17 +02:00
Endpoint: u,
client: client,
2016-04-17 16:39:10 +02:00
}
2016-04-27 11:59:43 +02:00
log.Debugf("initialized a registry client: %s", endpoint)
2016-04-15 07:17:32 +02:00
2016-04-27 11:59:43 +02:00
return registry, nil
2016-04-15 07:17:32 +02:00
}
2016-04-27 11:59:43 +02:00
// NewRegistryWithUsername returns a Registry instance which will authorize the request
// according to the privileges of user
func NewRegistryWithUsername(endpoint, username string) (*Registry, error) {
endpoint = strings.TrimRight(endpoint, "/")
2016-04-13 08:43:17 +02:00
2016-04-27 11:59:43 +02:00
u, err := url.Parse(endpoint)
2016-04-13 08:43:17 +02:00
if err != nil {
2016-04-27 11:59:43 +02:00
return nil, err
2016-04-15 07:17:32 +02:00
}
2016-04-13 08:43:17 +02:00
2016-04-28 12:49:59 +02:00
client, err := newClient(endpoint, username, nil, "registry", "catalog", "*")
2016-04-13 08:43:17 +02:00
if err != nil {
2016-04-27 11:59:43 +02:00
return nil, err
2016-04-21 08:55:15 +02:00
}
2016-04-13 08:43:17 +02:00
2016-04-27 11:59:43 +02:00
registry := &Registry{
Endpoint: u,
2016-04-28 12:49:59 +02:00
client: client,
2016-04-13 08:43:17 +02:00
}
2016-04-29 10:59:54 +02:00
log.Debugf("initialized a registry client with username: %s %s", endpoint, username)
2016-04-27 11:59:43 +02:00
return registry, nil
2016-04-13 08:43:17 +02:00
}
2016-04-27 11:59:43 +02:00
// Catalog ...
func (r *Registry) Catalog() ([]string, error) {
repos := []string{}
2016-04-21 08:55:15 +02:00
2016-04-27 11:59:43 +02:00
req, err := http.NewRequest("GET", buildCatalogURL(r.Endpoint.String()), nil)
2016-04-13 08:43:17 +02:00
if err != nil {
2016-04-27 11:59:43 +02:00
return repos, err
2016-04-13 08:43:17 +02:00
}
resp, err := r.client.Do(req)
if err != nil {
2016-05-13 09:37:12 +02:00
ok, e := isUnauthorizedError(err)
2016-05-10 16:01:38 +02:00
if ok {
return repos, e
}
2016-04-27 11:59:43 +02:00
return repos, err
2016-04-13 08:43:17 +02:00
}
defer resp.Body.Close()
2016-04-15 07:17:32 +02:00
b, err := ioutil.ReadAll(resp.Body)
2016-04-13 08:43:17 +02:00
if err != nil {
2016-04-27 11:59:43 +02:00
return repos, err
2016-04-21 08:55:15 +02:00
}
if resp.StatusCode == http.StatusOK {
2016-04-27 11:59:43 +02:00
catalogResp := struct {
Repositories []string `json:"repositories"`
}{}
2016-04-21 08:55:15 +02:00
2016-04-27 11:59:43 +02:00
if err := json.Unmarshal(b, &catalogResp); err != nil {
return repos, err
2016-04-21 08:55:15 +02:00
}
2016-04-13 08:43:17 +02:00
2016-04-27 11:59:43 +02:00
repos = catalogResp.Repositories
2016-04-13 08:43:17 +02:00
2016-04-27 11:59:43 +02:00
return repos, nil
2016-04-13 08:43:17 +02:00
}
2016-04-27 11:59:43 +02:00
return repos, errors.Error{
2016-04-15 07:17:32 +02:00
StatusCode: resp.StatusCode,
2016-05-10 16:01:38 +02:00
StatusText: resp.Status,
2016-04-15 07:17:32 +02:00
Message: string(b),
}
}
2016-04-27 11:59:43 +02:00
func buildCatalogURL(endpoint string) string {
return fmt.Sprintf("%s/v2/_catalog", endpoint)
2016-04-21 08:55:15 +02:00
}
2016-04-28 12:49:59 +02:00
func newClient(endpoint, username string, credential auth.Credential,
scopeType, scopeName string, scopeActions ...string) (*http.Client, error) {
endpoint = strings.TrimRight(endpoint, "/")
resp, err := http.Get(buildPingURL(endpoint))
if err != nil {
return nil, err
}
var handlers []auth.Handler
var handler auth.Handler
if credential != nil {
handler = auth.NewStandardTokenHandler(credential, scopeType, scopeName, scopeActions...)
} else {
handler = auth.NewUsernameTokenHandler(username, scopeType, scopeName, scopeActions...)
}
handlers = append(handlers, handler)
challenges := auth.ParseChallengeFromResponse(resp)
authorizer := auth.NewRequestAuthorizer(handlers, challenges)
transport := NewTransport(http.DefaultTransport, []RequestModifier{authorizer})
return &http.Client{
Transport: transport,
}, nil
}