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 (
|
|
|
|
"encoding/json"
|
2016-02-24 07:31:52 +01:00
|
|
|
"net/http"
|
2016-04-15 07:17:32 +02:00
|
|
|
"os"
|
2016-05-20 08:02:44 +02:00
|
|
|
"sort"
|
2016-02-01 12:59:10 +01:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2016-04-25 07:31:58 +02:00
|
|
|
"github.com/docker/distribution/manifest/schema1"
|
2016-02-01 12:59:10 +01:00
|
|
|
"github.com/vmware/harbor/dao"
|
|
|
|
"github.com/vmware/harbor/models"
|
2016-05-25 09:24:44 +02:00
|
|
|
"github.com/vmware/harbor/service/cache"
|
2016-02-01 12:59:10 +01:00
|
|
|
svc_utils "github.com/vmware/harbor/service/utils"
|
2016-03-28 02:50:09 +02:00
|
|
|
"github.com/vmware/harbor/utils/log"
|
2016-04-15 07:17:32 +02:00
|
|
|
"github.com/vmware/harbor/utils/registry"
|
2016-05-24 08:59:36 +02:00
|
|
|
registry_error "github.com/vmware/harbor/utils/registry/error"
|
2016-02-01 12:59:10 +01:00
|
|
|
)
|
|
|
|
|
2016-02-26 11:35:55 +01:00
|
|
|
// RepositoryAPI handles request to /api/repositories /api/repositories/tags /api/repositories/manifests, the parm has to be put
|
|
|
|
// in the query string as the web framework can not parse the URL if it contains veriadic sectors.
|
|
|
|
// For repostiories, we won't check the session in this API due to search functionality, querying manifest will be contorlled by
|
|
|
|
// the security of registry
|
2016-02-01 12:59:10 +01:00
|
|
|
type RepositoryAPI struct {
|
|
|
|
BaseAPI
|
|
|
|
}
|
|
|
|
|
2016-02-26 11:35:55 +01:00
|
|
|
// Get ...
|
2016-02-01 12:59:10 +01:00
|
|
|
func (ra *RepositoryAPI) Get() {
|
2016-05-19 12:36:40 +02:00
|
|
|
projectID, err := ra.GetInt64("project_id")
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to get project id, error: %v", err)
|
2016-02-24 07:31:52 +01:00
|
|
|
ra.RenderError(http.StatusBadRequest, "Invalid project id")
|
2016-02-01 12:59:10 +01:00
|
|
|
return
|
|
|
|
}
|
2016-02-26 04:26:54 +01:00
|
|
|
p, err := dao.GetProjectByID(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 GetProjectById, error: %v", err)
|
2016-02-24 07:31:52 +01:00
|
|
|
ra.CustomAbort(http.StatusInternalServerError, "Internal error.")
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
|
|
|
if p == nil {
|
2016-03-28 02:50:09 +02:00
|
|
|
log.Warningf("Project with Id: %d does not exist", projectID)
|
2016-02-24 07:31:52 +01:00
|
|
|
ra.RenderError(http.StatusNotFound, "")
|
2016-02-01 12:59:10 +01:00
|
|
|
return
|
|
|
|
}
|
2016-05-19 12:36:40 +02:00
|
|
|
|
|
|
|
if p.Public == 0 {
|
2016-05-26 08:12:26 +02:00
|
|
|
var userID int
|
|
|
|
|
|
|
|
if svc_utils.VerifySecret(ra.Ctx.Request) {
|
|
|
|
userID = 1
|
|
|
|
} else {
|
|
|
|
userID = ra.ValidateUser()
|
|
|
|
}
|
2016-05-19 12:36:40 +02:00
|
|
|
|
|
|
|
if !checkProjectPermission(userID, projectID) {
|
|
|
|
ra.RenderError(http.StatusForbidden, "")
|
|
|
|
return
|
|
|
|
}
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
2016-04-15 07:17:32 +02:00
|
|
|
|
2016-05-25 09:24:44 +02:00
|
|
|
repoList, err := cache.GetRepoFromCache()
|
2016-02-01 12:59:10 +01:00
|
|
|
if err != nil {
|
2016-03-28 02:50:09 +02:00
|
|
|
log.Errorf("Failed to get repo from cache, error: %v", err)
|
2016-02-24 07:31:52 +01:00
|
|
|
ra.RenderError(http.StatusInternalServerError, "internal sever error")
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
2016-04-15 07:17:32 +02:00
|
|
|
|
2016-02-01 12:59:10 +01:00
|
|
|
projectName := p.Name
|
|
|
|
q := ra.GetString("q")
|
|
|
|
var resp []string
|
|
|
|
if len(q) > 0 {
|
|
|
|
for _, r := range repoList {
|
|
|
|
if strings.Contains(r, "/") && strings.Contains(r[strings.LastIndex(r, "/")+1:], q) && r[0:strings.LastIndex(r, "/")] == projectName {
|
|
|
|
resp = append(resp, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ra.Data["json"] = resp
|
|
|
|
} else if len(projectName) > 0 {
|
|
|
|
for _, r := range repoList {
|
|
|
|
if strings.Contains(r, "/") && r[0:strings.LastIndex(r, "/")] == projectName {
|
|
|
|
resp = append(resp, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ra.Data["json"] = resp
|
|
|
|
} else {
|
|
|
|
ra.Data["json"] = repoList
|
|
|
|
}
|
|
|
|
ra.ServeJSON()
|
|
|
|
}
|
|
|
|
|
2016-04-15 07:17:32 +02:00
|
|
|
// Delete ...
|
|
|
|
func (ra *RepositoryAPI) Delete() {
|
|
|
|
repoName := ra.GetString("repo_name")
|
|
|
|
if len(repoName) == 0 {
|
|
|
|
ra.CustomAbort(http.StatusBadRequest, "repo_name is nil")
|
|
|
|
}
|
|
|
|
|
2016-05-26 07:26:10 +02:00
|
|
|
rc, err := ra.initRepositoryClient(repoName)
|
2016-04-27 11:59:43 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("error occurred while initializing repository client for %s: %v", repoName, err)
|
|
|
|
ra.CustomAbort(http.StatusInternalServerError, "internal error")
|
|
|
|
}
|
|
|
|
|
2016-04-15 07:17:32 +02:00
|
|
|
tags := []string{}
|
|
|
|
tag := ra.GetString("tag")
|
|
|
|
if len(tag) == 0 {
|
2016-04-27 11:59:43 +02:00
|
|
|
tagList, err := rc.ListTag()
|
2016-04-15 07:17:32 +02:00
|
|
|
if err != nil {
|
2016-05-24 08:59:36 +02:00
|
|
|
if regErr, ok := err.(*registry_error.Error); ok {
|
|
|
|
ra.CustomAbort(regErr.StatusCode, regErr.Detail)
|
2016-04-15 07:17:32 +02:00
|
|
|
}
|
2016-05-24 08:59:36 +02:00
|
|
|
|
|
|
|
log.Errorf("error occurred while listing tags of %s: %v", repoName, err)
|
|
|
|
ra.CustomAbort(http.StatusInternalServerError, "internal error")
|
2016-04-15 07:17:32 +02:00
|
|
|
}
|
|
|
|
tags = append(tags, tagList...)
|
|
|
|
} else {
|
|
|
|
tags = append(tags, tag)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, t := range tags {
|
2016-04-27 11:59:43 +02:00
|
|
|
if err := rc.DeleteTag(t); err != nil {
|
2016-05-24 08:59:36 +02:00
|
|
|
if regErr, ok := err.(*registry_error.Error); ok {
|
|
|
|
ra.CustomAbort(regErr.StatusCode, regErr.Detail)
|
2016-04-15 07:17:32 +02:00
|
|
|
}
|
2016-05-24 08:59:36 +02:00
|
|
|
|
|
|
|
log.Errorf("error occurred while deleting tags of %s: %v", repoName, err)
|
|
|
|
ra.CustomAbort(http.StatusInternalServerError, "internal error")
|
2016-04-15 07:17:32 +02:00
|
|
|
}
|
|
|
|
log.Infof("delete tag: %s %s", repoName, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
log.Debug("refreshing catalog cache")
|
2016-05-25 09:24:44 +02:00
|
|
|
if err := cache.RefreshCatalogCache(); err != nil {
|
2016-04-15 07:17:32 +02:00
|
|
|
log.Errorf("error occurred while refresh catalog cache: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-02-26 11:35:55 +01:00
|
|
|
type tag struct {
|
2016-02-26 07:24:44 +01:00
|
|
|
Name string `json:"name"`
|
2016-02-01 12:59:10 +01:00
|
|
|
Tags []string `json:"tags"`
|
|
|
|
}
|
|
|
|
|
2016-02-26 11:35:55 +01:00
|
|
|
// GetTags handles GET /api/repositories/tags
|
2016-02-01 12:59:10 +01:00
|
|
|
func (ra *RepositoryAPI) GetTags() {
|
2016-04-27 11:59:43 +02:00
|
|
|
repoName := ra.GetString("repo_name")
|
|
|
|
if len(repoName) == 0 {
|
|
|
|
ra.CustomAbort(http.StatusBadRequest, "repo_name is nil")
|
|
|
|
}
|
2016-02-01 12:59:10 +01:00
|
|
|
|
2016-05-26 07:26:10 +02:00
|
|
|
rc, err := ra.initRepositoryClient(repoName)
|
2016-04-27 11:59:43 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("error occurred while initializing repository client for %s: %v", repoName, err)
|
|
|
|
ra.CustomAbort(http.StatusInternalServerError, "internal error")
|
|
|
|
}
|
2016-02-01 12:59:10 +01:00
|
|
|
|
2016-04-27 11:59:43 +02:00
|
|
|
tags := []string{}
|
2016-04-17 16:39:10 +02:00
|
|
|
|
2016-04-27 11:59:43 +02:00
|
|
|
ts, err := rc.ListTag()
|
2016-02-01 12:59:10 +01:00
|
|
|
if err != nil {
|
2016-05-24 08:59:36 +02:00
|
|
|
if regErr, ok := err.(*registry_error.Error); ok {
|
|
|
|
ra.CustomAbort(regErr.StatusCode, regErr.Detail)
|
2016-04-17 16:39:10 +02:00
|
|
|
}
|
2016-05-24 08:59:36 +02:00
|
|
|
|
|
|
|
log.Errorf("error occurred while listing tags of %s: %v", repoName, err)
|
|
|
|
ra.CustomAbort(http.StatusInternalServerError, "internal error")
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
2016-04-17 16:39:10 +02:00
|
|
|
|
2016-04-27 11:59:43 +02:00
|
|
|
tags = append(tags, ts...)
|
|
|
|
|
2016-05-20 08:02:44 +02:00
|
|
|
sort.Strings(tags)
|
|
|
|
|
2016-02-01 12:59:10 +01:00
|
|
|
ra.Data["json"] = tags
|
|
|
|
ra.ServeJSON()
|
|
|
|
}
|
|
|
|
|
2016-02-26 11:35:55 +01:00
|
|
|
// GetManifests handles GET /api/repositories/manifests
|
2016-02-01 12:59:10 +01:00
|
|
|
func (ra *RepositoryAPI) GetManifests() {
|
|
|
|
repoName := ra.GetString("repo_name")
|
|
|
|
tag := ra.GetString("tag")
|
|
|
|
|
2016-04-27 11:59:43 +02:00
|
|
|
if len(repoName) == 0 || len(tag) == 0 {
|
|
|
|
ra.CustomAbort(http.StatusBadRequest, "repo_name or tag is nil")
|
|
|
|
}
|
|
|
|
|
2016-05-26 07:26:10 +02:00
|
|
|
rc, err := ra.initRepositoryClient(repoName)
|
2016-04-27 11:59:43 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("error occurred while initializing repository client for %s: %v", repoName, err)
|
|
|
|
ra.CustomAbort(http.StatusInternalServerError, "internal error")
|
|
|
|
}
|
|
|
|
|
2016-02-01 12:59:10 +01:00
|
|
|
item := models.RepoItem{}
|
|
|
|
|
2016-04-25 07:31:58 +02:00
|
|
|
mediaTypes := []string{schema1.MediaTypeManifest}
|
2016-04-27 11:59:43 +02:00
|
|
|
_, _, payload, err := rc.PullManifest(tag, mediaTypes)
|
2016-02-01 12:59:10 +01:00
|
|
|
if err != nil {
|
2016-05-24 08:59:36 +02:00
|
|
|
if regErr, ok := err.(*registry_error.Error); ok {
|
|
|
|
ra.CustomAbort(regErr.StatusCode, regErr.Detail)
|
2016-04-17 16:39:10 +02:00
|
|
|
}
|
2016-05-24 08:59:36 +02:00
|
|
|
|
|
|
|
log.Errorf("error occurred while getting manifest of %s:%s: %v", repoName, tag, err)
|
|
|
|
ra.CustomAbort(http.StatusInternalServerError, "internal error")
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
2016-04-21 18:28:59 +02:00
|
|
|
mani := models.Manifest{}
|
2016-04-17 16:39:10 +02:00
|
|
|
err = json.Unmarshal(payload, &mani)
|
2016-02-25 06:40:08 +01:00
|
|
|
if err != nil {
|
2016-03-28 02:50:09 +02:00
|
|
|
log.Errorf("Failed to decode json from response for manifests, repo name: %s, tag: %s, error: %v", repoName, tag, err)
|
2016-02-25 06:40:08 +01:00
|
|
|
ra.RenderError(http.StatusInternalServerError, "Internal Server Error")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
v1Compatibility := mani.History[0].V1Compatibility
|
|
|
|
|
|
|
|
err = json.Unmarshal([]byte(v1Compatibility), &item)
|
|
|
|
if err != nil {
|
2016-03-28 02:50:09 +02:00
|
|
|
log.Errorf("Failed to decode V1 field for repo, repo name: %s, tag: %s, error: %v", repoName, tag, err)
|
2016-02-25 06:40:08 +01:00
|
|
|
ra.RenderError(http.StatusInternalServerError, "Internal Server Error")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
item.DurationDays = strconv.Itoa(int(time.Since(item.Created).Hours()/24)) + " days"
|
2016-02-01 12:59:10 +01:00
|
|
|
|
|
|
|
ra.Data["json"] = item
|
|
|
|
ra.ServeJSON()
|
|
|
|
}
|
2016-04-27 11:59:43 +02:00
|
|
|
|
2016-05-26 07:26:10 +02:00
|
|
|
func (ra *RepositoryAPI) initRepositoryClient(repoName string) (r *registry.Repository, err error) {
|
|
|
|
username, err := ra.getUsername()
|
2016-05-16 08:39:57 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-04-27 11:59:43 +02:00
|
|
|
}
|
|
|
|
|
2016-05-16 08:39:57 +02:00
|
|
|
endpoint := os.Getenv("REGISTRY_URL")
|
|
|
|
|
2016-05-26 07:26:10 +02:00
|
|
|
return registry.NewRepositoryWithUsername(repoName, endpoint, username)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ra *RepositoryAPI) getUsername() (string, error) {
|
2016-05-19 12:36:40 +02:00
|
|
|
// get username from basic auth
|
|
|
|
username, _, ok := ra.Ctx.Request.BasicAuth()
|
|
|
|
if ok {
|
2016-05-26 07:26:10 +02:00
|
|
|
return username, nil
|
2016-04-27 11:59:43 +02:00
|
|
|
}
|
2016-05-19 12:36:40 +02:00
|
|
|
|
|
|
|
// get username from session
|
2016-05-26 07:26:10 +02:00
|
|
|
sessionUsername := ra.GetSession("username")
|
2016-05-19 12:36:40 +02:00
|
|
|
if sessionUsername != nil {
|
|
|
|
username, ok = sessionUsername.(string)
|
|
|
|
if ok {
|
2016-05-26 07:26:10 +02:00
|
|
|
return username, nil
|
2016-05-19 12:36:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if username does not exist in session, try to get userId from sessiion
|
|
|
|
// and then get username from DB according to the userId
|
2016-05-26 07:26:10 +02:00
|
|
|
sessionUserID := ra.GetSession("userId")
|
2016-05-19 12:36:40 +02:00
|
|
|
if sessionUserID != nil {
|
|
|
|
userID, ok := sessionUserID.(int)
|
|
|
|
if ok {
|
|
|
|
u := models.User{
|
|
|
|
UserID: userID,
|
|
|
|
}
|
|
|
|
user, err := dao.GetUser(u)
|
|
|
|
if err != nil {
|
2016-05-26 07:26:10 +02:00
|
|
|
return "", err
|
2016-05-19 12:36:40 +02:00
|
|
|
}
|
2016-05-26 07:26:10 +02:00
|
|
|
|
|
|
|
return user.Username, nil
|
2016-05-19 12:36:40 +02:00
|
|
|
}
|
2016-04-27 11:59:43 +02:00
|
|
|
}
|
|
|
|
|
2016-05-26 07:26:10 +02:00
|
|
|
return "", nil
|
2016-04-27 11:59:43 +02:00
|
|
|
}
|