2018-10-19 00:01:31 +08:00
|
|
|
// Copyright 2018 Project Harbor Authors
|
2017-05-09 18:18:23 +08:00
|
|
|
//
|
|
|
|
|
// 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 (
|
2019-08-16 13:54:57 +08:00
|
|
|
"encoding/json"
|
2019-07-19 16:19:10 +08:00
|
|
|
"errors"
|
2019-08-16 13:54:57 +08:00
|
|
|
"fmt"
|
2019-09-19 01:17:07 +08:00
|
|
|
"github.com/goharbor/harbor/src/common/models"
|
2017-05-09 18:18:23 +08:00
|
|
|
"net/http"
|
|
|
|
|
|
2019-04-17 16:43:06 +08:00
|
|
|
"github.com/ghodss/yaml"
|
2018-08-23 15:02:20 +08:00
|
|
|
"github.com/goharbor/harbor/src/common/api"
|
2019-08-22 03:07:19 +08:00
|
|
|
"github.com/goharbor/harbor/src/common/rbac"
|
2018-08-23 15:02:20 +08:00
|
|
|
"github.com/goharbor/harbor/src/common/security"
|
2019-08-22 03:07:19 +08:00
|
|
|
"github.com/goharbor/harbor/src/common/utils"
|
2018-08-23 15:02:20 +08:00
|
|
|
"github.com/goharbor/harbor/src/common/utils/log"
|
2018-09-12 14:38:29 +08:00
|
|
|
"github.com/goharbor/harbor/src/core/config"
|
2018-09-20 23:30:36 +08:00
|
|
|
"github.com/goharbor/harbor/src/core/filter"
|
2018-09-12 14:38:29 +08:00
|
|
|
"github.com/goharbor/harbor/src/core/promgr"
|
2019-07-19 16:19:10 +08:00
|
|
|
"github.com/goharbor/harbor/src/pkg/project"
|
|
|
|
|
"github.com/goharbor/harbor/src/pkg/repository"
|
2019-08-22 03:07:19 +08:00
|
|
|
"github.com/goharbor/harbor/src/pkg/retention"
|
|
|
|
|
"github.com/goharbor/harbor/src/pkg/scheduler"
|
2017-05-09 18:18:23 +08:00
|
|
|
)
|
|
|
|
|
|
2018-09-19 14:45:52 +08:00
|
|
|
const (
|
|
|
|
|
yamlFileContentType = "application/x-yaml"
|
2019-09-19 01:17:07 +08:00
|
|
|
userSessionKey = "user"
|
2019-07-19 16:19:10 +08:00
|
|
|
)
|
|
|
|
|
|
2019-07-19 19:44:11 +08:00
|
|
|
// the managers/controllers used globally
|
2019-07-19 16:19:10 +08:00
|
|
|
var (
|
2019-07-24 17:22:26 +08:00
|
|
|
projectMgr project.Manager
|
|
|
|
|
repositoryMgr repository.Manager
|
|
|
|
|
retentionScheduler scheduler.Scheduler
|
|
|
|
|
retentionMgr retention.Manager
|
|
|
|
|
retentionLauncher retention.Launcher
|
|
|
|
|
retentionController retention.APIController
|
2018-09-19 14:45:52 +08:00
|
|
|
)
|
|
|
|
|
|
2019-08-22 03:07:19 +08:00
|
|
|
var (
|
|
|
|
|
errNotFound = errors.New("not found")
|
|
|
|
|
)
|
|
|
|
|
|
2017-05-09 18:18:23 +08:00
|
|
|
// BaseController ...
|
|
|
|
|
type BaseController struct {
|
|
|
|
|
api.BaseAPI
|
2017-05-11 17:02:01 +08:00
|
|
|
// SecurityCtx is the security context used to authN &authZ
|
|
|
|
|
SecurityCtx security.Context
|
|
|
|
|
// ProjectMgr is the project manager which abstracts the operations
|
2017-05-09 18:18:23 +08:00
|
|
|
// related to projects
|
2017-09-26 16:41:08 +08:00
|
|
|
ProjectMgr promgr.ProjectManager
|
2017-05-09 18:18:23 +08:00
|
|
|
}
|
|
|
|
|
|
2017-05-11 16:45:24 +08:00
|
|
|
// Prepare inits security context and project manager from request
|
2017-05-09 18:18:23 +08:00
|
|
|
// context
|
|
|
|
|
func (b *BaseController) Prepare() {
|
2018-09-20 23:30:36 +08:00
|
|
|
ctx, err := filter.GetSecurityContext(b.Ctx.Request)
|
2017-05-11 16:45:24 +08:00
|
|
|
if err != nil {
|
2017-06-18 13:51:42 +08:00
|
|
|
log.Errorf("failed to get security context: %v", err)
|
2019-04-17 16:43:06 +08:00
|
|
|
b.SendInternalServerError(errors.New(""))
|
|
|
|
|
return
|
2017-05-09 18:18:23 +08:00
|
|
|
}
|
2017-05-11 16:45:24 +08:00
|
|
|
b.SecurityCtx = ctx
|
2017-05-09 18:18:23 +08:00
|
|
|
|
2017-05-11 16:45:24 +08:00
|
|
|
pm, err := filter.GetProjectManager(b.Ctx.Request)
|
|
|
|
|
if err != nil {
|
2017-06-18 13:51:42 +08:00
|
|
|
log.Errorf("failed to get project manager: %v", err)
|
2019-04-17 16:43:06 +08:00
|
|
|
b.SendInternalServerError(errors.New(""))
|
|
|
|
|
return
|
2017-05-09 18:18:23 +08:00
|
|
|
}
|
2018-09-20 23:30:36 +08:00
|
|
|
b.ProjectMgr = pm
|
2019-11-07 13:02:17 +08:00
|
|
|
|
|
|
|
|
if !filter.ReqCarriesSession(b.Ctx.Request) {
|
|
|
|
|
b.EnableXSRF = false
|
|
|
|
|
}
|
2017-05-09 18:18:23 +08:00
|
|
|
}
|
2018-07-19 23:50:25 +08:00
|
|
|
|
2019-08-22 03:07:19 +08:00
|
|
|
// RequireAuthenticated returns true when the request is authenticated
|
|
|
|
|
// otherwise send Unauthorized response and returns false
|
|
|
|
|
func (b *BaseController) RequireAuthenticated() bool {
|
|
|
|
|
if !b.SecurityCtx.IsAuthenticated() {
|
|
|
|
|
b.SendUnAuthorizedError(errors.New("Unauthorized"))
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// HasProjectPermission returns true when the request has action permission on project subresource
|
|
|
|
|
func (b *BaseController) HasProjectPermission(projectIDOrName interface{}, action rbac.Action, subresource ...rbac.Resource) (bool, error) {
|
|
|
|
|
projectID, projectName, err := utils.ParseProjectIDOrName(projectIDOrName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if projectName != "" {
|
|
|
|
|
project, err := b.ProjectMgr.Get(projectName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
if project == nil {
|
|
|
|
|
return false, errNotFound
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
projectID = project.ProjectID
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resource := rbac.NewProjectNamespace(projectID).Resource(subresource...)
|
|
|
|
|
if !b.SecurityCtx.Can(action, resource) {
|
|
|
|
|
return false, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RequireProjectAccess returns true when the request has action access on project subresource
|
|
|
|
|
// otherwise send UnAuthorized or Forbidden response and returns false
|
|
|
|
|
func (b *BaseController) RequireProjectAccess(projectIDOrName interface{}, action rbac.Action, subresource ...rbac.Resource) bool {
|
|
|
|
|
hasPermission, err := b.HasProjectPermission(projectIDOrName, action, subresource...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if err == errNotFound {
|
|
|
|
|
b.SendNotFoundError(fmt.Errorf("project %v not found", projectIDOrName))
|
|
|
|
|
} else {
|
|
|
|
|
b.SendInternalServerError(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !hasPermission {
|
|
|
|
|
if !b.SecurityCtx.IsAuthenticated() {
|
|
|
|
|
b.SendUnAuthorizedError(errors.New("UnAuthorized"))
|
|
|
|
|
} else {
|
|
|
|
|
b.SendForbiddenError(errors.New(b.SecurityCtx.GetUsername()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-19 14:45:52 +08:00
|
|
|
// WriteJSONData writes the JSON data to the client.
|
|
|
|
|
func (b *BaseController) WriteJSONData(object interface{}) {
|
|
|
|
|
b.Data["json"] = object
|
|
|
|
|
b.ServeJSON()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WriteYamlData writes the yaml data to the client.
|
|
|
|
|
func (b *BaseController) WriteYamlData(object interface{}) {
|
|
|
|
|
yData, err := yaml.Marshal(object)
|
|
|
|
|
if err != nil {
|
|
|
|
|
b.SendInternalServerError(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w := b.Ctx.ResponseWriter
|
|
|
|
|
w.Header().Set("Content-Type", yamlFileContentType)
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
2019-07-24 17:27:37 +08:00
|
|
|
_, _ = w.Write(yData)
|
2018-09-19 14:45:52 +08:00
|
|
|
}
|
|
|
|
|
|
2019-09-19 01:17:07 +08:00
|
|
|
// PopulateUserSession generates a new session ID and fill the user model in parm to the session
|
|
|
|
|
func (b *BaseController) PopulateUserSession(u models.User) {
|
|
|
|
|
b.SessionRegenerateID()
|
|
|
|
|
b.SetSession(userSessionKey, u)
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-05 16:16:31 +08:00
|
|
|
// Init related objects/configurations for the API controllers
|
2018-07-19 23:50:25 +08:00
|
|
|
func Init() error {
|
2019-01-11 12:53:34 +08:00
|
|
|
registerHealthCheckers()
|
2019-07-19 16:19:10 +08:00
|
|
|
|
|
|
|
|
// init chart controller
|
|
|
|
|
if err := initChartController(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// init project manager
|
|
|
|
|
initProjectManager()
|
|
|
|
|
|
|
|
|
|
// init repository manager
|
|
|
|
|
initRepositoryManager()
|
|
|
|
|
|
2019-07-24 17:22:26 +08:00
|
|
|
initRetentionScheduler()
|
|
|
|
|
|
|
|
|
|
retentionMgr = retention.NewManager()
|
|
|
|
|
|
|
|
|
|
retentionLauncher = retention.NewLauncher(projectMgr, repositoryMgr, retentionMgr)
|
|
|
|
|
|
2019-07-25 15:39:20 +08:00
|
|
|
retentionController = retention.NewAPIController(retentionMgr, projectMgr, repositoryMgr, retentionScheduler, retentionLauncher)
|
2019-07-24 17:22:26 +08:00
|
|
|
|
|
|
|
|
callbackFun := func(p interface{}) error {
|
2019-08-16 13:54:57 +08:00
|
|
|
str, ok := p.(string)
|
|
|
|
|
if !ok {
|
|
|
|
|
return fmt.Errorf("the type of param %v isn't string", p)
|
2019-07-24 17:22:26 +08:00
|
|
|
}
|
2019-08-16 13:54:57 +08:00
|
|
|
param := &retention.TriggerParam{}
|
|
|
|
|
if err := json.Unmarshal([]byte(str), param); err != nil {
|
|
|
|
|
return fmt.Errorf("failed to unmarshal the param: %v", err)
|
|
|
|
|
}
|
|
|
|
|
_, err := retentionController.TriggerRetentionExec(param.PolicyID, param.Trigger, false)
|
|
|
|
|
return err
|
2019-07-24 17:22:26 +08:00
|
|
|
}
|
2019-07-25 11:32:56 +08:00
|
|
|
err := scheduler.Register(retention.SchedulerCallback, callbackFun)
|
2019-07-24 17:22:26 +08:00
|
|
|
|
|
|
|
|
return err
|
2019-07-19 16:19:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func initChartController() error {
|
2018-09-05 16:16:31 +08:00
|
|
|
// If chart repository is not enabled then directly return
|
2018-07-19 23:50:25 +08:00
|
|
|
if !config.WithChartMuseum() {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
chartCtl, err := initializeChartController()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
chartController = chartCtl
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2019-07-19 16:19:10 +08:00
|
|
|
|
|
|
|
|
func initProjectManager() {
|
|
|
|
|
projectMgr = project.New()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func initRepositoryManager() {
|
|
|
|
|
repositoryMgr = repository.New(projectMgr, chartController)
|
|
|
|
|
}
|
2019-07-24 17:22:26 +08:00
|
|
|
|
|
|
|
|
func initRetentionScheduler() {
|
|
|
|
|
retentionScheduler = scheduler.GlobalScheduler
|
|
|
|
|
}
|