integration_handler.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. package api
  2. import (
  3. "context"
  4. "crypto/hmac"
  5. "crypto/sha256"
  6. "encoding/hex"
  7. "encoding/json"
  8. "fmt"
  9. "io/ioutil"
  10. "net/http"
  11. "net/url"
  12. "sort"
  13. "strconv"
  14. "strings"
  15. "github.com/go-chi/chi"
  16. "github.com/google/go-github/github"
  17. "github.com/porter-dev/porter/internal/analytics"
  18. "github.com/porter-dev/porter/internal/forms"
  19. "github.com/porter-dev/porter/internal/oauth"
  20. "golang.org/x/oauth2"
  21. "gorm.io/gorm"
  22. "github.com/porter-dev/porter/internal/models/integrations"
  23. ints "github.com/porter-dev/porter/internal/models/integrations"
  24. )
  25. // HandleListClusterIntegrations lists the cluster integrations available to the
  26. // instance
  27. func (app *App) HandleListClusterIntegrations(w http.ResponseWriter, r *http.Request) {
  28. clusters := ints.PorterClusterIntegrations
  29. w.WriteHeader(http.StatusOK)
  30. if err := json.NewEncoder(w).Encode(&clusters); err != nil {
  31. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  32. return
  33. }
  34. }
  35. // HandleListRegistryIntegrations lists the image registry integrations available to the
  36. // instance
  37. func (app *App) HandleListRegistryIntegrations(w http.ResponseWriter, r *http.Request) {
  38. registries := ints.PorterRegistryIntegrations
  39. w.WriteHeader(http.StatusOK)
  40. if err := json.NewEncoder(w).Encode(&registries); err != nil {
  41. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  42. return
  43. }
  44. }
  45. // HandleListHelmRepoIntegrations lists the Helm repo integrations available to the
  46. // instance
  47. func (app *App) HandleListHelmRepoIntegrations(w http.ResponseWriter, r *http.Request) {
  48. hrs := ints.PorterHelmRepoIntegrations
  49. w.WriteHeader(http.StatusOK)
  50. if err := json.NewEncoder(w).Encode(&hrs); err != nil {
  51. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  52. return
  53. }
  54. }
  55. // HandleListRepoIntegrations lists the repo integrations available to the
  56. // instance
  57. func (app *App) HandleListRepoIntegrations(w http.ResponseWriter, r *http.Request) {
  58. repos := ints.PorterGitRepoIntegrations
  59. w.WriteHeader(http.StatusOK)
  60. if err := json.NewEncoder(w).Encode(&repos); err != nil {
  61. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  62. return
  63. }
  64. }
  65. // HandleCreateGCPIntegration creates a new GCP integration in the DB
  66. func (app *App) HandleCreateGCPIntegration(w http.ResponseWriter, r *http.Request) {
  67. userID, err := app.getUserIDFromRequest(r)
  68. if err != nil {
  69. http.Error(w, err.Error(), http.StatusInternalServerError)
  70. return
  71. }
  72. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  73. if err != nil || projID == 0 {
  74. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  75. return
  76. }
  77. form := &forms.CreateGCPIntegrationForm{
  78. UserID: userID,
  79. ProjectID: uint(projID),
  80. }
  81. // decode from JSON to form value
  82. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  83. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  84. return
  85. }
  86. // validate the form
  87. if err := app.validator.Struct(form); err != nil {
  88. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  89. return
  90. }
  91. // convert the form to a gcp integration
  92. gcp, err := form.ToGCPIntegration()
  93. if err != nil {
  94. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  95. return
  96. }
  97. // handle write to the database
  98. gcp, err = app.Repo.GCPIntegration.CreateGCPIntegration(gcp)
  99. if err != nil {
  100. app.handleErrorDataWrite(err, w)
  101. return
  102. }
  103. app.Logger.Info().Msgf("New gcp integration created: %d", gcp.ID)
  104. w.WriteHeader(http.StatusCreated)
  105. gcpExt := gcp.Externalize()
  106. if err := json.NewEncoder(w).Encode(gcpExt); err != nil {
  107. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  108. return
  109. }
  110. }
  111. // HandleCreateAWSIntegration creates a new AWS integration in the DB
  112. func (app *App) HandleCreateAWSIntegration(w http.ResponseWriter, r *http.Request) {
  113. userID, err := app.getUserIDFromRequest(r)
  114. if err != nil {
  115. http.Error(w, err.Error(), http.StatusInternalServerError)
  116. return
  117. }
  118. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  119. if err != nil || projID == 0 {
  120. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  121. return
  122. }
  123. form := &forms.CreateAWSIntegrationForm{
  124. UserID: userID,
  125. ProjectID: uint(projID),
  126. }
  127. // decode from JSON to form value
  128. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  129. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  130. return
  131. }
  132. // validate the form
  133. if err := app.validator.Struct(form); err != nil {
  134. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  135. return
  136. }
  137. // convert the form to a aws integration
  138. aws, err := form.ToAWSIntegration()
  139. if err != nil {
  140. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  141. return
  142. }
  143. // handle write to the database
  144. aws, err = app.Repo.AWSIntegration.CreateAWSIntegration(aws)
  145. if err != nil {
  146. app.handleErrorDataWrite(err, w)
  147. return
  148. }
  149. app.Logger.Info().Msgf("New aws integration created: %d", aws.ID)
  150. w.WriteHeader(http.StatusCreated)
  151. awsExt := aws.Externalize()
  152. if err := json.NewEncoder(w).Encode(awsExt); err != nil {
  153. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  154. return
  155. }
  156. }
  157. // HandleOverwriteAWSIntegration overwrites the ID of an AWS integration in the DB
  158. func (app *App) HandleOverwriteAWSIntegration(w http.ResponseWriter, r *http.Request) {
  159. userID, err := app.getUserIDFromRequest(r)
  160. if err != nil {
  161. http.Error(w, err.Error(), http.StatusInternalServerError)
  162. return
  163. }
  164. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  165. if err != nil || projID == 0 {
  166. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  167. return
  168. }
  169. awsIntegrationID, err := strconv.ParseUint(chi.URLParam(r, "aws_integration_id"), 0, 64)
  170. if err != nil || awsIntegrationID == 0 {
  171. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  172. return
  173. }
  174. form := &forms.OverwriteAWSIntegrationForm{
  175. UserID: userID,
  176. ProjectID: uint(projID),
  177. }
  178. // decode from JSON to form value
  179. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  180. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  181. return
  182. }
  183. // validate the form
  184. if err := app.validator.Struct(form); err != nil {
  185. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  186. return
  187. }
  188. // read the aws integration by ID and overwrite the access id/secret
  189. awsIntegration, err := app.Repo.AWSIntegration.ReadAWSIntegration(uint(awsIntegrationID))
  190. if err != nil {
  191. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  192. return
  193. }
  194. awsIntegration.AWSAccessKeyID = []byte(form.AWSAccessKeyID)
  195. awsIntegration.AWSSecretAccessKey = []byte(form.AWSSecretAccessKey)
  196. // handle write to the database
  197. awsIntegration, err = app.Repo.AWSIntegration.OverwriteAWSIntegration(awsIntegration)
  198. if err != nil {
  199. app.handleErrorDataWrite(err, w)
  200. return
  201. }
  202. // clear the cluster token cache if cluster_id exists
  203. vals, err := url.ParseQuery(r.URL.RawQuery)
  204. if err != nil {
  205. app.handleErrorDataWrite(err, w)
  206. return
  207. }
  208. if len(vals["cluster_id"]) > 0 {
  209. clusterID, err := strconv.ParseUint(vals["cluster_id"][0], 10, 64)
  210. if err != nil {
  211. app.handleErrorDataWrite(err, w)
  212. return
  213. }
  214. cluster, err := app.Repo.Cluster.ReadCluster(uint(clusterID))
  215. // clear the token
  216. cluster.TokenCache.Token = []byte("")
  217. cluster, err = app.Repo.Cluster.UpdateClusterTokenCache(&cluster.TokenCache)
  218. if err != nil {
  219. app.handleErrorDataWrite(err, w)
  220. return
  221. }
  222. }
  223. app.Logger.Info().Msgf("AWS integration overwritten: %d", awsIntegration.ID)
  224. w.WriteHeader(http.StatusCreated)
  225. awsExt := awsIntegration.Externalize()
  226. if err := json.NewEncoder(w).Encode(awsExt); err != nil {
  227. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  228. return
  229. }
  230. }
  231. // HandleCreateBasicAuthIntegration creates a new basic auth integration in the DB
  232. func (app *App) HandleCreateBasicAuthIntegration(w http.ResponseWriter, r *http.Request) {
  233. userID, err := app.getUserIDFromRequest(r)
  234. if err != nil {
  235. http.Error(w, err.Error(), http.StatusInternalServerError)
  236. return
  237. }
  238. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  239. if err != nil || projID == 0 {
  240. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  241. return
  242. }
  243. form := &forms.CreateBasicAuthIntegrationForm{
  244. UserID: userID,
  245. ProjectID: uint(projID),
  246. }
  247. // decode from JSON to form value
  248. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  249. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  250. return
  251. }
  252. // validate the form
  253. if err := app.validator.Struct(form); err != nil {
  254. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  255. return
  256. }
  257. // convert the form to a gcp integration
  258. basic, err := form.ToBasicIntegration()
  259. if err != nil {
  260. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  261. return
  262. }
  263. // handle write to the database
  264. basic, err = app.Repo.BasicIntegration.CreateBasicIntegration(basic)
  265. if err != nil {
  266. app.handleErrorDataWrite(err, w)
  267. return
  268. }
  269. app.Logger.Info().Msgf("New basic integration created: %d", basic.ID)
  270. w.WriteHeader(http.StatusCreated)
  271. basicExt := basic.Externalize()
  272. if err := json.NewEncoder(w).Encode(basicExt); err != nil {
  273. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  274. return
  275. }
  276. }
  277. // HandleListProjectOAuthIntegrations lists the oauth integrations for the project
  278. func (app *App) HandleListProjectOAuthIntegrations(w http.ResponseWriter, r *http.Request) {
  279. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  280. if err != nil || projID == 0 {
  281. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  282. return
  283. }
  284. oauthInts, err := app.Repo.OAuthIntegration.ListOAuthIntegrationsByProjectID(uint(projID))
  285. if err != nil {
  286. app.handleErrorDataRead(err, w)
  287. return
  288. }
  289. res := make([]*integrations.OAuthIntegrationExternal, 0)
  290. for _, oauthInt := range oauthInts {
  291. res = append(res, oauthInt.Externalize())
  292. }
  293. w.WriteHeader(http.StatusOK)
  294. if err := json.NewEncoder(w).Encode(res); err != nil {
  295. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  296. return
  297. }
  298. }
  299. // verifySignature verifies a signature based on hmac protocal
  300. // https://docs.github.com/en/developers/webhooks-and-events/webhooks/securing-your-webhooks
  301. func verifySignature(secret []byte, signature string, body []byte) bool {
  302. if len(signature) != 71 || !strings.HasPrefix(signature, "sha256=") {
  303. return false
  304. }
  305. actual := make([]byte, 32)
  306. hex.Decode(actual, []byte(signature[7:]))
  307. computed := hmac.New(sha256.New, secret)
  308. computed.Write(body)
  309. return hmac.Equal(computed.Sum(nil), actual)
  310. }
  311. func (app *App) HandleGithubAppEvent(w http.ResponseWriter, r *http.Request) {
  312. payload, err := ioutil.ReadAll(r.Body)
  313. if err != nil {
  314. app.handleErrorInternal(err, w)
  315. return
  316. }
  317. // verify webhook secret
  318. signature := r.Header.Get("X-Hub-Signature-256")
  319. if !verifySignature([]byte(app.GithubAppConf.WebhookSecret), signature, payload) {
  320. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  321. return
  322. }
  323. event, err := github.ParseWebHook(github.WebHookType(r), payload)
  324. if err != nil {
  325. app.handleErrorInternal(err, w)
  326. return
  327. }
  328. switch e := event.(type) {
  329. case *github.InstallationEvent:
  330. if *e.Action == "created" {
  331. _, err := app.Repo.GithubAppInstallation.ReadGithubAppInstallationByAccountID(*e.Installation.Account.ID)
  332. if err != nil && err == gorm.ErrRecordNotFound {
  333. // insert account/installation pair into database
  334. _, err := app.Repo.GithubAppInstallation.CreateGithubAppInstallation(&ints.GithubAppInstallation{
  335. AccountID: *e.Installation.Account.ID,
  336. InstallationID: *e.Installation.ID,
  337. })
  338. if err != nil {
  339. app.handleErrorInternal(err, w)
  340. }
  341. return
  342. } else if err != nil {
  343. app.handleErrorInternal(err, w)
  344. return
  345. }
  346. }
  347. if *e.Action == "deleted" {
  348. err := app.Repo.GithubAppInstallation.DeleteGithubAppInstallationByAccountID(*e.Installation.Account.ID)
  349. if err != nil {
  350. app.handleErrorInternal(err, w)
  351. return
  352. }
  353. }
  354. }
  355. }
  356. // HandleGithubAppAuthorize starts the oauth2 flow for a project repo request.
  357. func (app *App) HandleGithubAppAuthorize(w http.ResponseWriter, r *http.Request) {
  358. state := oauth.CreateRandomState()
  359. err := app.populateOAuthSession(w, r, state, false)
  360. if err != nil {
  361. app.handleErrorDataRead(err, w)
  362. return
  363. }
  364. // specify access type offline to get a refresh token
  365. url := app.GithubAppConf.AuthCodeURL(state, oauth2.AccessTypeOffline)
  366. http.Redirect(w, r, url, 302)
  367. }
  368. // HandleGithubAppOauthInit redirects the user to the Porter github app authorization page
  369. func (app *App) HandleGithubAppOauthInit(w http.ResponseWriter, r *http.Request) {
  370. userID, _ := app.getUserIDFromRequest(r)
  371. app.AnalyticsClient.Track(analytics.GithubConnectionStartTrack(
  372. &analytics.GithubConnectionStartTrackOpts{
  373. UserScopedTrackOpts: analytics.GetUserScopedTrackOpts(userID),
  374. },
  375. ))
  376. http.Redirect(w, r, app.GithubAppConf.AuthCodeURL("", oauth2.AccessTypeOffline), 302)
  377. }
  378. // HandleGithubAppInstall redirects the user to the Porter github app installation page
  379. func (app *App) HandleGithubAppInstall(w http.ResponseWriter, r *http.Request) {
  380. http.Redirect(w, r, fmt.Sprintf("https://github.com/apps/%s/installations/new", app.GithubAppConf.AppName), 302)
  381. }
  382. // HandleListGithubAppAccessResp is the response returned by HandleListGithubAppAccess
  383. type HandleListGithubAppAccessResp struct {
  384. HasAccess bool `json:"has_access"`
  385. LoginName string `json:"username,omitempty"`
  386. Accounts []string `json:"accounts,omitempty"`
  387. }
  388. // HandleListGithubAppAccess provides basic info on if the current user is authenticated through the GitHub app
  389. // and what accounts/organizations their authentication has access to
  390. func (app *App) HandleListGithubAppAccess(w http.ResponseWriter, r *http.Request) {
  391. tok, err := app.getGithubAppOauthTokenFromRequest(r)
  392. if err != nil {
  393. res := HandleListGithubAppAccessResp{
  394. HasAccess: false,
  395. }
  396. json.NewEncoder(w).Encode(res)
  397. return
  398. }
  399. client := github.NewClient(app.GithubProjectConf.Client(oauth2.NoContext, tok))
  400. opts := &github.ListOptions{
  401. PerPage: 100,
  402. Page: 1,
  403. }
  404. res := HandleListGithubAppAccessResp{
  405. HasAccess: true,
  406. }
  407. for {
  408. orgs, pages, err := client.Organizations.List(context.Background(), "", opts)
  409. if err != nil {
  410. res := HandleListGithubAppAccessResp{
  411. HasAccess: false,
  412. }
  413. json.NewEncoder(w).Encode(res)
  414. return
  415. }
  416. for _, org := range orgs {
  417. res.Accounts = append(res.Accounts, *org.Login)
  418. }
  419. if pages.NextPage == 0 {
  420. break
  421. }
  422. }
  423. AuthUser, _, err := client.Users.Get(context.Background(), "")
  424. if err != nil {
  425. app.handleErrorInternal(err, w)
  426. return
  427. }
  428. res.LoginName = *AuthUser.Login
  429. // check if user has app installed in their account
  430. Installation, err := app.Repo.GithubAppInstallation.ReadGithubAppInstallationByAccountID(*AuthUser.ID)
  431. if err != nil && err != gorm.ErrRecordNotFound {
  432. app.handleErrorInternal(err, w)
  433. return
  434. }
  435. if Installation != nil {
  436. res.Accounts = append(res.Accounts, *AuthUser.Login)
  437. }
  438. sort.Strings(res.Accounts)
  439. json.NewEncoder(w).Encode(res)
  440. }
  441. // getGithubAppOauthTokenFromRequest gets the oauth token from the request based on the currently
  442. // logged in user. Note that this authenticates as the user, rather than the installation.
  443. func (app *App) getGithubAppOauthTokenFromRequest(r *http.Request) (*oauth2.Token, error) {
  444. userID, err := app.getUserIDFromRequest(r)
  445. if err != nil {
  446. return nil, err
  447. }
  448. user, err := app.Repo.User.ReadUser(userID)
  449. if err != nil {
  450. return nil, err
  451. }
  452. oauthInt, err := app.Repo.GithubAppOAuthIntegration.ReadGithubAppOauthIntegration(user.GithubAppIntegrationID)
  453. if err != nil {
  454. return nil, fmt.Errorf("Could not get GH app integration for user %d: %s", user.ID, err.Error())
  455. }
  456. _, _, err = oauth.GetAccessToken(oauthInt.SharedOAuthModel,
  457. &app.GithubAppConf.Config,
  458. oauth.MakeUpdateGithubAppOauthIntegrationFunction(oauthInt, *app.Repo))
  459. if err != nil {
  460. // try again, in case the token got updated
  461. oauthInt2, err := app.Repo.GithubAppOAuthIntegration.ReadGithubAppOauthIntegration(user.GithubAppIntegrationID)
  462. if err != nil {
  463. return nil, err
  464. }
  465. if oauthInt2.Expiry == oauthInt.Expiry {
  466. return nil, err
  467. } else {
  468. oauthInt.AccessToken = oauthInt2.AccessToken
  469. oauthInt.RefreshToken = oauthInt2.RefreshToken
  470. oauthInt.Expiry = oauthInt2.Expiry
  471. }
  472. }
  473. return &oauth2.Token{
  474. AccessToken: string(oauthInt.AccessToken),
  475. RefreshToken: string(oauthInt.RefreshToken),
  476. Expiry: oauthInt.Expiry,
  477. TokenType: "Bearer",
  478. }, nil
  479. }