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 api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-02-24 07:31:52 +01:00
|
|
|
"net/http"
|
2016-02-01 12:59:10 +01:00
|
|
|
|
|
|
|
"github.com/vmware/harbor/dao"
|
|
|
|
"github.com/vmware/harbor/models"
|
2016-03-28 02:50:09 +02:00
|
|
|
"github.com/vmware/harbor/utils/log"
|
2016-02-01 12:59:10 +01:00
|
|
|
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2016-02-26 11:35:55 +01:00
|
|
|
// ProjectAPI handles request to /api/projects/{} /api/projects/{}/logs
|
2016-02-01 12:59:10 +01:00
|
|
|
type ProjectAPI struct {
|
|
|
|
BaseAPI
|
2016-02-25 06:40:08 +01:00
|
|
|
userID int
|
|
|
|
projectID int64
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type projectReq struct {
|
|
|
|
ProjectName string `json:"project_name"`
|
|
|
|
Public bool `json:"public"`
|
|
|
|
}
|
|
|
|
|
2016-02-25 06:40:08 +01:00
|
|
|
const projectNameMaxLen int = 30
|
2016-02-17 07:29:05 +01:00
|
|
|
|
2016-02-26 11:35:55 +01:00
|
|
|
// Prepare validates the URL and the user
|
2016-02-01 12:59:10 +01:00
|
|
|
func (p *ProjectAPI) Prepare() {
|
2016-02-25 06:40:08 +01:00
|
|
|
p.userID = p.ValidateUser()
|
|
|
|
idStr := p.Ctx.Input.Param(":id")
|
|
|
|
if len(idStr) > 0 {
|
2016-02-01 12:59:10 +01:00
|
|
|
var err error
|
2016-02-25 06:40:08 +01:00
|
|
|
p.projectID, err = strconv.ParseInt(idStr, 10, 64)
|
2016-02-01 12:59:10 +01:00
|
|
|
if err != nil {
|
2016-03-28 02:50:09 +02:00
|
|
|
log.Errorf("Error parsing project id: %s, error: %v", idStr, err)
|
2016-02-24 07:31:52 +01:00
|
|
|
p.CustomAbort(http.StatusBadRequest, "invalid project id")
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
2016-02-25 06:40:08 +01:00
|
|
|
exist, err := dao.ProjectExists(p.projectID)
|
2016-02-01 12:59:10 +01:00
|
|
|
if err != nil {
|
2016-03-28 02:50:09 +02:00
|
|
|
log.Errorf("Error occurred in ProjectExists, error: %v", err)
|
2016-02-24 07:31:52 +01:00
|
|
|
p.CustomAbort(http.StatusInternalServerError, "Internal error.")
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
2016-02-17 07:29:05 +01:00
|
|
|
if !exist {
|
2016-02-25 06:40:08 +01:00
|
|
|
p.CustomAbort(http.StatusNotFound, fmt.Sprintf("project does not exist, id: %v", p.projectID))
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-26 11:35:55 +01:00
|
|
|
// Post ...
|
2016-02-01 12:59:10 +01:00
|
|
|
func (p *ProjectAPI) Post() {
|
|
|
|
var req projectReq
|
|
|
|
var public int
|
2016-02-25 06:40:08 +01:00
|
|
|
p.DecodeJSONReq(&req)
|
2016-02-01 12:59:10 +01:00
|
|
|
if req.Public {
|
|
|
|
public = 1
|
|
|
|
}
|
2016-02-17 07:29:05 +01:00
|
|
|
err := validateProjectReq(req)
|
|
|
|
if err != nil {
|
2016-03-28 02:50:09 +02:00
|
|
|
log.Errorf("Invalid project request, error: %v", err)
|
2016-02-24 07:31:52 +01:00
|
|
|
p.RenderError(http.StatusBadRequest, "Invalid request for creating project")
|
2016-02-17 07:29:05 +01:00
|
|
|
return
|
|
|
|
}
|
2016-02-01 12:59:10 +01:00
|
|
|
projectName := req.ProjectName
|
|
|
|
exist, err := dao.ProjectExists(projectName)
|
|
|
|
if err != nil {
|
2016-03-28 02:50:09 +02:00
|
|
|
log.Errorf("Error happened checking project existence in db, error: %v, project name: %s", err, projectName)
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
|
|
|
if exist {
|
2016-02-24 07:31:52 +01:00
|
|
|
p.RenderError(http.StatusConflict, "")
|
2016-02-01 12:59:10 +01:00
|
|
|
return
|
|
|
|
}
|
2016-02-26 03:15:01 +01:00
|
|
|
project := models.Project{OwnerID: p.userID, Name: projectName, CreationTime: time.Now(), Public: public}
|
2016-04-22 11:57:39 +02:00
|
|
|
projectID, err := dao.AddProject(project)
|
2016-02-17 07:29:05 +01:00
|
|
|
if err != nil {
|
2016-03-28 02:50:09 +02:00
|
|
|
log.Errorf("Failed to add project, error: %v", err)
|
2016-02-24 07:31:52 +01:00
|
|
|
p.RenderError(http.StatusInternalServerError, "Failed to add project")
|
2016-02-17 07:29:05 +01:00
|
|
|
}
|
2016-04-22 11:57:39 +02:00
|
|
|
|
2016-04-25 06:10:15 +02:00
|
|
|
p.Redirect(http.StatusCreated, strconv.FormatInt(projectID, 10))
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
|
|
|
|
2016-02-26 11:35:55 +01:00
|
|
|
// Head ...
|
2016-02-01 12:59:10 +01:00
|
|
|
func (p *ProjectAPI) Head() {
|
|
|
|
projectName := p.GetString("project_name")
|
|
|
|
result, err := dao.ProjectExists(projectName)
|
|
|
|
if err != nil {
|
2016-03-28 02:50:09 +02:00
|
|
|
log.Errorf("Error while communicating with DB, error: %v", err)
|
2016-02-24 07:31:52 +01:00
|
|
|
p.RenderError(http.StatusInternalServerError, "Error while communicating with DB")
|
2016-02-01 12:59:10 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !result {
|
2016-02-24 07:31:52 +01:00
|
|
|
p.RenderError(http.StatusNotFound, "")
|
2016-02-01 12:59:10 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-26 11:35:55 +01:00
|
|
|
// Get ...
|
2016-02-01 12:59:10 +01:00
|
|
|
func (p *ProjectAPI) Get() {
|
2016-02-26 03:15:01 +01:00
|
|
|
queryProject := models.Project{UserID: p.userID}
|
2016-02-01 12:59:10 +01:00
|
|
|
projectName := p.GetString("project_name")
|
|
|
|
if len(projectName) > 0 {
|
|
|
|
queryProject.Name = "%" + projectName + "%"
|
|
|
|
}
|
|
|
|
public, _ := p.GetInt("is_public")
|
|
|
|
queryProject.Public = public
|
|
|
|
|
|
|
|
projectList, err := dao.QueryProject(queryProject)
|
|
|
|
if err != nil {
|
2016-03-28 02:50:09 +02:00
|
|
|
log.Errorf("Error occurred in QueryProject, error: %v", err)
|
2016-02-24 07:31:52 +01:00
|
|
|
p.CustomAbort(http.StatusInternalServerError, "Internal error.")
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
|
|
|
for i := 0; i < len(projectList); i++ {
|
2016-05-13 16:45:30 +02:00
|
|
|
if projectList[i].Role == models.PROJECTADMIN {
|
2016-02-01 12:59:10 +01:00
|
|
|
projectList[i].Togglable = true
|
|
|
|
}
|
2016-05-13 16:45:30 +02:00
|
|
|
projectList[i].RepoCount = getRepoCountByProject(projectList[i].Name)
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
|
|
|
p.Data["json"] = projectList
|
|
|
|
p.ServeJSON()
|
|
|
|
}
|
|
|
|
|
2016-02-26 11:35:55 +01:00
|
|
|
// Put ...
|
2016-02-01 12:59:10 +01:00
|
|
|
func (p *ProjectAPI) Put() {
|
|
|
|
var req projectReq
|
|
|
|
var public int
|
|
|
|
|
2016-02-25 06:40:08 +01:00
|
|
|
projectID, err := strconv.ParseInt(p.Ctx.Input.Param(":id"), 10, 64)
|
2016-02-01 12:59:10 +01:00
|
|
|
if err != nil {
|
2016-03-28 02:50:09 +02:00
|
|
|
log.Errorf("Error parsing project id: %d, error: %v", projectID, err)
|
2016-02-24 07:31:52 +01:00
|
|
|
p.RenderError(http.StatusBadRequest, "invalid project id")
|
2016-02-01 12:59:10 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-02-25 06:40:08 +01:00
|
|
|
p.DecodeJSONReq(&req)
|
2016-02-01 12:59:10 +01:00
|
|
|
if req.Public {
|
|
|
|
public = 1
|
|
|
|
}
|
2016-02-25 06:40:08 +01:00
|
|
|
if !isProjectAdmin(p.userID, projectID) {
|
2016-03-28 02:50:09 +02:00
|
|
|
log.Warningf("Current user, id: %d does not have project admin role for project, id: %d", p.userID, projectID)
|
2016-02-24 07:31:52 +01:00
|
|
|
p.RenderError(http.StatusForbidden, "")
|
2016-02-01 12:59:10 +01:00
|
|
|
return
|
|
|
|
}
|
2016-02-25 06:40:08 +01:00
|
|
|
err = dao.ToggleProjectPublicity(p.projectID, public)
|
2016-02-01 12:59:10 +01:00
|
|
|
if err != nil {
|
2016-03-28 02:50:09 +02:00
|
|
|
log.Errorf("Error while updating project, project id: %d, error: %v", projectID, err)
|
2016-02-24 07:31:52 +01:00
|
|
|
p.RenderError(http.StatusInternalServerError, "Failed to update project")
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-26 11:35:55 +01:00
|
|
|
// FilterAccessLog handles GET to /api/projects/{}/logs
|
2016-02-01 12:59:10 +01:00
|
|
|
func (p *ProjectAPI) FilterAccessLog() {
|
|
|
|
|
|
|
|
var filter models.AccessLog
|
2016-02-25 06:40:08 +01:00
|
|
|
p.DecodeJSONReq(&filter)
|
2016-02-01 12:59:10 +01:00
|
|
|
|
|
|
|
username := filter.Username
|
|
|
|
keywords := filter.Keywords
|
|
|
|
|
2016-02-19 05:34:18 +01:00
|
|
|
beginTime := time.Unix(filter.BeginTimestamp, 0)
|
|
|
|
endTime := time.Unix(filter.EndTimestamp, 0)
|
|
|
|
|
2016-02-26 03:15:01 +01:00
|
|
|
query := models.AccessLog{ProjectID: p.projectID, Username: "%" + username + "%", Keywords: keywords, BeginTime: beginTime, BeginTimestamp: filter.BeginTimestamp, EndTime: endTime, EndTimestamp: filter.EndTimestamp}
|
2016-02-19 05:34:18 +01:00
|
|
|
|
2016-03-28 02:50:09 +02:00
|
|
|
log.Infof("Query AccessLog: begin: %v, end: %v, keywords: %s", query.BeginTime, query.EndTime, query.Keywords)
|
2016-02-19 05:34:18 +01:00
|
|
|
|
2016-02-01 12:59:10 +01:00
|
|
|
accessLogList, err := dao.GetAccessLogs(query)
|
|
|
|
if err != nil {
|
2016-03-28 02:50:09 +02:00
|
|
|
log.Errorf("Error occurred in GetAccessLogs, error: %v", err)
|
2016-02-24 07:31:52 +01:00
|
|
|
p.CustomAbort(http.StatusInternalServerError, "Internal error.")
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
|
|
|
p.Data["json"] = accessLogList
|
2016-04-21 18:28:59 +02:00
|
|
|
|
2016-02-01 12:59:10 +01:00
|
|
|
p.ServeJSON()
|
|
|
|
}
|
|
|
|
|
2016-02-25 06:40:08 +01:00
|
|
|
func isProjectAdmin(userID int, pid int64) bool {
|
2016-05-09 06:45:45 +02:00
|
|
|
isSysAdmin, err := dao.IsAdminRole(userID)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Error occurred in IsAdminRole, returning false, error: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if isSysAdmin {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-03-29 06:09:27 +02:00
|
|
|
rolelist, err := dao.GetUserProjectRoles(userID, pid)
|
2016-02-01 12:59:10 +01:00
|
|
|
if err != nil {
|
2016-03-28 02:50:09 +02:00
|
|
|
log.Errorf("Error occurred in GetUserProjectRoles, returning false, error: %v", err)
|
2016-02-01 12:59:10 +01:00
|
|
|
return false
|
|
|
|
}
|
2016-03-29 06:09:27 +02:00
|
|
|
|
|
|
|
hasProjectAdminRole := false
|
|
|
|
for _, role := range rolelist {
|
|
|
|
if role.RoleID == models.PROJECTADMIN {
|
|
|
|
hasProjectAdminRole = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return hasProjectAdminRole
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
2016-02-17 07:29:05 +01:00
|
|
|
|
|
|
|
func validateProjectReq(req projectReq) error {
|
|
|
|
pn := req.ProjectName
|
|
|
|
if len(pn) == 0 {
|
|
|
|
return fmt.Errorf("Project name can not be empty")
|
|
|
|
}
|
2016-02-25 06:40:08 +01:00
|
|
|
if len(pn) > projectNameMaxLen {
|
2016-02-17 07:29:05 +01:00
|
|
|
return fmt.Errorf("Project name is too long")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|