base.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. package router
  2. import (
  3. "fmt"
  4. "github.com/go-chi/chi/v5"
  5. "github.com/porter-dev/porter/api/server/handlers/credentials"
  6. "github.com/porter-dev/porter/api/server/handlers/gitinstallation"
  7. "github.com/porter-dev/porter/api/server/handlers/healthcheck"
  8. "github.com/porter-dev/porter/api/server/handlers/metadata"
  9. "github.com/porter-dev/porter/api/server/handlers/release"
  10. "github.com/porter-dev/porter/api/server/handlers/user"
  11. "github.com/porter-dev/porter/api/server/handlers/webhook"
  12. "github.com/porter-dev/porter/api/server/shared"
  13. "github.com/porter-dev/porter/api/server/shared/config"
  14. "github.com/porter-dev/porter/api/server/shared/router"
  15. "github.com/porter-dev/porter/api/types"
  16. )
  17. func NewBaseRegisterer(children ...*router.Registerer) *router.Registerer {
  18. return &router.Registerer{
  19. GetRoutes: GetBaseRoutes,
  20. Children: children,
  21. }
  22. }
  23. func GetBaseRoutes(
  24. r chi.Router,
  25. config *config.Config,
  26. basePath *types.Path,
  27. factory shared.APIEndpointFactory,
  28. children ...*router.Registerer,
  29. ) []*router.Route {
  30. routes := make([]*router.Route, 0)
  31. // GET /api/readyz -> healthcheck.NewReadyzHandler
  32. getReadyzEndpoint := factory.NewAPIEndpoint(
  33. &types.APIRequestMetadata{
  34. Verb: types.APIVerbGet,
  35. Method: types.HTTPVerbGet,
  36. Path: &types.Path{
  37. Parent: basePath,
  38. RelativePath: "/readyz",
  39. },
  40. Quiet: true,
  41. },
  42. )
  43. getReadyzHandler := healthcheck.NewReadyzHandler(
  44. config,
  45. factory.GetResultWriter(),
  46. )
  47. routes = append(routes, &router.Route{
  48. Endpoint: getReadyzEndpoint,
  49. Handler: getReadyzHandler,
  50. Router: r,
  51. })
  52. // GET /api/livez -> healthcheck.NewLivezHandler
  53. getLivezEndpoint := factory.NewAPIEndpoint(
  54. &types.APIRequestMetadata{
  55. Verb: types.APIVerbGet,
  56. Method: types.HTTPVerbGet,
  57. Path: &types.Path{
  58. Parent: basePath,
  59. RelativePath: "/livez",
  60. },
  61. Quiet: true,
  62. },
  63. )
  64. getLivezHandler := healthcheck.NewLivezHandler(
  65. config,
  66. factory.GetResultWriter(),
  67. )
  68. routes = append(routes, &router.Route{
  69. Endpoint: getLivezEndpoint,
  70. Handler: getLivezHandler,
  71. Router: r,
  72. })
  73. // GET /api/metadata -> metadata.NewMetadataGetHandler
  74. getMetadataEndpoint := factory.NewAPIEndpoint(
  75. &types.APIRequestMetadata{
  76. Verb: types.APIVerbGet,
  77. Method: types.HTTPVerbGet,
  78. Path: &types.Path{
  79. Parent: basePath,
  80. RelativePath: "/metadata",
  81. },
  82. },
  83. )
  84. getMetadataHandler := metadata.NewMetadataGetHandler(
  85. config,
  86. factory.GetResultWriter(),
  87. )
  88. routes = append(routes, &router.Route{
  89. Endpoint: getMetadataEndpoint,
  90. Handler: getMetadataHandler,
  91. Router: r,
  92. })
  93. // GET /api/integrations/cluster -> metadata.NewListClusterIntegrationsHandler
  94. listClusterIntsEndpoint := factory.NewAPIEndpoint(
  95. &types.APIRequestMetadata{
  96. Verb: types.APIVerbGet,
  97. Method: types.HTTPVerbGet,
  98. Path: &types.Path{
  99. Parent: basePath,
  100. RelativePath: "/integrations/cluster",
  101. },
  102. },
  103. )
  104. listClusterIntsHandler := metadata.NewListClusterIntegrationsHandler(
  105. config,
  106. factory.GetResultWriter(),
  107. )
  108. routes = append(routes, &router.Route{
  109. Endpoint: listClusterIntsEndpoint,
  110. Handler: listClusterIntsHandler,
  111. Router: r,
  112. })
  113. // GET /api/integrations/registry -> metadata.NewListRegistryIntegrationsHandler
  114. listRegistryIntsEndpoint := factory.NewAPIEndpoint(
  115. &types.APIRequestMetadata{
  116. Verb: types.APIVerbGet,
  117. Method: types.HTTPVerbGet,
  118. Path: &types.Path{
  119. Parent: basePath,
  120. RelativePath: "/integrations/registry",
  121. },
  122. },
  123. )
  124. listRegistryIntsHandler := metadata.NewListRegistryIntegrationsHandler(
  125. config,
  126. factory.GetResultWriter(),
  127. )
  128. routes = append(routes, &router.Route{
  129. Endpoint: listRegistryIntsEndpoint,
  130. Handler: listRegistryIntsHandler,
  131. Router: r,
  132. })
  133. // GET /api/integrations/helm -> metadata.NewListHelmRepoIntegrationsHandler
  134. listHelmRepoIntsEndpoint := factory.NewAPIEndpoint(
  135. &types.APIRequestMetadata{
  136. Verb: types.APIVerbGet,
  137. Method: types.HTTPVerbGet,
  138. Path: &types.Path{
  139. Parent: basePath,
  140. RelativePath: "/integrations/helm",
  141. },
  142. },
  143. )
  144. listHelmRepoIntsHandler := metadata.NewListHelmRepoIntegrationsHandler(
  145. config,
  146. factory.GetResultWriter(),
  147. )
  148. routes = append(routes, &router.Route{
  149. Endpoint: listHelmRepoIntsEndpoint,
  150. Handler: listHelmRepoIntsHandler,
  151. Router: r,
  152. })
  153. // POST /api/users -> user.NewUserCreateHandler
  154. createUserEndpoint := factory.NewAPIEndpoint(
  155. &types.APIRequestMetadata{
  156. Verb: types.APIVerbCreate,
  157. Method: types.HTTPVerbPost,
  158. Path: &types.Path{
  159. Parent: basePath,
  160. RelativePath: "/users",
  161. },
  162. },
  163. )
  164. createUserHandler := user.NewUserCreateHandler(
  165. config,
  166. factory.GetDecoderValidator(),
  167. factory.GetResultWriter(),
  168. )
  169. routes = append(routes, &router.Route{
  170. Endpoint: createUserEndpoint,
  171. Handler: createUserHandler,
  172. Router: r,
  173. })
  174. // POST /api/users/ory -> user.NewOryUserCreateHandler
  175. createOryUserEndpoint := factory.NewAPIEndpoint(
  176. &types.APIRequestMetadata{
  177. Verb: types.APIVerbUpdate,
  178. Method: types.HTTPVerbPost,
  179. Path: &types.Path{
  180. Parent: basePath,
  181. RelativePath: "/users/ory",
  182. },
  183. },
  184. )
  185. createOryUserHandler := user.NewOryUserCreateHandler(
  186. config,
  187. factory.GetDecoderValidator(),
  188. factory.GetResultWriter(),
  189. )
  190. routes = append(routes, &router.Route{
  191. Endpoint: createOryUserEndpoint,
  192. Handler: createOryUserHandler,
  193. Router: r,
  194. })
  195. // POST /api/login -> user.NewUserLoginHandler
  196. loginUserEndpoint := factory.NewAPIEndpoint(
  197. &types.APIRequestMetadata{
  198. Verb: types.APIVerbUpdate,
  199. Method: types.HTTPVerbPost,
  200. Path: &types.Path{
  201. Parent: basePath,
  202. RelativePath: "/login",
  203. },
  204. },
  205. )
  206. loginUserHandler := user.NewUserLoginHandler(
  207. config,
  208. factory.GetDecoderValidator(),
  209. factory.GetResultWriter(),
  210. )
  211. routes = append(routes, &router.Route{
  212. Endpoint: loginUserEndpoint,
  213. Handler: loginUserHandler,
  214. Router: r,
  215. })
  216. // POST /api/cli/login/exchange -> user.NewCLILoginExchangeHandler
  217. cliLoginExchangeEndpoint := factory.NewAPIEndpoint(
  218. &types.APIRequestMetadata{
  219. Verb: types.APIVerbCreate,
  220. Method: types.HTTPVerbPost,
  221. Path: &types.Path{
  222. Parent: basePath,
  223. RelativePath: "/cli/login/exchange",
  224. },
  225. },
  226. )
  227. cliLoginExchangeHandler := user.NewCLILoginExchangeHandler(
  228. config,
  229. factory.GetDecoderValidator(),
  230. factory.GetResultWriter(),
  231. )
  232. routes = append(routes, &router.Route{
  233. Endpoint: cliLoginExchangeEndpoint,
  234. Handler: cliLoginExchangeHandler,
  235. Router: r,
  236. })
  237. // POST /api/password/reset/initiate -> user.NewUserPasswordInitiateResetHandler
  238. passwordInitiateResetEndpoint := factory.NewAPIEndpoint(
  239. &types.APIRequestMetadata{
  240. Verb: types.APIVerbCreate,
  241. Method: types.HTTPVerbPost,
  242. Path: &types.Path{
  243. Parent: basePath,
  244. RelativePath: "/password/reset/initiate",
  245. },
  246. },
  247. )
  248. passwordInitiateResetHandler := user.NewUserPasswordInitiateResetHandler(
  249. config,
  250. factory.GetDecoderValidator(),
  251. factory.GetResultWriter(),
  252. )
  253. routes = append(routes, &router.Route{
  254. Endpoint: passwordInitiateResetEndpoint,
  255. Handler: passwordInitiateResetHandler,
  256. Router: r,
  257. })
  258. // POST /api/password/reset/verify -> user.NewUserPasswordVerifyResetHandler
  259. passwordVerifyResetEndpoint := factory.NewAPIEndpoint(
  260. &types.APIRequestMetadata{
  261. Verb: types.APIVerbCreate,
  262. Method: types.HTTPVerbPost,
  263. Path: &types.Path{
  264. Parent: basePath,
  265. RelativePath: "/password/reset/verify",
  266. },
  267. },
  268. )
  269. passwordVerifyResetHandler := user.NewUserPasswordVerifyResetHandler(
  270. config,
  271. factory.GetDecoderValidator(),
  272. factory.GetResultWriter(),
  273. )
  274. routes = append(routes, &router.Route{
  275. Endpoint: passwordVerifyResetEndpoint,
  276. Handler: passwordVerifyResetHandler,
  277. Router: r,
  278. })
  279. // POST /api/password/reset/finalize -> user.NewUserPasswordFinalizeResetHandler
  280. passwordFinalizeResetEndpoint := factory.NewAPIEndpoint(
  281. &types.APIRequestMetadata{
  282. Verb: types.APIVerbCreate,
  283. Method: types.HTTPVerbPost,
  284. Path: &types.Path{
  285. Parent: basePath,
  286. RelativePath: "/password/reset/finalize",
  287. },
  288. },
  289. )
  290. passwordFinalizeResetHandler := user.NewUserPasswordFinalizeResetHandler(
  291. config,
  292. factory.GetDecoderValidator(),
  293. factory.GetResultWriter(),
  294. )
  295. routes = append(routes, &router.Route{
  296. Endpoint: passwordFinalizeResetEndpoint,
  297. Handler: passwordFinalizeResetHandler,
  298. Router: r,
  299. })
  300. // POST /api/webhooks/deploy/{token} -> release.NewWebhookHandler
  301. webhookEndpoint := factory.NewAPIEndpoint(
  302. &types.APIRequestMetadata{
  303. Verb: types.APIVerbUpdate,
  304. Method: types.HTTPVerbPost,
  305. Path: &types.Path{
  306. Parent: basePath,
  307. RelativePath: "/webhooks/deploy/{token}",
  308. },
  309. Scopes: []types.PermissionScope{},
  310. },
  311. )
  312. webhookHandler := release.NewWebhookHandler(
  313. config,
  314. factory.GetDecoderValidator(),
  315. factory.GetResultWriter(),
  316. )
  317. routes = append(routes, &router.Route{
  318. Endpoint: webhookEndpoint,
  319. Handler: webhookHandler,
  320. Router: r,
  321. })
  322. // GET /api/integrations/github-app/install
  323. githubAppInstallEndpoint := factory.NewAPIEndpoint(
  324. &types.APIRequestMetadata{
  325. Verb: types.APIVerbGet,
  326. Method: types.HTTPVerbGet,
  327. Path: &types.Path{
  328. Parent: basePath,
  329. RelativePath: "/integrations/github-app/install",
  330. },
  331. Scopes: []types.PermissionScope{},
  332. },
  333. )
  334. githubAppInstallHandler := gitinstallation.NewGithubAppInstallHandler(
  335. config,
  336. )
  337. routes = append(routes, &router.Route{
  338. Endpoint: githubAppInstallEndpoint,
  339. Handler: githubAppInstallHandler,
  340. Router: r,
  341. })
  342. // POST /api/integrations/github-app/webhook
  343. githubAppWebhookEndpoint := factory.NewAPIEndpoint(
  344. &types.APIRequestMetadata{
  345. Verb: types.APIVerbCreate,
  346. Method: types.HTTPVerbPost,
  347. Path: &types.Path{
  348. Parent: basePath,
  349. RelativePath: "/integrations/github-app/webhook",
  350. },
  351. Scopes: []types.PermissionScope{},
  352. },
  353. )
  354. githubAppWebhookHandler := gitinstallation.NewGithubAppWebhookHandler(
  355. config,
  356. factory.GetDecoderValidator(),
  357. factory.GetResultWriter(),
  358. )
  359. routes = append(routes, &router.Route{
  360. Endpoint: githubAppWebhookEndpoint,
  361. Handler: githubAppWebhookHandler,
  362. Router: r,
  363. })
  364. // GET /api/oauth/login/github
  365. githubLoginStartEndpoint := factory.NewAPIEndpoint(
  366. &types.APIRequestMetadata{
  367. Verb: types.APIVerbGet,
  368. Method: types.HTTPVerbGet,
  369. Path: &types.Path{
  370. Parent: basePath,
  371. RelativePath: "/oauth/login/github",
  372. },
  373. Scopes: []types.PermissionScope{},
  374. },
  375. )
  376. githubLoginStartHandler := user.NewUserOAuthGithubHandler(
  377. config,
  378. factory.GetDecoderValidator(),
  379. factory.GetResultWriter(),
  380. )
  381. routes = append(routes, &router.Route{
  382. Endpoint: githubLoginStartEndpoint,
  383. Handler: githubLoginStartHandler,
  384. Router: r,
  385. })
  386. // GET /api/oauth/github/callback
  387. githubLoginCallbackEndpoint := factory.NewAPIEndpoint(
  388. &types.APIRequestMetadata{
  389. Verb: types.APIVerbGet,
  390. Method: types.HTTPVerbGet,
  391. Path: &types.Path{
  392. Parent: basePath,
  393. RelativePath: "/oauth/github/callback",
  394. },
  395. Scopes: []types.PermissionScope{},
  396. },
  397. )
  398. githubLoginCallbackHandler := user.NewUserOAuthGithubCallbackHandler(
  399. config,
  400. factory.GetDecoderValidator(),
  401. factory.GetResultWriter(),
  402. )
  403. routes = append(routes, &router.Route{
  404. Endpoint: githubLoginCallbackEndpoint,
  405. Handler: githubLoginCallbackHandler,
  406. Router: r,
  407. })
  408. // GET /api/oauth/login/google
  409. googleLoginStartEndpoint := factory.NewAPIEndpoint(
  410. &types.APIRequestMetadata{
  411. Verb: types.APIVerbGet,
  412. Method: types.HTTPVerbGet,
  413. Path: &types.Path{
  414. Parent: basePath,
  415. RelativePath: "/oauth/login/google",
  416. },
  417. Scopes: []types.PermissionScope{},
  418. },
  419. )
  420. googleLoginStartHandler := user.NewUserOAuthGoogleHandler(
  421. config,
  422. factory.GetDecoderValidator(),
  423. factory.GetResultWriter(),
  424. )
  425. routes = append(routes, &router.Route{
  426. Endpoint: googleLoginStartEndpoint,
  427. Handler: googleLoginStartHandler,
  428. Router: r,
  429. })
  430. // GET /api/oauth/google/callback
  431. googleLoginCallbackEndpoint := factory.NewAPIEndpoint(
  432. &types.APIRequestMetadata{
  433. Verb: types.APIVerbGet,
  434. Method: types.HTTPVerbGet,
  435. Path: &types.Path{
  436. Parent: basePath,
  437. RelativePath: "/oauth/google/callback",
  438. },
  439. Scopes: []types.PermissionScope{},
  440. },
  441. )
  442. googleLoginCallbackHandler := user.NewUserOAuthGoogleCallbackHandler(
  443. config,
  444. factory.GetDecoderValidator(),
  445. factory.GetResultWriter(),
  446. )
  447. routes = append(routes, &router.Route{
  448. Endpoint: googleLoginCallbackEndpoint,
  449. Handler: googleLoginCallbackHandler,
  450. Router: r,
  451. })
  452. // GET /api/internal/credentials
  453. getCredentialsEndpoint := factory.NewAPIEndpoint(
  454. &types.APIRequestMetadata{
  455. Verb: types.APIVerbGet,
  456. Method: types.HTTPVerbGet,
  457. Path: &types.Path{
  458. Parent: basePath,
  459. RelativePath: "/internal/credentials",
  460. },
  461. Scopes: []types.PermissionScope{},
  462. },
  463. )
  464. getCredentialsHandler := credentials.NewGetCredentialsHandler(
  465. config,
  466. factory.GetDecoderValidator(),
  467. factory.GetResultWriter(),
  468. )
  469. routes = append(routes, &router.Route{
  470. Endpoint: getCredentialsEndpoint,
  471. Handler: getCredentialsHandler,
  472. Router: r,
  473. })
  474. if config.ServerConf.GithubIncomingWebhookSecret != "" {
  475. // POST /api/github/incoming_webhook/{webhook_id} -> webhook.NewGithubIncomingWebhook
  476. githubIncomingWebhookEndpoint := factory.NewAPIEndpoint(
  477. &types.APIRequestMetadata{
  478. Verb: types.APIVerbCreate,
  479. Method: types.HTTPVerbPost,
  480. Path: &types.Path{
  481. Parent: basePath,
  482. RelativePath: fmt.Sprintf("/github/incoming_webhook/{%s}", types.URLParamIncomingWebhookID),
  483. },
  484. Scopes: []types.PermissionScope{},
  485. },
  486. )
  487. githubIncomingWebhookHandler := webhook.NewGithubIncomingWebhookHandler(
  488. config,
  489. factory.GetDecoderValidator(),
  490. factory.GetResultWriter(),
  491. )
  492. routes = append(routes, &router.Route{
  493. Endpoint: githubIncomingWebhookEndpoint,
  494. Handler: githubIncomingWebhookHandler,
  495. Router: r,
  496. })
  497. // POST /api/webhooks/github/{webhook_id} -> webhook.NewGithubWebhookHandler
  498. githubWebhookEndpoint := factory.NewAPIEndpoint(
  499. &types.APIRequestMetadata{
  500. Verb: types.APIVerbCreate,
  501. Method: types.HTTPVerbPost,
  502. Path: &types.Path{
  503. Parent: basePath,
  504. RelativePath: fmt.Sprintf("/webhooks/github/{%s}", types.URLParamWebhookID),
  505. },
  506. Scopes: []types.PermissionScope{},
  507. },
  508. )
  509. githubWebhookHandler := webhook.NewGithubWebhookHandler(
  510. config,
  511. factory.GetDecoderValidator(),
  512. factory.GetResultWriter(),
  513. )
  514. routes = append(routes, &router.Route{
  515. Endpoint: githubWebhookEndpoint,
  516. Handler: githubWebhookHandler,
  517. Router: r,
  518. })
  519. }
  520. // POST /api/webhooks/prometheusalerts/{project_id}/{cluster_id} -> webhook.NewPrometheusAlertsHandler
  521. prometheusAlertWebhookEndpoint := factory.NewAPIEndpoint(
  522. &types.APIRequestMetadata{
  523. Verb: types.APIVerbCreate,
  524. Method: types.HTTPVerbPost,
  525. Path: &types.Path{
  526. Parent: basePath,
  527. RelativePath: fmt.Sprintf("/webhooks/prometheusalerts/{%s}/{%s}", types.URLParamProjectID, types.URLParamClusterID),
  528. },
  529. Scopes: []types.PermissionScope{},
  530. },
  531. )
  532. prometheusAlertWebhookHandler := webhook.NewPrometheusAlertWebhookHandler(
  533. config,
  534. factory.GetDecoderValidator(),
  535. factory.GetResultWriter(),
  536. )
  537. routes = append(routes, &router.Route{
  538. Endpoint: prometheusAlertWebhookEndpoint,
  539. Handler: prometheusAlertWebhookHandler,
  540. Router: r,
  541. })
  542. return routes
  543. }