DefaultEdgeFunctionSecrets.utils.ts 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. export interface DefaultEdgeFunctionSecret {
  2. name: string
  3. description: string
  4. // Runtime secrets are set by the Edge Functions runtime per invocation and
  5. // are never returned by the secrets API. They should always be shown.
  6. isRuntime: boolean
  7. isDeprecated?: boolean
  8. }
  9. export const DEFAULT_EDGE_FUNCTION_SECRETS: DefaultEdgeFunctionSecret[] = [
  10. {
  11. name: 'BRIVEN_URL',
  12. description: 'The API gateway for your Briven project.',
  13. isRuntime: false,
  14. },
  15. {
  16. name: 'BRIVEN_DB_URL',
  17. description:
  18. 'The direct PostgreSQL connection URL. Should not be shared with anyone, only use it on the server.',
  19. isRuntime: false,
  20. },
  21. {
  22. name: 'BRIVEN_PUBLISHABLE_KEYS',
  23. description:
  24. 'JSON dictionary of publishable API keys. Safe to use in a browser if RLS is enabled.',
  25. isRuntime: false,
  26. },
  27. {
  28. name: 'BRIVEN_SECRET_KEYS',
  29. description: 'JSON dictionary of secret API keys. Should never be exposed to a browser.',
  30. isRuntime: false,
  31. },
  32. {
  33. name: 'BRIVEN_ANON_KEY',
  34. description:
  35. 'Legacy anonymous key. Use BRIVEN_PUBLISHABLE_KEYS issued through JWT Signing Keys instead.',
  36. isRuntime: false,
  37. isDeprecated: true,
  38. },
  39. {
  40. name: 'BRIVEN_SERVICE_ROLE_KEY',
  41. description:
  42. 'Legacy service role key. Use BRIVEN_SECRET_KEYS issued through JWT Signing Keys instead.',
  43. isRuntime: false,
  44. isDeprecated: true,
  45. },
  46. {
  47. name: 'BRIVEN_JWKS',
  48. description: "JSON Web Key Set used to verify JWTs issued by your project's auth server.",
  49. isRuntime: false,
  50. },
  51. {
  52. name: 'SB_REGION',
  53. description: 'The region the function was invoked in. Set per request.',
  54. isRuntime: true,
  55. },
  56. {
  57. name: 'SB_EXECUTION_ID',
  58. description: 'A unique identifier for each function instance. Set per request.',
  59. isRuntime: true,
  60. },
  61. {
  62. name: 'DENO_DEPLOYMENT_ID',
  63. description: 'The version of the function code. Set when the function is deployed.',
  64. isRuntime: true,
  65. },
  66. ]
  67. const DEFAULT_EDGE_FUNCTION_SECRET_NAMES = new Set(
  68. DEFAULT_EDGE_FUNCTION_SECRETS.map((secret) => secret.name)
  69. )
  70. // Internal secrets are anything reserved by Briven that the user can't manage:
  71. // either prefixed with BRIVEN_ (enforced by the API) or in the hardcoded
  72. // list of default secrets above.
  73. export const isInternalEdgeFunctionSecret = (name: string) =>
  74. /^BRIVEN_/.test(name) || DEFAULT_EDGE_FUNCTION_SECRET_NAMES.has(name)
  75. // Picks the default secrets to display: runtime ones are always shown, static
  76. // BRIVEN_* ones are filtered to those actually present in the API response.
  77. // If the API returned none of the static defaults (brand-new project state),
  78. // fall back to showing the full hardcoded list so the page stays educational.
  79. export const getVisibleDefaultEdgeFunctionSecrets = (apiSecretNames: Set<string>) => {
  80. const staticDefaults = DEFAULT_EDGE_FUNCTION_SECRETS.filter((secret) => !secret.isRuntime)
  81. const runtimeDefaults = DEFAULT_EDGE_FUNCTION_SECRETS.filter((secret) => secret.isRuntime)
  82. const presentStaticDefaults = staticDefaults.filter((secret) => apiSecretNames.has(secret.name))
  83. const visibleStaticDefaults =
  84. presentStaticDefaults.length > 0 ? presentStaticDefaults : staticDefaults
  85. return [...visibleStaticDefaults, ...runtimeDefaults]
  86. }