From 7449b3604c22ce2c9b354422b3d14e97cd5945d6 Mon Sep 17 00:00:00 2001 From: Wenkai Yin Date: Mon, 23 May 2016 18:41:48 +0800 Subject: [PATCH] move target.go to api/ --- api/{jobs => }/target.go | 47 +++++++++++++++++++++++++++++++++++----- dao/replication_job.go | 6 ++--- jobservice/router.go | 2 -- ui/router.go | 2 ++ 4 files changed, 46 insertions(+), 11 deletions(-) rename api/{jobs => }/target.go (80%) diff --git a/api/jobs/target.go b/api/target.go similarity index 80% rename from api/jobs/target.go rename to api/target.go index 244806168..8f6f19cf2 100644 --- a/api/jobs/target.go +++ b/api/target.go @@ -16,12 +16,12 @@ package api import ( + "encoding/base64" "fmt" "net/http" "net/url" "strconv" - "github.com/vmware/harbor/api" "github.com/vmware/harbor/dao" "github.com/vmware/harbor/models" "github.com/vmware/harbor/utils/log" @@ -31,7 +31,7 @@ import ( // TargetAPI handles request to /api/targets/ping /api/targets/{} type TargetAPI struct { - api.BaseAPI + BaseAPI } // Prepare validates the user @@ -74,7 +74,14 @@ func (t *TargetAPI) Ping() { } username = t.GetString("username") - password = t.GetString("password") + pwd := t.GetString("password") + b, err := base64.StdEncoding.DecodeString(pwd) + if err != nil { + log.Errorf("failed to decode password: %v", err) + t.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError)) + } + + password = string(b) } credential := auth.NewBasicAuthCredential(username, password) @@ -129,6 +136,18 @@ func (t *TargetAPI) Get() { log.Errorf("failed to get all targets: %v", err) t.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError)) } + + for _, target := range targets { + if len(target.Password) != 0 { + b, err := base64.StdEncoding.DecodeString(target.Password) + if err != nil { + log.Errorf("failed to decode password: %v", err) + t.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError)) + } + target.Password = string(b) + } + } + t.Data["json"] = targets t.ServeJSON() return @@ -144,6 +163,15 @@ func (t *TargetAPI) Get() { t.CustomAbort(http.StatusNotFound, http.StatusText(http.StatusNotFound)) } + if len(target.Password) != 0 { + b, err := base64.StdEncoding.DecodeString(target.Password) + if err != nil { + log.Errorf("failed to decode password: %v", err) + t.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError)) + } + target.Password = string(b) + } + t.Data["json"] = target t.ServeJSON() } @@ -157,6 +185,10 @@ func (t *TargetAPI) Post() { t.CustomAbort(http.StatusBadRequest, "name or URL is nil") } + if len(target.Password) != 0 { + target.Password = base64.StdEncoding.EncodeToString([]byte(target.Password)) + } + id, err := dao.AddRepTarget(*target) if err != nil { log.Errorf("failed to add target: %v", err) @@ -180,6 +212,10 @@ func (t *TargetAPI) Put() { t.CustomAbort(http.StatusBadRequest, "IDs mismatch") } + if len(target.Password) != 0 { + target.Password = base64.StdEncoding.EncodeToString([]byte(target.Password)) + } + if err := dao.UpdateRepTarget(*target); err != nil { log.Errorf("failed to update target %d: %v", id, err) t.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError)) @@ -187,15 +223,14 @@ func (t *TargetAPI) Put() { } func (t *TargetAPI) getIDFromURL() int64 { - idStr := t.Ctx.Input.Param("id") + idStr := t.Ctx.Input.Param(":id") if len(idStr) == 0 { return 0 } id, err := strconv.ParseInt(idStr, 10, 64) if err != nil { - log.Errorf("failed to get ID from URL: %v", err) - t.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError)) + t.CustomAbort(http.StatusBadRequest, "invalid ID in request URL") } return id diff --git a/dao/replication_job.go b/dao/replication_job.go index b7d29670d..90d154a44 100644 --- a/dao/replication_job.go +++ b/dao/replication_job.go @@ -32,11 +32,11 @@ func UpdateRepTarget(target models.RepTarget) error { return err } -func GetAllRepTargets() ([]models.RepTarget, error) { +func GetAllRepTargets() ([]*models.RepTarget, error) { o := orm.NewOrm() qs := o.QueryTable(&models.RepTarget{}) - var targets []models.RepTarget - _, err := qs.All(targets) + var targets []*models.RepTarget + _, err := qs.All(&targets) return targets, err } diff --git a/jobservice/router.go b/jobservice/router.go index 4f5c880d2..c173020b4 100644 --- a/jobservice/router.go +++ b/jobservice/router.go @@ -9,6 +9,4 @@ import ( func initRouters() { beego.Router("/api/jobs/replication", &api.ReplicationJob{}) beego.Router("/api/jobs/replication/actions", &api.ReplicationJob{}, "post:HandleAction") - beego.Router("/api/jobs/targets/?:id", &api.TargetAPI{}) - beego.Router("/api/jobs/targets/ping", &api.TargetAPI{}, "get:Ping") } diff --git a/ui/router.go b/ui/router.go index 401745410..d5a07c434 100644 --- a/ui/router.go +++ b/ui/router.go @@ -61,6 +61,8 @@ func initRouters() { beego.Router("/api/repositories", &api.RepositoryAPI{}) beego.Router("/api/repositories/tags", &api.RepositoryAPI{}, "get:GetTags") beego.Router("/api/repositories/manifests", &api.RepositoryAPI{}, "get:GetManifests") + beego.Router("/api/targets/?:id", &api.TargetAPI{}) + beego.Router("/api/targets/ping", &api.TargetAPI{}, "get:Ping") //external service that hosted on harbor process: beego.Router("/service/notifications", &service.NotificationHandler{})