integration_handler.go 11 KB

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