@reventlessdev/reventless-seed 1.0.0-alpha.2 → 1.0.0-alpha.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +149 -0
- package/lib/bs/.compiler.log +2 -2
- package/lib/bs/.sourcedirs.json +1 -0
- package/lib/bs/ReventlessSeed.cmi +0 -0
- package/lib/bs/ReventlessSeed.cmt +0 -0
- package/lib/bs/ReventlessSeed.mlmap +6 -0
- package/lib/bs/compiler-info.json +2 -2
- package/lib/bs/src/Seed-ReventlessSeed.cmi +0 -0
- package/lib/bs/src/Seed-ReventlessSeed.cmt +0 -0
- package/lib/bs/src/Seed.ast +0 -0
- package/lib/bs/src/Seed.res +12 -3
- package/lib/bs/src/Seed.res.mjs +3 -0
- package/lib/bs/src/Seed_Client-ReventlessSeed.cmi +0 -0
- package/lib/bs/src/Seed_Client-ReventlessSeed.cmj +0 -0
- package/lib/bs/src/Seed_Client-ReventlessSeed.cmt +0 -0
- package/lib/bs/src/Seed_Client.ast +0 -0
- package/lib/bs/src/Seed_Client.res +382 -21
- package/lib/bs/src/Seed_Client.res.mjs +332 -24
- package/lib/bs/src/Seed_Connect-ReventlessSeed.cmi +0 -0
- package/lib/bs/src/Seed_Connect-ReventlessSeed.cmj +0 -0
- package/lib/bs/src/Seed_Connect-ReventlessSeed.cmt +0 -0
- package/lib/bs/src/Seed_Connect.ast +0 -0
- package/lib/bs/src/Seed_Connect.res +281 -33
- package/lib/bs/src/Seed_Connect.res.mjs +162 -8
- package/lib/bs/src/Seed_Prompt-ReventlessSeed.cmi +0 -0
- package/lib/bs/src/Seed_Prompt-ReventlessSeed.cmj +0 -0
- package/lib/bs/src/Seed_Prompt-ReventlessSeed.cmt +0 -0
- package/lib/bs/src/Seed_Prompt.ast +0 -0
- package/lib/bs/src/Seed_Prompt.res +140 -41
- package/lib/bs/src/Seed_Prompt.res.mjs +88 -13
- package/lib/bs/src/Seed_Random-ReventlessSeed.cmi +0 -0
- package/lib/bs/src/Seed_Random-ReventlessSeed.cmt +0 -0
- package/lib/bs/src/Seed_Random.ast +0 -0
- package/lib/bs/src/Seed_Random.res +4 -2
- package/lib/bs/src/Seed_Runner-ReventlessSeed.cmi +0 -0
- package/lib/bs/src/Seed_Runner-ReventlessSeed.cmj +0 -0
- package/lib/bs/src/Seed_Runner-ReventlessSeed.cmt +0 -0
- package/lib/bs/src/Seed_Runner.ast +0 -0
- package/lib/bs/src/Seed_Runner.res +45 -19
- package/lib/bs/src/Seed_Runner.res.mjs +19 -1
- package/lib/bs/src/Seed_Types-ReventlessSeed.cmt +0 -0
- package/lib/bs/src/Seed_Upload-ReventlessSeed.cmi +0 -0
- package/lib/bs/src/Seed_Upload-ReventlessSeed.cmj +0 -0
- package/lib/bs/src/Seed_Upload-ReventlessSeed.cmt +0 -0
- package/lib/bs/src/Seed_Upload.ast +0 -0
- package/lib/bs/src/Seed_Upload.res +43 -43
- package/lib/bs/src/Seed_Upload.res.mjs +65 -69
- package/lib/bs/src/Seed_Users-ReventlessSeed.cmi +0 -0
- package/lib/bs/src/Seed_Users-ReventlessSeed.cmj +0 -0
- package/lib/bs/src/Seed_Users-ReventlessSeed.cmt +0 -0
- package/lib/bs/src/Seed_Users.ast +0 -0
- package/lib/bs/src/Seed_Users.res +92 -0
- package/lib/bs/src/Seed_Users.res.mjs +91 -0
- package/lib/bs/tests/Seed_ClientTest-ReventlessSeed.cmi +0 -0
- package/lib/bs/tests/Seed_ClientTest-ReventlessSeed.cmj +0 -0
- package/lib/bs/tests/Seed_ClientTest-ReventlessSeed.cmt +0 -0
- package/lib/bs/tests/Seed_ClientTest.ast +0 -0
- package/lib/bs/tests/Seed_ClientTest.res +287 -0
- package/lib/bs/tests/Seed_ClientTest.res.mjs +268 -0
- package/lib/bs/tests/Seed_ConnectTest-ReventlessSeed.cmi +0 -0
- package/lib/bs/tests/Seed_ConnectTest-ReventlessSeed.cmj +0 -0
- package/lib/bs/tests/Seed_ConnectTest-ReventlessSeed.cmt +0 -0
- package/lib/bs/tests/Seed_ConnectTest.ast +0 -0
- package/lib/bs/tests/Seed_ConnectTest.res +64 -0
- package/lib/bs/tests/Seed_ConnectTest.res.mjs +56 -0
- package/lib/bs/tests/Seed_PromptTest-ReventlessSeed.cmi +0 -0
- package/lib/bs/tests/Seed_PromptTest-ReventlessSeed.cmj +0 -0
- package/lib/bs/tests/Seed_PromptTest-ReventlessSeed.cmt +0 -0
- package/lib/bs/tests/Seed_PromptTest.ast +0 -0
- package/lib/bs/tests/Seed_PromptTest.res +55 -0
- package/lib/bs/tests/Seed_PromptTest.res.mjs +63 -0
- package/lib/bs/tests/Seed_RandomTest-ReventlessSeed.cmi +0 -0
- package/lib/bs/tests/Seed_RandomTest-ReventlessSeed.cmj +0 -0
- package/lib/bs/tests/Seed_RandomTest-ReventlessSeed.cmt +0 -0
- package/lib/bs/tests/Seed_RandomTest.ast +0 -0
- package/lib/bs/tests/Seed_RandomTest.res +200 -0
- package/lib/bs/tests/Seed_RandomTest.res.mjs +208 -0
- package/lib/bs/tests/Seed_UsersTest-ReventlessSeed.cmi +0 -0
- package/lib/bs/tests/Seed_UsersTest-ReventlessSeed.cmj +0 -0
- package/lib/bs/tests/Seed_UsersTest-ReventlessSeed.cmt +0 -0
- package/lib/bs/tests/Seed_UsersTest.ast +0 -0
- package/lib/bs/tests/Seed_UsersTest.res +129 -0
- package/lib/bs/tests/Seed_UsersTest.res.mjs +113 -0
- package/lib/ocaml/.compiler.log +2 -2
- package/lib/ocaml/ReventlessSeed.cmi +0 -0
- package/lib/ocaml/ReventlessSeed.cmt +0 -0
- package/lib/ocaml/ReventlessSeed.mlmap +6 -0
- package/lib/ocaml/Seed-ReventlessSeed.cmi +0 -0
- package/lib/ocaml/Seed-ReventlessSeed.cmt +0 -0
- package/lib/ocaml/Seed.ast +0 -0
- package/lib/ocaml/Seed.res +12 -3
- package/lib/ocaml/Seed_Client-ReventlessSeed.cmi +0 -0
- package/lib/ocaml/Seed_Client-ReventlessSeed.cmj +0 -0
- package/lib/ocaml/Seed_Client-ReventlessSeed.cmt +0 -0
- package/lib/ocaml/Seed_Client.ast +0 -0
- package/lib/ocaml/Seed_Client.res +382 -21
- package/lib/ocaml/Seed_ClientTest-ReventlessSeed.cmi +0 -0
- package/lib/ocaml/Seed_ClientTest-ReventlessSeed.cmj +0 -0
- package/lib/ocaml/Seed_ClientTest-ReventlessSeed.cmt +0 -0
- package/lib/ocaml/Seed_ClientTest.ast +0 -0
- package/lib/ocaml/Seed_ClientTest.res +287 -0
- package/lib/ocaml/Seed_Connect-ReventlessSeed.cmi +0 -0
- package/lib/ocaml/Seed_Connect-ReventlessSeed.cmj +0 -0
- package/lib/ocaml/Seed_Connect-ReventlessSeed.cmt +0 -0
- package/lib/ocaml/Seed_Connect.ast +0 -0
- package/lib/ocaml/Seed_Connect.res +281 -33
- package/lib/ocaml/Seed_ConnectTest-ReventlessSeed.cmi +0 -0
- package/lib/ocaml/Seed_ConnectTest-ReventlessSeed.cmj +0 -0
- package/lib/ocaml/Seed_ConnectTest-ReventlessSeed.cmt +0 -0
- package/lib/ocaml/Seed_ConnectTest.ast +0 -0
- package/lib/ocaml/Seed_ConnectTest.res +64 -0
- package/lib/ocaml/Seed_Prompt-ReventlessSeed.cmi +0 -0
- package/lib/ocaml/Seed_Prompt-ReventlessSeed.cmj +0 -0
- package/lib/ocaml/Seed_Prompt-ReventlessSeed.cmt +0 -0
- package/lib/ocaml/Seed_Prompt.ast +0 -0
- package/lib/ocaml/Seed_Prompt.res +140 -41
- package/lib/ocaml/Seed_PromptTest-ReventlessSeed.cmi +0 -0
- package/lib/ocaml/Seed_PromptTest-ReventlessSeed.cmj +0 -0
- package/lib/ocaml/Seed_PromptTest-ReventlessSeed.cmt +0 -0
- package/lib/ocaml/Seed_PromptTest.ast +0 -0
- package/lib/ocaml/Seed_PromptTest.res +55 -0
- package/lib/ocaml/Seed_Random-ReventlessSeed.cmi +0 -0
- package/lib/ocaml/Seed_Random-ReventlessSeed.cmt +0 -0
- package/lib/ocaml/Seed_Random.ast +0 -0
- package/lib/ocaml/Seed_Random.res +4 -2
- package/lib/ocaml/Seed_RandomTest-ReventlessSeed.cmi +0 -0
- package/lib/ocaml/Seed_RandomTest-ReventlessSeed.cmj +0 -0
- package/lib/ocaml/Seed_RandomTest-ReventlessSeed.cmt +0 -0
- package/lib/ocaml/Seed_RandomTest.ast +0 -0
- package/lib/ocaml/Seed_RandomTest.res +200 -0
- package/lib/ocaml/Seed_Runner-ReventlessSeed.cmi +0 -0
- package/lib/ocaml/Seed_Runner-ReventlessSeed.cmj +0 -0
- package/lib/ocaml/Seed_Runner-ReventlessSeed.cmt +0 -0
- package/lib/ocaml/Seed_Runner.ast +0 -0
- package/lib/ocaml/Seed_Runner.res +45 -19
- package/lib/ocaml/Seed_Types-ReventlessSeed.cmt +0 -0
- package/lib/ocaml/Seed_Upload-ReventlessSeed.cmi +0 -0
- package/lib/ocaml/Seed_Upload-ReventlessSeed.cmj +0 -0
- package/lib/ocaml/Seed_Upload-ReventlessSeed.cmt +0 -0
- package/lib/ocaml/Seed_Upload.ast +0 -0
- package/lib/ocaml/Seed_Upload.res +43 -43
- package/lib/ocaml/Seed_Users-ReventlessSeed.cmi +0 -0
- package/lib/ocaml/Seed_Users-ReventlessSeed.cmj +0 -0
- package/lib/ocaml/Seed_Users-ReventlessSeed.cmt +0 -0
- package/lib/ocaml/Seed_Users.ast +0 -0
- package/lib/ocaml/Seed_Users.res +92 -0
- package/lib/ocaml/Seed_UsersTest-ReventlessSeed.cmi +0 -0
- package/lib/ocaml/Seed_UsersTest-ReventlessSeed.cmj +0 -0
- package/lib/ocaml/Seed_UsersTest-ReventlessSeed.cmt +0 -0
- package/lib/ocaml/Seed_UsersTest.ast +0 -0
- package/lib/ocaml/Seed_UsersTest.res +129 -0
- package/package.json +7 -2
- package/rescript.json +9 -1
- package/src/Seed.res +12 -3
- package/src/Seed.res.mjs +3 -0
- package/src/Seed_Client.res +382 -21
- package/src/Seed_Client.res.mjs +332 -24
- package/src/Seed_Connect.res +281 -33
- package/src/Seed_Connect.res.mjs +162 -8
- package/src/Seed_Prompt.res +140 -41
- package/src/Seed_Prompt.res.mjs +88 -13
- package/src/Seed_Random.res +4 -2
- package/src/Seed_Runner.res +45 -19
- package/src/Seed_Runner.res.mjs +19 -1
- package/src/Seed_Upload.res +43 -43
- package/src/Seed_Upload.res.mjs +65 -69
- package/src/Seed_Users.res +92 -0
- package/src/Seed_Users.res.mjs +91 -0
- package/tests/Seed_ClientTest.res +287 -0
- package/tests/Seed_ClientTest.res.mjs +268 -0
- package/tests/Seed_ConnectTest.res +64 -0
- package/tests/Seed_ConnectTest.res.mjs +56 -0
- package/tests/Seed_PromptTest.res +55 -0
- package/tests/Seed_PromptTest.res.mjs +63 -0
- package/tests/Seed_RandomTest.res +200 -0
- package/tests/Seed_RandomTest.res.mjs +208 -0
- package/tests/Seed_UsersTest.res +129 -0
- package/tests/Seed_UsersTest.res.mjs +113 -0
package/src/Seed_Connect.res
CHANGED
|
@@ -1,40 +1,223 @@
|
|
|
1
1
|
// Turns endpoints plus a login function into a ready `connection`.
|
|
2
2
|
//
|
|
3
3
|
// A connection is what a data set seeds against: an authenticated GraphQL
|
|
4
|
-
// client, the upload
|
|
5
|
-
// a human label for the target. `make` is provider-agnostic — it prompts for
|
|
4
|
+
// client, the deployment's upload endpoints (empty when it serves no uploads),
|
|
5
|
+
// and a human label for the target. `make` is provider-agnostic — it prompts for
|
|
6
6
|
// credentials, calls the supplied `login` to obtain a bearer, and wires it onto
|
|
7
7
|
// the client via `useToken`. Where the bearer comes from (a local `/login`
|
|
8
8
|
// round-trip, a Cognito id token) is the caller's concern.
|
|
9
9
|
|
|
10
10
|
open Seed_Types
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
// ── Active role ───────────────────────────────────────────────────────────────
|
|
13
|
+
//
|
|
14
|
+
// Both platforms narrow a token to the single role the caller last chose, and on
|
|
15
|
+
// Cognito that choice is STORED — so an operator who acted as one role in the
|
|
16
|
+
// host shell gets a narrowed token from the next seed login, days later, with
|
|
17
|
+
// nothing in the seed's own environment to explain it. That is a refusal on the
|
|
18
|
+
// first gated call and no way out of the terminal.
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Applies an active-role choice, if the platform offers one. `None` clears the
|
|
22
|
+
* choice and widens back to the caller's full membership.
|
|
23
|
+
*
|
|
24
|
+
* Returns the fresh bearer when the platform mints one itself, and `None` when
|
|
25
|
+
* the choice is merely stored and a re-authentication is what picks it up. The
|
|
26
|
+
* two platforms genuinely differ here: the local server re-issues on the spot,
|
|
27
|
+
* while Cognito's pre-token-generation trigger reads the stored row on the next
|
|
28
|
+
* login, so nothing changes until the seed authenticates again.
|
|
29
|
+
*
|
|
30
|
+
* Supplied by the platform adapter for the reason `~login` is: this package must
|
|
31
|
+
* not learn which provider signed the token, and the routes are not alike — a
|
|
32
|
+
* GraphQL mutation on one, an HTTP endpoint on the other.
|
|
33
|
+
*/
|
|
34
|
+
type roleSwitch = (~client: Seed_Client.t, ~role: option<string>) => promise<option<string>>
|
|
35
|
+
|
|
36
|
+
/** What a run should act as. `Full` is the default because seeding wants every
|
|
37
|
+
right the account has; `Narrowed` exists so a run can deliberately act as one
|
|
38
|
+
role — the only way to exercise what a restricted caller is refused. */
|
|
39
|
+
type roleChoice = Full | Narrowed(string)
|
|
40
|
+
|
|
41
|
+
// `SEED_ROLE`: a role name, or `full`/`all` to clear. Honoured whether or not the
|
|
42
|
+
// token arrived narrowed, so a deliberate narrowed run needs no TTY.
|
|
43
|
+
let roleFromEnv = (): option<roleChoice> =>
|
|
44
|
+
Seed_Prompt.envValue("SEED_ROLE")->Option.map(v =>
|
|
45
|
+
switch v->String.toLowerCase {
|
|
46
|
+
| "full" | "all" | "none" | "clear" => Full
|
|
47
|
+
| _ => Narrowed(v)
|
|
48
|
+
}
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
// Under route B the client mints uploads through the domain API's `Upload_Presign`
|
|
52
|
+
// mutation (authenticated by the same bearer as commands), so a connection needs no
|
|
53
|
+
// upload endpoint — just the authenticated client and whether uploads are skipped this
|
|
54
|
+
// run (`SEED_SKIP_UPLOADS`). Which store an asset uploads into is the caller's `~store`.
|
|
55
|
+
type connection = {
|
|
56
|
+
client: Seed_Client.t,
|
|
57
|
+
uploadsSkipped: bool,
|
|
58
|
+
label: string,
|
|
59
|
+
// Every account the platform's accounts file declares — `[]` when there was no
|
|
60
|
+
// file, since the `REVENTLESS_DEMO_USER`/`REVENTLESS_DEMO_PASSWORD` path
|
|
61
|
+
// bypasses it entirely. A data set resolves a demo owner through this, so the
|
|
62
|
+
// id it seeds is the id the platform stamps rather than a literal that only
|
|
63
|
+
// happens to match one platform's accounts.
|
|
64
|
+
accounts: array<Seed_Users.user>,
|
|
65
|
+
// The account this run authenticated as, and the id its bearer actually
|
|
66
|
+
// carries — which is the only id available when there is no accounts file.
|
|
67
|
+
caller: Seed_Users.user,
|
|
68
|
+
callerId: option<string>,
|
|
69
|
+
// The login this run authenticated with, kept so a data set can mint a second
|
|
70
|
+
// client for another account. An owner-scoped read is only ever verified by
|
|
71
|
+
// the account the rows belong to: the seeding client is elevated, and an
|
|
72
|
+
// elevated token answers for every owner at once.
|
|
73
|
+
login: (~username: string, ~password: string) => promise<string>,
|
|
74
|
+
// How this platform stores an active-role choice, when it offers one at all.
|
|
75
|
+
// `None` means the deployment has no such door (a unified-mode AWS API does
|
|
76
|
+
// not carry `Platform_SetActiveRole`), and a narrowing can then only be
|
|
77
|
+
// reported, not fixed from here.
|
|
78
|
+
roleSwitch: option<roleSwitch>,
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
let announceIdentity = (client: Seed_Client.t, ~prefix: string="Acting as"): unit =>
|
|
82
|
+
switch Seed_Client.identitySummary(client) {
|
|
83
|
+
| Some(summary) => Console.log(`${prefix}: ${summary}`)
|
|
84
|
+
| None => ()
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Applies a role choice and leaves `client` holding a bearer that reflects it.
|
|
89
|
+
*
|
|
90
|
+
* The re-authentication is not optional bookkeeping: on Cognito the stored row
|
|
91
|
+
* is read at token generation, so a run that set a role and kept its old bearer
|
|
92
|
+
* would carry on with exactly the token it just tried to change.
|
|
93
|
+
*/
|
|
94
|
+
let applyRole = async (
|
|
95
|
+
~client: Seed_Client.t,
|
|
96
|
+
~roleSwitch: roleSwitch,
|
|
97
|
+
~login: (~username: string, ~password: string) => promise<string>,
|
|
98
|
+
~caller: Seed_Users.user,
|
|
99
|
+
~role: option<string>,
|
|
100
|
+
): unit => {
|
|
101
|
+
let minted = await roleSwitch(~client, ~role)
|
|
102
|
+
let token = switch minted {
|
|
103
|
+
| Some(fresh) => fresh
|
|
104
|
+
| None => await login(~username=caller.username, ~password=caller.password)
|
|
105
|
+
}
|
|
106
|
+
client->Seed_Client.useToken(token)
|
|
107
|
+
// A second line rather than a replacement, and worded so the pair reads as a
|
|
108
|
+
// change: the first says what the login gave, this says what the run proceeds
|
|
109
|
+
// under, and a reader needs both to see that the switch actually took.
|
|
110
|
+
client->announceIdentity(~prefix="Now acting as")
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Chooses what the run acts as, when the platform lets it choose.
|
|
115
|
+
*
|
|
116
|
+
* Deliberately quiet on a healthy run: an unnarrowed token has nothing to ask
|
|
117
|
+
* about, so the common case gains no prompt. The menu appears only when the
|
|
118
|
+
* bearer arrived narrowed — the case where the account list says one thing and
|
|
119
|
+
* the token another, which is unreadable from the terminal without it.
|
|
120
|
+
*
|
|
121
|
+
* `SEED_ROLE` answers non-interactively and is honoured even when the token is
|
|
122
|
+
* NOT narrowed, which is what makes a deliberate restricted run possible: acting
|
|
123
|
+
* as one role is the only way to exercise what a restricted caller is refused.
|
|
124
|
+
*
|
|
125
|
+
* Without a TTY and without `SEED_ROLE` it proceeds untouched rather than
|
|
126
|
+
* throwing. A narrowing is not always fatal — narrowed to Admin is fine — so
|
|
127
|
+
* refusing to start would break runs that would have worked.
|
|
128
|
+
*/
|
|
129
|
+
let resolveRole = async (
|
|
130
|
+
~client: Seed_Client.t,
|
|
131
|
+
~roleSwitch: option<roleSwitch>,
|
|
132
|
+
~login: (~username: string, ~password: string) => promise<string>,
|
|
133
|
+
~caller: Seed_Users.user,
|
|
134
|
+
): unit =>
|
|
135
|
+
switch roleSwitch {
|
|
136
|
+
| None => ()
|
|
137
|
+
| Some(roleSwitch) =>
|
|
138
|
+
let narrowedFrom = Seed_Client.narrowedFrom(client)
|
|
139
|
+
let chosen = switch roleFromEnv() {
|
|
140
|
+
| Some(choice) => Some(choice)
|
|
141
|
+
| None =>
|
|
142
|
+
switch narrowedFrom {
|
|
143
|
+
| Some(available) if available->Array.length > 1 && Seed_Prompt.hasTty() =>
|
|
144
|
+
Console.log("")
|
|
145
|
+
Console.log(
|
|
146
|
+
"This token is narrowed to one role. Seeding needs every right the account has;",
|
|
147
|
+
)
|
|
148
|
+
Console.log("clearing also widens your host-shell session, which shares the stored choice.")
|
|
149
|
+
let options = Array.concat(
|
|
150
|
+
[(`full membership (${available->Array.join(", ")})`, Full)],
|
|
151
|
+
available->Array.map(r => (r, Narrowed(r))),
|
|
152
|
+
)
|
|
153
|
+
Some(await Seed_Prompt.select(~title="Act as:", ~options, ~defaultIndex=0))
|
|
154
|
+
| _ => None
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
switch chosen {
|
|
158
|
+
| None => ()
|
|
159
|
+
| Some(Full) => await applyRole(~client, ~roleSwitch, ~login, ~caller, ~role=None)
|
|
160
|
+
| Some(Narrowed(role)) =>
|
|
161
|
+
await applyRole(~client, ~roleSwitch, ~login, ~caller, ~role=Some(role))
|
|
162
|
+
}
|
|
163
|
+
}
|
|
13
164
|
|
|
14
165
|
/**
|
|
15
166
|
* Prompts for credentials, calls `login` to mint a bearer, and returns a
|
|
16
167
|
* connection with that bearer already applied. `login` returns the bearer so a
|
|
17
168
|
* single `useToken` path serves every provider.
|
|
169
|
+
*
|
|
170
|
+
* `roleSwitch` is the platform's active-role door, when it has one; supplying it
|
|
171
|
+
* is what lets a narrowed token be widened here instead of in a browser.
|
|
18
172
|
*/
|
|
19
173
|
let make = async (
|
|
20
174
|
~label: string,
|
|
21
175
|
~endpoint: string,
|
|
22
|
-
~uploadEndpoint: string,
|
|
23
176
|
~login: (~username: string, ~password: string) => promise<string>,
|
|
177
|
+
~roleSwitch: option<roleSwitch>=?,
|
|
24
178
|
~localDefaults: bool=false,
|
|
25
179
|
): connection => {
|
|
26
|
-
// `SEED_SKIP_UPLOADS` forces the
|
|
27
|
-
//
|
|
28
|
-
//
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
let uploadEndpoint = Seed_Prompt.envValue("SEED_SKIP_UPLOADS")->Option.isSome ? "" : uploadEndpoint
|
|
33
|
-
let (username, password) = await Seed_Prompt.credentials(~localDefaults)
|
|
34
|
-
let token = await login(~username, ~password)
|
|
180
|
+
// `SEED_SKIP_UPLOADS` forces the upload phase to no-op — seed domain data fast, or
|
|
181
|
+
// skip a broken/absent upload path without editing the data set. The data set reads
|
|
182
|
+
// `connection.uploadsSkipped` and reports the skip.
|
|
183
|
+
let uploadsSkipped = Seed_Upload.uploadsSkipped()
|
|
184
|
+
let {caller, accounts} = await Seed_Prompt.credentials(~localDefaults)
|
|
185
|
+
let token = await login(~username=caller.username, ~password=caller.password)
|
|
35
186
|
let client = Seed_Client.make(~config={endpoint: endpoint})
|
|
36
187
|
client->Seed_Client.useToken(token)
|
|
37
|
-
|
|
188
|
+
// What the bearer grants, which is not always what the account list showed: a
|
|
189
|
+
// token narrowed to one role presents that role alone. Said at login so the
|
|
190
|
+
// run starts from the identity it will actually be refused or served under.
|
|
191
|
+
announceIdentity(client)
|
|
192
|
+
// Offered before anything is probed or written, so a narrowing is settled
|
|
193
|
+
// while the run can still be redirected rather than after a refusal.
|
|
194
|
+
await resolveRole(~client, ~roleSwitch, ~login, ~caller)
|
|
195
|
+
{
|
|
196
|
+
client,
|
|
197
|
+
uploadsSkipped,
|
|
198
|
+
label,
|
|
199
|
+
accounts,
|
|
200
|
+
caller,
|
|
201
|
+
callerId: Seed_Client.callerId(client),
|
|
202
|
+
login,
|
|
203
|
+
roleSwitch,
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* A second authenticated client, for one of the accounts the platform's accounts
|
|
209
|
+
* file declares. Same endpoint, same login route, a different bearer.
|
|
210
|
+
*
|
|
211
|
+
* It exists so a data set can read back what it seeded as the account the rows
|
|
212
|
+
* belong to. The seeding client cannot answer that question: it is elevated, so
|
|
213
|
+
* an owner-scoped view counts every owner's rows for it and reads non-empty
|
|
214
|
+
* whether or not a single row is reachable by the person it was seeded for.
|
|
215
|
+
*/
|
|
216
|
+
let clientFor = async (c: connection, ~account: Seed_Users.user): Seed_Client.t => {
|
|
217
|
+
let token = await c.login(~username=account.username, ~password=account.password)
|
|
218
|
+
let client = Seed_Client.make(~config={endpoint: Seed_Client.endpoint(c.client)})
|
|
219
|
+
client->Seed_Client.useToken(token)
|
|
220
|
+
client
|
|
38
221
|
}
|
|
39
222
|
|
|
40
223
|
/**
|
|
@@ -42,19 +225,77 @@ let make = async (
|
|
|
42
225
|
* `/__inmemory/login` shape: POST `{username, password}` → `{token}`). Reuses
|
|
43
226
|
* `Seed_Client.login` but hands the token back for `useToken`.
|
|
44
227
|
*/
|
|
45
|
-
let viaLoginEndpoint = (~loginEndpoint: string) =>
|
|
46
|
-
~username: string,
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
228
|
+
let viaLoginEndpoint = (~loginEndpoint: string) =>
|
|
229
|
+
async (~username: string, ~password: string): string => {
|
|
230
|
+
let client = Seed_Client.make(
|
|
231
|
+
~config={endpoint: loginEndpoint, loginEndpoint, username, password},
|
|
232
|
+
)
|
|
233
|
+
await Seed_Client.login(client)
|
|
234
|
+
switch Seed_Client.currentToken(client) {
|
|
235
|
+
| Some(token) => token
|
|
236
|
+
| None => throw(Failed(`login at ${loginEndpoint} returned no token`))
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* The local dev platform's active-role door: POST `/__inmemory/switch-role` with
|
|
242
|
+
* `{activeRole}` (null to clear) and the current bearer, which answers with a
|
|
243
|
+
* freshly minted one.
|
|
244
|
+
*
|
|
245
|
+
* The local server re-issues on the spot rather than storing a choice, so this
|
|
246
|
+
* returns the new token and no re-authentication follows. Its narrowing is also
|
|
247
|
+
* per-session, which is why a local seed never arrives narrowed on its own —
|
|
248
|
+
* this exists for a *deliberate* `SEED_ROLE` run, which is where a restricted
|
|
249
|
+
* caller's refusals can be exercised without a deployed stack.
|
|
250
|
+
*/
|
|
251
|
+
let viaSwitchRoleEndpoint = (~switchRoleEndpoint: string): roleSwitch =>
|
|
252
|
+
async (~client: Seed_Client.t, ~role: option<string>) => {
|
|
253
|
+
let token = switch Seed_Client.currentToken(client) {
|
|
254
|
+
| Some(t) => t
|
|
255
|
+
| None => throw(Failed("switch-role: no bearer to present — log in first"))
|
|
256
|
+
}
|
|
257
|
+
// Clearing OMITS the key rather than sending `null`. The local handler reads
|
|
258
|
+
// its body through `Obj.magic` into an `option<string>`, and ReScript spells
|
|
259
|
+
// absence `undefined` — so a JSON `null` arrives as a present value and the
|
|
260
|
+
// server tries to act as a role literally named "null", refusing with
|
|
261
|
+
// `Cannot act as "null": not a group this user holds`. An absent key is the
|
|
262
|
+
// only thing it reads as "no role", which is what widens back to full.
|
|
263
|
+
let body = JSON.stringify(
|
|
264
|
+
JSON.Encode.object(
|
|
265
|
+
switch role {
|
|
266
|
+
| Some(r) => Dict.fromArray([("activeRole", JSON.Encode.string(r))])
|
|
267
|
+
| None => Dict.make()
|
|
268
|
+
},
|
|
269
|
+
),
|
|
270
|
+
)
|
|
271
|
+
let res = try await Seed_Client.fetch(
|
|
272
|
+
switchRoleEndpoint,
|
|
273
|
+
{
|
|
274
|
+
method: "POST",
|
|
275
|
+
headers: Dict.fromArray([
|
|
276
|
+
("content-type", "application/json"),
|
|
277
|
+
("authorization", `Bearer ${token}`),
|
|
278
|
+
]),
|
|
279
|
+
body,
|
|
280
|
+
},
|
|
281
|
+
) catch {
|
|
282
|
+
| _ => throw(Failed(`switch-role: cannot reach ${switchRoleEndpoint}`))
|
|
283
|
+
}
|
|
284
|
+
let json = await res->Seed_Client.responseJson
|
|
285
|
+
if !(res->Seed_Client.responseOk) {
|
|
286
|
+
throw(
|
|
287
|
+
Failed(
|
|
288
|
+
`switch-role at ${switchRoleEndpoint} failed with HTTP ${res
|
|
289
|
+
->Seed_Client.responseStatus
|
|
290
|
+
->Int.toString}: ${JSON.stringify(json)}`,
|
|
291
|
+
),
|
|
292
|
+
)
|
|
293
|
+
}
|
|
294
|
+
switch json->Seed_Client.field("token")->Option.flatMap(Seed_Client.asString) {
|
|
295
|
+
| Some(fresh) => Some(fresh)
|
|
296
|
+
| None => throw(Failed(`switch-role at ${switchRoleEndpoint} returned no token`))
|
|
297
|
+
}
|
|
56
298
|
}
|
|
57
|
-
}
|
|
58
299
|
|
|
59
300
|
let envOr = (key: string, fallback: string): string =>
|
|
60
301
|
Seed_Prompt.envValue(key)->Option.getOr(fallback)
|
|
@@ -63,22 +304,29 @@ let envOr = (key: string, fallback: string): string =>
|
|
|
63
304
|
* A ready `connect` thunk for the local dev platform, with the localhost
|
|
64
305
|
* defaults baked in and overridable per endpoint (arg, then env var, then
|
|
65
306
|
* default). Empty input at the credential prompt falls back to `admin`/`admin`.
|
|
307
|
+
*
|
|
308
|
+
* No upload endpoint: uploads mint through the domain API's `Upload_Presign` mutation
|
|
309
|
+
* on the same `graphql` endpoint, with the store passed per asset.
|
|
66
310
|
*/
|
|
67
|
-
let local = (~graphql=?, ~
|
|
311
|
+
let local = (~graphql=?, ~login=?, ()): (unit => promise<connection>) => {
|
|
68
312
|
let endpoint =
|
|
69
313
|
graphql->Option.getOr(envOr("REVENTLESS_GRAPHQL_ENDPOINT", "http://localhost:4000/graphql"))
|
|
70
|
-
let uploadEndpoint =
|
|
71
|
-
upload->Option.getOr(
|
|
72
|
-
envOr("REVENTLESS_UPLOAD_ENDPOINT", "http://localhost:4000/__inmemory/upload"),
|
|
73
|
-
)
|
|
74
314
|
let loginEndpoint =
|
|
75
|
-
login->Option.getOr(
|
|
315
|
+
login->Option.getOr(
|
|
316
|
+
envOr("REVENTLESS_LOGIN_ENDPOINT", "http://localhost:4000/__inmemory/login"),
|
|
317
|
+
)
|
|
318
|
+
// The switch-role route sits beside the login one on the same server, so it is
|
|
319
|
+
// derived rather than configured — a second env var could name a different
|
|
320
|
+
// host from the login it is supposed to re-issue, which is the mismatch
|
|
321
|
+
// `LocalSeedTarget.loginFor` exists to prevent one level up.
|
|
322
|
+
let switchRoleEndpoint =
|
|
323
|
+
loginEndpoint->String.replace("/__inmemory/login", "/__inmemory/switch-role")
|
|
76
324
|
() =>
|
|
77
325
|
make(
|
|
78
326
|
~label="local",
|
|
79
327
|
~endpoint,
|
|
80
|
-
~uploadEndpoint,
|
|
81
328
|
~login=viaLoginEndpoint(~loginEndpoint),
|
|
329
|
+
~roleSwitch=viaSwitchRoleEndpoint(~switchRoleEndpoint),
|
|
82
330
|
~localDefaults=true,
|
|
83
331
|
)
|
|
84
332
|
}
|
package/src/Seed_Connect.res.mjs
CHANGED
|
@@ -4,23 +4,111 @@ import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
|
|
|
4
4
|
import * as Seed_Types$ReventlessSeed from "./Seed_Types.res.mjs";
|
|
5
5
|
import * as Seed_Client$ReventlessSeed from "./Seed_Client.res.mjs";
|
|
6
6
|
import * as Seed_Prompt$ReventlessSeed from "./Seed_Prompt.res.mjs";
|
|
7
|
+
import * as Seed_Upload$ReventlessSeed from "./Seed_Upload.res.mjs";
|
|
7
8
|
|
|
8
|
-
|
|
9
|
+
function roleFromEnv() {
|
|
10
|
+
return Stdlib_Option.map(Seed_Prompt$ReventlessSeed.envValue("SEED_ROLE"), v => {
|
|
11
|
+
let match = v.toLowerCase();
|
|
12
|
+
switch (match) {
|
|
13
|
+
case "all" :
|
|
14
|
+
case "clear" :
|
|
15
|
+
case "full" :
|
|
16
|
+
case "none" :
|
|
17
|
+
return "Full";
|
|
18
|
+
default:
|
|
19
|
+
return {
|
|
20
|
+
TAG: "Narrowed",
|
|
21
|
+
_0: v
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function announceIdentity(client, prefixOpt) {
|
|
28
|
+
let prefix = prefixOpt !== undefined ? prefixOpt : "Acting as";
|
|
29
|
+
let summary = Seed_Client$ReventlessSeed.identitySummary(client);
|
|
30
|
+
if (summary !== undefined) {
|
|
31
|
+
console.log(prefix + `: ` + summary);
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async function applyRole(client, roleSwitch, login, caller, role) {
|
|
37
|
+
let minted = await roleSwitch(client, role);
|
|
38
|
+
let token = minted !== undefined ? minted : await login(caller.username, caller.password);
|
|
39
|
+
Seed_Client$ReventlessSeed.useToken(client, token);
|
|
40
|
+
return announceIdentity(client, "Now acting as");
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async function resolveRole(client, roleSwitch, login, caller) {
|
|
44
|
+
if (roleSwitch === undefined) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
let narrowedFrom = Seed_Client$ReventlessSeed.narrowedFrom(client);
|
|
48
|
+
let choice = roleFromEnv();
|
|
49
|
+
let chosen;
|
|
50
|
+
if (choice !== undefined) {
|
|
51
|
+
chosen = choice;
|
|
52
|
+
} else if (narrowedFrom !== undefined && narrowedFrom.length > 1 && Seed_Prompt$ReventlessSeed.hasTty()) {
|
|
53
|
+
console.log("");
|
|
54
|
+
console.log("This token is narrowed to one role. Seeding needs every right the account has;");
|
|
55
|
+
console.log("clearing also widens your host-shell session, which shares the stored choice.");
|
|
56
|
+
let options = [[
|
|
57
|
+
`full membership (` + narrowedFrom.join(", ") + `)`,
|
|
58
|
+
"Full"
|
|
59
|
+
]].concat(narrowedFrom.map(r => [
|
|
60
|
+
r,
|
|
61
|
+
{
|
|
62
|
+
TAG: "Narrowed",
|
|
63
|
+
_0: r
|
|
64
|
+
}
|
|
65
|
+
]));
|
|
66
|
+
chosen = await Seed_Prompt$ReventlessSeed.select("Act as:", options, undefined, 0);
|
|
67
|
+
} else {
|
|
68
|
+
chosen = undefined;
|
|
69
|
+
}
|
|
70
|
+
if (chosen !== undefined) {
|
|
71
|
+
if (typeof chosen !== "object") {
|
|
72
|
+
return await applyRole(client, roleSwitch, login, caller, undefined);
|
|
73
|
+
} else {
|
|
74
|
+
return await applyRole(client, roleSwitch, login, caller, chosen._0);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async function make(label, endpoint, login, roleSwitch, localDefaultsOpt) {
|
|
9
80
|
let localDefaults = localDefaultsOpt !== undefined ? localDefaultsOpt : false;
|
|
10
|
-
let
|
|
81
|
+
let uploadsSkipped = Seed_Upload$ReventlessSeed.uploadsSkipped();
|
|
11
82
|
let match = await Seed_Prompt$ReventlessSeed.credentials(localDefaults);
|
|
12
|
-
let
|
|
83
|
+
let caller = match.caller;
|
|
84
|
+
let token = await login(caller.username, caller.password);
|
|
13
85
|
let client = Seed_Client$ReventlessSeed.make({
|
|
14
86
|
endpoint: endpoint
|
|
15
87
|
});
|
|
16
88
|
Seed_Client$ReventlessSeed.useToken(client, token);
|
|
89
|
+
announceIdentity(client, undefined);
|
|
90
|
+
await resolveRole(client, roleSwitch, login, caller);
|
|
17
91
|
return {
|
|
18
92
|
client: client,
|
|
19
|
-
|
|
20
|
-
label: label
|
|
93
|
+
uploadsSkipped: uploadsSkipped,
|
|
94
|
+
label: label,
|
|
95
|
+
accounts: match.accounts,
|
|
96
|
+
caller: caller,
|
|
97
|
+
callerId: Seed_Client$ReventlessSeed.callerId(client),
|
|
98
|
+
login: login,
|
|
99
|
+
roleSwitch: roleSwitch
|
|
21
100
|
};
|
|
22
101
|
}
|
|
23
102
|
|
|
103
|
+
async function clientFor(c, account) {
|
|
104
|
+
let token = await c.login(account.username, account.password);
|
|
105
|
+
let client = Seed_Client$ReventlessSeed.make({
|
|
106
|
+
endpoint: Seed_Client$ReventlessSeed.endpoint(c.client)
|
|
107
|
+
});
|
|
108
|
+
Seed_Client$ReventlessSeed.useToken(client, token);
|
|
109
|
+
return client;
|
|
110
|
+
}
|
|
111
|
+
|
|
24
112
|
function viaLoginEndpoint(loginEndpoint) {
|
|
25
113
|
return async (username, password) => {
|
|
26
114
|
let client = Seed_Client$ReventlessSeed.make({
|
|
@@ -42,20 +130,86 @@ function viaLoginEndpoint(loginEndpoint) {
|
|
|
42
130
|
};
|
|
43
131
|
}
|
|
44
132
|
|
|
133
|
+
function viaSwitchRoleEndpoint(switchRoleEndpoint) {
|
|
134
|
+
return async (client, role) => {
|
|
135
|
+
let t = Seed_Client$ReventlessSeed.currentToken(client);
|
|
136
|
+
let token;
|
|
137
|
+
if (t !== undefined) {
|
|
138
|
+
token = t;
|
|
139
|
+
} else {
|
|
140
|
+
throw {
|
|
141
|
+
RE_EXN_ID: Seed_Types$ReventlessSeed.Failed,
|
|
142
|
+
_1: "switch-role: no bearer to present — log in first",
|
|
143
|
+
Error: new Error()
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
let body = JSON.stringify(role !== undefined ? Object.fromEntries([[
|
|
147
|
+
"activeRole",
|
|
148
|
+
role
|
|
149
|
+
]]) : ({}));
|
|
150
|
+
let res;
|
|
151
|
+
try {
|
|
152
|
+
res = await fetch(switchRoleEndpoint, {
|
|
153
|
+
method: "POST",
|
|
154
|
+
headers: Object.fromEntries([
|
|
155
|
+
[
|
|
156
|
+
"content-type",
|
|
157
|
+
"application/json"
|
|
158
|
+
],
|
|
159
|
+
[
|
|
160
|
+
"authorization",
|
|
161
|
+
`Bearer ` + token
|
|
162
|
+
]
|
|
163
|
+
]),
|
|
164
|
+
body: body
|
|
165
|
+
});
|
|
166
|
+
} catch (exn) {
|
|
167
|
+
throw {
|
|
168
|
+
RE_EXN_ID: Seed_Types$ReventlessSeed.Failed,
|
|
169
|
+
_1: `switch-role: cannot reach ` + switchRoleEndpoint,
|
|
170
|
+
Error: new Error()
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
let json = await res.json();
|
|
174
|
+
if (!res.ok) {
|
|
175
|
+
throw {
|
|
176
|
+
RE_EXN_ID: Seed_Types$ReventlessSeed.Failed,
|
|
177
|
+
_1: `switch-role at ` + switchRoleEndpoint + ` failed with HTTP ` + res.status.toString() + `: ` + JSON.stringify(json),
|
|
178
|
+
Error: new Error()
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
let fresh = Stdlib_Option.flatMap(Seed_Client$ReventlessSeed.field(json, "token"), Seed_Client$ReventlessSeed.asString);
|
|
182
|
+
if (fresh !== undefined) {
|
|
183
|
+
return fresh;
|
|
184
|
+
}
|
|
185
|
+
throw {
|
|
186
|
+
RE_EXN_ID: Seed_Types$ReventlessSeed.Failed,
|
|
187
|
+
_1: `switch-role at ` + switchRoleEndpoint + ` returned no token`,
|
|
188
|
+
Error: new Error()
|
|
189
|
+
};
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
|
|
45
193
|
function envOr(key, fallback) {
|
|
46
194
|
return Stdlib_Option.getOr(Seed_Prompt$ReventlessSeed.envValue(key), fallback);
|
|
47
195
|
}
|
|
48
196
|
|
|
49
|
-
function local(graphql,
|
|
197
|
+
function local(graphql, login, param) {
|
|
50
198
|
let endpoint = Stdlib_Option.getOr(graphql, Stdlib_Option.getOr(Seed_Prompt$ReventlessSeed.envValue("REVENTLESS_GRAPHQL_ENDPOINT"), "http://localhost:4000/graphql"));
|
|
51
|
-
let uploadEndpoint = Stdlib_Option.getOr(upload, Stdlib_Option.getOr(Seed_Prompt$ReventlessSeed.envValue("REVENTLESS_UPLOAD_ENDPOINT"), "http://localhost:4000/__inmemory/upload"));
|
|
52
199
|
let loginEndpoint = Stdlib_Option.getOr(login, Stdlib_Option.getOr(Seed_Prompt$ReventlessSeed.envValue("REVENTLESS_LOGIN_ENDPOINT"), "http://localhost:4000/__inmemory/login"));
|
|
53
|
-
|
|
200
|
+
let switchRoleEndpoint = loginEndpoint.replace("/__inmemory/login", "/__inmemory/switch-role");
|
|
201
|
+
return () => make("local", endpoint, viaLoginEndpoint(loginEndpoint), viaSwitchRoleEndpoint(switchRoleEndpoint), true);
|
|
54
202
|
}
|
|
55
203
|
|
|
56
204
|
export {
|
|
205
|
+
roleFromEnv,
|
|
206
|
+
announceIdentity,
|
|
207
|
+
applyRole,
|
|
208
|
+
resolveRole,
|
|
57
209
|
make,
|
|
210
|
+
clientFor,
|
|
58
211
|
viaLoginEndpoint,
|
|
212
|
+
viaSwitchRoleEndpoint,
|
|
59
213
|
envOr,
|
|
60
214
|
local,
|
|
61
215
|
}
|