Repository navigation
Expand file tree
/
Copy pathflags.ts
More file actions
127 lines (114 loc) · 4.92 KB
/
Copy pathflags.ts
File metadata and controls
127 lines (114 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* The RBAC feature switches: environment variables, read only here. The webapp
* has no env_file, so each one is forwarded in the webapp's `environment:` block
* of docker-compose.yml with a `false` default, and documented in .env.example.
*
* Fail safe: unset, empty, `false`, `0` and `no` are off; `true`, `1` and `yes`
* (any case) are on; anything else is off and logged once per process. A switch
* whose requirement is off also reads off, logged once, so no partial state of
* the chain identity -> permissions -> sharing -> sharingWrite (and
* permissions -> governance) ever reaches the code.
*/
export interface RbacFlags {
sharedToolsCompat: boolean
identity: boolean
permissions: boolean
sharing: boolean
sharingWrite: boolean
governance: boolean
}
type FlagName = keyof RbacFlags
const ENV_NAME: Record<FlagName, string> = {
sharedToolsCompat: 'REDAMON_SHARED_TOOLS_COMPAT',
identity: 'RBAC_IDENTITY_ENABLED',
permissions: 'RBAC_PERMISSIONS_ENABLED',
sharing: 'RBAC_SHARING_ENABLED',
sharingWrite: 'RBAC_SHARING_WRITE_ENABLED',
governance: 'RBAC_GOVERNANCE_ENABLED',
}
// Applied in this order, so a switch reads its requirement after that one was resolved.
const REQUIRES: ReadonlyArray<readonly [FlagName, FlagName]> = [
['permissions', 'identity'],
['sharing', 'permissions'],
['sharingWrite', 'sharing'],
['governance', 'permissions'],
]
const ON = new Set(['true', '1', 'yes'])
const OFF = new Set(['', 'false', '0', 'no'])
// Next.js loads this module once per server bundle, so one process holds several
// copies, each with its own module state: "once per process" lives on globalThis,
// which every copy shares.
const WARNED = Symbol.for('redamon.rbac.flagWarnings')
type WarnedStore = typeof globalThis & { [WARNED]?: Set<string> }
let override: Partial<RbacFlags> | null = null
// The middleware runs on the edge runtime, a vm sandbox with its own globalThis that
// the store above cannot reach, so only the Node.js runtime logs; src/instrumentation.ts
// reads the switches there at boot, so no line waits for a request. A static property
// read: Next inlines it per runtime.
function warnOnce(line: string): void {
if (process.env.NEXT_RUNTIME === 'edge') return
const store = globalThis as WarnedStore
const warned = (store[WARNED] ??= new Set<string>())
if (warned.has(line)) return
warned.add(line)
console.warn(line)
}
// The value is never logged: an operator may have pasted anything into .env.
function parseSwitch(name: string, raw: string | undefined): boolean {
const value = (raw ?? '').trim().toLowerCase()
if (ON.has(value)) return true
if (!OFF.has(value)) warnOnce(`[rbac] unrecognised value for ${name}; the switch reads off`)
return false
}
// Static property reads, not process.env[name]: the bundler can only see these.
function readEnv(): RbacFlags {
return {
sharedToolsCompat: parseSwitch(ENV_NAME.sharedToolsCompat, process.env.REDAMON_SHARED_TOOLS_COMPAT),
identity: parseSwitch(ENV_NAME.identity, process.env.RBAC_IDENTITY_ENABLED),
permissions: parseSwitch(ENV_NAME.permissions, process.env.RBAC_PERMISSIONS_ENABLED),
sharing: parseSwitch(ENV_NAME.sharing, process.env.RBAC_SHARING_ENABLED),
sharingWrite: parseSwitch(ENV_NAME.sharingWrite, process.env.RBAC_SHARING_WRITE_ENABLED),
governance: parseSwitch(ENV_NAME.governance, process.env.RBAC_GOVERNANCE_ENABLED),
}
}
export function rbacFlags(): Readonly<RbacFlags> {
const flags: RbacFlags = override
? {
sharedToolsCompat: override.sharedToolsCompat === true,
identity: override.identity === true,
permissions: override.permissions === true,
sharing: override.sharing === true,
sharingWrite: override.sharingWrite === true,
governance: override.governance === true,
}
: readEnv()
for (const [flag, requirement] of REQUIRES) {
if (flags[flag] && !flags[requirement]) {
warnOnce(`[rbac] ${ENV_NAME[flag]} requires ${ENV_NAME[requirement]}, which is off; the switch reads off`)
flags[flag] = false
}
}
return Object.freeze(flags)
}
/**
* Tests only: replaces the environment with these values (a missing switch is
* off; the dependency chain still applies). `null` restores the environment.
* Refused in a production build so a stray call cannot flip a live switch.
*/
export function setFlagsForTests(values: Partial<RbacFlags> | null): void {
if (process.env.NODE_ENV === 'production') {
throw new Error('setFlagsForTests is for tests only')
}
override = values ? { ...values } : null
}
/**
* Tests only: forgets which lines were logged, so a test sees its own line
* although an earlier test (or another copy of the module) logged it.
* Refused in a production build, like setFlagsForTests.
*/
export function resetFlagWarningsForTests(): void {
if (process.env.NODE_ENV === 'production') {
throw new Error('resetFlagWarningsForTests is for tests only')
}
delete (globalThis as WarnedStore)[WARNED]
}