integration_handler.go 11 KB

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