2016-02-01 12:59:10 +01: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.
|
|
|
|
*/
|
2016-02-26 11:54:14 +01:00
|
|
|
|
2016-02-01 12:59:10 +01:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2016-03-25 02:08:44 +01:00
|
|
|
|
2016-03-25 02:31:50 +01:00
|
|
|
"github.com/vmware/harbor/utils/log"
|
2016-02-01 12:59:10 +01:00
|
|
|
)
|
|
|
|
|
2016-02-26 11:35:55 +01:00
|
|
|
// BuildRegistryURL ...
|
2016-02-25 07:00:29 +01:00
|
|
|
func BuildRegistryURL(segments ...string) string {
|
2016-02-01 12:59:10 +01:00
|
|
|
registryURL := os.Getenv("REGISTRY_URL")
|
|
|
|
if registryURL == "" {
|
|
|
|
registryURL = "http://localhost:5000"
|
|
|
|
}
|
|
|
|
url := registryURL + "/v2"
|
|
|
|
for _, s := range segments {
|
|
|
|
if s == "v2" {
|
2016-03-25 02:31:50 +01:00
|
|
|
log.Debugf("unnecessary v2 in %v", segments)
|
2016-02-01 12:59:10 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
url += "/" + s
|
|
|
|
}
|
|
|
|
return url
|
|
|
|
}
|
|
|
|
|
2016-02-26 11:35:55 +01:00
|
|
|
// RegistryAPIGet triggers GET request to the URL which is the endpoint of registry and returns the response body.
|
|
|
|
// It will attach a valid jwt token to the request if registry requires.
|
2016-02-25 07:00:29 +01:00
|
|
|
func RegistryAPIGet(url, username string) ([]byte, error) {
|
2016-03-29 13:37:04 +02:00
|
|
|
|
|
|
|
log.Debugf("Registry API url: %s", url)
|
2016-02-01 12:59:10 +01:00
|
|
|
response, err := http.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result, err := ioutil.ReadAll(response.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
2016-02-24 07:31:52 +01:00
|
|
|
if response.StatusCode == http.StatusOK {
|
2016-02-01 12:59:10 +01:00
|
|
|
return result, nil
|
2016-02-24 07:31:52 +01:00
|
|
|
} else if response.StatusCode == http.StatusUnauthorized {
|
2016-02-01 12:59:10 +01:00
|
|
|
authenticate := response.Header.Get("WWW-Authenticate")
|
2016-03-29 13:37:04 +02:00
|
|
|
log.Debugf("authenticate header: %s", authenticate)
|
2016-02-01 12:59:10 +01:00
|
|
|
str := strings.Split(authenticate, " ")[1]
|
|
|
|
var service string
|
|
|
|
var scope string
|
|
|
|
strs := strings.Split(str, ",")
|
|
|
|
for _, s := range strs {
|
|
|
|
if strings.Contains(s, "service") {
|
|
|
|
service = s
|
|
|
|
} else if strings.Contains(s, "scope") {
|
|
|
|
scope = s
|
|
|
|
}
|
|
|
|
}
|
2016-03-29 13:37:04 +02:00
|
|
|
if arr := strings.Split(service, "\""); len(arr) > 1 {
|
|
|
|
service = arr[1]
|
|
|
|
}
|
|
|
|
if arr := strings.Split(scope, "\""); len(arr) > 1 {
|
|
|
|
scope = arr[1]
|
|
|
|
}
|
2016-02-01 12:59:10 +01:00
|
|
|
token, err := GenTokenForUI(username, service, scope)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
request, err := http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
request.Header.Add("Authorization", "Bearer "+token)
|
|
|
|
client := &http.Client{}
|
|
|
|
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
|
2016-03-25 02:08:44 +01:00
|
|
|
// log.Infof("via length: %d\n", len(via))
|
2016-02-01 12:59:10 +01:00
|
|
|
if len(via) >= 10 {
|
|
|
|
return fmt.Errorf("too many redirects")
|
|
|
|
}
|
|
|
|
for k, v := range via[0].Header {
|
|
|
|
if _, ok := req.Header[k]; !ok {
|
|
|
|
req.Header[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
response, err = client.Do(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-02-24 07:31:52 +01:00
|
|
|
if response.StatusCode != http.StatusOK {
|
2016-02-01 12:59:10 +01:00
|
|
|
errMsg := fmt.Sprintf("Unexpected return code from registry: %d", response.StatusCode)
|
2016-03-25 02:08:44 +01:00
|
|
|
log.Error(errMsg)
|
2016-02-01 12:59:10 +01:00
|
|
|
return nil, fmt.Errorf(errMsg)
|
|
|
|
}
|
|
|
|
result, err = ioutil.ReadAll(response.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
return result, nil
|
|
|
|
} else {
|
|
|
|
return nil, errors.New(string(result))
|
|
|
|
}
|
|
|
|
}
|