| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164 |
- package middleware
- import (
- "bytes"
- "encoding/json"
- "errors"
- "io/ioutil"
- "net/http"
- "net/url"
- "strconv"
- "strings"
- "github.com/go-chi/chi"
- "github.com/gorilla/sessions"
- "github.com/porter-dev/porter/internal/auth/token"
- "github.com/porter-dev/porter/internal/models"
- "github.com/porter-dev/porter/internal/repository"
- )
- // Auth implements the authorization functions
- type Auth struct {
- store sessions.Store
- cookieName string
- tokenConf *token.TokenGeneratorConf
- repo *repository.Repository
- }
- // NewAuth returns a new Auth instance
- func NewAuth(
- store sessions.Store,
- cookieName string,
- tokenConf *token.TokenGeneratorConf,
- repo *repository.Repository,
- ) *Auth {
- return &Auth{store, cookieName, tokenConf, repo}
- }
- // BasicAuthenticate just checks that a user is logged in
- func (auth *Auth) BasicAuthenticate(next http.Handler) http.Handler {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- if auth.isLoggedIn(w, r) {
- next.ServeHTTP(w, r)
- } else {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- return
- })
- }
- // BasicAuthenticateWithRedirect checks that a user is logged in, and if they're not, the
- // user is redirected to the login page with the redirect path stored in the session
- func (auth *Auth) BasicAuthenticateWithRedirect(next http.Handler) http.Handler {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- if auth.isLoggedIn(w, r) {
- next.ServeHTTP(w, r)
- } else {
- session, err := auth.store.Get(r, auth.cookieName)
- if err != nil {
- http.Redirect(w, r, "/dashboard", 302)
- }
- // need state parameter to validate when redirected
- if r.URL.RawQuery == "" {
- session.Values["redirect"] = r.URL.Path
- } else {
- session.Values["redirect"] = r.URL.Path + "?" + r.URL.RawQuery
- }
- session.Save(r, w)
- http.Redirect(w, r, "/dashboard", 302)
- return
- }
- return
- })
- }
- // IDLocation represents the location of the ID to use for authentication
- type IDLocation uint
- const (
- // URLParam location looks for a parameter in the URL endpoint
- URLParam IDLocation = iota
- // BodyParam location looks for a parameter in the body
- BodyParam
- // QueryParam location looks for a parameter in the query string
- QueryParam
- )
- type bodyUserID struct {
- UserID uint64 `json:"user_id"`
- }
- type bodyProjectID struct {
- ProjectID uint64 `json:"project_id"`
- }
- type bodyClusterID struct {
- ClusterID uint64 `json:"cluster_id"`
- }
- type bodyRegistryID struct {
- RegistryID uint64 `json:"registry_id"`
- }
- type bodyGitRepoID struct {
- GitRepoID uint64 `json:"git_repo_id"`
- }
- type bodyInfraID struct {
- InfraID uint64 `json:"infra_id"`
- }
- type bodyInviteID struct {
- InviteID uint64 `json:"invite_id"`
- }
- type bodyAWSIntegrationID struct {
- AWSIntegrationID uint64 `json:"aws_integration_id"`
- }
- type bodyGCPIntegrationID struct {
- GCPIntegrationID uint64 `json:"gcp_integration_id"`
- }
- type bodyDOIntegrationID struct {
- DOIntegrationID uint64 `json:"do_integration_id"`
- }
- // DoesUserIDMatch checks the id URL parameter and verifies that it matches
- // the one stored in the session
- func (auth *Auth) DoesUserIDMatch(next http.Handler, loc IDLocation) http.Handler {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- var err error
- id, err := findUserIDInRequest(r, loc)
- // first check for token
- tok := auth.getTokenFromRequest(r)
- if err != nil {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- } else if tok != nil && tok.IBy == uint(id) {
- next.ServeHTTP(w, r)
- return
- } else if auth.doesSessionMatchID(r, uint(id)) {
- next.ServeHTTP(w, r)
- } else {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- return
- })
- }
- // AccessType represents the various access types for a project
- type AccessType string
- // The various access types
- const (
- AdminAccess AccessType = "admin"
- ReadAccess AccessType = "read"
- WriteAccess AccessType = "write"
- )
- // DoesUserHaveProjectAccess looks for a project_id parameter and checks that the
- // user has access via the specified accessType
- func (auth *Auth) DoesUserHaveProjectAccess(
- next http.Handler,
- projLoc IDLocation,
- accessType AccessType,
- ) http.Handler {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- var err error
- projID, err := findProjIDInRequest(r, projLoc)
- if err != nil {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- // first check for token
- tok := auth.getTokenFromRequest(r)
- var userID uint
- if tok != nil && tok.ProjectID != 0 && tok.ProjectID == uint(projID) {
- next.ServeHTTP(w, r)
- return
- } else if tok != nil {
- userID = tok.IBy
- } else {
- session, err := auth.store.Get(r, auth.cookieName)
- if err != nil {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- sessionUserID, ok := session.Values["user_id"]
- userID = sessionUserID.(uint)
- if !ok {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- }
- // get the project
- proj, err := auth.repo.Project.ReadProject(uint(projID))
- if err != nil {
- http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
- return
- }
- // look for the user role in the project
- for _, role := range proj.Roles {
- if role.UserID == userID {
- if accessType == AdminAccess {
- if role.Kind == models.RoleAdmin {
- next.ServeHTTP(w, r)
- return
- }
- } else if accessType == WriteAccess {
- if role.Kind == models.RoleAdmin || role.Kind == models.RoleDeveloper {
- next.ServeHTTP(w, r)
- return
- }
- } else if accessType == ReadAccess {
- next.ServeHTTP(w, r)
- return
- }
- }
- }
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- })
- }
- // DoesUserHaveClusterAccess looks for a project_id parameter and a
- // cluster_id parameter, and verifies that the cluster belongs
- // to the project
- func (auth *Auth) DoesUserHaveClusterAccess(
- next http.Handler,
- projLoc IDLocation,
- clusterLoc IDLocation,
- ) http.Handler {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- clusterID, err := findClusterIDInRequest(r, clusterLoc)
- if err != nil {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- projID, err := findProjIDInRequest(r, projLoc)
- if err != nil {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- // get the service accounts belonging to the project
- clusters, err := auth.repo.Cluster.ListClustersByProjectID(uint(projID))
- if err != nil {
- http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
- return
- }
- doesExist := false
- for _, cluster := range clusters {
- if cluster.ID == uint(clusterID) {
- doesExist = true
- break
- }
- }
- if doesExist {
- next.ServeHTTP(w, r)
- return
- }
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- })
- }
- // DoesUserHaveInviteAccess looks for a project_id parameter and a
- // invite_id parameter, and verifies that the invite belongs
- // to the project
- func (auth *Auth) DoesUserHaveInviteAccess(
- next http.Handler,
- projLoc IDLocation,
- inviteLoc IDLocation,
- ) http.Handler {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- inviteID, err := findInviteIDInRequest(r, inviteLoc)
- if err != nil {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- projID, err := findProjIDInRequest(r, projLoc)
- if err != nil {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- // get the service accounts belonging to the project
- invites, err := auth.repo.Invite.ListInvitesByProjectID(uint(projID))
- if err != nil {
- http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
- return
- }
- doesExist := false
- for _, invite := range invites {
- if invite.ID == uint(inviteID) {
- doesExist = true
- break
- }
- }
- if doesExist {
- next.ServeHTTP(w, r)
- return
- }
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- })
- }
- // DoesUserHaveRegistryAccess looks for a project_id parameter and a
- // registry_id parameter, and verifies that the registry belongs
- // to the project
- func (auth *Auth) DoesUserHaveRegistryAccess(
- next http.Handler,
- projLoc IDLocation,
- registryLoc IDLocation,
- ) http.Handler {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- regID, err := findRegistryIDInRequest(r, registryLoc)
- if err != nil {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- projID, err := findProjIDInRequest(r, projLoc)
- if err != nil {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- // get the service accounts belonging to the project
- regs, err := auth.repo.Registry.ListRegistriesByProjectID(uint(projID))
- if err != nil {
- http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
- return
- }
- doesExist := false
- for _, reg := range regs {
- if reg.ID == uint(regID) {
- doesExist = true
- break
- }
- }
- if doesExist {
- next.ServeHTTP(w, r)
- return
- }
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- })
- }
- // DoesUserHaveGitRepoAccess looks for a project_id parameter and a
- // git_repo_id parameter, and verifies that the git repo belongs
- // to the project
- func (auth *Auth) DoesUserHaveGitRepoAccess(
- next http.Handler,
- projLoc IDLocation,
- gitRepoLoc IDLocation,
- ) http.Handler {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- grID, err := findGitRepoIDInRequest(r, gitRepoLoc)
- if err != nil {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- projID, err := findProjIDInRequest(r, projLoc)
- if err != nil {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- // get the service accounts belonging to the project
- grs, err := auth.repo.GitRepo.ListGitReposByProjectID(uint(projID))
- if err != nil {
- http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
- return
- }
- doesExist := false
- for _, gr := range grs {
- if gr.ID == uint(grID) {
- doesExist = true
- break
- }
- }
- if doesExist {
- next.ServeHTTP(w, r)
- return
- }
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- })
- }
- // DoesUserHaveInfraAccess looks for a project_id parameter and an
- // infra_id parameter, and verifies that the infra belongs
- // to the project
- func (auth *Auth) DoesUserHaveInfraAccess(
- next http.Handler,
- projLoc IDLocation,
- infraLoc IDLocation,
- ) http.Handler {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- infraID, err := findInfraIDInRequest(r, infraLoc)
- if err != nil {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- projID, err := findProjIDInRequest(r, projLoc)
- if err != nil {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- infras, err := auth.repo.Infra.ListInfrasByProjectID(uint(projID))
- if err != nil {
- http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
- return
- }
- doesExist := false
- for _, infra := range infras {
- if infra.ID == uint(infraID) {
- doesExist = true
- break
- }
- }
- if doesExist {
- next.ServeHTTP(w, r)
- return
- }
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- })
- }
- // DoesUserHaveAWSIntegrationAccess looks for a project_id parameter and an
- // aws_integration_id parameter, and verifies that the infra belongs
- // to the project
- func (auth *Auth) DoesUserHaveAWSIntegrationAccess(
- next http.Handler,
- projLoc IDLocation,
- awsLoc IDLocation,
- optional bool,
- ) http.Handler {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- awsID, err := findAWSIntegrationIDInRequest(r, awsLoc)
- if awsID == 0 && optional {
- next.ServeHTTP(w, r)
- return
- }
- if awsID == 0 || err != nil {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- projID, err := findProjIDInRequest(r, projLoc)
- if err != nil {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- awsInts, err := auth.repo.AWSIntegration.ListAWSIntegrationsByProjectID(uint(projID))
- if err != nil {
- http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
- return
- }
- doesExist := false
- for _, awsInt := range awsInts {
- if awsInt.ID == uint(awsID) {
- doesExist = true
- break
- }
- }
- if doesExist {
- next.ServeHTTP(w, r)
- return
- }
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- })
- }
- // DoesUserHaveGCPIntegrationAccess looks for a project_id parameter and an
- // gcp_integration_id parameter, and verifies that the infra belongs
- // to the project
- func (auth *Auth) DoesUserHaveGCPIntegrationAccess(
- next http.Handler,
- projLoc IDLocation,
- gcpLoc IDLocation,
- optional bool,
- ) http.Handler {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- gcpID, err := findGCPIntegrationIDInRequest(r, gcpLoc)
- if gcpID == 0 && optional {
- next.ServeHTTP(w, r)
- return
- }
- if gcpID == 0 || err != nil {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- projID, err := findProjIDInRequest(r, projLoc)
- if err != nil {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- gcpInts, err := auth.repo.GCPIntegration.ListGCPIntegrationsByProjectID(uint(projID))
- if err != nil {
- http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
- return
- }
- doesExist := false
- for _, awsInt := range gcpInts {
- if awsInt.ID == uint(gcpID) {
- doesExist = true
- break
- }
- }
- if doesExist {
- next.ServeHTTP(w, r)
- return
- }
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- })
- }
- // DoesUserHaveDOIntegrationAccess looks for a project_id parameter and an
- // do_integration_id parameter, and verifies that the infra belongs
- // to the project
- func (auth *Auth) DoesUserHaveDOIntegrationAccess(
- next http.Handler,
- projLoc IDLocation,
- doLoc IDLocation,
- optional bool,
- ) http.Handler {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- doID, err := findDOIntegrationIDInRequest(r, doLoc)
- if doID == 0 && optional {
- next.ServeHTTP(w, r)
- return
- }
- if doID == 0 || err != nil {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- projID, err := findProjIDInRequest(r, projLoc)
- if err != nil {
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- }
- oauthInts, err := auth.repo.OAuthIntegration.ListOAuthIntegrationsByProjectID(uint(projID))
- if err != nil {
- http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
- return
- }
- doesExist := false
- for _, oauthInt := range oauthInts {
- if oauthInt.ID == uint(doID) {
- doesExist = true
- break
- }
- }
- if doesExist {
- next.ServeHTTP(w, r)
- return
- }
- http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
- return
- })
- }
- // Helpers
- func (auth *Auth) doesSessionMatchID(r *http.Request, id uint) bool {
- session, _ := auth.store.Get(r, auth.cookieName)
- if sessID, ok := session.Values["user_id"].(uint); !ok || sessID != id {
- return false
- }
- return true
- }
- func (auth *Auth) isLoggedIn(w http.ResponseWriter, r *http.Request) bool {
- // first check for Bearer token
- tok := auth.getTokenFromRequest(r)
- if tok != nil {
- return true
- }
- session, err := auth.store.Get(r, auth.cookieName)
- if err != nil {
- session.Values["authenticated"] = false
- if err := session.Save(r, w); err != nil {
- return false
- }
- return false
- }
- if auth, ok := session.Values["authenticated"].(bool); !auth || !ok {
- return false
- }
- return true
- }
- func (auth *Auth) getTokenFromRequest(r *http.Request) *token.Token {
- reqToken := r.Header.Get("Authorization")
- splitToken := strings.Split(reqToken, "Bearer")
- if len(splitToken) != 2 {
- return nil
- }
- reqToken = strings.TrimSpace(splitToken[1])
- tok, err := token.GetTokenFromEncoded(reqToken, auth.tokenConf)
- if err != nil {
- return nil
- }
- return tok
- }
- func findUserIDInRequest(r *http.Request, userLoc IDLocation) (uint64, error) {
- var userID uint64
- var err error
- if userLoc == URLParam {
- userID, err = strconv.ParseUint(chi.URLParam(r, "user_id"), 0, 64)
- if err != nil {
- return 0, err
- }
- } else if userLoc == BodyParam {
- form := &bodyUserID{}
- body, err := ioutil.ReadAll(r.Body)
- if err != nil {
- return 0, err
- }
- err = json.Unmarshal(body, form)
- if err != nil {
- return 0, err
- }
- userID = form.UserID
- // need to create a new stream for the body
- r.Body = ioutil.NopCloser(bytes.NewReader(body))
- } else {
- vals, err := url.ParseQuery(r.URL.RawQuery)
- if err != nil {
- return 0, err
- }
- if userStrArr, ok := vals["user_id"]; ok && len(userStrArr) == 1 {
- userID, err = strconv.ParseUint(userStrArr[0], 10, 64)
- } else {
- return 0, errors.New("user id not found")
- }
- }
- return userID, nil
- }
- func findProjIDInRequest(r *http.Request, projLoc IDLocation) (uint64, error) {
- var projID uint64
- var err error
- if projLoc == URLParam {
- projID, err = strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
- if err != nil {
- return 0, err
- }
- } else if projLoc == BodyParam {
- form := &bodyProjectID{}
- body, err := ioutil.ReadAll(r.Body)
- if err != nil {
- return 0, err
- }
- err = json.Unmarshal(body, form)
- if err != nil {
- return 0, err
- }
- projID = form.ProjectID
- // need to create a new stream for the body
- r.Body = ioutil.NopCloser(bytes.NewReader(body))
- } else {
- vals, err := url.ParseQuery(r.URL.RawQuery)
- if err != nil {
- return 0, err
- }
- if projStrArr, ok := vals["project_id"]; ok && len(projStrArr) == 1 {
- projID, err = strconv.ParseUint(projStrArr[0], 10, 64)
- } else {
- return 0, errors.New("project id not found")
- }
- }
- return projID, nil
- }
- func findClusterIDInRequest(r *http.Request, clusterLoc IDLocation) (uint64, error) {
- var clusterID uint64
- var err error
- if clusterLoc == URLParam {
- clusterID, err = strconv.ParseUint(chi.URLParam(r, "cluster_id"), 0, 64)
- if err != nil {
- return 0, err
- }
- } else if clusterLoc == BodyParam {
- form := &bodyClusterID{}
- body, err := ioutil.ReadAll(r.Body)
- if err != nil {
- return 0, err
- }
- err = json.Unmarshal(body, form)
- if err != nil {
- return 0, err
- }
- clusterID = form.ClusterID
- // need to create a new stream for the body
- r.Body = ioutil.NopCloser(bytes.NewReader(body))
- } else {
- vals, err := url.ParseQuery(r.URL.RawQuery)
- if err != nil {
- return 0, err
- }
- if clStrArr, ok := vals["cluster_id"]; ok && len(clStrArr) == 1 {
- clusterID, err = strconv.ParseUint(clStrArr[0], 10, 64)
- } else {
- return 0, errors.New("cluster id not found")
- }
- }
- return clusterID, nil
- }
- func findInviteIDInRequest(r *http.Request, inviteLoc IDLocation) (uint64, error) {
- var inviteID uint64
- var err error
- if inviteLoc == URLParam {
- inviteID, err = strconv.ParseUint(chi.URLParam(r, "invite_id"), 0, 64)
- if err != nil {
- return 0, err
- }
- } else if inviteLoc == BodyParam {
- form := &bodyInviteID{}
- body, err := ioutil.ReadAll(r.Body)
- if err != nil {
- return 0, err
- }
- err = json.Unmarshal(body, form)
- if err != nil {
- return 0, err
- }
- inviteID = form.InviteID
- // need to create a new stream for the body
- r.Body = ioutil.NopCloser(bytes.NewReader(body))
- } else {
- vals, err := url.ParseQuery(r.URL.RawQuery)
- if err != nil {
- return 0, err
- }
- if invStrArr, ok := vals["invite_id"]; ok && len(invStrArr) == 1 {
- inviteID, err = strconv.ParseUint(invStrArr[0], 10, 64)
- } else {
- return 0, errors.New("invite id not found")
- }
- }
- return inviteID, nil
- }
- func findRegistryIDInRequest(r *http.Request, registryLoc IDLocation) (uint64, error) {
- var regID uint64
- var err error
- if registryLoc == URLParam {
- regID, err = strconv.ParseUint(chi.URLParam(r, "registry_id"), 0, 64)
- if err != nil {
- return 0, err
- }
- } else if registryLoc == BodyParam {
- form := &bodyRegistryID{}
- body, err := ioutil.ReadAll(r.Body)
- if err != nil {
- return 0, err
- }
- err = json.Unmarshal(body, form)
- if err != nil {
- return 0, err
- }
- regID = form.RegistryID
- // need to create a new stream for the body
- r.Body = ioutil.NopCloser(bytes.NewReader(body))
- } else {
- vals, err := url.ParseQuery(r.URL.RawQuery)
- if err != nil {
- return 0, err
- }
- if regStrArr, ok := vals["registry_id"]; ok && len(regStrArr) == 1 {
- regID, err = strconv.ParseUint(regStrArr[0], 10, 64)
- } else {
- return 0, errors.New("registry id not found")
- }
- }
- return regID, nil
- }
- func findGitRepoIDInRequest(r *http.Request, gitRepoLoc IDLocation) (uint64, error) {
- var grID uint64
- var err error
- if gitRepoLoc == URLParam {
- grID, err = strconv.ParseUint(chi.URLParam(r, "git_repo_id"), 0, 64)
- if err != nil {
- return 0, err
- }
- } else if gitRepoLoc == BodyParam {
- form := &bodyGitRepoID{}
- body, err := ioutil.ReadAll(r.Body)
- if err != nil {
- return 0, err
- }
- err = json.Unmarshal(body, form)
- if err != nil {
- return 0, err
- }
- grID = form.GitRepoID
- // need to create a new stream for the body
- r.Body = ioutil.NopCloser(bytes.NewReader(body))
- } else {
- vals, err := url.ParseQuery(r.URL.RawQuery)
- if err != nil {
- return 0, err
- }
- if regStrArr, ok := vals["git_repo_id"]; ok && len(regStrArr) == 1 {
- grID, err = strconv.ParseUint(regStrArr[0], 10, 64)
- } else {
- return 0, errors.New("git repo id not found")
- }
- }
- return grID, nil
- }
- func findInfraIDInRequest(r *http.Request, infraLoc IDLocation) (uint64, error) {
- var infraID uint64
- var err error
- if infraLoc == URLParam {
- infraID, err = strconv.ParseUint(chi.URLParam(r, "infra_id"), 0, 64)
- if err != nil {
- return 0, err
- }
- } else if infraLoc == BodyParam {
- form := &bodyInfraID{}
- body, err := ioutil.ReadAll(r.Body)
- if err != nil {
- return 0, err
- }
- err = json.Unmarshal(body, form)
- if err != nil {
- return 0, err
- }
- infraID = form.InfraID
- // need to create a new stream for the body
- r.Body = ioutil.NopCloser(bytes.NewReader(body))
- } else {
- vals, err := url.ParseQuery(r.URL.RawQuery)
- if err != nil {
- return 0, err
- }
- if regStrArr, ok := vals["infra_id"]; ok && len(regStrArr) == 1 {
- infraID, err = strconv.ParseUint(regStrArr[0], 10, 64)
- } else {
- return 0, errors.New("infra id not found")
- }
- }
- return infraID, nil
- }
- func findAWSIntegrationIDInRequest(r *http.Request, awsLoc IDLocation) (uint64, error) {
- var awsID uint64
- var err error
- if awsLoc == URLParam {
- awsID, err = strconv.ParseUint(chi.URLParam(r, "aws_integration_id"), 0, 64)
- if err != nil {
- return 0, err
- }
- } else if awsLoc == BodyParam {
- form := &bodyAWSIntegrationID{}
- body, err := ioutil.ReadAll(r.Body)
- if err != nil {
- return 0, err
- }
- err = json.Unmarshal(body, form)
- if err != nil {
- return 0, err
- }
- awsID = form.AWSIntegrationID
- // need to create a new stream for the body
- r.Body = ioutil.NopCloser(bytes.NewReader(body))
- } else {
- vals, err := url.ParseQuery(r.URL.RawQuery)
- if err != nil {
- return 0, err
- }
- if regStrArr, ok := vals["aws_integration_id"]; ok && len(regStrArr) == 1 {
- awsID, err = strconv.ParseUint(regStrArr[0], 10, 64)
- } else {
- return 0, errors.New("aws integration id not found")
- }
- }
- return awsID, nil
- }
- func findGCPIntegrationIDInRequest(r *http.Request, gcpLoc IDLocation) (uint64, error) {
- var gcpID uint64
- var err error
- if gcpLoc == URLParam {
- gcpID, err = strconv.ParseUint(chi.URLParam(r, "gcp_integration_id"), 0, 64)
- if err != nil {
- return 0, err
- }
- } else if gcpLoc == BodyParam {
- form := &bodyGCPIntegrationID{}
- body, err := ioutil.ReadAll(r.Body)
- if err != nil {
- return 0, err
- }
- err = json.Unmarshal(body, form)
- if err != nil {
- return 0, err
- }
- gcpID = form.GCPIntegrationID
- // need to create a new stream for the body
- r.Body = ioutil.NopCloser(bytes.NewReader(body))
- } else {
- vals, err := url.ParseQuery(r.URL.RawQuery)
- if err != nil {
- return 0, err
- }
- if regStrArr, ok := vals["gcp_integration_id"]; ok && len(regStrArr) == 1 {
- gcpID, err = strconv.ParseUint(regStrArr[0], 10, 64)
- } else {
- return 0, errors.New("gcp integration id not found")
- }
- }
- return gcpID, nil
- }
- func findDOIntegrationIDInRequest(r *http.Request, doLoc IDLocation) (uint64, error) {
- var doID uint64
- var err error
- if doLoc == URLParam {
- doID, err = strconv.ParseUint(chi.URLParam(r, "do_integration_id"), 0, 64)
- if err != nil {
- return 0, err
- }
- } else if doLoc == BodyParam {
- form := &bodyDOIntegrationID{}
- body, err := ioutil.ReadAll(r.Body)
- if err != nil {
- return 0, err
- }
- err = json.Unmarshal(body, form)
- if err != nil {
- return 0, err
- }
- doID = form.DOIntegrationID
- // need to create a new stream for the body
- r.Body = ioutil.NopCloser(bytes.NewReader(body))
- } else {
- vals, err := url.ParseQuery(r.URL.RawQuery)
- if err != nil {
- return 0, err
- }
- if regStrArr, ok := vals["do_integration_id"]; ok && len(regStrArr) == 1 {
- doID, err = strconv.ParseUint(regStrArr[0], 10, 64)
- } else {
- return 0, errors.New("do integration id not found")
- }
- }
- return doID, nil
- }
|