auth.go 25 KB

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