auth.go 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  1. package middleware
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "io/ioutil"
  8. "net/http"
  9. "net/url"
  10. "strconv"
  11. "strings"
  12. "github.com/go-chi/chi"
  13. "github.com/gorilla/sessions"
  14. "github.com/porter-dev/porter/internal/auth/token"
  15. "github.com/porter-dev/porter/internal/models"
  16. "github.com/porter-dev/porter/internal/repository"
  17. )
  18. // Auth implements the authorization functions
  19. type Auth struct {
  20. store sessions.Store
  21. cookieName string
  22. tokenConf *token.TokenGeneratorConf
  23. repo *repository.Repository
  24. }
  25. // NewAuth returns a new Auth instance
  26. func NewAuth(
  27. store sessions.Store,
  28. cookieName string,
  29. tokenConf *token.TokenGeneratorConf,
  30. repo *repository.Repository,
  31. ) *Auth {
  32. return &Auth{store, cookieName, tokenConf, repo}
  33. }
  34. // BasicAuthenticate just checks that a user is logged in
  35. func (auth *Auth) BasicAuthenticate(next http.Handler) http.Handler {
  36. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  37. if auth.isLoggedIn(w, r) {
  38. next.ServeHTTP(w, r)
  39. } else {
  40. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  41. return
  42. }
  43. return
  44. })
  45. }
  46. // BasicAuthenticateWithRedirect checks that a user is logged in, and if they're not, the
  47. // user is redirected to the login page with the redirect path stored in the session
  48. func (auth *Auth) BasicAuthenticateWithRedirect(next http.Handler) http.Handler {
  49. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  50. if auth.isLoggedIn(w, r) {
  51. next.ServeHTTP(w, r)
  52. } else {
  53. session, err := auth.store.Get(r, auth.cookieName)
  54. if err != nil {
  55. http.Redirect(w, r, "/dashboard", 302)
  56. }
  57. // need state parameter to validate when redirected
  58. session.Values["redirect"] = r.URL.Path
  59. session.Save(r, w)
  60. http.Redirect(w, r, "/dashboard", 302)
  61. return
  62. }
  63. return
  64. })
  65. }
  66. // IDLocation represents the location of the ID to use for authentication
  67. type IDLocation uint
  68. const (
  69. // URLParam location looks for a parameter in the URL endpoint
  70. URLParam IDLocation = iota
  71. // BodyParam location looks for a parameter in the body
  72. BodyParam
  73. // QueryParam location looks for a parameter in the query string
  74. QueryParam
  75. )
  76. type bodyUserID struct {
  77. UserID uint64 `json:"user_id"`
  78. }
  79. type bodyProjectID struct {
  80. ProjectID uint64 `json:"project_id"`
  81. }
  82. type bodyClusterID struct {
  83. ClusterID uint64 `json:"cluster_id"`
  84. }
  85. type bodyRegistryID struct {
  86. RegistryID uint64 `json:"registry_id"`
  87. }
  88. type bodyGitRepoID struct {
  89. GitRepoID uint64 `json:"git_repo_id"`
  90. }
  91. type bodyInfraID struct {
  92. InfraID uint64 `json:"infra_id"`
  93. }
  94. type bodyInviteID struct {
  95. InviteID uint64 `json:"invite_id"`
  96. }
  97. type bodyAWSIntegrationID struct {
  98. AWSIntegrationID uint64 `json:"aws_integration_id"`
  99. }
  100. type bodyGCPIntegrationID struct {
  101. GCPIntegrationID uint64 `json:"gcp_integration_id"`
  102. }
  103. type bodyDOIntegrationID struct {
  104. DOIntegrationID uint64 `json:"do_integration_id"`
  105. }
  106. // DoesUserIDMatch checks the id URL parameter and verifies that it matches
  107. // the one stored in the session
  108. func (auth *Auth) DoesUserIDMatch(next http.Handler, loc IDLocation) http.Handler {
  109. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  110. // first check for token
  111. tok := auth.getTokenFromRequest(r)
  112. if tok != nil && tok.SubKind == token.User && auth.doesSessionMatchID(r, tok.IBy) {
  113. next.ServeHTTP(w, r)
  114. return
  115. }
  116. var err error
  117. id, err := findUserIDInRequest(r, loc)
  118. if err == nil && auth.doesSessionMatchID(r, uint(id)) {
  119. next.ServeHTTP(w, r)
  120. } else {
  121. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  122. return
  123. }
  124. return
  125. })
  126. }
  127. // AccessType represents the various access types for a project
  128. type AccessType string
  129. // The various access types
  130. const (
  131. ReadAccess AccessType = "read"
  132. WriteAccess AccessType = "write"
  133. )
  134. // DoesUserHaveProjectAccess looks for a project_id parameter and checks that the
  135. // user has access via the specified accessType
  136. func (auth *Auth) DoesUserHaveProjectAccess(
  137. next http.Handler,
  138. projLoc IDLocation,
  139. accessType AccessType,
  140. ) http.Handler {
  141. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  142. var err error
  143. projID, err := findProjIDInRequest(r, projLoc)
  144. if err != nil {
  145. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  146. return
  147. }
  148. // first check for token
  149. tok := auth.getTokenFromRequest(r)
  150. if tok != nil && tok.ProjectID == uint(projID) {
  151. next.ServeHTTP(w, r)
  152. return
  153. }
  154. session, err := auth.store.Get(r, auth.cookieName)
  155. if err != nil {
  156. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  157. return
  158. }
  159. userID, ok := session.Values["user_id"].(uint)
  160. if !ok {
  161. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  162. return
  163. }
  164. // get the project
  165. proj, err := auth.repo.Project.ReadProject(uint(projID))
  166. if err != nil {
  167. http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
  168. return
  169. }
  170. // look for the user role in the project
  171. for _, role := range proj.Roles {
  172. if role.UserID == userID {
  173. if accessType == ReadAccess {
  174. next.ServeHTTP(w, r)
  175. return
  176. } else if accessType == WriteAccess {
  177. if role.Kind == models.RoleAdmin {
  178. next.ServeHTTP(w, r)
  179. return
  180. }
  181. }
  182. }
  183. }
  184. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  185. return
  186. })
  187. }
  188. // DoesUserHaveClusterAccess looks for a project_id parameter and a
  189. // cluster_id parameter, and verifies that the cluster belongs
  190. // to the project
  191. func (auth *Auth) DoesUserHaveClusterAccess(
  192. next http.Handler,
  193. projLoc IDLocation,
  194. clusterLoc IDLocation,
  195. ) http.Handler {
  196. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  197. clusterID, err := findClusterIDInRequest(r, clusterLoc)
  198. if err != nil {
  199. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  200. return
  201. }
  202. projID, err := findProjIDInRequest(r, projLoc)
  203. if err != nil {
  204. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  205. return
  206. }
  207. // get the service accounts belonging to the project
  208. clusters, err := auth.repo.Cluster.ListClustersByProjectID(uint(projID))
  209. if err != nil {
  210. http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
  211. return
  212. }
  213. doesExist := false
  214. for _, cluster := range clusters {
  215. if cluster.ID == uint(clusterID) {
  216. doesExist = true
  217. break
  218. }
  219. }
  220. if doesExist {
  221. next.ServeHTTP(w, r)
  222. return
  223. }
  224. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  225. return
  226. })
  227. }
  228. // DoesUserHaveInviteAccess looks for a project_id parameter and a
  229. // invite_id parameter, and verifies that the invite belongs
  230. // to the project
  231. func (auth *Auth) DoesUserHaveInviteAccess(
  232. next http.Handler,
  233. projLoc IDLocation,
  234. inviteLoc IDLocation,
  235. ) http.Handler {
  236. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  237. inviteID, err := findInviteIDInRequest(r, inviteLoc)
  238. if err != nil {
  239. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  240. return
  241. }
  242. projID, err := findProjIDInRequest(r, projLoc)
  243. if err != nil {
  244. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  245. return
  246. }
  247. // get the service accounts belonging to the project
  248. invites, err := auth.repo.Invite.ListInvitesByProjectID(uint(projID))
  249. if err != nil {
  250. http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
  251. return
  252. }
  253. doesExist := false
  254. for _, invite := range invites {
  255. if invite.ID == uint(inviteID) {
  256. doesExist = true
  257. break
  258. }
  259. }
  260. if doesExist {
  261. next.ServeHTTP(w, r)
  262. return
  263. }
  264. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  265. return
  266. })
  267. }
  268. // DoesUserHaveRegistryAccess looks for a project_id parameter and a
  269. // registry_id parameter, and verifies that the registry belongs
  270. // to the project
  271. func (auth *Auth) DoesUserHaveRegistryAccess(
  272. next http.Handler,
  273. projLoc IDLocation,
  274. registryLoc IDLocation,
  275. ) http.Handler {
  276. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  277. regID, err := findRegistryIDInRequest(r, registryLoc)
  278. if err != nil {
  279. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  280. return
  281. }
  282. projID, err := findProjIDInRequest(r, projLoc)
  283. if err != nil {
  284. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  285. return
  286. }
  287. // get the service accounts belonging to the project
  288. regs, err := auth.repo.Registry.ListRegistriesByProjectID(uint(projID))
  289. if err != nil {
  290. http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
  291. return
  292. }
  293. doesExist := false
  294. for _, reg := range regs {
  295. if reg.ID == uint(regID) {
  296. doesExist = true
  297. break
  298. }
  299. }
  300. if doesExist {
  301. next.ServeHTTP(w, r)
  302. return
  303. }
  304. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  305. return
  306. })
  307. }
  308. // DoesUserHaveGitRepoAccess looks for a project_id parameter and a
  309. // git_repo_id parameter, and verifies that the git repo belongs
  310. // to the project
  311. func (auth *Auth) DoesUserHaveGitRepoAccess(
  312. next http.Handler,
  313. projLoc IDLocation,
  314. gitRepoLoc IDLocation,
  315. ) http.Handler {
  316. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  317. grID, err := findGitRepoIDInRequest(r, gitRepoLoc)
  318. if err != nil {
  319. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  320. return
  321. }
  322. projID, err := findProjIDInRequest(r, projLoc)
  323. if err != nil {
  324. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  325. return
  326. }
  327. // get the service accounts belonging to the project
  328. grs, err := auth.repo.GitRepo.ListGitReposByProjectID(uint(projID))
  329. if err != nil {
  330. http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
  331. return
  332. }
  333. doesExist := false
  334. for _, gr := range grs {
  335. if gr.ID == uint(grID) {
  336. doesExist = true
  337. break
  338. }
  339. }
  340. if doesExist {
  341. next.ServeHTTP(w, r)
  342. return
  343. }
  344. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  345. return
  346. })
  347. }
  348. // DoesUserHaveInfraAccess looks for a project_id parameter and an
  349. // infra_id parameter, and verifies that the infra belongs
  350. // to the project
  351. func (auth *Auth) DoesUserHaveInfraAccess(
  352. next http.Handler,
  353. projLoc IDLocation,
  354. infraLoc IDLocation,
  355. ) http.Handler {
  356. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  357. infraID, err := findInfraIDInRequest(r, infraLoc)
  358. if err != nil {
  359. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  360. return
  361. }
  362. projID, err := findProjIDInRequest(r, projLoc)
  363. if err != nil {
  364. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  365. return
  366. }
  367. infras, err := auth.repo.Infra.ListInfrasByProjectID(uint(projID))
  368. if err != nil {
  369. http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
  370. return
  371. }
  372. doesExist := false
  373. for _, infra := range infras {
  374. if infra.ID == uint(infraID) {
  375. doesExist = true
  376. break
  377. }
  378. }
  379. if doesExist {
  380. next.ServeHTTP(w, r)
  381. return
  382. }
  383. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  384. return
  385. })
  386. }
  387. // DoesUserHaveAWSIntegrationAccess looks for a project_id parameter and an
  388. // aws_integration_id parameter, and verifies that the infra belongs
  389. // to the project
  390. func (auth *Auth) DoesUserHaveAWSIntegrationAccess(
  391. next http.Handler,
  392. projLoc IDLocation,
  393. awsLoc IDLocation,
  394. optional bool,
  395. ) http.Handler {
  396. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  397. awsID, err := findAWSIntegrationIDInRequest(r, awsLoc)
  398. if awsID == 0 && optional {
  399. next.ServeHTTP(w, r)
  400. return
  401. }
  402. if awsID == 0 || err != nil {
  403. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  404. return
  405. }
  406. projID, err := findProjIDInRequest(r, projLoc)
  407. if err != nil {
  408. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  409. return
  410. }
  411. awsInts, err := auth.repo.AWSIntegration.ListAWSIntegrationsByProjectID(uint(projID))
  412. if err != nil {
  413. http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
  414. return
  415. }
  416. doesExist := false
  417. for _, awsInt := range awsInts {
  418. if awsInt.ID == uint(awsID) {
  419. doesExist = true
  420. break
  421. }
  422. }
  423. if doesExist {
  424. next.ServeHTTP(w, r)
  425. return
  426. }
  427. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  428. return
  429. })
  430. }
  431. // DoesUserHaveGCPIntegrationAccess looks for a project_id parameter and an
  432. // gcp_integration_id parameter, and verifies that the infra belongs
  433. // to the project
  434. func (auth *Auth) DoesUserHaveGCPIntegrationAccess(
  435. next http.Handler,
  436. projLoc IDLocation,
  437. gcpLoc IDLocation,
  438. optional bool,
  439. ) http.Handler {
  440. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  441. gcpID, err := findGCPIntegrationIDInRequest(r, gcpLoc)
  442. if gcpID == 0 && optional {
  443. next.ServeHTTP(w, r)
  444. return
  445. }
  446. if gcpID == 0 || err != nil {
  447. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  448. return
  449. }
  450. projID, err := findProjIDInRequest(r, projLoc)
  451. if err != nil {
  452. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  453. return
  454. }
  455. gcpInts, err := auth.repo.GCPIntegration.ListGCPIntegrationsByProjectID(uint(projID))
  456. if err != nil {
  457. http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
  458. return
  459. }
  460. doesExist := false
  461. for _, awsInt := range gcpInts {
  462. if awsInt.ID == uint(gcpID) {
  463. doesExist = true
  464. break
  465. }
  466. }
  467. if doesExist {
  468. next.ServeHTTP(w, r)
  469. return
  470. }
  471. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  472. return
  473. })
  474. }
  475. // DoesUserHaveDOIntegrationAccess looks for a project_id parameter and an
  476. // do_integration_id parameter, and verifies that the infra belongs
  477. // to the project
  478. func (auth *Auth) DoesUserHaveDOIntegrationAccess(
  479. next http.Handler,
  480. projLoc IDLocation,
  481. doLoc IDLocation,
  482. optional bool,
  483. ) http.Handler {
  484. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  485. doID, err := findDOIntegrationIDInRequest(r, doLoc)
  486. if doID == 0 && optional {
  487. next.ServeHTTP(w, r)
  488. return
  489. }
  490. if doID == 0 || err != nil {
  491. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  492. return
  493. }
  494. projID, err := findProjIDInRequest(r, projLoc)
  495. if err != nil {
  496. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  497. return
  498. }
  499. oauthInts, err := auth.repo.OAuthIntegration.ListOAuthIntegrationsByProjectID(uint(projID))
  500. if err != nil {
  501. http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
  502. return
  503. }
  504. doesExist := false
  505. for _, oauthInt := range oauthInts {
  506. if oauthInt.ID == uint(doID) {
  507. doesExist = true
  508. break
  509. }
  510. }
  511. if doesExist {
  512. next.ServeHTTP(w, r)
  513. return
  514. }
  515. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  516. return
  517. })
  518. }
  519. // Helpers
  520. func (auth *Auth) doesSessionMatchID(r *http.Request, id uint) bool {
  521. session, _ := auth.store.Get(r, auth.cookieName)
  522. if sessID, ok := session.Values["user_id"].(uint); !ok || sessID != id {
  523. return false
  524. }
  525. return true
  526. }
  527. func (auth *Auth) isLoggedIn(w http.ResponseWriter, r *http.Request) bool {
  528. // first check for Bearer token
  529. session, err := auth.store.Get(r, auth.cookieName)
  530. if err != nil {
  531. session.Values["authenticated"] = false
  532. if err := session.Save(r, w); err != nil {
  533. fmt.Println("error while saving session in isLoggedIn", err)
  534. }
  535. return false
  536. }
  537. if auth, ok := session.Values["authenticated"].(bool); !auth || !ok {
  538. return false
  539. }
  540. return true
  541. }
  542. func (auth *Auth) getTokenFromRequest(r *http.Request) *token.Token {
  543. reqToken := r.Header.Get("Authorization")
  544. splitToken := strings.Split(reqToken, "Bearer")
  545. if len(splitToken) != 2 {
  546. return nil
  547. }
  548. reqToken = strings.TrimSpace(splitToken[1])
  549. tok, _ := token.GetTokenFromEncoded(reqToken, auth.tokenConf)
  550. return tok
  551. }
  552. func findUserIDInRequest(r *http.Request, userLoc IDLocation) (uint64, error) {
  553. var userID uint64
  554. var err error
  555. if userLoc == URLParam {
  556. userID, err = strconv.ParseUint(chi.URLParam(r, "user_id"), 0, 64)
  557. if err != nil {
  558. return 0, err
  559. }
  560. } else if userLoc == BodyParam {
  561. form := &bodyUserID{}
  562. body, err := ioutil.ReadAll(r.Body)
  563. if err != nil {
  564. return 0, err
  565. }
  566. err = json.Unmarshal(body, form)
  567. if err != nil {
  568. return 0, err
  569. }
  570. userID = form.UserID
  571. // need to create a new stream for the body
  572. r.Body = ioutil.NopCloser(bytes.NewReader(body))
  573. } else {
  574. vals, err := url.ParseQuery(r.URL.RawQuery)
  575. if err != nil {
  576. return 0, err
  577. }
  578. if userStrArr, ok := vals["user_id"]; ok && len(userStrArr) == 1 {
  579. userID, err = strconv.ParseUint(userStrArr[0], 10, 64)
  580. } else {
  581. return 0, errors.New("user id not found")
  582. }
  583. }
  584. return userID, nil
  585. }
  586. func findProjIDInRequest(r *http.Request, projLoc IDLocation) (uint64, error) {
  587. var projID uint64
  588. var err error
  589. if projLoc == URLParam {
  590. projID, err = strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  591. if err != nil {
  592. return 0, err
  593. }
  594. } else if projLoc == BodyParam {
  595. form := &bodyProjectID{}
  596. body, err := ioutil.ReadAll(r.Body)
  597. if err != nil {
  598. return 0, err
  599. }
  600. err = json.Unmarshal(body, form)
  601. if err != nil {
  602. return 0, err
  603. }
  604. projID = form.ProjectID
  605. // need to create a new stream for the body
  606. r.Body = ioutil.NopCloser(bytes.NewReader(body))
  607. } else {
  608. vals, err := url.ParseQuery(r.URL.RawQuery)
  609. if err != nil {
  610. return 0, err
  611. }
  612. if projStrArr, ok := vals["project_id"]; ok && len(projStrArr) == 1 {
  613. projID, err = strconv.ParseUint(projStrArr[0], 10, 64)
  614. } else {
  615. return 0, errors.New("project id not found")
  616. }
  617. }
  618. return projID, nil
  619. }
  620. func findClusterIDInRequest(r *http.Request, clusterLoc IDLocation) (uint64, error) {
  621. var clusterID uint64
  622. var err error
  623. if clusterLoc == URLParam {
  624. clusterID, err = strconv.ParseUint(chi.URLParam(r, "cluster_id"), 0, 64)
  625. if err != nil {
  626. return 0, err
  627. }
  628. } else if clusterLoc == BodyParam {
  629. form := &bodyClusterID{}
  630. body, err := ioutil.ReadAll(r.Body)
  631. if err != nil {
  632. return 0, err
  633. }
  634. err = json.Unmarshal(body, form)
  635. if err != nil {
  636. return 0, err
  637. }
  638. clusterID = form.ClusterID
  639. // need to create a new stream for the body
  640. r.Body = ioutil.NopCloser(bytes.NewReader(body))
  641. } else {
  642. vals, err := url.ParseQuery(r.URL.RawQuery)
  643. if err != nil {
  644. return 0, err
  645. }
  646. if clStrArr, ok := vals["cluster_id"]; ok && len(clStrArr) == 1 {
  647. clusterID, err = strconv.ParseUint(clStrArr[0], 10, 64)
  648. } else {
  649. return 0, errors.New("cluster id not found")
  650. }
  651. }
  652. return clusterID, nil
  653. }
  654. func findInviteIDInRequest(r *http.Request, inviteLoc IDLocation) (uint64, error) {
  655. var inviteID uint64
  656. var err error
  657. if inviteLoc == URLParam {
  658. inviteID, err = strconv.ParseUint(chi.URLParam(r, "invite_id"), 0, 64)
  659. if err != nil {
  660. return 0, err
  661. }
  662. } else if inviteLoc == BodyParam {
  663. form := &bodyInviteID{}
  664. body, err := ioutil.ReadAll(r.Body)
  665. if err != nil {
  666. return 0, err
  667. }
  668. err = json.Unmarshal(body, form)
  669. if err != nil {
  670. return 0, err
  671. }
  672. inviteID = form.InviteID
  673. // need to create a new stream for the body
  674. r.Body = ioutil.NopCloser(bytes.NewReader(body))
  675. } else {
  676. vals, err := url.ParseQuery(r.URL.RawQuery)
  677. if err != nil {
  678. return 0, err
  679. }
  680. if invStrArr, ok := vals["invite_id"]; ok && len(invStrArr) == 1 {
  681. inviteID, err = strconv.ParseUint(invStrArr[0], 10, 64)
  682. } else {
  683. return 0, errors.New("invite id not found")
  684. }
  685. }
  686. return inviteID, nil
  687. }
  688. func findRegistryIDInRequest(r *http.Request, registryLoc IDLocation) (uint64, error) {
  689. var regID uint64
  690. var err error
  691. if registryLoc == URLParam {
  692. regID, err = strconv.ParseUint(chi.URLParam(r, "registry_id"), 0, 64)
  693. if err != nil {
  694. return 0, err
  695. }
  696. } else if registryLoc == BodyParam {
  697. form := &bodyRegistryID{}
  698. body, err := ioutil.ReadAll(r.Body)
  699. if err != nil {
  700. return 0, err
  701. }
  702. err = json.Unmarshal(body, form)
  703. if err != nil {
  704. return 0, err
  705. }
  706. regID = form.RegistryID
  707. // need to create a new stream for the body
  708. r.Body = ioutil.NopCloser(bytes.NewReader(body))
  709. } else {
  710. vals, err := url.ParseQuery(r.URL.RawQuery)
  711. if err != nil {
  712. return 0, err
  713. }
  714. if regStrArr, ok := vals["registry_id"]; ok && len(regStrArr) == 1 {
  715. regID, err = strconv.ParseUint(regStrArr[0], 10, 64)
  716. } else {
  717. return 0, errors.New("registry id not found")
  718. }
  719. }
  720. return regID, nil
  721. }
  722. func findGitRepoIDInRequest(r *http.Request, gitRepoLoc IDLocation) (uint64, error) {
  723. var grID uint64
  724. var err error
  725. if gitRepoLoc == URLParam {
  726. grID, err = strconv.ParseUint(chi.URLParam(r, "git_repo_id"), 0, 64)
  727. if err != nil {
  728. return 0, err
  729. }
  730. } else if gitRepoLoc == BodyParam {
  731. form := &bodyGitRepoID{}
  732. body, err := ioutil.ReadAll(r.Body)
  733. if err != nil {
  734. return 0, err
  735. }
  736. err = json.Unmarshal(body, form)
  737. if err != nil {
  738. return 0, err
  739. }
  740. grID = form.GitRepoID
  741. // need to create a new stream for the body
  742. r.Body = ioutil.NopCloser(bytes.NewReader(body))
  743. } else {
  744. vals, err := url.ParseQuery(r.URL.RawQuery)
  745. if err != nil {
  746. return 0, err
  747. }
  748. if regStrArr, ok := vals["git_repo_id"]; ok && len(regStrArr) == 1 {
  749. grID, err = strconv.ParseUint(regStrArr[0], 10, 64)
  750. } else {
  751. return 0, errors.New("git repo id not found")
  752. }
  753. }
  754. return grID, nil
  755. }
  756. func findInfraIDInRequest(r *http.Request, infraLoc IDLocation) (uint64, error) {
  757. var infraID uint64
  758. var err error
  759. if infraLoc == URLParam {
  760. infraID, err = strconv.ParseUint(chi.URLParam(r, "infra_id"), 0, 64)
  761. if err != nil {
  762. return 0, err
  763. }
  764. } else if infraLoc == BodyParam {
  765. form := &bodyInfraID{}
  766. body, err := ioutil.ReadAll(r.Body)
  767. if err != nil {
  768. return 0, err
  769. }
  770. err = json.Unmarshal(body, form)
  771. if err != nil {
  772. return 0, err
  773. }
  774. infraID = form.InfraID
  775. // need to create a new stream for the body
  776. r.Body = ioutil.NopCloser(bytes.NewReader(body))
  777. } else {
  778. vals, err := url.ParseQuery(r.URL.RawQuery)
  779. if err != nil {
  780. return 0, err
  781. }
  782. if regStrArr, ok := vals["infra_id"]; ok && len(regStrArr) == 1 {
  783. infraID, err = strconv.ParseUint(regStrArr[0], 10, 64)
  784. } else {
  785. return 0, errors.New("infra id not found")
  786. }
  787. }
  788. return infraID, nil
  789. }
  790. func findAWSIntegrationIDInRequest(r *http.Request, awsLoc IDLocation) (uint64, error) {
  791. var awsID uint64
  792. var err error
  793. if awsLoc == URLParam {
  794. awsID, err = strconv.ParseUint(chi.URLParam(r, "aws_integration_id"), 0, 64)
  795. if err != nil {
  796. return 0, err
  797. }
  798. } else if awsLoc == BodyParam {
  799. form := &bodyAWSIntegrationID{}
  800. body, err := ioutil.ReadAll(r.Body)
  801. if err != nil {
  802. return 0, err
  803. }
  804. err = json.Unmarshal(body, form)
  805. if err != nil {
  806. return 0, err
  807. }
  808. awsID = form.AWSIntegrationID
  809. // need to create a new stream for the body
  810. r.Body = ioutil.NopCloser(bytes.NewReader(body))
  811. } else {
  812. vals, err := url.ParseQuery(r.URL.RawQuery)
  813. if err != nil {
  814. return 0, err
  815. }
  816. if regStrArr, ok := vals["aws_integration_id"]; ok && len(regStrArr) == 1 {
  817. awsID, err = strconv.ParseUint(regStrArr[0], 10, 64)
  818. } else {
  819. return 0, errors.New("aws integration id not found")
  820. }
  821. }
  822. return awsID, nil
  823. }
  824. func findGCPIntegrationIDInRequest(r *http.Request, gcpLoc IDLocation) (uint64, error) {
  825. var gcpID uint64
  826. var err error
  827. if gcpLoc == URLParam {
  828. gcpID, err = strconv.ParseUint(chi.URLParam(r, "gcp_integration_id"), 0, 64)
  829. if err != nil {
  830. return 0, err
  831. }
  832. } else if gcpLoc == BodyParam {
  833. form := &bodyGCPIntegrationID{}
  834. body, err := ioutil.ReadAll(r.Body)
  835. if err != nil {
  836. return 0, err
  837. }
  838. err = json.Unmarshal(body, form)
  839. if err != nil {
  840. return 0, err
  841. }
  842. gcpID = form.GCPIntegrationID
  843. // need to create a new stream for the body
  844. r.Body = ioutil.NopCloser(bytes.NewReader(body))
  845. } else {
  846. vals, err := url.ParseQuery(r.URL.RawQuery)
  847. if err != nil {
  848. return 0, err
  849. }
  850. if regStrArr, ok := vals["gcp_integration_id"]; ok && len(regStrArr) == 1 {
  851. gcpID, err = strconv.ParseUint(regStrArr[0], 10, 64)
  852. } else {
  853. return 0, errors.New("gcp integration id not found")
  854. }
  855. }
  856. return gcpID, nil
  857. }
  858. func findDOIntegrationIDInRequest(r *http.Request, doLoc IDLocation) (uint64, error) {
  859. var doID uint64
  860. var err error
  861. if doLoc == URLParam {
  862. doID, err = strconv.ParseUint(chi.URLParam(r, "do_integration_id"), 0, 64)
  863. if err != nil {
  864. return 0, err
  865. }
  866. } else if doLoc == BodyParam {
  867. form := &bodyDOIntegrationID{}
  868. body, err := ioutil.ReadAll(r.Body)
  869. if err != nil {
  870. return 0, err
  871. }
  872. err = json.Unmarshal(body, form)
  873. if err != nil {
  874. return 0, err
  875. }
  876. doID = form.DOIntegrationID
  877. // need to create a new stream for the body
  878. r.Body = ioutil.NopCloser(bytes.NewReader(body))
  879. } else {
  880. vals, err := url.ParseQuery(r.URL.RawQuery)
  881. if err != nil {
  882. return 0, err
  883. }
  884. if regStrArr, ok := vals["do_integration_id"]; ok && len(regStrArr) == 1 {
  885. doID, err = strconv.ParseUint(regStrArr[0], 10, 64)
  886. } else {
  887. return 0, errors.New("do integration id not found")
  888. }
  889. }
  890. return doID, nil
  891. }