integration_handler.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. package api
  2. import (
  3. "encoding/json"
  4. "github.com/google/go-github/github"
  5. "github.com/porter-dev/porter/internal/oauth"
  6. "golang.org/x/oauth2"
  7. "gorm.io/gorm"
  8. "io/ioutil"
  9. "net/http"
  10. "net/url"
  11. "strconv"
  12. "github.com/go-chi/chi"
  13. "github.com/porter-dev/porter/internal/forms"
  14. "github.com/porter-dev/porter/internal/models/integrations"
  15. ints "github.com/porter-dev/porter/internal/models/integrations"
  16. )
  17. // HandleListClusterIntegrations lists the cluster integrations available to the
  18. // instance
  19. func (app *App) HandleListClusterIntegrations(w http.ResponseWriter, r *http.Request) {
  20. clusters := ints.PorterClusterIntegrations
  21. w.WriteHeader(http.StatusOK)
  22. if err := json.NewEncoder(w).Encode(&clusters); err != nil {
  23. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  24. return
  25. }
  26. }
  27. // HandleListRegistryIntegrations lists the image registry integrations available to the
  28. // instance
  29. func (app *App) HandleListRegistryIntegrations(w http.ResponseWriter, r *http.Request) {
  30. registries := ints.PorterRegistryIntegrations
  31. w.WriteHeader(http.StatusOK)
  32. if err := json.NewEncoder(w).Encode(&registries); err != nil {
  33. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  34. return
  35. }
  36. }
  37. // HandleListHelmRepoIntegrations lists the Helm repo integrations available to the
  38. // instance
  39. func (app *App) HandleListHelmRepoIntegrations(w http.ResponseWriter, r *http.Request) {
  40. hrs := ints.PorterHelmRepoIntegrations
  41. w.WriteHeader(http.StatusOK)
  42. if err := json.NewEncoder(w).Encode(&hrs); err != nil {
  43. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  44. return
  45. }
  46. }
  47. // HandleListRepoIntegrations lists the repo integrations available to the
  48. // instance
  49. func (app *App) HandleListRepoIntegrations(w http.ResponseWriter, r *http.Request) {
  50. repos := ints.PorterGitRepoIntegrations
  51. w.WriteHeader(http.StatusOK)
  52. if err := json.NewEncoder(w).Encode(&repos); err != nil {
  53. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  54. return
  55. }
  56. }
  57. // HandleCreateGCPIntegration creates a new GCP integration in the DB
  58. func (app *App) HandleCreateGCPIntegration(w http.ResponseWriter, r *http.Request) {
  59. userID, err := app.getUserIDFromRequest(r)
  60. if err != nil {
  61. http.Error(w, err.Error(), http.StatusInternalServerError)
  62. return
  63. }
  64. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  65. if err != nil || projID == 0 {
  66. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  67. return
  68. }
  69. form := &forms.CreateGCPIntegrationForm{
  70. UserID: userID,
  71. ProjectID: uint(projID),
  72. }
  73. // decode from JSON to form value
  74. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  75. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  76. return
  77. }
  78. // validate the form
  79. if err := app.validator.Struct(form); err != nil {
  80. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  81. return
  82. }
  83. // convert the form to a gcp integration
  84. gcp, err := form.ToGCPIntegration()
  85. if err != nil {
  86. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  87. return
  88. }
  89. // handle write to the database
  90. gcp, err = app.Repo.GCPIntegration.CreateGCPIntegration(gcp)
  91. if err != nil {
  92. app.handleErrorDataWrite(err, w)
  93. return
  94. }
  95. app.Logger.Info().Msgf("New gcp integration created: %d", gcp.ID)
  96. w.WriteHeader(http.StatusCreated)
  97. gcpExt := gcp.Externalize()
  98. if err := json.NewEncoder(w).Encode(gcpExt); err != nil {
  99. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  100. return
  101. }
  102. }
  103. // HandleCreateAWSIntegration creates a new AWS integration in the DB
  104. func (app *App) HandleCreateAWSIntegration(w http.ResponseWriter, r *http.Request) {
  105. userID, err := app.getUserIDFromRequest(r)
  106. if err != nil {
  107. http.Error(w, err.Error(), http.StatusInternalServerError)
  108. return
  109. }
  110. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  111. if err != nil || projID == 0 {
  112. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  113. return
  114. }
  115. form := &forms.CreateAWSIntegrationForm{
  116. UserID: userID,
  117. ProjectID: uint(projID),
  118. }
  119. // decode from JSON to form value
  120. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  121. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  122. return
  123. }
  124. // validate the form
  125. if err := app.validator.Struct(form); err != nil {
  126. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  127. return
  128. }
  129. // convert the form to a aws integration
  130. aws, err := form.ToAWSIntegration()
  131. if err != nil {
  132. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  133. return
  134. }
  135. // handle write to the database
  136. aws, err = app.Repo.AWSIntegration.CreateAWSIntegration(aws)
  137. if err != nil {
  138. app.handleErrorDataWrite(err, w)
  139. return
  140. }
  141. app.Logger.Info().Msgf("New aws integration created: %d", aws.ID)
  142. w.WriteHeader(http.StatusCreated)
  143. awsExt := aws.Externalize()
  144. if err := json.NewEncoder(w).Encode(awsExt); err != nil {
  145. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  146. return
  147. }
  148. }
  149. // HandleOverwriteAWSIntegration overwrites the ID of an AWS integration in the DB
  150. func (app *App) HandleOverwriteAWSIntegration(w http.ResponseWriter, r *http.Request) {
  151. userID, err := app.getUserIDFromRequest(r)
  152. if err != nil {
  153. http.Error(w, err.Error(), http.StatusInternalServerError)
  154. return
  155. }
  156. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  157. if err != nil || projID == 0 {
  158. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  159. return
  160. }
  161. awsIntegrationID, err := strconv.ParseUint(chi.URLParam(r, "aws_integration_id"), 0, 64)
  162. if err != nil || awsIntegrationID == 0 {
  163. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  164. return
  165. }
  166. form := &forms.OverwriteAWSIntegrationForm{
  167. UserID: userID,
  168. ProjectID: uint(projID),
  169. }
  170. // decode from JSON to form value
  171. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  172. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  173. return
  174. }
  175. // validate the form
  176. if err := app.validator.Struct(form); err != nil {
  177. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  178. return
  179. }
  180. // read the aws integration by ID and overwrite the access id/secret
  181. awsIntegration, err := app.Repo.AWSIntegration.ReadAWSIntegration(uint(awsIntegrationID))
  182. if err != nil {
  183. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  184. return
  185. }
  186. awsIntegration.AWSAccessKeyID = []byte(form.AWSAccessKeyID)
  187. awsIntegration.AWSSecretAccessKey = []byte(form.AWSSecretAccessKey)
  188. // handle write to the database
  189. awsIntegration, err = app.Repo.AWSIntegration.OverwriteAWSIntegration(awsIntegration)
  190. if err != nil {
  191. app.handleErrorDataWrite(err, w)
  192. return
  193. }
  194. // clear the cluster token cache if cluster_id exists
  195. vals, err := url.ParseQuery(r.URL.RawQuery)
  196. if err != nil {
  197. app.handleErrorDataWrite(err, w)
  198. return
  199. }
  200. if len(vals["cluster_id"]) > 0 {
  201. clusterID, err := strconv.ParseUint(vals["cluster_id"][0], 10, 64)
  202. if err != nil {
  203. app.handleErrorDataWrite(err, w)
  204. return
  205. }
  206. cluster, err := app.Repo.Cluster.ReadCluster(uint(clusterID))
  207. // clear the token
  208. cluster.TokenCache.Token = []byte("")
  209. cluster, err = app.Repo.Cluster.UpdateClusterTokenCache(&cluster.TokenCache)
  210. if err != nil {
  211. app.handleErrorDataWrite(err, w)
  212. return
  213. }
  214. }
  215. app.Logger.Info().Msgf("AWS integration overwritten: %d", awsIntegration.ID)
  216. w.WriteHeader(http.StatusCreated)
  217. awsExt := awsIntegration.Externalize()
  218. if err := json.NewEncoder(w).Encode(awsExt); err != nil {
  219. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  220. return
  221. }
  222. }
  223. // HandleCreateBasicAuthIntegration creates a new basic auth integration in the DB
  224. func (app *App) HandleCreateBasicAuthIntegration(w http.ResponseWriter, r *http.Request) {
  225. userID, err := app.getUserIDFromRequest(r)
  226. if err != nil {
  227. http.Error(w, err.Error(), http.StatusInternalServerError)
  228. return
  229. }
  230. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  231. if err != nil || projID == 0 {
  232. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  233. return
  234. }
  235. form := &forms.CreateBasicAuthIntegrationForm{
  236. UserID: userID,
  237. ProjectID: uint(projID),
  238. }
  239. // decode from JSON to form value
  240. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  241. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  242. return
  243. }
  244. // validate the form
  245. if err := app.validator.Struct(form); err != nil {
  246. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  247. return
  248. }
  249. // convert the form to a gcp integration
  250. basic, err := form.ToBasicIntegration()
  251. if err != nil {
  252. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  253. return
  254. }
  255. // handle write to the database
  256. basic, err = app.Repo.BasicIntegration.CreateBasicIntegration(basic)
  257. if err != nil {
  258. app.handleErrorDataWrite(err, w)
  259. return
  260. }
  261. app.Logger.Info().Msgf("New basic integration created: %d", basic.ID)
  262. w.WriteHeader(http.StatusCreated)
  263. basicExt := basic.Externalize()
  264. if err := json.NewEncoder(w).Encode(basicExt); err != nil {
  265. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  266. return
  267. }
  268. }
  269. // HandleListProjectOAuthIntegrations lists the oauth integrations for the project
  270. func (app *App) HandleListProjectOAuthIntegrations(w http.ResponseWriter, r *http.Request) {
  271. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  272. if err != nil || projID == 0 {
  273. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  274. return
  275. }
  276. oauthInts, err := app.Repo.OAuthIntegration.ListOAuthIntegrationsByProjectID(uint(projID))
  277. if err != nil {
  278. app.handleErrorDataRead(err, w)
  279. return
  280. }
  281. res := make([]*integrations.OAuthIntegrationExternal, 0)
  282. for _, oauthInt := range oauthInts {
  283. res = append(res, oauthInt.Externalize())
  284. }
  285. w.WriteHeader(http.StatusOK)
  286. if err := json.NewEncoder(w).Encode(res); err != nil {
  287. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  288. return
  289. }
  290. }
  291. func (app *App) HandleGithubAppEvent(w http.ResponseWriter, r *http.Request) {
  292. payload, err := ioutil.ReadAll(r.Body)
  293. if err != nil {
  294. app.handleErrorInternal(err, w)
  295. return
  296. }
  297. event, err := github.ParseWebHook(github.WebHookType(r), payload)
  298. if err != nil {
  299. app.handleErrorInternal(err, w)
  300. return
  301. }
  302. switch e := event.(type) {
  303. case *github.InstallationEvent:
  304. if *e.Action == "created" {
  305. _, err := app.Repo.GithubAppInstallation.ReadGithubAppInstallationByAccountID(*e.Installation.Account.ID)
  306. if err != nil && err == gorm.ErrRecordNotFound {
  307. // insert account/installation pair into database
  308. _, err := app.Repo.GithubAppInstallation.CreateGithubAppInstallation(&ints.GithubAppInstallation{
  309. AccountID: *e.Installation.Account.ID,
  310. InstallationID: *e.Installation.ID,
  311. })
  312. if err != nil {
  313. app.handleErrorInternal(err, w)
  314. }
  315. return
  316. } else if err != nil {
  317. app.handleErrorInternal(err, w)
  318. return
  319. }
  320. }
  321. if *e.Action == "deleted" {
  322. err := app.Repo.GithubAppInstallation.DeleteGithubAppInstallationByAccountID(*e.Installation.Account.ID)
  323. if err != nil {
  324. app.handleErrorInternal(err, w)
  325. return
  326. }
  327. }
  328. }
  329. }
  330. // HandleGithubAppInstall starts the oauth2 flow for a project repo request.
  331. func (app *App) HandleGithubAppInstall(w http.ResponseWriter, r *http.Request) {
  332. state := oauth.CreateRandomState()
  333. err := app.populateOAuthSession(w, r, state, false)
  334. if err != nil {
  335. app.handleErrorDataRead(err, w)
  336. return
  337. }
  338. // specify access type offline to get a refresh token
  339. url := app.GithubAppConf.AuthCodeURL(state, oauth2.AccessTypeOffline)
  340. http.Redirect(w, r, url, 302)
  341. }