@platforma-sdk/pl-cli 0.13.5 → 0.14.0
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/README.md +208 -0
- package/dist/address.d.ts +6 -0
- package/dist/base_command.d.ts +10 -3
- package/dist/cli.js +649 -230
- package/dist/cli.js.map +1 -1
- package/dist/cmd/login.d.ts +21 -0
- package/dist/cmd/logout.d.ts +7 -0
- package/dist/cmd/session/list.d.ts +6 -0
- package/dist/cmd/session/remove.d.ts +7 -0
- package/dist/cmd/session/select.d.ts +9 -0
- package/dist/cmd-opts.d.ts +8 -3
- package/dist/connection.d.ts +59 -3
- package/dist/index.js +1 -1
- package/dist/instance.d.ts +7 -0
- package/dist/jwt.d.ts +12 -0
- package/dist/lib.d.ts +2 -1
- package/dist/project_ops-CHhJu5k5.js +524 -0
- package/dist/project_ops-CHhJu5k5.js.map +1 -0
- package/dist/prompt.d.ts +5 -0
- package/dist/session_store.d.ts +112 -0
- package/dist/sso.d.ts +30 -0
- package/package.json +3 -3
- package/src/address.ts +15 -0
- package/src/base_command.ts +24 -6
- package/src/cli.ts +74 -3
- package/src/cmd/admin/delete-user.ts +7 -0
- package/src/cmd/login.ts +262 -0
- package/src/cmd/logout.ts +55 -0
- package/src/cmd/session/list.ts +71 -0
- package/src/cmd/session/remove.ts +29 -0
- package/src/cmd/session/select.ts +36 -0
- package/src/cmd-opts.ts +24 -15
- package/src/commands.test.ts +637 -0
- package/src/connection.ts +275 -15
- package/src/instance.ts +19 -0
- package/src/jwt.ts +36 -0
- package/src/lib.ts +12 -0
- package/src/prompt.ts +54 -0
- package/src/session_store.test.ts +237 -0
- package/src/session_store.ts +315 -0
- package/src/sso.test.ts +269 -0
- package/src/sso.ts +336 -0
- package/src/test_backend.ts +151 -0
- package/dist/output-D41Wn0wM.js +0 -229
- package/dist/output-D41Wn0wM.js.map +0 -1
|
@@ -0,0 +1,637 @@
|
|
|
1
|
+
import { mkdtempSync, readFileSync, rmSync } from "node:fs";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
|
|
5
|
+
import { backends, fakeToken, initCalls, mockPlClient, newFakeBackend } from "./test_backend";
|
|
6
|
+
|
|
7
|
+
vi.mock("@milaboratories/pl-client", (importOriginal) => mockPlClient(importOriginal));
|
|
8
|
+
|
|
9
|
+
const sso = vi.hoisted(() => ({ runs: [] as { nonce: string; clientSecret?: string }[] }));
|
|
10
|
+
vi.mock("./sso", async (importOriginal) => ({
|
|
11
|
+
...(await importOriginal<Record<string, unknown>>()),
|
|
12
|
+
openInBrowser: async () => true,
|
|
13
|
+
runOidcLogin: async (args: {
|
|
14
|
+
nonce: string;
|
|
15
|
+
clientSecret?: string;
|
|
16
|
+
openUrl: (u: string) => Promise<void> | void;
|
|
17
|
+
}) => {
|
|
18
|
+
sso.runs.push({ nonce: args.nonce, clientSecret: args.clientSecret });
|
|
19
|
+
return new TextEncoder().encode("idp-token-response");
|
|
20
|
+
},
|
|
21
|
+
}));
|
|
22
|
+
|
|
23
|
+
const nav = vi.hoisted(() => ({ targets: [] as string[] }));
|
|
24
|
+
vi.mock("./project_ops", async (importOriginal) => ({
|
|
25
|
+
...(await importOriginal<Record<string, unknown>>()),
|
|
26
|
+
getProjectListRid: async () => "own-list",
|
|
27
|
+
navigateToUserRoot: async (_pl: unknown, user: string) => {
|
|
28
|
+
nav.targets.push(user);
|
|
29
|
+
return { projectListRid: `list-of-${user}` };
|
|
30
|
+
},
|
|
31
|
+
}));
|
|
32
|
+
|
|
33
|
+
const { buildProgram, run } = await import("./cli");
|
|
34
|
+
const { connect } = await import("./base_command");
|
|
35
|
+
const { createPlConnection } = await import("./connection");
|
|
36
|
+
const { connectClient } = await import("./base_command");
|
|
37
|
+
const { SessionStore } = await import("./session_store");
|
|
38
|
+
|
|
39
|
+
// Two Backends behind the same kind of local address, as with two SSH tunnels used in turn.
|
|
40
|
+
const ADDR = "127.0.0.1:16345";
|
|
41
|
+
|
|
42
|
+
let dir: string;
|
|
43
|
+
let store: InstanceType<typeof SessionStore>;
|
|
44
|
+
let out: string[];
|
|
45
|
+
|
|
46
|
+
async function cli(...args: string[]): Promise<void> {
|
|
47
|
+
await run(["node", "pl-cli", ...args]);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
beforeEach(() => {
|
|
51
|
+
dir = mkdtempSync(join(tmpdir(), "pl-cli-cmd-"));
|
|
52
|
+
vi.stubEnv("PL_CLI_SESSIONS_FILE", join(dir, "pl-cli.sessions"));
|
|
53
|
+
for (const v of ["PL_USER", "PL_PASSWORD", "PL_ADMIN_USER", "PL_ADMIN_PASSWORD", "PL_ADDRESS"])
|
|
54
|
+
vi.stubEnv(v, undefined);
|
|
55
|
+
store = new SessionStore();
|
|
56
|
+
backends.clear();
|
|
57
|
+
initCalls.length = 0;
|
|
58
|
+
sso.runs.length = 0;
|
|
59
|
+
nav.targets.length = 0;
|
|
60
|
+
out = [];
|
|
61
|
+
vi.spyOn(console, "log").mockImplementation((m: string) => void out.push(m));
|
|
62
|
+
vi.spyOn(process.stderr, "write").mockImplementation(() => true);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
afterEach(() => {
|
|
66
|
+
vi.restoreAllMocks();
|
|
67
|
+
vi.unstubAllEnvs();
|
|
68
|
+
rmSync(dir, { recursive: true, force: true });
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
const twoSso = [
|
|
72
|
+
{
|
|
73
|
+
kind: "sso" as const,
|
|
74
|
+
id: "corp-sso",
|
|
75
|
+
title: "Corporate SSO",
|
|
76
|
+
description: "",
|
|
77
|
+
issuer: "https://corp.example.com",
|
|
78
|
+
},
|
|
79
|
+
{ kind: "basic" as const, id: "ldap", title: "LDAP", description: "" },
|
|
80
|
+
{
|
|
81
|
+
kind: "sso" as const,
|
|
82
|
+
id: "partner-sso",
|
|
83
|
+
title: "Partner SSO",
|
|
84
|
+
description: "",
|
|
85
|
+
issuer: "https://partner.example.com",
|
|
86
|
+
},
|
|
87
|
+
];
|
|
88
|
+
|
|
89
|
+
describe("login", () => {
|
|
90
|
+
test("password login stores only the Backend token, keyed by instance ID", async () => {
|
|
91
|
+
backends.set(ADDR, newFakeBackend("inst-A"));
|
|
92
|
+
await cli("login", "-a", ADDR, "--method", "htpasswd", "-u", "alice", "-p", "secret");
|
|
93
|
+
|
|
94
|
+
const s = store.get("inst-A");
|
|
95
|
+
expect(s?.login).toBe("alice");
|
|
96
|
+
expect(s?.address).toBe(ADDR);
|
|
97
|
+
expect(s?.loginMethod).toEqual({ id: "htpasswd", kind: "basic" });
|
|
98
|
+
expect(JSON.stringify(store.read())).not.toContain("secret");
|
|
99
|
+
expect(out.join("\n")).toMatch(/Signed in .* as "alice"/);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test("SSO with --method: passes the client secret; stores no IdP tokens", async () => {
|
|
103
|
+
backends.set(ADDR, newFakeBackend("inst-A", { methods: twoSso }));
|
|
104
|
+
await cli("login", "-a", ADDR, "--method", "partner-sso", "--no-browser");
|
|
105
|
+
|
|
106
|
+
expect(sso.runs).toEqual([{ nonce: "n-1", clientSecret: "cs" }]);
|
|
107
|
+
expect(backends.get(ADDR)?.calls).toContain("beginSSO:partner-sso");
|
|
108
|
+
expect(backends.get(ADDR)?.calls).toContain("loginSSO:idp-token-response:partner-sso");
|
|
109
|
+
expect(store.get("inst-A")?.login).toBe("sso-user");
|
|
110
|
+
expect(store.get("inst-A")?.loginMethod).toEqual({ id: "partner-sso", kind: "sso" });
|
|
111
|
+
expect(JSON.stringify(store.read())).not.toContain("idp-token-response");
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
test("--method is required: no implicit choice, the error shows the methods", async () => {
|
|
115
|
+
backends.set(ADDR, newFakeBackend("inst-A", { methods: twoSso }));
|
|
116
|
+
// Credentials do not select a method either.
|
|
117
|
+
for (const args of [[], ["-u", "alice", "-p", "secret"]])
|
|
118
|
+
await expect(cli("login", "-a", ADDR, ...args)).rejects.toThrow(
|
|
119
|
+
/Select a login method with --method <id>[\s\S]*corp-sso\s+sso\s+Corporate SSO\s+https:\/\/corp\.example\.com[\s\S]*ldap\s+basic\s+LDAP[\s\S]*Run: pl-cli login -a 127\.0\.0\.1:16345 --method <id>/,
|
|
120
|
+
);
|
|
121
|
+
expect(sso.runs).toHaveLength(0);
|
|
122
|
+
expect(backends.get(ADDR)?.calls.filter((c) => c.startsWith("login"))).toEqual([]);
|
|
123
|
+
|
|
124
|
+
await expect(cli("login", "-a", ADDR, "--method", "nope")).rejects.toThrow(
|
|
125
|
+
/no login method "nope"[\s\S]*partner-sso/,
|
|
126
|
+
);
|
|
127
|
+
const loginOptions = buildProgram()
|
|
128
|
+
.commands.find((c) => c.name() === "login")
|
|
129
|
+
?.options.map((o) => o.long);
|
|
130
|
+
expect(loginOptions).toContain("--method");
|
|
131
|
+
expect(loginOptions).not.toContain("--sso");
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
test("--user/--password fill in the credentials of the selected password method", async () => {
|
|
135
|
+
backends.set(ADDR, newFakeBackend("inst-A", { methods: twoSso }));
|
|
136
|
+
await cli("login", "-a", ADDR, "--method", "ldap", "-u", "alice", "-p", "secret");
|
|
137
|
+
expect(sso.runs).toHaveLength(0);
|
|
138
|
+
expect(backends.get(ADDR)?.calls).toContain("login:alice:ldap");
|
|
139
|
+
expect(store.get("inst-A")?.loginMethod).toEqual({ id: "ldap", kind: "basic" });
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
test("--list-methods prints the methods and does not sign in", async () => {
|
|
143
|
+
backends.set(ADDR, newFakeBackend("inst-A", { methods: twoSso }));
|
|
144
|
+
await cli("login", "-a", ADDR, "--list-methods");
|
|
145
|
+
const text = out.join("\n");
|
|
146
|
+
expect(text).toMatch(/ID\s+KIND\s+TITLE\s+ISSUER/);
|
|
147
|
+
expect(text).toMatch(/partner-sso\s+sso\s+Partner SSO\s+https:\/\/partner\.example\.com/);
|
|
148
|
+
expect(text).toMatch(/ldap\s+basic\s+LDAP/);
|
|
149
|
+
|
|
150
|
+
out.length = 0;
|
|
151
|
+
await cli("login", "-a", ADDR, "--list-methods", "-f", "json");
|
|
152
|
+
expect(JSON.parse(out.join("\n"))).toEqual([
|
|
153
|
+
{ id: "corp-sso", kind: "sso", title: "Corporate SSO", issuer: "https://corp.example.com" },
|
|
154
|
+
{ id: "ldap", kind: "basic", title: "LDAP" },
|
|
155
|
+
{
|
|
156
|
+
id: "partner-sso",
|
|
157
|
+
kind: "sso",
|
|
158
|
+
title: "Partner SSO",
|
|
159
|
+
issuer: "https://partner.example.com",
|
|
160
|
+
},
|
|
161
|
+
]);
|
|
162
|
+
expect(store.list()).toEqual([]);
|
|
163
|
+
expect(backends.get(ADDR)?.calls.filter((c) => c !== "ping")).toEqual([]);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
test("Backend without auth:v2 (older than v3.6.0): login stops at once", async () => {
|
|
167
|
+
backends.set(ADDR, newFakeBackend("inst-A", { authV2: false, methods: [] }));
|
|
168
|
+
for (const args of [[], ["-u", "alice", "-p", "secret"], ["--method", "htpasswd"]])
|
|
169
|
+
await expect(cli("login", "-a", ADDR, ...args)).rejects.toThrow(/older than v3\.6\.0/);
|
|
170
|
+
expect(backends.get(ADDR)?.calls.filter((c) => c.startsWith("login"))).toEqual([]);
|
|
171
|
+
expect(store.list()).toEqual([]);
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
test("auth:v2 Backend with no usable method: clear error, no login call", async () => {
|
|
175
|
+
backends.set(ADDR, newFakeBackend("inst-A", { methods: [] }));
|
|
176
|
+
await expect(cli("login", "-a", ADDR, "-u", "alice", "-p", "secret")).rejects.toThrow(
|
|
177
|
+
/advertises no login method that pl-cli can use/,
|
|
178
|
+
);
|
|
179
|
+
expect(backends.get(ADDR)?.calls.filter((c) => c.startsWith("login"))).toEqual([]);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
test("credentials in the address URL are used but never stored or printed", async () => {
|
|
183
|
+
backends.set(ADDR, newFakeBackend("inst-A"));
|
|
184
|
+
await cli("login", "-a", `http://alice:secret@${ADDR}`, "--method", "htpasswd");
|
|
185
|
+
expect(backends.get(ADDR)?.calls).toContain("login:alice:htpasswd");
|
|
186
|
+
expect(store.get("inst-A")?.address).toBe(`http://${ADDR}`);
|
|
187
|
+
expect(readFileSync(store.path, "utf8")).not.toContain("secret");
|
|
188
|
+
|
|
189
|
+
await cli("session", "list", "-f", "json");
|
|
190
|
+
await cli("session", "list");
|
|
191
|
+
await cli("logout", "-a", `http://alice:secret@${ADDR}`, "--login", "nobody");
|
|
192
|
+
expect(out.join("\n")).not.toContain("secret");
|
|
193
|
+
expect(out.join("\n")).toContain(`http://${ADDR}`);
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
test("proxy credentials in the address query are never stored or printed", async () => {
|
|
197
|
+
backends.set(ADDR, newFakeBackend("inst-A"));
|
|
198
|
+
const address = `http://${ADDR}/?http-proxy=http://u:pw@proxy:3128&grpc-proxy=http://u:pw@proxy:3129`;
|
|
199
|
+
await cli("login", "-a", address, "--method", "htpasswd", "-u", "alice", "-p", "secret");
|
|
200
|
+
expect(store.get("inst-A")?.address).toBe(`http://${ADDR}`);
|
|
201
|
+
expect(readFileSync(store.path, "utf8")).not.toContain("pw");
|
|
202
|
+
|
|
203
|
+
await cli("session", "list", "-f", "json");
|
|
204
|
+
await cli("session", "list");
|
|
205
|
+
await cli("logout", "-a", address, "--login", "nobody");
|
|
206
|
+
await expect(cli("project", "list", "-a", address, "-u", "nobody")).rejects.toThrow(
|
|
207
|
+
/No stored session for "nobody" on the Backend at http:\/\/127\.0\.0\.1:16345 \(/,
|
|
208
|
+
);
|
|
209
|
+
expect(out.join("\n")).not.toContain("pw");
|
|
210
|
+
expect(out.join("\n")).toContain(`http://${ADDR}`);
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
test("session list hides credentials in addresses of older files", async () => {
|
|
214
|
+
const now = new Date().toISOString();
|
|
215
|
+
store.put("inst-A", {
|
|
216
|
+
login: "alice",
|
|
217
|
+
address: `https://alice:secret@${ADDR}`,
|
|
218
|
+
token: fakeToken("alice", "inst-A"),
|
|
219
|
+
createdAt: now,
|
|
220
|
+
updatedAt: now,
|
|
221
|
+
});
|
|
222
|
+
await cli("session", "list", "-f", "json");
|
|
223
|
+
expect(out.join("\n")).not.toContain("secret");
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
test("wrong password stores nothing", async () => {
|
|
227
|
+
backends.set(ADDR, newFakeBackend("inst-A"));
|
|
228
|
+
await expect(
|
|
229
|
+
cli("login", "-a", ADDR, "--method", "htpasswd", "-u", "alice", "-p", "bad"),
|
|
230
|
+
).rejects.toThrow();
|
|
231
|
+
expect(store.list()).toEqual([]);
|
|
232
|
+
});
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
describe("commands use the stored session of the instance at the address", () => {
|
|
236
|
+
test("picks the session by the instance ID from the ping answer, not by address", async () => {
|
|
237
|
+
backends.set(ADDR, newFakeBackend("inst-A"));
|
|
238
|
+
await cli("login", "-a", ADDR, "--method", "htpasswd", "-u", "alice", "-p", "secret");
|
|
239
|
+
const tokenA = store.get("inst-A")?.token;
|
|
240
|
+
|
|
241
|
+
// Another Backend answers at the same address now.
|
|
242
|
+
backends.set(ADDR, newFakeBackend("inst-B", { passwords: { bob: "pw" } }));
|
|
243
|
+
await expect(createPlConnection({ address: ADDR, sessionStore: store })).rejects.toThrow(
|
|
244
|
+
/no stored session for its instance inst-B/,
|
|
245
|
+
);
|
|
246
|
+
await cli("login", "-a", ADDR, "--method", "htpasswd", "-u", "bob", "-p", "pw");
|
|
247
|
+
const tokenB = store.get("inst-B")?.token;
|
|
248
|
+
|
|
249
|
+
await createPlConnection({ address: ADDR, sessionStore: store });
|
|
250
|
+
backends.set(ADDR, newFakeBackend("inst-A"));
|
|
251
|
+
await createPlConnection({ address: ADDR, sessionStore: store });
|
|
252
|
+
expect(initCalls.map((c) => c.jwtToken)).toEqual([tokenB, tokenA]);
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
test("precedence: user+password over session; --user alone selects that login's session", async () => {
|
|
256
|
+
backends.set(ADDR, newFakeBackend("inst-A", { passwords: { alice: "secret", bob: "pw" } }));
|
|
257
|
+
await cli("login", "-a", ADDR, "--method", "htpasswd", "-u", "alice", "-p", "secret");
|
|
258
|
+
await cli("login", "-a", ADDR, "--method", "htpasswd", "-u", "bob", "-p", "pw");
|
|
259
|
+
const calls = backends.get(ADDR)?.calls ?? [];
|
|
260
|
+
|
|
261
|
+
calls.length = 0;
|
|
262
|
+
await createPlConnection({ address: ADDR, user: "alice", password: "secret" });
|
|
263
|
+
expect(calls).toContain("login:alice:");
|
|
264
|
+
|
|
265
|
+
await createPlConnection({ address: ADDR, user: "alice" });
|
|
266
|
+
await createPlConnection({ address: ADDR });
|
|
267
|
+
expect(initCalls.slice(-2).map((c) => c.jwtToken)).toEqual([
|
|
268
|
+
store.get("inst-A", "alice")?.token,
|
|
269
|
+
store.get("inst-A", "bob")?.token,
|
|
270
|
+
]);
|
|
271
|
+
|
|
272
|
+
await expect(createPlConnection({ address: ADDR, user: "carol" })).rejects.toThrow(
|
|
273
|
+
/No stored session for "carol"/,
|
|
274
|
+
);
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
test("admin commands: --admin-user alone selects a session; no flags use the current one", async () => {
|
|
278
|
+
backends.set(ADDR, newFakeBackend("inst-A", { passwords: { alice: "secret", root: "r" } }));
|
|
279
|
+
await cli("login", "-a", ADDR, "--method", "htpasswd", "-u", "root", "-p", "r");
|
|
280
|
+
await cli("login", "-a", ADDR, "--method", "htpasswd", "-u", "alice", "-p", "secret");
|
|
281
|
+
|
|
282
|
+
await connectClient({ address: ADDR, adminUser: "root" });
|
|
283
|
+
await connectClient({ address: ADDR });
|
|
284
|
+
expect(initCalls.slice(-2).map((c) => c.jwtToken)).toEqual([
|
|
285
|
+
store.get("inst-A", "root")?.token,
|
|
286
|
+
store.get("inst-A", "alice")?.token,
|
|
287
|
+
]);
|
|
288
|
+
await expect(connectClient({ address: ADDR, adminPassword: "x" })).rejects.toThrow(
|
|
289
|
+
/--admin-password needs --admin-user/,
|
|
290
|
+
);
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
test("anonymous Backend needs no session", async () => {
|
|
294
|
+
backends.set(ADDR, newFakeBackend("inst-A", { requireAuth: false }));
|
|
295
|
+
await createPlConnection({ address: ADDR });
|
|
296
|
+
expect(initCalls).toEqual([{ address: ADDR, jwtToken: undefined }]);
|
|
297
|
+
});
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
describe("expiry and renewal", () => {
|
|
301
|
+
function storeToken(token: string) {
|
|
302
|
+
const now = new Date().toISOString();
|
|
303
|
+
store.put("inst-A", { login: "alice", address: ADDR, token, createdAt: now, updatedAt: now });
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
test("an expired token is not sent; the user is told to log in", async () => {
|
|
307
|
+
backends.set(ADDR, newFakeBackend("inst-A"));
|
|
308
|
+
const past = Math.floor(Date.now() / 1000) - 10;
|
|
309
|
+
storeToken(fakeToken("alice", "inst-A", { iat: past - 100, exp: past }));
|
|
310
|
+
await expect(createPlConnection({ address: ADDR })).rejects.toThrow(
|
|
311
|
+
/session of "alice" on instance inst-A has expired.*pl-cli login/,
|
|
312
|
+
);
|
|
313
|
+
expect(initCalls).toEqual([]);
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
test("a token past half of its life is renewed and saved", async () => {
|
|
317
|
+
backends.set(ADDR, newFakeBackend("inst-A"));
|
|
318
|
+
const now = Math.floor(Date.now() / 1000);
|
|
319
|
+
const old = fakeToken("alice", "inst-A", { iat: now - 1000, exp: now + 100 });
|
|
320
|
+
storeToken(old);
|
|
321
|
+
await createPlConnection({ address: ADDR });
|
|
322
|
+
const renewed = store.get("inst-A")?.token;
|
|
323
|
+
expect(renewed).not.toBe(old);
|
|
324
|
+
expect(initCalls[0].jwtToken).toBe(renewed);
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
test("a fresh token is not renewed", async () => {
|
|
328
|
+
backends.set(ADDR, newFakeBackend("inst-A"));
|
|
329
|
+
const token = fakeToken("alice", "inst-A");
|
|
330
|
+
storeToken(token);
|
|
331
|
+
await createPlConnection({ address: ADDR });
|
|
332
|
+
expect(backends.get(ADDR)?.calls).not.toContain("refresh");
|
|
333
|
+
expect(initCalls[0].jwtToken).toBe(token);
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
test("a refused token gives the login hint", async () => {
|
|
337
|
+
const now = Math.floor(Date.now() / 1000);
|
|
338
|
+
const due = fakeToken("alice", "inst-A", { iat: now - 1000, exp: now + 100 });
|
|
339
|
+
const fresh = fakeToken("alice", "inst-A", { iat: now - 1 });
|
|
340
|
+
backends.set(ADDR, newFakeBackend("inst-A", { refused: new Set([due, fresh]) }));
|
|
341
|
+
|
|
342
|
+
storeToken(due);
|
|
343
|
+
await expect(createPlConnection({ address: ADDR })).rejects.toThrow(
|
|
344
|
+
/refused the stored session of "alice".*pl-cli login/,
|
|
345
|
+
);
|
|
346
|
+
storeToken(fresh);
|
|
347
|
+
await expect(createPlConnection({ address: ADDR })).rejects.toThrow(
|
|
348
|
+
/refused the stored session of "alice".*pl-cli login/,
|
|
349
|
+
);
|
|
350
|
+
});
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
describe("logout and session commands", () => {
|
|
354
|
+
test("logout removes the current session of the instance at the address", async () => {
|
|
355
|
+
backends.set(ADDR, newFakeBackend("inst-A", { passwords: { alice: "secret", bob: "pw" } }));
|
|
356
|
+
await cli("login", "-a", ADDR, "--method", "htpasswd", "-u", "alice", "-p", "secret");
|
|
357
|
+
await cli("login", "-a", ADDR, "--method", "htpasswd", "-u", "bob", "-p", "pw");
|
|
358
|
+
|
|
359
|
+
await cli("logout", "-a", ADDR);
|
|
360
|
+
expect(store.logins("inst-A")).toEqual(["alice"]);
|
|
361
|
+
expect(store.get("inst-A")).toBeUndefined();
|
|
362
|
+
await expect(createPlConnection({ address: ADDR })).rejects.toThrow(
|
|
363
|
+
/no current session.*Stored logins: alice.*session select/,
|
|
364
|
+
);
|
|
365
|
+
|
|
366
|
+
await cli("logout", "-a", ADDR, "--login", "alice");
|
|
367
|
+
expect(store.list()).toEqual([]);
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
test("session list never prints tokens; select and remove work by instance", async () => {
|
|
371
|
+
backends.set(ADDR, newFakeBackend("inst-A", { passwords: { alice: "secret", bob: "pw" } }));
|
|
372
|
+
await cli("login", "-a", ADDR, "--method", "htpasswd", "-u", "alice", "-p", "secret");
|
|
373
|
+
await cli("login", "-a", ADDR, "--method", "htpasswd", "-u", "bob", "-p", "pw");
|
|
374
|
+
const tokens = store.list().map((r) => r.session.token);
|
|
375
|
+
|
|
376
|
+
out.length = 0;
|
|
377
|
+
await cli("session", "list");
|
|
378
|
+
await cli("session", "list", "-f", "json");
|
|
379
|
+
const printed = out.join("\n");
|
|
380
|
+
for (const t of tokens) expect(printed).not.toContain(t);
|
|
381
|
+
expect(printed).toMatch(/\*\s+inst-A\s+bob/);
|
|
382
|
+
|
|
383
|
+
await cli("session", "select", "alice", "-a", ADDR);
|
|
384
|
+
expect(store.get("inst-A")?.login).toBe("alice");
|
|
385
|
+
|
|
386
|
+
// --instance works without a reachable Backend.
|
|
387
|
+
backends.clear();
|
|
388
|
+
await cli("session", "remove", "bob", "--instance", "inst-A");
|
|
389
|
+
expect(store.logins("inst-A")).toEqual(["alice"]);
|
|
390
|
+
await expect(cli("session", "remove", "bob", "--instance", "inst-A")).rejects.toThrow(
|
|
391
|
+
/No stored session/,
|
|
392
|
+
);
|
|
393
|
+
await expect(cli("session", "select", "alice")).rejects.toThrow(/Give --instance/);
|
|
394
|
+
});
|
|
395
|
+
|
|
396
|
+
test("the program has the new commands", () => {
|
|
397
|
+
const names = buildProgram().commands.map((c) => c.name());
|
|
398
|
+
expect(names).toEqual(expect.arrayContaining(["login", "logout", "session"]));
|
|
399
|
+
});
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
describe("review fixes", () => {
|
|
403
|
+
test("credentials in the address URL are used, as before stored sessions", async () => {
|
|
404
|
+
backends.set(ADDR, newFakeBackend("inst-A"));
|
|
405
|
+
await createPlConnection({ address: `http://alice:secret@${ADDR}` });
|
|
406
|
+
expect(backends.get(ADDR)?.calls).toContain("login:alice:");
|
|
407
|
+
expect(store.list()).toEqual([]);
|
|
408
|
+
// A user in the URL without a password selects that login's stored session.
|
|
409
|
+
await cli("login", "-a", ADDR, "--method", "htpasswd", "-u", "alice", "-p", "secret");
|
|
410
|
+
await createPlConnection({ address: `http://alice@${ADDR}` });
|
|
411
|
+
expect(initCalls.at(-1)?.jwtToken).toBe(store.get("inst-A", "alice")?.token);
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
test("sessionStore: null does not read stored sessions", async () => {
|
|
415
|
+
backends.set(ADDR, newFakeBackend("inst-A"));
|
|
416
|
+
await cli("login", "-a", ADDR, "--method", "htpasswd", "-u", "alice", "-p", "secret");
|
|
417
|
+
await expect(createPlConnection({ address: ADDR, sessionStore: null })).rejects.toThrow(
|
|
418
|
+
/no credentials provided/,
|
|
419
|
+
);
|
|
420
|
+
await createPlConnection({
|
|
421
|
+
address: ADDR,
|
|
422
|
+
user: "alice",
|
|
423
|
+
password: "secret",
|
|
424
|
+
sessionStore: null,
|
|
425
|
+
});
|
|
426
|
+
});
|
|
427
|
+
|
|
428
|
+
test("login stores nothing when the token issuer differs from the ping answer", async () => {
|
|
429
|
+
backends.set(ADDR, newFakeBackend("inst-A", { tokenIssuer: "inst-OTHER" }));
|
|
430
|
+
await expect(
|
|
431
|
+
cli("login", "-a", ADDR, "--method", "htpasswd", "-u", "alice", "-p", "secret"),
|
|
432
|
+
).rejects.toThrow(/token is for instance inst-OTHER.*No session was stored/);
|
|
433
|
+
expect(store.list()).toEqual([]);
|
|
434
|
+
});
|
|
435
|
+
|
|
436
|
+
test("a stored token of another instance is not sent", async () => {
|
|
437
|
+
backends.set(ADDR, newFakeBackend("inst-A"));
|
|
438
|
+
const now = new Date().toISOString();
|
|
439
|
+
store.put("inst-A", {
|
|
440
|
+
login: "alice",
|
|
441
|
+
address: ADDR,
|
|
442
|
+
token: fakeToken("alice", "inst-B"),
|
|
443
|
+
createdAt: now,
|
|
444
|
+
updatedAt: now,
|
|
445
|
+
});
|
|
446
|
+
await expect(createPlConnection({ address: ADDR })).rejects.toThrow(
|
|
447
|
+
/token of instance inst-B, not of instance inst-A/,
|
|
448
|
+
);
|
|
449
|
+
expect(initCalls).toEqual([]);
|
|
450
|
+
});
|
|
451
|
+
|
|
452
|
+
test("a different instance after init is refused", async () => {
|
|
453
|
+
backends.set(ADDR, newFakeBackend("inst-A", { instanceIdAfterInit: "inst-B" }));
|
|
454
|
+
await cli("login", "-a", ADDR, "--method", "htpasswd", "-u", "alice", "-p", "secret");
|
|
455
|
+
await expect(createPlConnection({ address: ADDR })).rejects.toThrow(
|
|
456
|
+
/changed from inst-A to inst-B/,
|
|
457
|
+
);
|
|
458
|
+
});
|
|
459
|
+
|
|
460
|
+
test("--target-user works with admin credentials and with an admin's stored session", async () => {
|
|
461
|
+
backends.set(ADDR, newFakeBackend("inst-A", { passwords: { alice: "secret", root: "r" } }));
|
|
462
|
+
const withPassword = await connect({
|
|
463
|
+
address: ADDR,
|
|
464
|
+
adminUser: "root",
|
|
465
|
+
adminPassword: "r",
|
|
466
|
+
targetUser: "alice",
|
|
467
|
+
});
|
|
468
|
+
expect(withPassword.projectListRid).toBe("list-of-alice");
|
|
469
|
+
expect(backends.get(ADDR)?.calls).toContain("login:root:");
|
|
470
|
+
|
|
471
|
+
await cli("login", "-a", ADDR, "--method", "htpasswd", "-u", "root", "-p", "r");
|
|
472
|
+
const withSession = await connect({ address: ADDR, targetUser: "alice" });
|
|
473
|
+
expect(withSession.projectListRid).toBe("list-of-alice");
|
|
474
|
+
expect(initCalls.at(-1)?.jwtToken).toBe(store.get("inst-A", "root")?.token);
|
|
475
|
+
|
|
476
|
+
await expect(
|
|
477
|
+
connect({ address: ADDR, user: "alice", password: "secret", targetUser: "bob" }),
|
|
478
|
+
).rejects.toThrow(/--target-user requires/);
|
|
479
|
+
expect(nav.targets).toEqual(["alice", "alice"]);
|
|
480
|
+
});
|
|
481
|
+
|
|
482
|
+
test("a token that the client renews during a command is saved (onUpdate)", async () => {
|
|
483
|
+
backends.set(ADDR, newFakeBackend("inst-A"));
|
|
484
|
+
await cli("login", "-a", ADDR, "--method", "htpasswd", "-u", "alice", "-p", "secret");
|
|
485
|
+
await createPlConnection({ address: ADDR });
|
|
486
|
+
const renewed = fakeToken("alice", "inst-A", { iat: Math.floor(Date.now() / 1000) + 5 });
|
|
487
|
+
initCalls.at(-1)?.onUpdate?.({ jwtToken: renewed });
|
|
488
|
+
expect(store.get("inst-A")?.token).toBe(renewed);
|
|
489
|
+
});
|
|
490
|
+
|
|
491
|
+
test("prints a note when credentials come from the environment", async () => {
|
|
492
|
+
backends.set(ADDR, newFakeBackend("inst-A"));
|
|
493
|
+
vi.stubEnv("PL_USER", "alice");
|
|
494
|
+
vi.stubEnv("PL_PASSWORD", "secret");
|
|
495
|
+
const written: string[] = [];
|
|
496
|
+
vi.mocked(process.stderr.write).mockImplementation((chunk: string | Uint8Array) => {
|
|
497
|
+
written.push(String(chunk));
|
|
498
|
+
return true;
|
|
499
|
+
});
|
|
500
|
+
await cli("login", "-a", ADDR, "--method", "htpasswd");
|
|
501
|
+
expect(written.join("")).toContain(
|
|
502
|
+
"note: using credentials from the environment: PL_USER, PL_PASSWORD",
|
|
503
|
+
);
|
|
504
|
+
});
|
|
505
|
+
|
|
506
|
+
test("Backend older than v3.6.0 (no iss): login and stored sessions refused, flags work", async () => {
|
|
507
|
+
backends.set(ADDR, newFakeBackend("inst-A", { tokenIssuer: null }));
|
|
508
|
+
await expect(
|
|
509
|
+
cli("login", "-a", ADDR, "--method", "htpasswd", "-u", "alice", "-p", "secret"),
|
|
510
|
+
).rejects.toThrow(
|
|
511
|
+
"This Backend is older than v3.6.0; pl-cli login and stored sessions need v3.6.0 or " +
|
|
512
|
+
"newer; use --user/--password per command.",
|
|
513
|
+
);
|
|
514
|
+
expect(store.list()).toEqual([]);
|
|
515
|
+
|
|
516
|
+
const now = new Date().toISOString();
|
|
517
|
+
store.put("inst-A", {
|
|
518
|
+
login: "alice",
|
|
519
|
+
address: ADDR,
|
|
520
|
+
token: fakeToken("alice", undefined),
|
|
521
|
+
createdAt: now,
|
|
522
|
+
updatedAt: now,
|
|
523
|
+
});
|
|
524
|
+
await expect(createPlConnection({ address: ADDR })).rejects.toThrow(/older than v3\.6\.0/);
|
|
525
|
+
expect(initCalls).toEqual([]);
|
|
526
|
+
|
|
527
|
+
await createPlConnection({ address: ADDR, user: "alice", password: "secret" });
|
|
528
|
+
expect(initCalls).toHaveLength(1);
|
|
529
|
+
});
|
|
530
|
+
|
|
531
|
+
test("Backend without an instance ID: explicit credentials work, sessions do not", async () => {
|
|
532
|
+
backends.set(ADDR, newFakeBackend("", { methods: [] }));
|
|
533
|
+
await createPlConnection({ address: ADDR, user: "alice", password: "secret" });
|
|
534
|
+
await createPlConnection({ address: `http://alice:secret@${ADDR}`, sessionStore: null });
|
|
535
|
+
expect(initCalls).toHaveLength(2);
|
|
536
|
+
await expect(createPlConnection({ address: ADDR })).rejects.toThrow(/older than v3\.6\.0/);
|
|
537
|
+
await expect(cli("logout", "-a", ADDR)).rejects.toThrow(/older than v3\.6\.0/);
|
|
538
|
+
// --list-methods needs no instance ID.
|
|
539
|
+
await cli("login", "-a", ADDR, "--list-methods");
|
|
540
|
+
expect(out.join("\n")).toContain("advertises no login methods");
|
|
541
|
+
});
|
|
542
|
+
|
|
543
|
+
describe("default session: commands without --address", () => {
|
|
544
|
+
const OTHER = "127.0.0.1:16346";
|
|
545
|
+
|
|
546
|
+
test("use the stored address and the default session; select changes it", async () => {
|
|
547
|
+
backends.set(ADDR, newFakeBackend("inst-A", { passwords: { alice: "secret", bob: "pw" } }));
|
|
548
|
+
backends.set(OTHER, newFakeBackend("inst-B", { passwords: { carol: "c" } }));
|
|
549
|
+
await cli("login", "-a", ADDR, "--method", "htpasswd", "-u", "alice", "-p", "secret");
|
|
550
|
+
await cli("login", "-a", ADDR, "--method", "htpasswd", "-u", "bob", "-p", "pw");
|
|
551
|
+
await cli("login", "-a", OTHER, "--method", "htpasswd", "-u", "carol", "-p", "c");
|
|
552
|
+
|
|
553
|
+
await connect({});
|
|
554
|
+
expect(initCalls.at(-1)).toEqual(
|
|
555
|
+
expect.objectContaining({ address: OTHER, jwtToken: store.get("inst-B", "carol")?.token }),
|
|
556
|
+
);
|
|
557
|
+
|
|
558
|
+
await cli("session", "select", "alice", "-a", ADDR);
|
|
559
|
+
await connectClient({});
|
|
560
|
+
expect(initCalls.at(-1)).toEqual(
|
|
561
|
+
expect.objectContaining({ address: ADDR, jwtToken: store.get("inst-A", "alice")?.token }),
|
|
562
|
+
);
|
|
563
|
+
// --user without an address: that login on the default session's instance.
|
|
564
|
+
await createPlConnection({ user: "bob" });
|
|
565
|
+
expect(initCalls.at(-1)?.jwtToken).toBe(store.get("inst-A", "bob")?.token);
|
|
566
|
+
|
|
567
|
+
// --address still selects by the instance there.
|
|
568
|
+
await createPlConnection({ address: OTHER });
|
|
569
|
+
expect(initCalls.at(-1)?.jwtToken).toBe(store.get("inst-B", "carol")?.token);
|
|
570
|
+
|
|
571
|
+
out.length = 0;
|
|
572
|
+
await cli("session", "list", "-f", "json");
|
|
573
|
+
const rows: { login: string; default: boolean; current: boolean }[] = JSON.parse(
|
|
574
|
+
out.join("\n"),
|
|
575
|
+
);
|
|
576
|
+
expect(rows.map((r) => [r.login, r.default, r.current])).toEqual([
|
|
577
|
+
["alice", true, true],
|
|
578
|
+
["bob", false, false],
|
|
579
|
+
["carol", false, true],
|
|
580
|
+
]);
|
|
581
|
+
});
|
|
582
|
+
|
|
583
|
+
test("another instance at the stored address: refuse and send no token", async () => {
|
|
584
|
+
backends.set(ADDR, newFakeBackend("inst-A"));
|
|
585
|
+
await cli("login", "-a", `http://alice:secret@${ADDR}`, "--method", "htpasswd");
|
|
586
|
+
// The tunnel now points to another Backend.
|
|
587
|
+
backends.set(ADDR, newFakeBackend("inst-B"));
|
|
588
|
+
initCalls.length = 0;
|
|
589
|
+
await expect(createPlConnection({})).rejects.toThrow(
|
|
590
|
+
/default session of "alice" is for instance inst-A, but the Backend at http:\/\/127\.0\.0\.1:16345 is instance inst-B\. No token was sent/,
|
|
591
|
+
);
|
|
592
|
+
expect(initCalls).toEqual([]);
|
|
593
|
+
expect(backends.get(ADDR)?.calls).toEqual(["ping"]);
|
|
594
|
+
});
|
|
595
|
+
|
|
596
|
+
test("no default session and no address: hint", async () => {
|
|
597
|
+
await expect(createPlConnection({})).rejects.toThrow(
|
|
598
|
+
/No Backend address and no default session[\s\S]*pl-cli login -a <address> --method <id>[\s\S]*pl-cli session select/,
|
|
599
|
+
);
|
|
600
|
+
await expect(createPlConnection({ sessionStore: null })).rejects.toThrow(/--address/);
|
|
601
|
+
await expect(connectClient({ adminUser: "root", adminPassword: "r" })).rejects.toThrow(
|
|
602
|
+
/address/,
|
|
603
|
+
);
|
|
604
|
+
});
|
|
605
|
+
|
|
606
|
+
test("logout without --address and session remove clear the default", async () => {
|
|
607
|
+
backends.set(ADDR, newFakeBackend("inst-A", { passwords: { alice: "secret", bob: "pw" } }));
|
|
608
|
+
await cli("login", "-a", ADDR, "--method", "htpasswd", "-u", "alice", "-p", "secret");
|
|
609
|
+
await cli("login", "-a", ADDR, "--method", "htpasswd", "-u", "bob", "-p", "pw");
|
|
610
|
+
backends.clear(); // logout without an address needs no Backend.
|
|
611
|
+
await cli("logout");
|
|
612
|
+
expect(store.getDefault()).toBeUndefined();
|
|
613
|
+
expect(store.logins("inst-A")).toEqual(["alice"]);
|
|
614
|
+
|
|
615
|
+
store.select("inst-A", "alice");
|
|
616
|
+
await cli("session", "remove", "alice", "--instance", "inst-A");
|
|
617
|
+
expect(store.getDefault()).toBeUndefined();
|
|
618
|
+
await expect(cli("logout")).rejects.toThrow(/No default session/);
|
|
619
|
+
});
|
|
620
|
+
|
|
621
|
+
test("--address is optional for commands, required for login", () => {
|
|
622
|
+
const program = buildProgram();
|
|
623
|
+
const find = (path: string[]) =>
|
|
624
|
+
path.reduce(
|
|
625
|
+
(cmd, name) => cmd?.commands.find((c) => c.name() === name),
|
|
626
|
+
program as typeof program | undefined,
|
|
627
|
+
);
|
|
628
|
+
const mandatory = (path: string[]) =>
|
|
629
|
+
find(path)?.options.find((o) => o.long === "--address")?.mandatory;
|
|
630
|
+
expect(mandatory(["project", "list"])).toBe(false);
|
|
631
|
+
expect(mandatory(["admin", "user-list"])).toBe(false);
|
|
632
|
+
expect(mandatory(["maintenance", "list-resources"])).toBe(false);
|
|
633
|
+
expect(mandatory(["logout"])).toBe(false);
|
|
634
|
+
expect(mandatory(["login"])).toBe(true);
|
|
635
|
+
});
|
|
636
|
+
});
|
|
637
|
+
});
|