From 2c2a4354aef436f379359558a384f5859c9c97e2 Mon Sep 17 00:00:00 2001 From: Jordan Brockopp Date: Wed, 5 Feb 2020 10:02:16 -0600 Subject: [PATCH] feat: add logic to create repo hash (#85) --- api/{authentication.go => authenticate.go} | 23 -------------- api/login.go | 34 ++++++++++++++++++++ api/repo.go | 37 ++++++++++++++++++++++ 3 files changed, 71 insertions(+), 23 deletions(-) rename api/{authentication.go => authenticate.go} (90%) create mode 100644 api/login.go diff --git a/api/authentication.go b/api/authenticate.go similarity index 90% rename from api/authentication.go rename to api/authenticate.go index 364868c08..5b4e13d07 100644 --- a/api/authentication.go +++ b/api/authenticate.go @@ -8,7 +8,6 @@ import ( "encoding/base64" "fmt" "net/http" - "strings" "github.com/go-vela/server/database" "github.com/go-vela/server/router/middleware/token" @@ -21,28 +20,6 @@ import ( "github.com/google/uuid" ) -// Login represents the API handler to -// process a user logging in to Vela. -func Login(c *gin.Context) { - // check if request was a POST - if strings.EqualFold(c.Request.Method, "POST") { - // assume all POST requests are coming from the CLI - AuthenticateCLI(c) - - return - } - - // capture an error if present - err := c.Request.FormValue("error") - if len(err) > 0 { - // redirect to initial login screen with error code - c.Redirect(http.StatusTemporaryRedirect, "/login/error?code="+err) - } - - // redirect to our authentication handler - c.Redirect(http.StatusTemporaryRedirect, "/authenticate") -} - // Authenticate represents the API handler to // process a user logging in to Vela from // the API or UI. diff --git a/api/login.go b/api/login.go new file mode 100644 index 000000000..169bea970 --- /dev/null +++ b/api/login.go @@ -0,0 +1,34 @@ +// Copyright (c) 2020 Target Brands, Inc. All rights reserved. +// +// Use of this source code is governed by the LICENSE file in this repository. + +package api + +import ( + "net/http" + "strings" + + "github.com/gin-gonic/gin" +) + +// Login represents the API handler to +// process a user logging in to Vela. +func Login(c *gin.Context) { + // check if request was a POST + if strings.EqualFold(c.Request.Method, "POST") { + // assume all POST requests are coming from the CLI + AuthenticateCLI(c) + + return + } + + // capture an error if present + err := c.Request.FormValue("error") + if len(err) > 0 { + // redirect to initial login screen with error code + c.Redirect(http.StatusTemporaryRedirect, "/login/error?code="+err) + } + + // redirect to our authentication handler + c.Redirect(http.StatusTemporaryRedirect, "/authenticate") +} diff --git a/api/repo.go b/api/repo.go index d8233cdec..eecd2b587 100644 --- a/api/repo.go +++ b/api/repo.go @@ -5,6 +5,7 @@ package api import ( + "encoding/base64" "fmt" "net/http" "strconv" @@ -20,6 +21,7 @@ import ( "github.com/go-vela/types/library" "github.com/gin-gonic/gin" + "github.com/google/uuid" "github.com/sirupsen/logrus" ) @@ -72,6 +74,22 @@ func CreateRepo(c *gin.Context) { input.SetAllowPush(true) } + // create unique id for the repo + uid, err := uuid.NewRandom() + if err != nil { + retErr := fmt.Errorf("unable to create UID for repo %s: %w", input.GetFullName(), err) + + util.HandleError(c, http.StatusServiceUnavailable, retErr) + + return + } + + input.SetHash( + base64.StdEncoding.EncodeToString( + []byte(uid.String()), + ), + ) + // ensure repo is allowed to be activated if !checkWhitelist(input, whitelist) { retErr := fmt.Errorf("unable to activate repo: %s is not on whitelist", input.GetFullName()) @@ -309,6 +327,25 @@ func UpdateRepo(c *gin.Context) { r.SetAllowPush(true) } + // set hash for repo if no hash is already set + if len(r.GetHash()) == 0 { + // create unique id for the repo + uid, err := uuid.NewRandom() + if err != nil { + retErr := fmt.Errorf("unable to create UID for repo %s: %w", input.GetFullName(), err) + + util.HandleError(c, http.StatusServiceUnavailable, retErr) + + return + } + + r.SetHash( + base64.StdEncoding.EncodeToString( + []byte(uid.String()), + ), + ) + } + // send API call to update the repo err = database.FromContext(c).UpdateRepo(r) if err != nil {