2018-09-13 09:06:15 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/goharbor/harbor/src/common"
|
|
|
|
"github.com/goharbor/harbor/src/common/models"
|
2019-02-01 11:55:06 +01:00
|
|
|
"github.com/goharbor/harbor/src/common/rbac"
|
2018-09-13 09:06:15 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
versionParam = ":version"
|
|
|
|
idParam = ":id"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ChartLabelAPI handles the requests of marking/removing lables to/from charts.
|
|
|
|
type ChartLabelAPI struct {
|
|
|
|
LabelResourceAPI
|
|
|
|
project *models.Project
|
|
|
|
chartFullName string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare required material for follow-up actions.
|
|
|
|
func (cla *ChartLabelAPI) Prepare() {
|
|
|
|
// Super
|
|
|
|
cla.LabelResourceAPI.Prepare()
|
|
|
|
|
|
|
|
// Check authorization
|
|
|
|
if !cla.SecurityCtx.IsAuthenticated() {
|
|
|
|
cla.SendUnAuthorizedError(errors.New("UnAuthorized"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
project := cla.GetStringFromPath(namespaceParam)
|
|
|
|
|
|
|
|
// Project should be a valid existing one
|
|
|
|
existingProject, err := cla.ProjectMgr.Get(project)
|
|
|
|
if err != nil {
|
|
|
|
cla.SendInternalServerError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if existingProject == nil {
|
|
|
|
cla.SendNotFoundError(fmt.Errorf("project '%s' not found", project))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cla.project = existingProject
|
|
|
|
|
|
|
|
// Check the existence of target chart
|
|
|
|
chartName := cla.GetStringFromPath(nameParam)
|
|
|
|
version := cla.GetStringFromPath(versionParam)
|
|
|
|
|
2018-09-19 08:45:52 +02:00
|
|
|
if _, err = chartController.GetChartVersion(project, chartName, version); err != nil {
|
2018-09-13 09:06:15 +02:00
|
|
|
cla.SendNotFoundError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cla.chartFullName = fmt.Sprintf("%s/%s:%s", project, chartName, version)
|
|
|
|
}
|
|
|
|
|
2019-02-01 11:55:06 +01:00
|
|
|
func (cla *ChartLabelAPI) requireAccess(action rbac.Action) bool {
|
|
|
|
resource := rbac.NewProjectNamespace(cla.project.ProjectID).Resource(rbac.ResourceHelmChartVersionLabel)
|
|
|
|
|
|
|
|
if !cla.SecurityCtx.Can(action, resource) {
|
2019-04-17 10:43:06 +02:00
|
|
|
cla.SendForbiddenError(errors.New(cla.SecurityCtx.GetUsername()))
|
2019-02-01 11:55:06 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-09-13 09:06:15 +02:00
|
|
|
// MarkLabel handles the request of marking label to chart.
|
|
|
|
func (cla *ChartLabelAPI) MarkLabel() {
|
2019-02-01 11:55:06 +01:00
|
|
|
if !cla.requireAccess(rbac.ActionCreate) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-13 09:06:15 +02:00
|
|
|
l := &models.Label{}
|
2019-04-17 10:43:06 +02:00
|
|
|
if err := cla.DecodeJSONReq(l); err != nil {
|
|
|
|
cla.SendBadRequestError(err)
|
|
|
|
return
|
|
|
|
}
|
2018-09-13 09:06:15 +02:00
|
|
|
|
|
|
|
label, ok := cla.validate(l.ID, cla.project.ProjectID)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
label2Res := &models.ResourceLabel{
|
|
|
|
LabelID: label.ID,
|
|
|
|
ResourceType: common.ResourceTypeChart,
|
|
|
|
ResourceName: cla.chartFullName,
|
|
|
|
}
|
|
|
|
|
|
|
|
cla.markLabelToResource(label2Res)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveLabel handles the request of removing label from chart.
|
|
|
|
func (cla *ChartLabelAPI) RemoveLabel() {
|
2019-02-01 11:55:06 +01:00
|
|
|
if !cla.requireAccess(rbac.ActionDelete) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-13 09:06:15 +02:00
|
|
|
lID, err := cla.GetInt64FromPath(idParam)
|
|
|
|
if err != nil {
|
|
|
|
cla.SendInternalServerError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
label, ok := cla.exists(lID)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cla.removeLabelFromResource(common.ResourceTypeChart, cla.chartFullName, label.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLabels gets labels for the specified chart version.
|
|
|
|
func (cla *ChartLabelAPI) GetLabels() {
|
|
|
|
cla.getLabelsOfResource(common.ResourceTypeChart, cla.chartFullName)
|
|
|
|
}
|