base.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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/login -> user.NewUserLoginHandler
  175. loginUserEndpoint := factory.NewAPIEndpoint(
  176. &types.APIRequestMetadata{
  177. Verb: types.APIVerbUpdate,
  178. Method: types.HTTPVerbPost,
  179. Path: &types.Path{
  180. Parent: basePath,
  181. RelativePath: "/login",
  182. },
  183. },
  184. )
  185. loginUserHandler := user.NewUserLoginHandler(
  186. config,
  187. factory.GetDecoderValidator(),
  188. factory.GetResultWriter(),
  189. )
  190. routes = append(routes, &router.Route{
  191. Endpoint: loginUserEndpoint,
  192. Handler: loginUserHandler,
  193. Router: r,
  194. })
  195. // POST /api/cli/login/exchange -> user.NewCLILoginExchangeHandler
  196. cliLoginExchangeEndpoint := factory.NewAPIEndpoint(
  197. &types.APIRequestMetadata{
  198. Verb: types.APIVerbCreate,
  199. Method: types.HTTPVerbPost,
  200. Path: &types.Path{
  201. Parent: basePath,
  202. RelativePath: "/cli/login/exchange",
  203. },
  204. },
  205. )
  206. cliLoginExchangeHandler := user.NewCLILoginExchangeHandler(
  207. config,
  208. factory.GetDecoderValidator(),
  209. factory.GetResultWriter(),
  210. )
  211. routes = append(routes, &router.Route{
  212. Endpoint: cliLoginExchangeEndpoint,
  213. Handler: cliLoginExchangeHandler,
  214. Router: r,
  215. })
  216. // POST /api/password/reset/initiate -> user.NewUserPasswordInitiateResetHandler
  217. passwordInitiateResetEndpoint := factory.NewAPIEndpoint(
  218. &types.APIRequestMetadata{
  219. Verb: types.APIVerbCreate,
  220. Method: types.HTTPVerbPost,
  221. Path: &types.Path{
  222. Parent: basePath,
  223. RelativePath: "/password/reset/initiate",
  224. },
  225. },
  226. )
  227. passwordInitiateResetHandler := user.NewUserPasswordInitiateResetHandler(
  228. config,
  229. factory.GetDecoderValidator(),
  230. factory.GetResultWriter(),
  231. )
  232. routes = append(routes, &router.Route{
  233. Endpoint: passwordInitiateResetEndpoint,
  234. Handler: passwordInitiateResetHandler,
  235. Router: r,
  236. })
  237. // POST /api/password/reset/verify -> user.NewUserPasswordVerifyResetHandler
  238. passwordVerifyResetEndpoint := factory.NewAPIEndpoint(
  239. &types.APIRequestMetadata{
  240. Verb: types.APIVerbCreate,
  241. Method: types.HTTPVerbPost,
  242. Path: &types.Path{
  243. Parent: basePath,
  244. RelativePath: "/password/reset/verify",
  245. },
  246. },
  247. )
  248. passwordVerifyResetHandler := user.NewUserPasswordVerifyResetHandler(
  249. config,
  250. factory.GetDecoderValidator(),
  251. factory.GetResultWriter(),
  252. )
  253. routes = append(routes, &router.Route{
  254. Endpoint: passwordVerifyResetEndpoint,
  255. Handler: passwordVerifyResetHandler,
  256. Router: r,
  257. })
  258. // POST /api/password/reset/finalize -> user.NewUserPasswordFinalizeResetHandler
  259. passwordFinalizeResetEndpoint := factory.NewAPIEndpoint(
  260. &types.APIRequestMetadata{
  261. Verb: types.APIVerbCreate,
  262. Method: types.HTTPVerbPost,
  263. Path: &types.Path{
  264. Parent: basePath,
  265. RelativePath: "/password/reset/finalize",
  266. },
  267. },
  268. )
  269. passwordFinalizeResetHandler := user.NewUserPasswordFinalizeResetHandler(
  270. config,
  271. factory.GetDecoderValidator(),
  272. factory.GetResultWriter(),
  273. )
  274. routes = append(routes, &router.Route{
  275. Endpoint: passwordFinalizeResetEndpoint,
  276. Handler: passwordFinalizeResetHandler,
  277. Router: r,
  278. })
  279. // POST /api/webhooks/deploy/{token} -> release.NewWebhookHandler
  280. webhookEndpoint := factory.NewAPIEndpoint(
  281. &types.APIRequestMetadata{
  282. Verb: types.APIVerbUpdate,
  283. Method: types.HTTPVerbPost,
  284. Path: &types.Path{
  285. Parent: basePath,
  286. RelativePath: "/webhooks/deploy/{token}",
  287. },
  288. Scopes: []types.PermissionScope{},
  289. },
  290. )
  291. webhookHandler := release.NewWebhookHandler(
  292. config,
  293. factory.GetDecoderValidator(),
  294. factory.GetResultWriter(),
  295. )
  296. routes = append(routes, &router.Route{
  297. Endpoint: webhookEndpoint,
  298. Handler: webhookHandler,
  299. Router: r,
  300. })
  301. // GET /api/integrations/github-app/install
  302. githubAppInstallEndpoint := factory.NewAPIEndpoint(
  303. &types.APIRequestMetadata{
  304. Verb: types.APIVerbGet,
  305. Method: types.HTTPVerbGet,
  306. Path: &types.Path{
  307. Parent: basePath,
  308. RelativePath: "/integrations/github-app/install",
  309. },
  310. Scopes: []types.PermissionScope{},
  311. },
  312. )
  313. githubAppInstallHandler := gitinstallation.NewGithubAppInstallHandler(
  314. config,
  315. )
  316. routes = append(routes, &router.Route{
  317. Endpoint: githubAppInstallEndpoint,
  318. Handler: githubAppInstallHandler,
  319. Router: r,
  320. })
  321. // POST /api/integrations/github-app/webhook
  322. githubAppWebhookEndpoint := factory.NewAPIEndpoint(
  323. &types.APIRequestMetadata{
  324. Verb: types.APIVerbCreate,
  325. Method: types.HTTPVerbPost,
  326. Path: &types.Path{
  327. Parent: basePath,
  328. RelativePath: "/integrations/github-app/webhook",
  329. },
  330. Scopes: []types.PermissionScope{},
  331. },
  332. )
  333. githubAppWebhookHandler := gitinstallation.NewGithubAppWebhookHandler(
  334. config,
  335. factory.GetDecoderValidator(),
  336. factory.GetResultWriter(),
  337. )
  338. routes = append(routes, &router.Route{
  339. Endpoint: githubAppWebhookEndpoint,
  340. Handler: githubAppWebhookHandler,
  341. Router: r,
  342. })
  343. // GET /api/oauth/login/github
  344. githubLoginStartEndpoint := factory.NewAPIEndpoint(
  345. &types.APIRequestMetadata{
  346. Verb: types.APIVerbGet,
  347. Method: types.HTTPVerbGet,
  348. Path: &types.Path{
  349. Parent: basePath,
  350. RelativePath: "/oauth/login/github",
  351. },
  352. Scopes: []types.PermissionScope{},
  353. },
  354. )
  355. githubLoginStartHandler := user.NewUserOAuthGithubHandler(
  356. config,
  357. factory.GetDecoderValidator(),
  358. factory.GetResultWriter(),
  359. )
  360. routes = append(routes, &router.Route{
  361. Endpoint: githubLoginStartEndpoint,
  362. Handler: githubLoginStartHandler,
  363. Router: r,
  364. })
  365. // GET /api/oauth/github/callback
  366. githubLoginCallbackEndpoint := factory.NewAPIEndpoint(
  367. &types.APIRequestMetadata{
  368. Verb: types.APIVerbGet,
  369. Method: types.HTTPVerbGet,
  370. Path: &types.Path{
  371. Parent: basePath,
  372. RelativePath: "/oauth/github/callback",
  373. },
  374. Scopes: []types.PermissionScope{},
  375. },
  376. )
  377. githubLoginCallbackHandler := user.NewUserOAuthGithubCallbackHandler(
  378. config,
  379. factory.GetDecoderValidator(),
  380. factory.GetResultWriter(),
  381. )
  382. routes = append(routes, &router.Route{
  383. Endpoint: githubLoginCallbackEndpoint,
  384. Handler: githubLoginCallbackHandler,
  385. Router: r,
  386. })
  387. // GET /api/oauth/login/google
  388. googleLoginStartEndpoint := factory.NewAPIEndpoint(
  389. &types.APIRequestMetadata{
  390. Verb: types.APIVerbGet,
  391. Method: types.HTTPVerbGet,
  392. Path: &types.Path{
  393. Parent: basePath,
  394. RelativePath: "/oauth/login/google",
  395. },
  396. Scopes: []types.PermissionScope{},
  397. },
  398. )
  399. googleLoginStartHandler := user.NewUserOAuthGoogleHandler(
  400. config,
  401. factory.GetDecoderValidator(),
  402. factory.GetResultWriter(),
  403. )
  404. routes = append(routes, &router.Route{
  405. Endpoint: googleLoginStartEndpoint,
  406. Handler: googleLoginStartHandler,
  407. Router: r,
  408. })
  409. // GET /api/oauth/google/callback
  410. googleLoginCallbackEndpoint := factory.NewAPIEndpoint(
  411. &types.APIRequestMetadata{
  412. Verb: types.APIVerbGet,
  413. Method: types.HTTPVerbGet,
  414. Path: &types.Path{
  415. Parent: basePath,
  416. RelativePath: "/oauth/google/callback",
  417. },
  418. Scopes: []types.PermissionScope{},
  419. },
  420. )
  421. googleLoginCallbackHandler := user.NewUserOAuthGoogleCallbackHandler(
  422. config,
  423. factory.GetDecoderValidator(),
  424. factory.GetResultWriter(),
  425. )
  426. routes = append(routes, &router.Route{
  427. Endpoint: googleLoginCallbackEndpoint,
  428. Handler: googleLoginCallbackHandler,
  429. Router: r,
  430. })
  431. // GET /api/internal/credentials
  432. getCredentialsEndpoint := factory.NewAPIEndpoint(
  433. &types.APIRequestMetadata{
  434. Verb: types.APIVerbGet,
  435. Method: types.HTTPVerbGet,
  436. Path: &types.Path{
  437. Parent: basePath,
  438. RelativePath: "/internal/credentials",
  439. },
  440. Scopes: []types.PermissionScope{},
  441. },
  442. )
  443. getCredentialsHandler := credentials.NewGetCredentialsHandler(
  444. config,
  445. factory.GetDecoderValidator(),
  446. factory.GetResultWriter(),
  447. )
  448. routes = append(routes, &router.Route{
  449. Endpoint: getCredentialsEndpoint,
  450. Handler: getCredentialsHandler,
  451. Router: r,
  452. })
  453. if config.ServerConf.GithubIncomingWebhookSecret != "" {
  454. // POST /api/github/incoming_webhook/{webhook_id} -> webhook.NewGithubIncomingWebhook
  455. githubIncomingWebhookEndpoint := factory.NewAPIEndpoint(
  456. &types.APIRequestMetadata{
  457. Verb: types.APIVerbCreate,
  458. Method: types.HTTPVerbPost,
  459. Path: &types.Path{
  460. Parent: basePath,
  461. RelativePath: fmt.Sprintf("/github/incoming_webhook/{%s}", types.URLParamIncomingWebhookID),
  462. },
  463. Scopes: []types.PermissionScope{},
  464. },
  465. )
  466. githubIncomingWebhookHandler := webhook.NewGithubIncomingWebhookHandler(
  467. config,
  468. factory.GetDecoderValidator(),
  469. factory.GetResultWriter(),
  470. )
  471. routes = append(routes, &router.Route{
  472. Endpoint: githubIncomingWebhookEndpoint,
  473. Handler: githubIncomingWebhookHandler,
  474. Router: r,
  475. })
  476. // POST /api/webhooks/github/{webhook_id} -> webhook.NewGithubWebhookHandler
  477. githubWebhookEndpoint := factory.NewAPIEndpoint(
  478. &types.APIRequestMetadata{
  479. Verb: types.APIVerbCreate,
  480. Method: types.HTTPVerbPost,
  481. Path: &types.Path{
  482. Parent: basePath,
  483. RelativePath: fmt.Sprintf("/webhooks/github/{%s}", types.URLParamWebhookID),
  484. },
  485. Scopes: []types.PermissionScope{},
  486. },
  487. )
  488. githubWebhookHandler := webhook.NewGithubWebhookHandler(
  489. config,
  490. factory.GetDecoderValidator(),
  491. factory.GetResultWriter(),
  492. )
  493. routes = append(routes, &router.Route{
  494. Endpoint: githubWebhookEndpoint,
  495. Handler: githubWebhookHandler,
  496. Router: r,
  497. })
  498. }
  499. // POST /api/webhooks/prometheusalerts/{project_id}/{cluster_id} -> webhook.NewPrometheusAlertsHandler
  500. prometheusAlertWebhookEndpoint := factory.NewAPIEndpoint(
  501. &types.APIRequestMetadata{
  502. Verb: types.APIVerbCreate,
  503. Method: types.HTTPVerbPost,
  504. Path: &types.Path{
  505. Parent: basePath,
  506. RelativePath: fmt.Sprintf("/webhooks/prometheusalerts/{%s}/{%s}", types.URLParamProjectID, types.URLParamClusterID),
  507. },
  508. Scopes: []types.PermissionScope{},
  509. },
  510. )
  511. prometheusAlertWebhookHandler := webhook.NewPrometheusAlertWebhookHandler(
  512. config,
  513. factory.GetDecoderValidator(),
  514. factory.GetResultWriter(),
  515. )
  516. routes = append(routes, &router.Route{
  517. Endpoint: prometheusAlertWebhookEndpoint,
  518. Handler: prometheusAlertWebhookHandler,
  519. Router: r,
  520. })
  521. return routes
  522. }