harbor/api/target.go

370 lines
11 KiB
Go
Raw Normal View History

2016-05-23 10:48:55 +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 api
import (
"fmt"
2016-05-24 08:59:36 +02:00
"net"
2016-05-23 10:48:55 +02:00
"net/http"
"net/url"
2016-08-03 11:25:24 +02:00
"os"
2016-05-23 10:48:55 +02:00
"strconv"
"github.com/vmware/harbor/dao"
"github.com/vmware/harbor/models"
2016-05-27 04:45:21 +02:00
"github.com/vmware/harbor/utils"
2016-05-23 10:48:55 +02:00
"github.com/vmware/harbor/utils/log"
"github.com/vmware/harbor/utils/registry"
2016-05-23 10:48:55 +02:00
"github.com/vmware/harbor/utils/registry/auth"
2016-05-24 08:59:36 +02:00
registry_error "github.com/vmware/harbor/utils/registry/error"
2016-05-23 10:48:55 +02:00
)
// TargetAPI handles request to /api/targets/ping /api/targets/{}
type TargetAPI struct {
2016-05-23 12:41:48 +02:00
BaseAPI
2016-08-03 11:25:24 +02:00
secretKey string
2016-05-23 10:48:55 +02:00
}
// Prepare validates the user
func (t *TargetAPI) Prepare() {
2016-08-03 11:25:24 +02:00
//TODO:move to config
t.secretKey = os.Getenv("SECRET_KEY")
2016-05-23 10:48:55 +02:00
userID := t.ValidateUser()
isSysAdmin, err := dao.IsAdminRole(userID)
if err != nil {
log.Errorf("error occurred in IsAdminRole: %v", err)
t.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
}
if !isSysAdmin {
t.CustomAbort(http.StatusForbidden, http.StatusText(http.StatusForbidden))
}
}
// Ping validates whether the target is reachable and whether the credential is valid
func (t *TargetAPI) Ping() {
var endpoint, username, password string
idStr := t.GetString("id")
if len(idStr) != 0 {
id, err := strconv.ParseInt(idStr, 10, 64)
if err != nil {
t.CustomAbort(http.StatusBadRequest, fmt.Sprintf("id %s is invalid", idStr))
}
target, err := dao.GetRepTarget(id)
if err != nil {
log.Errorf("failed to get target %d: %v", id, err)
t.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
}
2016-05-24 08:59:36 +02:00
if target == nil {
t.CustomAbort(http.StatusNotFound, http.StatusText(http.StatusNotFound))
}
2016-05-23 10:48:55 +02:00
endpoint = target.URL
username = target.Username
password = target.Password
2016-05-24 08:59:36 +02:00
if len(password) != 0 {
2016-08-03 11:25:24 +02:00
password, err = utils.ReversibleDecrypt(password, t.secretKey)
2016-05-24 08:59:36 +02:00
if err != nil {
2016-05-27 04:45:21 +02:00
log.Errorf("failed to decrypt password: %v", err)
2016-05-24 08:59:36 +02:00
t.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
}
}
2016-05-23 10:48:55 +02:00
} else {
endpoint = t.GetString("endpoint")
if len(endpoint) == 0 {
t.CustomAbort(http.StatusBadRequest, "id or endpoint is needed")
}
username = t.GetString("username")
2016-05-24 08:59:36 +02:00
password = t.GetString("password")
2016-05-23 10:48:55 +02:00
}
2016-06-29 12:52:24 +02:00
registry, err := newRegistryClient(endpoint, getIsInsecure(), username, password,
"", "", "")
2016-05-23 10:48:55 +02:00
if err != nil {
2016-05-24 08:59:36 +02:00
// timeout, dns resolve error, connection refused, etc.
if urlErr, ok := err.(*url.Error); ok {
if netErr, ok := urlErr.Err.(net.Error); ok {
t.CustomAbort(http.StatusBadRequest, netErr.Error())
}
t.CustomAbort(http.StatusBadRequest, urlErr.Error())
2016-05-24 08:59:36 +02:00
}
log.Errorf("failed to create registry client: %#v", err)
2016-05-23 10:48:55 +02:00
t.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
}
if err = registry.Ping(); err != nil {
2016-05-24 08:59:36 +02:00
if regErr, ok := err.(*registry_error.Error); ok {
2016-05-23 10:48:55 +02:00
t.CustomAbort(regErr.StatusCode, regErr.Detail)
}
2016-05-24 08:59:36 +02:00
log.Errorf("failed to ping registry %s: %v", registry.Endpoint.String(), err)
2016-05-23 10:48:55 +02:00
t.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
}
}
// Get ...
func (t *TargetAPI) Get() {
2016-06-13 10:49:46 +02:00
id := t.GetIDFromURL()
2016-05-23 10:48:55 +02:00
target, err := dao.GetRepTarget(id)
if err != nil {
log.Errorf("failed to get target %d: %v", id, err)
t.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
}
if target == nil {
t.CustomAbort(http.StatusNotFound, http.StatusText(http.StatusNotFound))
}
2016-06-13 02:42:11 +02:00
// The reason why the password is returned is that when user just wants to
// modify other fields of target he does not need to input the password again.
// The security issue can be fixed by enable https.
2016-05-23 12:41:48 +02:00
if len(target.Password) != 0 {
2016-08-03 11:25:24 +02:00
pwd, err := utils.ReversibleDecrypt(target.Password, t.secretKey)
2016-05-23 12:41:48 +02:00
if err != nil {
2016-05-27 04:45:21 +02:00
log.Errorf("failed to decrypt password: %v", err)
2016-05-23 12:41:48 +02:00
t.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
}
2016-05-27 04:45:21 +02:00
target.Password = pwd
2016-05-23 12:41:48 +02:00
}
2016-05-23 10:48:55 +02:00
t.Data["json"] = target
t.ServeJSON()
}
2016-06-13 02:31:32 +02:00
// List ...
func (t *TargetAPI) List() {
name := t.GetString("name")
targets, err := dao.FilterRepTargets(name)
if err != nil {
log.Errorf("failed to filter targets %s: %v", name, err)
t.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
}
for _, target := range targets {
if len(target.Password) == 0 {
continue
}
2016-08-03 11:25:24 +02:00
str, err := utils.ReversibleDecrypt(target.Password, t.secretKey)
2016-06-13 02:31:32 +02:00
if err != nil {
log.Errorf("failed to decrypt password: %v", err)
t.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
}
target.Password = str
}
t.Data["json"] = targets
t.ServeJSON()
return
}
2016-05-27 12:46:07 +02:00
// Post ...
2016-05-23 10:48:55 +02:00
func (t *TargetAPI) Post() {
target := &models.RepTarget{}
2016-06-06 11:46:19 +02:00
t.DecodeJSONReqAndValidate(target)
2016-05-23 10:48:55 +02:00
ta, err := dao.GetRepTargetByName(target.Name)
if err != nil {
log.Errorf("failed to get target %s: %v", target.Name, err)
t.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
}
if ta != nil {
t.CustomAbort(http.StatusConflict, "name is already used")
2016-05-23 10:48:55 +02:00
}
2016-07-13 08:35:52 +02:00
ta, err = dao.GetRepTargetByEndpoint(target.URL)
if err != nil {
2016-07-13 08:35:52 +02:00
log.Errorf("failed to get target [ %s ]: %v", target.URL, err)
t.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
}
if ta != nil {
2016-07-13 08:35:52 +02:00
t.CustomAbort(http.StatusConflict, fmt.Sprintf("the target whose endpoint is %s already exists", target.URL))
}
2016-05-23 12:41:48 +02:00
if len(target.Password) != 0 {
2016-08-03 11:25:24 +02:00
target.Password, err = utils.ReversibleEncrypt(target.Password, t.secretKey)
if err != nil {
log.Errorf("failed to encrypt password: %v", err)
t.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
}
2016-05-23 12:41:48 +02:00
}
2016-05-23 10:48:55 +02:00
id, err := dao.AddRepTarget(*target)
if err != nil {
log.Errorf("failed to add target: %v", err)
t.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
}
t.Redirect(http.StatusCreated, strconv.FormatInt(id, 10))
}
// Put ...
func (t *TargetAPI) Put() {
2016-06-13 10:49:46 +02:00
id := t.GetIDFromURL()
2016-05-23 10:48:55 +02:00
2016-06-13 11:32:22 +02:00
originalTarget, err := dao.GetRepTarget(id)
if err != nil {
log.Errorf("failed to get target %d: %v", id, err)
t.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
2016-05-23 10:48:55 +02:00
}
2016-06-13 11:32:22 +02:00
if originalTarget == nil {
2016-06-13 08:24:13 +02:00
t.CustomAbort(http.StatusNotFound, http.StatusText(http.StatusNotFound))
}
policies, err := dao.GetRepPolicyByTarget(id)
if err != nil {
log.Errorf("failed to get policies according target %d: %v", id, err)
t.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
}
hasEnabledPolicy := false
for _, policy := range policies {
if policy.Enabled == 1 {
hasEnabledPolicy = true
break
}
}
if hasEnabledPolicy {
t.CustomAbort(http.StatusBadRequest, "the target is associated with policy which is enabled")
}
2016-06-13 08:24:13 +02:00
target := &models.RepTarget{}
t.DecodeJSONReqAndValidate(target)
2016-06-13 11:32:22 +02:00
if target.Name != originalTarget.Name {
ta, err := dao.GetRepTargetByName(target.Name)
if err != nil {
log.Errorf("failed to get target %s: %v", target.Name, err)
t.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
}
if ta != nil {
t.CustomAbort(http.StatusConflict, "name is already used")
}
}
2016-07-13 08:35:52 +02:00
if target.URL != originalTarget.URL {
ta, err := dao.GetRepTargetByEndpoint(target.URL)
if err != nil {
2016-07-13 08:35:52 +02:00
log.Errorf("failed to get target [ %s ]: %v", target.URL, err)
t.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
}
if ta != nil {
2016-07-13 08:35:52 +02:00
t.CustomAbort(http.StatusConflict, fmt.Sprintf("the target whose endpoint is %s already exists", target.URL))
}
}
target.ID = id
2016-05-23 12:41:48 +02:00
if len(target.Password) != 0 {
2016-08-03 11:25:24 +02:00
target.Password, err = utils.ReversibleEncrypt(target.Password, t.secretKey)
if err != nil {
log.Errorf("failed to encrypt password: %v", err)
t.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
}
2016-05-23 12:41:48 +02:00
}
2016-05-23 10:48:55 +02:00
if err := dao.UpdateRepTarget(*target); err != nil {
log.Errorf("failed to update target %d: %v", id, err)
t.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
}
}
2016-05-27 12:46:07 +02:00
// Delete ...
2016-05-24 08:59:36 +02:00
func (t *TargetAPI) Delete() {
2016-06-13 10:49:46 +02:00
id := t.GetIDFromURL()
2016-05-24 08:59:36 +02:00
target, err := dao.GetRepTarget(id)
if err != nil {
log.Errorf("failed to get target %d: %v", id, err)
t.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
}
if target == nil {
t.CustomAbort(http.StatusNotFound, http.StatusText(http.StatusNotFound))
}
policies, err := dao.GetRepPolicyByTarget(id)
if err != nil {
log.Errorf("failed to get policies according target %d: %v", id, err)
t.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
}
if len(policies) > 0 {
t.CustomAbort(http.StatusBadRequest, "the target is used by policies, can not be deleted")
}
2016-05-24 08:59:36 +02:00
if err = dao.DeleteRepTarget(id); err != nil {
log.Errorf("failed to delete target %d: %v", id, err)
t.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
}
}
func newRegistryClient(endpoint string, insecure bool, username, password, scopeType, scopeName string,
scopeActions ...string) (*registry.Registry, error) {
credential := auth.NewBasicAuthCredential(username, password)
authorizer := auth.NewStandardTokenAuthorizer(credential, insecure, scopeType, scopeName, scopeActions...)
2016-06-22 06:03:50 +02:00
store, err := auth.NewAuthorizerStore(endpoint, insecure, authorizer)
if err != nil {
return nil, err
}
client, err := registry.NewRegistryWithModifiers(endpoint, insecure, store)
if err != nil {
return nil, err
}
return client, nil
}
// ListPolicies ...
func (t *TargetAPI) ListPolicies() {
id := t.GetIDFromURL()
target, err := dao.GetRepTarget(id)
if err != nil {
log.Errorf("failed to get target %d: %v", id, err)
t.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
}
if target == nil {
t.CustomAbort(http.StatusNotFound, http.StatusText(http.StatusNotFound))
}
policies, err := dao.GetRepPolicyByTarget(id)
if err != nil {
log.Errorf("failed to get policies according target %d: %v", id, err)
t.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
}
t.Data["json"] = policies
t.ServeJSON()
}