auth.go 25 KB

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