auth.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  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. "github.com/go-chi/chi"
  12. "github.com/gorilla/sessions"
  13. "github.com/porter-dev/porter/internal/models"
  14. "github.com/porter-dev/porter/internal/repository"
  15. )
  16. // Auth implements the authorization functions
  17. type Auth struct {
  18. store sessions.Store
  19. cookieName string
  20. repo *repository.Repository
  21. }
  22. // NewAuth returns a new Auth instance
  23. func NewAuth(
  24. store sessions.Store,
  25. cookieName string,
  26. repo *repository.Repository,
  27. ) *Auth {
  28. return &Auth{store, cookieName, repo}
  29. }
  30. // BasicAuthenticate just checks that a user is logged in
  31. func (auth *Auth) BasicAuthenticate(next http.Handler) http.Handler {
  32. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  33. if auth.isLoggedIn(w, r) {
  34. next.ServeHTTP(w, r)
  35. } else {
  36. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  37. return
  38. }
  39. return
  40. })
  41. }
  42. // IDLocation represents the location of the ID to use for authentication
  43. type IDLocation uint
  44. const (
  45. // URLParam location looks for a parameter in the URL endpoint
  46. URLParam IDLocation = iota
  47. // BodyParam location looks for a parameter in the body
  48. BodyParam
  49. // QueryParam location looks for a parameter in the query string
  50. QueryParam
  51. )
  52. type bodyUserID struct {
  53. UserID uint64 `json:"user_id"`
  54. }
  55. type bodyProjectID struct {
  56. ProjectID uint64 `json:"project_id"`
  57. }
  58. type bodyClusterID struct {
  59. ClusterID uint64 `json:"cluster_id"`
  60. }
  61. type bodyRegistryID struct {
  62. RegistryID uint64 `json:"registry_id"`
  63. }
  64. type bodyGitRepoID struct {
  65. GitRepoID uint64 `json:"git_repo_id"`
  66. }
  67. type bodyInfraID struct {
  68. InfraID uint64 `json:"infra_id"`
  69. }
  70. type bodyAWSIntegrationID struct {
  71. AWSIntegrationID uint64 `json:"aws_integration_id"`
  72. }
  73. type bodyGCPIntegrationID struct {
  74. GCPIntegrationID uint64 `json:"gcp_integration_id"`
  75. }
  76. // DoesUserIDMatch checks the id URL parameter and verifies that it matches
  77. // the one stored in the session
  78. func (auth *Auth) DoesUserIDMatch(next http.Handler, loc IDLocation) http.Handler {
  79. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  80. var err error
  81. id, err := findUserIDInRequest(r, loc)
  82. if err == nil && auth.doesSessionMatchID(r, uint(id)) {
  83. next.ServeHTTP(w, r)
  84. } else {
  85. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  86. return
  87. }
  88. return
  89. })
  90. }
  91. // AccessType represents the various access types for a project
  92. type AccessType string
  93. // The various access types
  94. const (
  95. ReadAccess AccessType = "read"
  96. WriteAccess AccessType = "write"
  97. )
  98. // DoesUserHaveProjectAccess looks for a project_id parameter and checks that the
  99. // user has access via the specified accessType
  100. func (auth *Auth) DoesUserHaveProjectAccess(
  101. next http.Handler,
  102. projLoc IDLocation,
  103. accessType AccessType,
  104. ) http.Handler {
  105. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  106. var err error
  107. projID, err := findProjIDInRequest(r, projLoc)
  108. if err != nil {
  109. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  110. return
  111. }
  112. session, err := auth.store.Get(r, auth.cookieName)
  113. if err != nil {
  114. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  115. return
  116. }
  117. userID, ok := session.Values["user_id"].(uint)
  118. if !ok {
  119. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  120. return
  121. }
  122. // get the project
  123. proj, err := auth.repo.Project.ReadProject(uint(projID))
  124. if err != nil {
  125. http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
  126. return
  127. }
  128. // look for the user role in the project
  129. for _, role := range proj.Roles {
  130. if role.UserID == userID {
  131. if accessType == ReadAccess {
  132. next.ServeHTTP(w, r)
  133. return
  134. } else if accessType == WriteAccess {
  135. if role.Kind == models.RoleAdmin {
  136. next.ServeHTTP(w, r)
  137. return
  138. }
  139. }
  140. }
  141. }
  142. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  143. return
  144. })
  145. }
  146. // DoesUserHaveClusterAccess looks for a project_id parameter and a
  147. // cluster_id parameter, and verifies that the cluster belongs
  148. // to the project
  149. func (auth *Auth) DoesUserHaveClusterAccess(
  150. next http.Handler,
  151. projLoc IDLocation,
  152. clusterLoc IDLocation,
  153. ) http.Handler {
  154. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  155. clusterID, err := findClusterIDInRequest(r, clusterLoc)
  156. if err != nil {
  157. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  158. return
  159. }
  160. projID, err := findProjIDInRequest(r, projLoc)
  161. if err != nil {
  162. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  163. return
  164. }
  165. // get the service accounts belonging to the project
  166. clusters, err := auth.repo.Cluster.ListClustersByProjectID(uint(projID))
  167. if err != nil {
  168. http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
  169. return
  170. }
  171. doesExist := false
  172. for _, cluster := range clusters {
  173. if cluster.ID == uint(clusterID) {
  174. doesExist = true
  175. break
  176. }
  177. }
  178. if doesExist {
  179. next.ServeHTTP(w, r)
  180. return
  181. }
  182. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  183. return
  184. })
  185. }
  186. // DoesUserHaveRegistryAccess looks for a project_id parameter and a
  187. // registry_id parameter, and verifies that the registry belongs
  188. // to the project
  189. func (auth *Auth) DoesUserHaveRegistryAccess(
  190. next http.Handler,
  191. projLoc IDLocation,
  192. registryLoc IDLocation,
  193. ) http.Handler {
  194. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  195. regID, err := findRegistryIDInRequest(r, registryLoc)
  196. if err != nil {
  197. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  198. return
  199. }
  200. projID, err := findProjIDInRequest(r, projLoc)
  201. if err != nil {
  202. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  203. return
  204. }
  205. // get the service accounts belonging to the project
  206. regs, err := auth.repo.Registry.ListRegistriesByProjectID(uint(projID))
  207. if err != nil {
  208. http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
  209. return
  210. }
  211. doesExist := false
  212. for _, reg := range regs {
  213. if reg.ID == uint(regID) {
  214. doesExist = true
  215. break
  216. }
  217. }
  218. if doesExist {
  219. next.ServeHTTP(w, r)
  220. return
  221. }
  222. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  223. return
  224. })
  225. }
  226. // DoesUserHaveGitRepoAccess looks for a project_id parameter and a
  227. // git_repo_id parameter, and verifies that the git repo belongs
  228. // to the project
  229. func (auth *Auth) DoesUserHaveGitRepoAccess(
  230. next http.Handler,
  231. projLoc IDLocation,
  232. gitRepoLoc IDLocation,
  233. ) http.Handler {
  234. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  235. grID, err := findGitRepoIDInRequest(r, gitRepoLoc)
  236. if err != nil {
  237. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  238. return
  239. }
  240. projID, err := findProjIDInRequest(r, projLoc)
  241. if err != nil {
  242. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  243. return
  244. }
  245. // get the service accounts belonging to the project
  246. grs, err := auth.repo.GitRepo.ListGitReposByProjectID(uint(projID))
  247. if err != nil {
  248. http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
  249. return
  250. }
  251. doesExist := false
  252. for _, gr := range grs {
  253. if gr.ID == uint(grID) {
  254. doesExist = true
  255. break
  256. }
  257. }
  258. if doesExist {
  259. next.ServeHTTP(w, r)
  260. return
  261. }
  262. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  263. return
  264. })
  265. }
  266. // DoesUserHaveInfraAccess looks for a project_id parameter and an
  267. // infra_id parameter, and verifies that the infra belongs
  268. // to the project
  269. func (auth *Auth) DoesUserHaveInfraAccess(
  270. next http.Handler,
  271. projLoc IDLocation,
  272. infraLoc IDLocation,
  273. ) http.Handler {
  274. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  275. infraID, err := findInfraIDInRequest(r, infraLoc)
  276. if err != nil {
  277. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  278. return
  279. }
  280. projID, err := findProjIDInRequest(r, projLoc)
  281. if err != nil {
  282. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  283. return
  284. }
  285. infras, err := auth.repo.AWSInfra.ListAWSInfrasByProjectID(uint(projID))
  286. if err != nil {
  287. http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
  288. return
  289. }
  290. doesExist := false
  291. for _, infra := range infras {
  292. if infra.ID == uint(infraID) {
  293. doesExist = true
  294. break
  295. }
  296. }
  297. if doesExist {
  298. next.ServeHTTP(w, r)
  299. return
  300. }
  301. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  302. return
  303. })
  304. }
  305. // DoesUserHaveAWSIntegrationAccess looks for a project_id parameter and an
  306. // aws_integration_id parameter, and verifies that the infra belongs
  307. // to the project
  308. func (auth *Auth) DoesUserHaveAWSIntegrationAccess(
  309. next http.Handler,
  310. projLoc IDLocation,
  311. awsLoc IDLocation,
  312. optional bool,
  313. ) http.Handler {
  314. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  315. awsID, err := findAWSIntegrationIDInRequest(r, awsLoc)
  316. if awsID == 0 && optional {
  317. next.ServeHTTP(w, r)
  318. return
  319. }
  320. if awsID == 0 || 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. awsInts, err := auth.repo.AWSIntegration.ListAWSIntegrationsByProjectID(uint(projID))
  330. if err != nil {
  331. http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
  332. return
  333. }
  334. doesExist := false
  335. for _, awsInt := range awsInts {
  336. if awsInt.ID == uint(awsID) {
  337. doesExist = true
  338. break
  339. }
  340. }
  341. if doesExist {
  342. next.ServeHTTP(w, r)
  343. return
  344. }
  345. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  346. return
  347. })
  348. }
  349. // DoesUserHaveGCPIntegrationAccess looks for a project_id parameter and an
  350. // gcp_integration_id parameter, and verifies that the infra belongs
  351. // to the project
  352. func (auth *Auth) DoesUserHaveGCPIntegrationAccess(
  353. next http.Handler,
  354. projLoc IDLocation,
  355. gcpLoc IDLocation,
  356. optional bool,
  357. ) http.Handler {
  358. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  359. gcpID, err := findGCPIntegrationIDInRequest(r, gcpLoc)
  360. if gcpID == 0 && optional {
  361. next.ServeHTTP(w, r)
  362. return
  363. }
  364. if gcpID == 0 || err != nil {
  365. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  366. return
  367. }
  368. projID, err := findProjIDInRequest(r, projLoc)
  369. if err != nil {
  370. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  371. return
  372. }
  373. gcpInts, err := auth.repo.GCPIntegration.ListGCPIntegrationsByProjectID(uint(projID))
  374. if err != nil {
  375. http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
  376. return
  377. }
  378. doesExist := false
  379. for _, awsInt := range gcpInts {
  380. if awsInt.ID == uint(gcpID) {
  381. doesExist = true
  382. break
  383. }
  384. }
  385. if doesExist {
  386. next.ServeHTTP(w, r)
  387. return
  388. }
  389. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  390. return
  391. })
  392. }
  393. // Helpers
  394. func (auth *Auth) doesSessionMatchID(r *http.Request, id uint) bool {
  395. session, _ := auth.store.Get(r, auth.cookieName)
  396. if sessID, ok := session.Values["user_id"].(uint); !ok || sessID != id {
  397. return false
  398. }
  399. return true
  400. }
  401. func (auth *Auth) isLoggedIn(w http.ResponseWriter, r *http.Request) bool {
  402. session, err := auth.store.Get(r, auth.cookieName)
  403. if err != nil {
  404. session.Values["authenticated"] = false
  405. if err := session.Save(r, w); err != nil {
  406. fmt.Println("error while saving session in isLoggedIn", err)
  407. }
  408. return false
  409. }
  410. if auth, ok := session.Values["authenticated"].(bool); !auth || !ok {
  411. return false
  412. }
  413. return true
  414. }
  415. func findUserIDInRequest(r *http.Request, userLoc IDLocation) (uint64, error) {
  416. var userID uint64
  417. var err error
  418. if userLoc == URLParam {
  419. userID, err = strconv.ParseUint(chi.URLParam(r, "user_id"), 0, 64)
  420. if err != nil {
  421. return 0, err
  422. }
  423. } else if userLoc == BodyParam {
  424. form := &bodyUserID{}
  425. body, err := ioutil.ReadAll(r.Body)
  426. if err != nil {
  427. return 0, err
  428. }
  429. err = json.Unmarshal(body, form)
  430. if err != nil {
  431. return 0, err
  432. }
  433. userID = form.UserID
  434. // need to create a new stream for the body
  435. r.Body = ioutil.NopCloser(bytes.NewReader(body))
  436. } else {
  437. vals, err := url.ParseQuery(r.URL.RawQuery)
  438. if err != nil {
  439. return 0, err
  440. }
  441. if userStrArr, ok := vals["user_id"]; ok && len(userStrArr) == 1 {
  442. userID, err = strconv.ParseUint(userStrArr[0], 10, 64)
  443. } else {
  444. return 0, errors.New("user id not found")
  445. }
  446. }
  447. return userID, nil
  448. }
  449. func findProjIDInRequest(r *http.Request, projLoc IDLocation) (uint64, error) {
  450. var projID uint64
  451. var err error
  452. if projLoc == URLParam {
  453. projID, err = strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  454. if err != nil {
  455. return 0, err
  456. }
  457. } else if projLoc == BodyParam {
  458. form := &bodyProjectID{}
  459. body, err := ioutil.ReadAll(r.Body)
  460. if err != nil {
  461. return 0, err
  462. }
  463. err = json.Unmarshal(body, form)
  464. if err != nil {
  465. return 0, err
  466. }
  467. projID = form.ProjectID
  468. // need to create a new stream for the body
  469. r.Body = ioutil.NopCloser(bytes.NewReader(body))
  470. } else {
  471. vals, err := url.ParseQuery(r.URL.RawQuery)
  472. if err != nil {
  473. return 0, err
  474. }
  475. if projStrArr, ok := vals["project_id"]; ok && len(projStrArr) == 1 {
  476. projID, err = strconv.ParseUint(projStrArr[0], 10, 64)
  477. } else {
  478. return 0, errors.New("project id not found")
  479. }
  480. }
  481. return projID, nil
  482. }
  483. func findClusterIDInRequest(r *http.Request, clusterLoc IDLocation) (uint64, error) {
  484. var clusterID uint64
  485. var err error
  486. if clusterLoc == URLParam {
  487. clusterID, err = strconv.ParseUint(chi.URLParam(r, "cluster_id"), 0, 64)
  488. if err != nil {
  489. return 0, err
  490. }
  491. } else if clusterLoc == BodyParam {
  492. form := &bodyClusterID{}
  493. body, err := ioutil.ReadAll(r.Body)
  494. if err != nil {
  495. return 0, err
  496. }
  497. err = json.Unmarshal(body, form)
  498. if err != nil {
  499. return 0, err
  500. }
  501. clusterID = form.ClusterID
  502. // need to create a new stream for the body
  503. r.Body = ioutil.NopCloser(bytes.NewReader(body))
  504. } else {
  505. vals, err := url.ParseQuery(r.URL.RawQuery)
  506. if err != nil {
  507. return 0, err
  508. }
  509. if clStrArr, ok := vals["cluster_id"]; ok && len(clStrArr) == 1 {
  510. clusterID, err = strconv.ParseUint(clStrArr[0], 10, 64)
  511. } else {
  512. return 0, errors.New("cluster id not found")
  513. }
  514. }
  515. return clusterID, nil
  516. }
  517. func findRegistryIDInRequest(r *http.Request, registryLoc IDLocation) (uint64, error) {
  518. var regID uint64
  519. var err error
  520. if registryLoc == URLParam {
  521. regID, err = strconv.ParseUint(chi.URLParam(r, "registry_id"), 0, 64)
  522. if err != nil {
  523. return 0, err
  524. }
  525. } else if registryLoc == BodyParam {
  526. form := &bodyRegistryID{}
  527. body, err := ioutil.ReadAll(r.Body)
  528. if err != nil {
  529. return 0, err
  530. }
  531. err = json.Unmarshal(body, form)
  532. if err != nil {
  533. return 0, err
  534. }
  535. regID = form.RegistryID
  536. // need to create a new stream for the body
  537. r.Body = ioutil.NopCloser(bytes.NewReader(body))
  538. } else {
  539. vals, err := url.ParseQuery(r.URL.RawQuery)
  540. if err != nil {
  541. return 0, err
  542. }
  543. if regStrArr, ok := vals["registry_id"]; ok && len(regStrArr) == 1 {
  544. regID, err = strconv.ParseUint(regStrArr[0], 10, 64)
  545. } else {
  546. return 0, errors.New("registry id not found")
  547. }
  548. }
  549. return regID, nil
  550. }
  551. func findGitRepoIDInRequest(r *http.Request, gitRepoLoc IDLocation) (uint64, error) {
  552. var grID uint64
  553. var err error
  554. if gitRepoLoc == URLParam {
  555. grID, err = strconv.ParseUint(chi.URLParam(r, "git_repo_id"), 0, 64)
  556. if err != nil {
  557. return 0, err
  558. }
  559. } else if gitRepoLoc == BodyParam {
  560. form := &bodyGitRepoID{}
  561. body, err := ioutil.ReadAll(r.Body)
  562. if err != nil {
  563. return 0, err
  564. }
  565. err = json.Unmarshal(body, form)
  566. if err != nil {
  567. return 0, err
  568. }
  569. grID = form.GitRepoID
  570. // need to create a new stream for the body
  571. r.Body = ioutil.NopCloser(bytes.NewReader(body))
  572. } else {
  573. vals, err := url.ParseQuery(r.URL.RawQuery)
  574. if err != nil {
  575. return 0, err
  576. }
  577. if regStrArr, ok := vals["git_repo_id"]; ok && len(regStrArr) == 1 {
  578. grID, err = strconv.ParseUint(regStrArr[0], 10, 64)
  579. } else {
  580. return 0, errors.New("git repo id not found")
  581. }
  582. }
  583. return grID, nil
  584. }
  585. func findInfraIDInRequest(r *http.Request, infraLoc IDLocation) (uint64, error) {
  586. var infraID uint64
  587. var err error
  588. if infraLoc == URLParam {
  589. infraID, err = strconv.ParseUint(chi.URLParam(r, "infra_id"), 0, 64)
  590. if err != nil {
  591. return 0, err
  592. }
  593. } else if infraLoc == BodyParam {
  594. form := &bodyInfraID{}
  595. body, err := ioutil.ReadAll(r.Body)
  596. if err != nil {
  597. return 0, err
  598. }
  599. err = json.Unmarshal(body, form)
  600. if err != nil {
  601. return 0, err
  602. }
  603. infraID = form.InfraID
  604. // need to create a new stream for the body
  605. r.Body = ioutil.NopCloser(bytes.NewReader(body))
  606. } else {
  607. vals, err := url.ParseQuery(r.URL.RawQuery)
  608. if err != nil {
  609. return 0, err
  610. }
  611. if regStrArr, ok := vals["infra_id"]; ok && len(regStrArr) == 1 {
  612. infraID, err = strconv.ParseUint(regStrArr[0], 10, 64)
  613. } else {
  614. return 0, errors.New("infra id not found")
  615. }
  616. }
  617. return infraID, nil
  618. }
  619. func findAWSIntegrationIDInRequest(r *http.Request, awsLoc IDLocation) (uint64, error) {
  620. var awsID uint64
  621. var err error
  622. if awsLoc == URLParam {
  623. awsID, err = strconv.ParseUint(chi.URLParam(r, "aws_integration_id"), 0, 64)
  624. if err != nil {
  625. return 0, err
  626. }
  627. } else if awsLoc == BodyParam {
  628. form := &bodyAWSIntegrationID{}
  629. body, err := ioutil.ReadAll(r.Body)
  630. if err != nil {
  631. return 0, err
  632. }
  633. err = json.Unmarshal(body, form)
  634. if err != nil {
  635. return 0, err
  636. }
  637. awsID = form.AWSIntegrationID
  638. // need to create a new stream for the body
  639. r.Body = ioutil.NopCloser(bytes.NewReader(body))
  640. } else {
  641. vals, err := url.ParseQuery(r.URL.RawQuery)
  642. if err != nil {
  643. return 0, err
  644. }
  645. if regStrArr, ok := vals["aws_integration_id"]; ok && len(regStrArr) == 1 {
  646. awsID, err = strconv.ParseUint(regStrArr[0], 10, 64)
  647. } else {
  648. return 0, errors.New("aws integration id not found")
  649. }
  650. }
  651. return awsID, nil
  652. }
  653. func findGCPIntegrationIDInRequest(r *http.Request, gcpLoc IDLocation) (uint64, error) {
  654. var gcpID uint64
  655. var err error
  656. if gcpLoc == URLParam {
  657. gcpID, err = strconv.ParseUint(chi.URLParam(r, "gcp_integration_id"), 0, 64)
  658. if err != nil {
  659. return 0, err
  660. }
  661. } else if gcpLoc == BodyParam {
  662. form := &bodyGCPIntegrationID{}
  663. body, err := ioutil.ReadAll(r.Body)
  664. if err != nil {
  665. return 0, err
  666. }
  667. err = json.Unmarshal(body, form)
  668. if err != nil {
  669. return 0, err
  670. }
  671. gcpID = form.GCPIntegrationID
  672. // need to create a new stream for the body
  673. r.Body = ioutil.NopCloser(bytes.NewReader(body))
  674. } else {
  675. vals, err := url.ParseQuery(r.URL.RawQuery)
  676. if err != nil {
  677. return 0, err
  678. }
  679. if regStrArr, ok := vals["gcp_integration_id"]; ok && len(regStrArr) == 1 {
  680. gcpID, err = strconv.ParseUint(regStrArr[0], 10, 64)
  681. } else {
  682. return 0, errors.New("gcp integration id not found")
  683. }
  684. }
  685. return gcpID, nil
  686. }