@app-vitals/agent-admin 1.62.0 → 1.64.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/package.json +2 -2
- package/src/admin-boot.ts +12 -0
- package/src/admin-ui-settings-layout.ts +11 -0
- package/src/admin-ui-slack-config-token-pages.ts +136 -0
- package/src/admin-ui-slack-config-token-pages.unit.test.ts +84 -0
- package/src/admin-ui-slack-config-token.smoke.test.ts +287 -0
- package/src/admin-ui-slack-config-token.ts +251 -0
- package/src/admin-ui.ts +44 -2
- package/src/slack-config-token.integration.test.ts +398 -1
- package/src/slack-config-token.ts +253 -1
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/admin-ui-slack-config-token.ts
|
|
3
|
+
* GET/POST /admin/settings/slack-config-token — Settings > Slack config token
|
|
4
|
+
* (SCT-3.1). A thin UI over SlackConfigTokenService, scoped like the Templates
|
|
5
|
+
* editor: platform admins manage the deployment-wide row and any account's row
|
|
6
|
+
* (?accountId=); account owners manage only their own account's row; plain
|
|
7
|
+
* members, ownerless callers and other accounts get 403.
|
|
8
|
+
*
|
|
9
|
+
* Neither the rendered HTML nor any redirect URL carries token material: the
|
|
10
|
+
* pasted refresh token is validated by rotating it once, the rotated pair goes
|
|
11
|
+
* straight to the service, and only metadata is read back.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import type { Context, Hono, MiddlewareHandler } from "hono";
|
|
15
|
+
import { accountIdFromScope } from "./account-agent-create.ts";
|
|
16
|
+
import type { AccountMemberService } from "./account-members.ts";
|
|
17
|
+
import type { AccountService } from "./accounts.ts";
|
|
18
|
+
import type { AdminUIEnv } from "./admin-ui.ts";
|
|
19
|
+
import type { SettingsNav } from "./admin-ui-settings-layout.ts";
|
|
20
|
+
import {
|
|
21
|
+
ADMIN_SLACK_CONFIG_TOKEN_PATH as BASE,
|
|
22
|
+
renderSlackConfigTokenPage,
|
|
23
|
+
type SlackConfigTokenScopeOption,
|
|
24
|
+
} from "./admin-ui-slack-config-token-pages.ts";
|
|
25
|
+
import type { CallerScopeResolver } from "./caller-scope.ts";
|
|
26
|
+
import {
|
|
27
|
+
ApiError,
|
|
28
|
+
ForbiddenError,
|
|
29
|
+
UnprocessableEntityError,
|
|
30
|
+
} from "./errors.ts";
|
|
31
|
+
import type { SlackConfigTokenService } from "./slack-config-token.ts";
|
|
32
|
+
import type { SlackConfigTokenClient } from "./slack-provisioning-client.ts";
|
|
33
|
+
|
|
34
|
+
export interface AdminSlackConfigTokenRouteDeps {
|
|
35
|
+
requireAuth: MiddlewareHandler<AdminUIEnv>;
|
|
36
|
+
tokens: Pick<
|
|
37
|
+
SlackConfigTokenService,
|
|
38
|
+
"get" | "save" | "clear" | "getAccessToken"
|
|
39
|
+
>;
|
|
40
|
+
/** tooling.tokens.rotate client, used to validate a pasted refresh token. */
|
|
41
|
+
rotateClient: SlackConfigTokenClient;
|
|
42
|
+
/** SHIPWRIGHT_ENCRYPTION_KEY is configured; save is refused without it. */
|
|
43
|
+
encryptionConfigured: () => boolean;
|
|
44
|
+
callerScopeResolver: CallerScopeResolver;
|
|
45
|
+
/** Needed to tell account owners from plain members; absent = admins only. */
|
|
46
|
+
members?: Pick<AccountMemberService, "getByEmail">;
|
|
47
|
+
accounts?: Pick<AccountService, "listWithCounts" | "getWithCounts">;
|
|
48
|
+
settingsNav?: SettingsNav;
|
|
49
|
+
html: (content: string, opts?: { status?: number }) => Response;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
type Actor = { kind: "admin" } | { kind: "account"; accountId: string };
|
|
53
|
+
|
|
54
|
+
const NOTICES: Record<string, string> = {
|
|
55
|
+
saved: "Slack config token validated and saved.",
|
|
56
|
+
cleared: "Slack config token cleared.",
|
|
57
|
+
rotated: "Slack config token rotated.",
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const REFRESH_PREFIX = "xoxe-1-";
|
|
61
|
+
|
|
62
|
+
export function registerAdminSlackConfigTokenRoutes(
|
|
63
|
+
app: Hono<AdminUIEnv>,
|
|
64
|
+
deps: AdminSlackConfigTokenRouteDeps,
|
|
65
|
+
): void {
|
|
66
|
+
async function resolveActor(c: Context<AdminUIEnv>): Promise<Actor> {
|
|
67
|
+
if (c.var.isAdmin === true) return { kind: "admin" };
|
|
68
|
+
const accountId = accountIdFromScope(
|
|
69
|
+
await deps.callerScopeResolver(c.var.userEmail, false),
|
|
70
|
+
);
|
|
71
|
+
if (accountId === null || !deps.members) {
|
|
72
|
+
throw new ForbiddenError("Admin or account owner access required");
|
|
73
|
+
}
|
|
74
|
+
const me = await deps.members.getByEmail(c.var.userEmail);
|
|
75
|
+
if (!me || me.role !== "owner" || me.accountId !== accountId) {
|
|
76
|
+
throw new ForbiddenError("Admin or account owner access required");
|
|
77
|
+
}
|
|
78
|
+
return { kind: "account", accountId };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Returns the account id to act on (null = deployment-wide); 403 otherwise. */
|
|
82
|
+
function resolveScope(actor: Actor, requested: string): string | null {
|
|
83
|
+
if (actor.kind === "account") {
|
|
84
|
+
if (requested !== "" && requested !== actor.accountId) {
|
|
85
|
+
throw new ForbiddenError(
|
|
86
|
+
"cannot manage another account's or the deployment-wide Slack config token",
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
return actor.accountId;
|
|
90
|
+
}
|
|
91
|
+
return requested === "" ? null : requested;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async function accountOptions(
|
|
95
|
+
actor: Actor,
|
|
96
|
+
): Promise<SlackConfigTokenScopeOption[]> {
|
|
97
|
+
if (actor.kind === "account") {
|
|
98
|
+
const acct = await deps.accounts?.getWithCounts(actor.accountId);
|
|
99
|
+
return [{ value: actor.accountId, label: acct?.name ?? actor.accountId }];
|
|
100
|
+
}
|
|
101
|
+
const accounts = (await deps.accounts?.listWithCounts()) ?? [];
|
|
102
|
+
return [
|
|
103
|
+
{ value: "", label: "Deployment-wide (all accounts)" },
|
|
104
|
+
...accounts.map((a) => ({ value: a.id, label: a.name })),
|
|
105
|
+
];
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async function renderPage(
|
|
109
|
+
c: Context<AdminUIEnv>,
|
|
110
|
+
actor: Actor,
|
|
111
|
+
accountId: string | null,
|
|
112
|
+
flash: { error?: string; notice?: string; status?: number } = {},
|
|
113
|
+
): Promise<Response> {
|
|
114
|
+
const row = await deps.tokens.get(accountId);
|
|
115
|
+
return deps.html(
|
|
116
|
+
renderSlackConfigTokenPage({
|
|
117
|
+
userEmail: c.var.userEmail,
|
|
118
|
+
isAdmin: c.var.isAdmin === true,
|
|
119
|
+
...(deps.settingsNav ? { nav: deps.settingsNav } : {}),
|
|
120
|
+
accountOptions: await accountOptions(actor),
|
|
121
|
+
accountId: accountId ?? "",
|
|
122
|
+
status: row
|
|
123
|
+
? {
|
|
124
|
+
lastRotatedAt: row.lastRotatedAt,
|
|
125
|
+
lastError: row.lastError,
|
|
126
|
+
slackTeamId: row.slackTeamId,
|
|
127
|
+
slackUserId: row.slackUserId,
|
|
128
|
+
}
|
|
129
|
+
: null,
|
|
130
|
+
encryptionConfigured: deps.encryptionConfigured(),
|
|
131
|
+
...(flash.error ? { error: flash.error } : {}),
|
|
132
|
+
...(flash.notice ? { notice: flash.notice } : {}),
|
|
133
|
+
}),
|
|
134
|
+
{ status: flash.status ?? 200 },
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** Slack's own error code, if it is a safe identifier; never a message. */
|
|
139
|
+
function rotateFailure(err: unknown): ApiError {
|
|
140
|
+
const code = (err as { code?: unknown } | null)?.code;
|
|
141
|
+
const safe =
|
|
142
|
+
typeof code === "string" && /^[a-z0-9_]{1,64}$/i.test(code)
|
|
143
|
+
? code
|
|
144
|
+
: "unknown_error";
|
|
145
|
+
return new UnprocessableEntityError(
|
|
146
|
+
`Slack rejected the refresh token (${safe}). Nothing was saved.`,
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
async function mutate(
|
|
151
|
+
c: Context<AdminUIEnv>,
|
|
152
|
+
notice: string,
|
|
153
|
+
run: (accountId: string | null, form: FormData) => Promise<void>,
|
|
154
|
+
): Promise<Response> {
|
|
155
|
+
let actor: Actor;
|
|
156
|
+
try {
|
|
157
|
+
actor = await resolveActor(c);
|
|
158
|
+
} catch {
|
|
159
|
+
return new Response("Forbidden", { status: 403 });
|
|
160
|
+
}
|
|
161
|
+
let form: FormData;
|
|
162
|
+
try {
|
|
163
|
+
form = await c.req.formData();
|
|
164
|
+
} catch {
|
|
165
|
+
form = new FormData();
|
|
166
|
+
}
|
|
167
|
+
let accountId: string | null;
|
|
168
|
+
try {
|
|
169
|
+
accountId = resolveScope(
|
|
170
|
+
actor,
|
|
171
|
+
(form.get("accountId")?.toString() ?? "").trim(),
|
|
172
|
+
);
|
|
173
|
+
} catch {
|
|
174
|
+
return new Response("Forbidden", { status: 403 });
|
|
175
|
+
}
|
|
176
|
+
try {
|
|
177
|
+
await run(accountId, form);
|
|
178
|
+
} catch (err) {
|
|
179
|
+
if (err instanceof ApiError) {
|
|
180
|
+
return renderPage(c, actor, accountId, {
|
|
181
|
+
error: err.message,
|
|
182
|
+
status: err.statusCode,
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
throw err;
|
|
186
|
+
}
|
|
187
|
+
const qs = new URLSearchParams({
|
|
188
|
+
...(accountId !== null ? { accountId } : {}),
|
|
189
|
+
notice,
|
|
190
|
+
});
|
|
191
|
+
return c.redirect(`${BASE}?${qs}`, 302);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
app.get(BASE, deps.requireAuth, async (c) => {
|
|
195
|
+
let actor: Actor;
|
|
196
|
+
let accountId: string | null;
|
|
197
|
+
try {
|
|
198
|
+
actor = await resolveActor(c);
|
|
199
|
+
accountId = resolveScope(actor, c.req.query("accountId")?.trim() ?? "");
|
|
200
|
+
} catch {
|
|
201
|
+
return new Response("Forbidden", { status: 403 });
|
|
202
|
+
}
|
|
203
|
+
const key = c.req.query("notice");
|
|
204
|
+
return renderPage(c, actor, accountId, {
|
|
205
|
+
...(key && NOTICES[key] ? { notice: NOTICES[key] } : {}),
|
|
206
|
+
});
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
app.post(`${BASE}/save`, deps.requireAuth, (c) =>
|
|
210
|
+
mutate(c, "saved", async (accountId, form) => {
|
|
211
|
+
if (!deps.encryptionConfigured()) {
|
|
212
|
+
throw new UnprocessableEntityError(
|
|
213
|
+
"SHIPWRIGHT_ENCRYPTION_KEY is not configured; refusing to store a Slack config token unencrypted. Set it to a 64-character hex string (openssl rand -hex 32) and retry.",
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
const pasted = (form.get("refreshToken")?.toString() ?? "").trim();
|
|
217
|
+
if (!pasted.startsWith(REFRESH_PREFIX)) {
|
|
218
|
+
throw new UnprocessableEntityError(
|
|
219
|
+
`Paste a Slack App Configuration refresh token (it starts with ${REFRESH_PREFIX}).`,
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
let rotated: Awaited<
|
|
223
|
+
ReturnType<SlackConfigTokenClient["rotateConfigToken"]>
|
|
224
|
+
>;
|
|
225
|
+
try {
|
|
226
|
+
rotated = await deps.rotateClient.rotateConfigToken(pasted);
|
|
227
|
+
} catch (err) {
|
|
228
|
+
throw rotateFailure(err);
|
|
229
|
+
}
|
|
230
|
+
await deps.tokens.save(accountId, {
|
|
231
|
+
refreshToken: rotated.refreshToken,
|
|
232
|
+
accessToken: rotated.accessToken,
|
|
233
|
+
accessTokenExpiresAt: new Date(rotated.exp * 1000),
|
|
234
|
+
slackTeamId: rotated.teamId,
|
|
235
|
+
slackUserId: rotated.userId,
|
|
236
|
+
});
|
|
237
|
+
}),
|
|
238
|
+
);
|
|
239
|
+
|
|
240
|
+
app.post(`${BASE}/clear`, deps.requireAuth, (c) =>
|
|
241
|
+
mutate(c, "cleared", async (accountId) => {
|
|
242
|
+
await deps.tokens.clear(accountId);
|
|
243
|
+
}),
|
|
244
|
+
);
|
|
245
|
+
|
|
246
|
+
app.post(`${BASE}/rotate`, deps.requireAuth, (c) =>
|
|
247
|
+
mutate(c, "rotated", async (accountId) => {
|
|
248
|
+
await deps.tokens.getAccessToken(accountId, { forceRotate: true });
|
|
249
|
+
}),
|
|
250
|
+
);
|
|
251
|
+
}
|
package/src/admin-ui.ts
CHANGED
|
@@ -100,6 +100,10 @@ import {
|
|
|
100
100
|
type SettingsNav,
|
|
101
101
|
withoutAccountItem,
|
|
102
102
|
} from "./admin-ui-settings-layout.ts";
|
|
103
|
+
import {
|
|
104
|
+
type AdminSlackConfigTokenRouteDeps,
|
|
105
|
+
registerAdminSlackConfigTokenRoutes,
|
|
106
|
+
} from "./admin-ui-slack-config-token.ts";
|
|
103
107
|
import { registerAdminTemplatesRoutes } from "./admin-ui-templates.ts";
|
|
104
108
|
import type { AgentCronJobService } from "./agent-cron-jobs.ts";
|
|
105
109
|
import type { DeleteAgentFullyDeps } from "./agent-deletion.ts";
|
|
@@ -474,6 +478,14 @@ export interface AdminUIDeps {
|
|
|
474
478
|
AgentTemplateService,
|
|
475
479
|
"list" | "replace" | "putItem" | "deleteItem" | "delete"
|
|
476
480
|
>;
|
|
481
|
+
/**
|
|
482
|
+
* SCT-3.1: Settings > Slack config token. Optional; absent → no
|
|
483
|
+
* /admin/settings/slack-config-token routes and no nav entry.
|
|
484
|
+
*/
|
|
485
|
+
slackConfigToken?: Pick<
|
|
486
|
+
AdminSlackConfigTokenRouteDeps,
|
|
487
|
+
"tokens" | "rotateClient" | "encryptionConfigured"
|
|
488
|
+
>;
|
|
477
489
|
githubEnterpriseApi?: (
|
|
478
490
|
path: string,
|
|
479
491
|
init?: RequestInit,
|
|
@@ -913,6 +925,7 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
|
|
|
913
925
|
accountServices,
|
|
914
926
|
githubEnterpriseApi,
|
|
915
927
|
templateService,
|
|
928
|
+
slackConfigToken,
|
|
916
929
|
templateReconcile,
|
|
917
930
|
agentsApi,
|
|
918
931
|
specialtyRegistry = new SpecialtyTypes(),
|
|
@@ -962,6 +975,7 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
|
|
|
962
975
|
const settingsNav = buildSettingsNav({
|
|
963
976
|
githubEnterprise: Boolean(githubEnterpriseApi),
|
|
964
977
|
templates: Boolean(templateService),
|
|
978
|
+
slackConfigToken: Boolean(slackConfigToken),
|
|
965
979
|
accounts: accountNavEnabled,
|
|
966
980
|
extraItems: deps.settingsNavItems ?? [],
|
|
967
981
|
});
|
|
@@ -986,7 +1000,12 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
|
|
|
986
1000
|
|
|
987
1001
|
// GHE-1.4: the admin-only "Settings" toolbar entry.
|
|
988
1002
|
// SGL-2.2: shown to admins and to account members.
|
|
989
|
-
if (
|
|
1003
|
+
if (
|
|
1004
|
+
githubEnterpriseApi ||
|
|
1005
|
+
templateService ||
|
|
1006
|
+
slackConfigToken ||
|
|
1007
|
+
accountNavEnabled
|
|
1008
|
+
) {
|
|
990
1009
|
app.use("/admin/*", async (c, next) => {
|
|
991
1010
|
const token = getCookie(c, SESSION_COOKIE);
|
|
992
1011
|
const user = token ? await getSessionUser(token, sessionSecret) : null;
|
|
@@ -4066,7 +4085,12 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
|
|
|
4066
4085
|
// ─── Enterprise GitHub apps settings (GHE-1.4) ───────────────────────────
|
|
4067
4086
|
|
|
4068
4087
|
// SGL-2.2: /admin/settings lands on the first nav item the viewer can see.
|
|
4069
|
-
if (
|
|
4088
|
+
if (
|
|
4089
|
+
githubEnterpriseApi ||
|
|
4090
|
+
templateService ||
|
|
4091
|
+
slackConfigToken ||
|
|
4092
|
+
accountNavEnabled
|
|
4093
|
+
) {
|
|
4070
4094
|
app.get("/admin/settings", requireAuth, async (c) => {
|
|
4071
4095
|
// Admins without an account would 404 on the Account item; skip it.
|
|
4072
4096
|
let nav = settingsNav;
|
|
@@ -4110,6 +4134,24 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
|
|
|
4110
4134
|
});
|
|
4111
4135
|
}
|
|
4112
4136
|
|
|
4137
|
+
// ─── Slack config token settings (SCT-3.1) ───────────────────────────────
|
|
4138
|
+
|
|
4139
|
+
if (slackConfigToken) {
|
|
4140
|
+
registerAdminSlackConfigTokenRoutes(app, {
|
|
4141
|
+
requireAuth,
|
|
4142
|
+
...slackConfigToken,
|
|
4143
|
+
settingsNav,
|
|
4144
|
+
callerScopeResolver,
|
|
4145
|
+
...(accountServices
|
|
4146
|
+
? {
|
|
4147
|
+
accounts: accountServices.accounts,
|
|
4148
|
+
members: accountServices.members,
|
|
4149
|
+
}
|
|
4150
|
+
: {}),
|
|
4151
|
+
html,
|
|
4152
|
+
});
|
|
4153
|
+
}
|
|
4154
|
+
|
|
4113
4155
|
// ─── Account page (SSP-3.2) ───────────────────────────────────────────────
|
|
4114
4156
|
|
|
4115
4157
|
if (accountServices) {
|