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 (
|
2016-07-31 13:58:33 +02:00
|
|
|
"fmt"
|
2016-08-02 04:53:53 +02:00
|
|
|
"io/ioutil"
|
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"
|
|
|
|
|
2016-04-25 07:31:58 +02:00
|
|
|
"github.com/docker/distribution/manifest/schema1"
|
2016-08-02 04:53:53 +02:00
|
|
|
"github.com/docker/distribution/manifest/schema2"
|
2016-02-01 12:59:10 +01:00
|
|
|
"github.com/vmware/harbor/dao"
|
|
|
|
"github.com/vmware/harbor/models"
|
2016-06-14 12:17:32 +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-06-14 12:17:32 +02:00
|
|
|
|
|
|
|
registry_error "github.com/vmware/harbor/utils/registry/error"
|
|
|
|
|
2016-05-27 06:18:12 +02:00
|
|
|
"github.com/vmware/harbor/utils/registry/auth"
|
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.
|
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")
|
2016-07-31 13:58:33 +02:00
|
|
|
if err != nil || projectID <= 0 {
|
|
|
|
ra.CustomAbort(http.StatusBadRequest, "invalid project_id")
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
2016-07-31 13:58:33 +02:00
|
|
|
|
|
|
|
page, pageSize := ra.getPaginationParams()
|
|
|
|
|
|
|
|
project, err := dao.GetProjectByID(projectID)
|
2016-02-01 12:59:10 +01:00
|
|
|
if err != nil {
|
2016-07-31 13:58:33 +02:00
|
|
|
log.Errorf("failed to get project %d: %v", projectID, err)
|
|
|
|
ra.CustomAbort(http.StatusInternalServerError, "")
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
2016-07-31 13:58:33 +02:00
|
|
|
|
|
|
|
if project == nil {
|
|
|
|
ra.CustomAbort(http.StatusNotFound, fmt.Sprintf("project %d not found", projectID))
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
2016-05-19 12:36:40 +02:00
|
|
|
|
2016-07-31 13:58:33 +02:00
|
|
|
if project.Public == 0 {
|
2016-06-14 12:17:32 +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) {
|
2016-07-31 13:58:33 +02:00
|
|
|
ra.CustomAbort(http.StatusForbidden, "")
|
2016-05-19 12:36:40 +02:00
|
|
|
}
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
2016-04-15 07:17:32 +02:00
|
|
|
|
2016-08-23 09:56:30 +02:00
|
|
|
repositories, err := getReposByProject(project.Name, ra.GetString("q"))
|
2016-02-01 12:59:10 +01:00
|
|
|
if err != nil {
|
2016-08-23 09:56:30 +02:00
|
|
|
log.Errorf("failed to get repository: %v", err)
|
2016-07-31 13:58:33 +02:00
|
|
|
ra.CustomAbort(http.StatusInternalServerError, "")
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
2016-04-15 07:17:32 +02:00
|
|
|
|
2016-07-31 13:58:33 +02:00
|
|
|
total := int64(len(repositories))
|
|
|
|
|
|
|
|
if (page-1)*pageSize > total {
|
|
|
|
repositories = []string{}
|
2016-02-01 12:59:10 +01:00
|
|
|
} else {
|
2016-07-31 13:58:33 +02:00
|
|
|
repositories = repositories[(page-1)*pageSize:]
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
2016-07-31 13:58:33 +02:00
|
|
|
|
|
|
|
if page*pageSize <= total {
|
|
|
|
repositories = repositories[:pageSize]
|
|
|
|
}
|
|
|
|
|
|
|
|
ra.setPaginationHeader(total, page, pageSize)
|
|
|
|
|
|
|
|
ra.Data["json"] = repositories
|
2016-02-01 12:59:10 +01:00
|
|
|
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-07-05 06:05:01 +02:00
|
|
|
projectName := getProjectName(repoName)
|
|
|
|
project, err := dao.GetProjectByName(projectName)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed to get project %s: %v", projectName, err)
|
|
|
|
ra.CustomAbort(http.StatusInternalServerError, "")
|
|
|
|
}
|
|
|
|
|
2016-08-11 08:11:45 +02:00
|
|
|
if project == nil {
|
|
|
|
ra.CustomAbort(http.StatusNotFound, fmt.Sprintf("project %s not found", projectName))
|
|
|
|
}
|
|
|
|
|
2016-07-05 06:05:01 +02:00
|
|
|
if project.Public == 0 {
|
|
|
|
userID := ra.ValidateUser()
|
|
|
|
if !hasProjectAdminRole(userID, project.ProjectID) {
|
|
|
|
ra.CustomAbort(http.StatusForbidden, "")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-06-14 12:17:32 +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-06-14 12:17:32 +02:00
|
|
|
|
|
|
|
log.Errorf("error occurred while listing tags of %s: %v", repoName, err)
|
|
|
|
ra.CustomAbort(http.StatusInternalServerError, "internal error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO remove the logic if the bug of registry is fixed
|
|
|
|
if len(tagList) == 0 {
|
|
|
|
ra.CustomAbort(http.StatusNotFound, http.StatusText(http.StatusNotFound))
|
2016-04-15 07:17:32 +02:00
|
|
|
}
|
2016-06-14 12:17:32 +02:00
|
|
|
|
2016-04-15 07:17:32 +02:00
|
|
|
tags = append(tags, tagList...)
|
|
|
|
} else {
|
|
|
|
tags = append(tags, tag)
|
|
|
|
}
|
|
|
|
|
2016-06-29 11:52:35 +02:00
|
|
|
user, _, ok := ra.Ctx.Request.BasicAuth()
|
|
|
|
if !ok {
|
|
|
|
user, err = ra.getUsername()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed to get user: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-15 07:17:32 +02:00
|
|
|
for _, t := range tags {
|
2016-04-27 11:59:43 +02:00
|
|
|
if err := rc.DeleteTag(t); err != nil {
|
2016-06-14 12:17:32 +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-06-14 12:17:32 +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)
|
2016-06-29 11:10:17 +02:00
|
|
|
go TriggerReplicationByRepository(repoName, []string{t}, models.RepOpDelete)
|
2016-06-29 11:52:35 +02:00
|
|
|
|
2016-06-29 12:14:50 +02:00
|
|
|
go func(tag string) {
|
2016-07-05 06:05:01 +02:00
|
|
|
if err := dao.AccessLog(user, projectName, repoName, tag, "delete"); err != nil {
|
2016-06-29 11:52:35 +02:00
|
|
|
log.Errorf("failed to add access log: %v", err)
|
|
|
|
}
|
2016-06-29 12:14:50 +02:00
|
|
|
}(t)
|
2016-04-15 07:17:32 +02:00
|
|
|
}
|
2016-06-29 11:10:17 +02:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
log.Debug("refreshing catalog cache")
|
|
|
|
if err := cache.RefreshCatalogCache(); err != nil {
|
|
|
|
log.Errorf("error occurred while refresh catalog cache: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
2016-04-15 07:17:32 +02:00
|
|
|
}
|
|
|
|
|
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-07-05 06:05:01 +02:00
|
|
|
projectName := getProjectName(repoName)
|
|
|
|
project, err := dao.GetProjectByName(projectName)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed to get project %s: %v", projectName, err)
|
|
|
|
ra.CustomAbort(http.StatusInternalServerError, "")
|
|
|
|
}
|
|
|
|
|
2016-07-31 14:21:19 +02:00
|
|
|
if project == nil {
|
|
|
|
ra.CustomAbort(http.StatusNotFound, fmt.Sprintf("project %s not found", projectName))
|
|
|
|
}
|
|
|
|
|
2016-07-05 06:05:01 +02:00
|
|
|
if project.Public == 0 {
|
|
|
|
userID := ra.ValidateUser()
|
|
|
|
if !checkProjectPermission(userID, project.ProjectID) {
|
|
|
|
ra.CustomAbort(http.StatusForbidden, "")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-07-18 14:37:52 +02:00
|
|
|
regErr, ok := err.(*registry_error.Error)
|
|
|
|
if !ok {
|
|
|
|
log.Errorf("error occurred while listing tags of %s: %v", repoName, err)
|
|
|
|
ra.CustomAbort(http.StatusInternalServerError, "internal error")
|
|
|
|
}
|
|
|
|
// TODO remove the logic if the bug of registry is fixed
|
|
|
|
// It's a workaround for a bug of registry: when listing tags of
|
|
|
|
// a repository which is being pushed, a "NAME_UNKNOWN" error will
|
|
|
|
// been returned, while the catalog API can list this repository.
|
|
|
|
if regErr.StatusCode != http.StatusNotFound {
|
2016-06-14 12:17:32 +02:00
|
|
|
ra.CustomAbort(regErr.StatusCode, regErr.Detail)
|
2016-04-17 16:39:10 +02:00
|
|
|
}
|
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-08-02 04:53:53 +02:00
|
|
|
version := ra.GetString("version")
|
|
|
|
if len(version) == 0 {
|
|
|
|
version = "v2"
|
|
|
|
}
|
|
|
|
|
|
|
|
if version != "v1" && version != "v2" {
|
|
|
|
ra.CustomAbort(http.StatusBadRequest, "version should be v1 or v2")
|
|
|
|
}
|
|
|
|
|
2016-07-05 06:05:01 +02:00
|
|
|
projectName := getProjectName(repoName)
|
|
|
|
project, err := dao.GetProjectByName(projectName)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed to get project %s: %v", projectName, err)
|
|
|
|
ra.CustomAbort(http.StatusInternalServerError, "")
|
|
|
|
}
|
|
|
|
|
2016-08-11 08:11:45 +02:00
|
|
|
if project == nil {
|
|
|
|
ra.CustomAbort(http.StatusNotFound, fmt.Sprintf("project %s not found", projectName))
|
|
|
|
}
|
|
|
|
|
2016-07-05 06:05:01 +02:00
|
|
|
if project.Public == 0 {
|
|
|
|
userID := ra.ValidateUser()
|
|
|
|
if !checkProjectPermission(userID, project.ProjectID) {
|
|
|
|
ra.CustomAbort(http.StatusForbidden, "")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-08-02 04:53:53 +02:00
|
|
|
result := struct {
|
|
|
|
Manifest interface{} `json:"manifest"`
|
|
|
|
Config interface{} `json:"config,omitempty" `
|
|
|
|
}{}
|
|
|
|
|
|
|
|
mediaTypes := []string{}
|
|
|
|
switch version {
|
|
|
|
case "v1":
|
|
|
|
mediaTypes = append(mediaTypes, schema1.MediaTypeManifest)
|
|
|
|
case "v2":
|
|
|
|
mediaTypes = append(mediaTypes, schema2.MediaTypeManifest)
|
|
|
|
}
|
2016-02-01 12:59:10 +01:00
|
|
|
|
2016-08-02 04:53:53 +02:00
|
|
|
_, mediaType, payload, err := rc.PullManifest(tag, mediaTypes)
|
2016-02-01 12:59:10 +01:00
|
|
|
if err != nil {
|
2016-06-14 12:17:32 +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-06-14 12:17:32 +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-08-02 04:53:53 +02:00
|
|
|
|
|
|
|
manifest, _, err := registry.UnMarshal(mediaType, payload)
|
2016-02-25 06:40:08 +01:00
|
|
|
if err != nil {
|
2016-08-02 04:53:53 +02:00
|
|
|
log.Errorf("an error occurred while parsing manifest of %s:%s: %v", repoName, tag, err)
|
|
|
|
ra.CustomAbort(http.StatusInternalServerError, "")
|
2016-02-25 06:40:08 +01:00
|
|
|
}
|
|
|
|
|
2016-08-02 04:53:53 +02:00
|
|
|
result.Manifest = manifest
|
|
|
|
|
|
|
|
deserializedmanifest, ok := manifest.(*schema2.DeserializedManifest)
|
|
|
|
if ok {
|
|
|
|
_, data, err := rc.PullBlob(deserializedmanifest.Target().Digest.String())
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed to get config of manifest %s:%s: %v", repoName, tag, err)
|
|
|
|
ra.CustomAbort(http.StatusInternalServerError, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := ioutil.ReadAll(data)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed to read config of manifest %s:%s: %v", repoName, tag, err)
|
|
|
|
ra.CustomAbort(http.StatusInternalServerError, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
result.Config = string(b)
|
2016-02-25 06:40:08 +01:00
|
|
|
}
|
2016-02-01 12:59:10 +01:00
|
|
|
|
2016-08-02 04:53:53 +02:00
|
|
|
ra.Data["json"] = result
|
2016-02-01 12:59:10 +01:00
|
|
|
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) {
|
2016-05-27 06:18:12 +02:00
|
|
|
endpoint := os.Getenv("REGISTRY_URL")
|
|
|
|
|
|
|
|
username, password, ok := ra.Ctx.Request.BasicAuth()
|
|
|
|
if ok {
|
2016-06-29 12:52:24 +02:00
|
|
|
return newRepositoryClient(endpoint, getIsInsecure(), username, password,
|
2016-06-21 10:39:03 +02:00
|
|
|
repoName, "repository", repoName, "pull", "push", "*")
|
2016-05-27 06:18:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
username, err = ra.getUsername()
|
2016-05-26 07:26:10 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-06-29 12:52:24 +02:00
|
|
|
return cache.NewRepositoryClient(endpoint, getIsInsecure(), username, repoName,
|
2016-06-21 10:39:03 +02:00
|
|
|
"repository", repoName, "pull", "push", "*")
|
2016-05-26 07:26:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ra *RepositoryAPI) getUsername() (string, error) {
|
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 {
|
2016-05-27 06:18:12 +02:00
|
|
|
username, ok := sessionUsername.(string)
|
2016-05-19 12:36:40 +02:00
|
|
|
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
|
|
|
}
|
2016-06-14 12:17:32 +02:00
|
|
|
|
|
|
|
//GetTopRepos handles request GET /api/repositories/top
|
|
|
|
func (ra *RepositoryAPI) GetTopRepos() {
|
|
|
|
var err error
|
|
|
|
var countNum int
|
|
|
|
count := ra.GetString("count")
|
|
|
|
if len(count) == 0 {
|
|
|
|
countNum = 10
|
|
|
|
} else {
|
|
|
|
countNum, err = strconv.Atoi(count)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Get parameters error--count, err: %v", err)
|
|
|
|
ra.CustomAbort(http.StatusBadRequest, "bad request of count")
|
|
|
|
}
|
|
|
|
if countNum <= 0 {
|
|
|
|
log.Warning("count must be a positive integer")
|
|
|
|
ra.CustomAbort(http.StatusBadRequest, "count is 0 or negative")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
repos, err := dao.GetTopRepos(countNum)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("error occured in get top 10 repos: %v", err)
|
|
|
|
ra.CustomAbort(http.StatusInternalServerError, "internal server error")
|
|
|
|
}
|
|
|
|
ra.Data["json"] = repos
|
|
|
|
ra.ServeJSON()
|
|
|
|
}
|
2016-06-21 10:39:03 +02:00
|
|
|
|
|
|
|
func newRepositoryClient(endpoint string, insecure bool, username, password, repository, scopeType, scopeName string,
|
|
|
|
scopeActions ...string) (*registry.Repository, error) {
|
|
|
|
|
|
|
|
credential := auth.NewBasicAuthCredential(username, password)
|
|
|
|
authorizer := auth.NewStandardTokenAuthorizer(credential, insecure, scopeType, scopeName, scopeActions...)
|
|
|
|
|
2016-06-22 06:03:50 +02:00
|
|
|
store, err := auth.NewAuthorizerStore(endpoint, insecure, authorizer)
|
2016-06-21 10:39:03 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := registry.NewRepositoryWithModifiers(repository, endpoint, insecure, store)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return client, nil
|
|
|
|
}
|
2016-07-05 06:05:01 +02:00
|
|
|
|
|
|
|
func getProjectName(repository string) string {
|
|
|
|
project := ""
|
|
|
|
if strings.Contains(repository, "/") {
|
|
|
|
project = repository[0:strings.LastIndex(repository, "/")]
|
|
|
|
}
|
|
|
|
return project
|
|
|
|
}
|