Skip to content
Myr

proxy.test.ts

This file

Path

packages/visibility/test/proxy.test.ts

180 lines · 5.4 KiB · TypeScript

Last changed by

Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503

History of this file

Take it

The walk stopped after 200 commits: lines older than 678c8b8 point at it.

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 1–68why, one step back
1import { describe, expect, it } from "@effect/vitest";
2import { EnvItem } from "@repo/protocol/domain/env";3import { EnvKey, LandedSha, ProjectName, RepoPath } from "@repo/protocol/ids";4import { Effect, Option, Schema } from "effect";56import committed from "../../../examples/datekit/.env.schema?raw";7import { EnvSchemaParser } from "../src/env-schema.ts";8import { EnvSchemaParserEnvSpec } from "../src/env-schema/env-spec.ts";9import { envSchemaViewOf } from "../src/env-shape.ts";10import { placeholderFor, proxyMap } from "../src/proxy.ts";1112const item = Schema.decodeUnknownSync(EnvItem);1314const sentryAuthToken = item({15 description:16 "Source-map upload token. Release step only; never enters a check.",17 key: "SENTRY_AUTH_TOKEN",18 proxy: { domains: ["sentry.io"], path: "/api/**" },19 required: "depends",20 sensitive: true,21 tags: ["release"],22});2324const datekit = {25 items: [26 item({27 key: "APP_ENV",28 required: true,29 sensitive: false,30 tags: [],31 type: "enum(development, test, production)",32 }),33 item({34 description: "Error reporting DSN. Public: it ships in builds.",35 key: "SENTRY_DSN",36 required: true,37 sensitive: false,38 tags: [],39 type: "url",40 }),41 sentryAuthToken,42 item({43 description: "Where locale data lives.",44 key: "DATEKIT_LOCALE_DIR",45 required: true,46 sensitive: false,47 tags: [],48 }),49 ],50};5152const sensitive = (key: string, rest: Partial<typeof EnvItem.Encoded>) =>53 item({ key, required: true, sensitive: true, tags: [], ...rest });5455describe("the outbound map", () => {56 it("gives datekit one rule: sentry.io's API, any method, the token behind a placeholder", () => {57 expect(proxyMap(datekit)).toStrictEqual([58 {59 block: false,60 host: "sentry.io",61 key: EnvKey.make("SENTRY_AUTH_TOKEN"),62 method: Option.none(),63 path: Option.some("/api/**"),64 placeholder: "__SENTRY_AUTH_TOKEN_PLACEHOLDER__",65 },66 ]);67 });68

Drop the private build plan's citations from the code, and fail verify if one comes backoscabrielbefore adoptiona29cef6

lines 69–72why, one step back
69 // The parser and the proxy map together: the map above is fed datekit's items
70 // as written by hand; this one reads the committed file through the real71 // parser, so a drift in either half (a lost path, a rule read as blocked)72 // changes the rule.

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 73–84why, one step back
73 it.effect(
74 "maps datekit's committed .env.schema, as the parser reads it, onto that same rule",75 () =>76 Effect.gen(function* () {77 const file = RepoPath.make(".env.schema");78 const parser = yield* EnvSchemaParser;79 const parsed = yield* parser.parse(file, committed);8081 const shown = envSchemaViewOf({82 at: LandedSha.make("b2".repeat(20)),83 file,84 parsed,

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

line 85why, one step back
85 project: ProjectName.make("oscabriel/datekit"),

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 86–180why, one step back
86 });
8788 expect(proxyMap(shown)).toStrictEqual(proxyMap(datekit));89 expect(90 proxyMap(shown).map(({ host, path }) => ({ host, path }))91 ).toStrictEqual([{ host: "sentry.io", path: Option.some("/api/**") }]);92 }).pipe(Effect.provide(EnvSchemaParserEnvSpec))93 );9495 it("gives a blocked item a rule that denies", () => {96 expect(97 proxyMap({98 items: [99 sensitive("ANALYTICS_KEY", {100 proxy: { block: true, domains: ["telemetry.example.com"] },101 }),102 ],103 })104 ).toStrictEqual([105 {106 block: true,107 host: "telemetry.example.com",108 key: EnvKey.make("ANALYTICS_KEY"),109 method: Option.none(),110 path: Option.none(),111 placeholder: "__ANALYTICS_KEY_PLACEHOLDER__",112 },113 ]);114 });115116 it("keeps a glob domain as written, and gives each domain its own rule", () => {117 expect(118 proxyMap({119 items: [120 sensitive("API_TOKEN", {121 proxy: {122 domains: ["*.example.com", "api.example.org"],123 method: "POST",124 },125 }),126 ],127 }).map(({ host, method }) => ({ host, method }))128 ).toStrictEqual([129 { host: "*.example.com", method: Option.some("POST") },130 { host: "api.example.org", method: Option.some("POST") },131 ]);132 });133134 it("ignores @proxy on an item that is not @sensitive: only those hold values", () => {135 expect(136 proxyMap({137 items: [138 item({139 key: "PUBLIC_URL",140 proxy: { domains: ["cdn.example.com"] },141 required: true,142 sensitive: false,143 tags: [],144 }),145 ],146 })147 ).toStrictEqual([]);148 });149});150151describe("a placeholder", () => {152 it("is the item's own @placeholder when it has one", () => {153 expect(154 placeholderFor(155 sensitive("STRIPE_KEY", { placeholder: "sk_test_placeholder" })156 )157 ).toBe("sk_test_placeholder");158 });159160 it("is otherwise made from the key alone, the same every time", () => {161 expect(placeholderFor(sentryAuthToken)).toBe(162 "__SENTRY_AUTH_TOKEN_PLACEHOLDER__"163 );164 expect(placeholderFor(sensitive("DB_PASSWORD", {}))).toBe(165 "__DB_PASSWORD_PLACEHOLDER__"166 );167 });168169 it("is made from the key when the item's @placeholder is empty: the gateway could not find an empty one to swap", () => {170 const blank = sensitive("SENTRY_AUTH_TOKEN", {171 placeholder: "",172 proxy: { domains: ["sentry.io"] },173 });174175 expect(placeholderFor(blank)).toBe("__SENTRY_AUTH_TOKEN_PLACEHOLDER__");176 expect(177 proxyMap({ items: [blank] }).map(({ placeholder }) => placeholder)178 ).toStrictEqual(["__SENTRY_AUTH_TOKEN_PLACEHOLDER__"]);179 });180});