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"
|
|
|
|
"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"
|
|
|
|
registry_util "github.com/vmware/harbor/utils/registry"
|
|
|
|
"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-05-23 10:48:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare validates the user
|
|
|
|
func (t *TargetAPI) Prepare() {
|
|
|
|
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-05-27 04:45:21 +02:00
|
|
|
password, err = utils.ReversibleDecrypt(password)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
credential := auth.NewBasicAuthCredential(username, password)
|
|
|
|
registry, err := registry_util.NewRegistryWithCredential(endpoint, credential)
|
|
|
|
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())
|
|
|
|
}
|
2016-06-01 09:09:10 +02:00
|
|
|
|
|
|
|
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() {
|
|
|
|
id := t.getIDFromURL()
|
|
|
|
// list targets
|
|
|
|
if id == 0 {
|
|
|
|
targets, err := dao.GetAllRepTargets()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed to get all targets: %v", err)
|
|
|
|
t.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
|
|
|
|
}
|
2016-05-23 12:41:48 +02:00
|
|
|
|
|
|
|
for _, target := range targets {
|
2016-05-27 12:46:07 +02:00
|
|
|
target.Password = ""
|
2016-05-23 12:41:48 +02:00
|
|
|
}
|
|
|
|
|
2016-05-23 10:48:55 +02:00
|
|
|
t.Data["json"] = targets
|
|
|
|
t.ServeJSON()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
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-05-23 12:41:48 +02:00
|
|
|
if len(target.Password) != 0 {
|
2016-05-27 04:45:21 +02:00
|
|
|
pwd, err := utils.ReversibleDecrypt(target.Password)
|
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-05-27 12:46:07 +02:00
|
|
|
// Post ...
|
2016-05-23 10:48:55 +02:00
|
|
|
func (t *TargetAPI) Post() {
|
|
|
|
target := &models.RepTarget{}
|
|
|
|
t.DecodeJSONReq(target)
|
|
|
|
|
|
|
|
if len(target.Name) == 0 || len(target.URL) == 0 {
|
|
|
|
t.CustomAbort(http.StatusBadRequest, "name or URL is nil")
|
|
|
|
}
|
|
|
|
|
2016-05-23 12:41:48 +02:00
|
|
|
if len(target.Password) != 0 {
|
2016-05-27 04:45:21 +02:00
|
|
|
target.Password = utils.ReversibleEncrypt(target.Password)
|
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() {
|
|
|
|
id := t.getIDFromURL()
|
|
|
|
if id == 0 {
|
|
|
|
t.CustomAbort(http.StatusBadRequest, http.StatusText(http.StatusBadRequest))
|
|
|
|
}
|
|
|
|
|
|
|
|
target := &models.RepTarget{}
|
|
|
|
t.DecodeJSONReq(target)
|
|
|
|
|
2016-05-27 12:46:07 +02:00
|
|
|
if target.ID == 0 {
|
|
|
|
target.ID = id
|
2016-05-23 10:48:55 +02:00
|
|
|
}
|
|
|
|
|
2016-05-23 12:41:48 +02:00
|
|
|
if len(target.Password) != 0 {
|
2016-05-27 04:45:21 +02:00
|
|
|
target.Password = utils.ReversibleEncrypt(target.Password)
|
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() {
|
|
|
|
id := t.getIDFromURL()
|
|
|
|
if id == 0 {
|
|
|
|
t.CustomAbort(http.StatusBadRequest, http.StatusText(http.StatusBadRequest))
|
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-23 10:48:55 +02:00
|
|
|
func (t *TargetAPI) getIDFromURL() int64 {
|
2016-05-23 12:41:48 +02:00
|
|
|
idStr := t.Ctx.Input.Param(":id")
|
2016-05-23 10:48:55 +02:00
|
|
|
if len(idStr) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := strconv.ParseInt(idStr, 10, 64)
|
|
|
|
if err != nil {
|
2016-05-23 12:41:48 +02:00
|
|
|
t.CustomAbort(http.StatusBadRequest, "invalid ID in request URL")
|
2016-05-23 10:48:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return id
|
|
|
|
}
|