Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/cli-organization-slug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@executor-js/app-management": patch
---

`executor apps` commands signed in with `executor apps login` accept the signed-in organization by
its slug as well as its ID. Sign-in prints the slug, but `--organization <slug>` failed every
command, including `executor apps call`, with "Forbidden (HTTP 403)" before any request was sent.
Any other organization now goes to the host, which refuses one the session cannot use.
14 changes: 7 additions & 7 deletions e2e/support/apps-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,14 +265,14 @@ export const appsCli = Effect.gen(function* () {
return { ...printed, finished: running };
});
/**
* `executor apps login --host <host>`, approved for the test organization by the synthetic
* owner. The grant is revoked with the caller's scope.
* `executor apps login --host <host>`, approved for the test organization by a synthetic actor,
* the owner unless another is named. The grant is revoked with the caller's scope.
*/
const login = (host: string) =>
const login = (host: string, actor: typeof actors.owner = actors.owner) =>
Effect.gen(function* () {
const started = yield* deviceLogin(host, "opens");
const lookup = yield* api.request(
actors.owner,
actor,
"GET",
`/api/auth/device/request?user_code=${encodeURIComponent(started.userCode)}`,
);
Expand All @@ -281,16 +281,16 @@ export const appsCli = Effect.gen(function* () {
Effect.gen(function* () {
const grants = yield* body(
Schema.Array(Schema.Struct({ id: Schema.String, clientId: Schema.String })),
yield* api.request(actors.owner, "GET", "/api/auth/oauth2/get-consents"),
yield* api.request(actor, "GET", "/api/auth/oauth2/get-consents"),
);
for (const grant of grants.filter((item) => item.clientId === clientId))
yield* api.request(actors.owner, "POST", "/api/auth/oauth2/delete-consent", {
yield* api.request(actor, "POST", "/api/auth/oauth2/delete-consent", {
id: grant.id,
});
}).pipe(Effect.orDie),
);
const decided = yield* api.request(
actors.owner,
actor,
"POST",
"/api/auth/device/decide",
{ user_code: started.userCode, accept: true },
Expand Down
13 changes: 13 additions & 0 deletions e2e/test-plan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,19 @@ export const scenarios = plan({
),
},
},
appsCliCallDeviceLogin: {
fixtures: "actors",
file: "apps-cli-call.spec.ts",
title:
"executor apps call signed in with a device code runs an app tool for a person who can use the app, with the signed-in organization named by slug, by ID or not at all, and the host refuses it for another organization and for a person who cannot use the app until it is shared with them",
targets: {
"self-host": scheduled,
cloud: na(
"Cloud serves the same hosted server's tool-call route and device sign-in; self-host runs this scenario against it.",
),
local: na("Local signs the CLI in with an API key, not a saved session."),
},
},
appGitKeptCredentials: {
fixtures: "actors",
file: "app-git-remotes.spec.ts",
Expand Down
120 changes: 120 additions & 0 deletions e2e/tests/apps-cli-call.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* `executor apps call` runs one app tool from a shell with an API key, as an import script does:
* input from a file or stdin, the tool's value on stdout, and the host's refusals as exit codes.
* A CLI signed in with a device code calls tools under its person's own access the same way.
*/
import { expect, layer } from "@effect/vitest";
import { Effect, Schema } from "effect";
Expand Down Expand Up @@ -231,4 +232,123 @@ layer(HostedLive, { excludeTestServices: true })("Apps CLI call", (it) => {
}),
),
);

it.effect(scenarios.appsCliCallDeviceLogin.title, (context) =>
withHostedCase(
context,
Effect.gen(function* () {
const hosts = targetHosts(yield* Target);
const api = yield* Api,
actors = yield* Actors,
evidence = yield* Evidence;
const prefix = `/api/organizations/${actors.organization.id}`;
const deployed = yield* api.request(actors.owner, "POST", `${prefix}/apps/deploy`, {
name: `CLI device call ${randomUUID().slice(0, 8)}`,
files,
});
expect(deployed.status).toBe(200);
const app = yield* body(App, deployed);
const path = `${prefix}/apps/${app.id}`;
yield* Effect.addFinalizer(() =>
api.request(actors.owner, "DELETE", path).pipe(Effect.orDie),
);
const input = JSON.stringify(records(2, "hello"));
/** Sign one actor's CLI in with a device code, approved for the test organization. */
const signedIn = (actor: typeof actors.owner) =>
Effect.gen(function* () {
const cli = yield* appsCli;
const login = yield* cli.login(hosts.api, actor);
expect(login.finished.code, login.finished.stderr).toBe(0);
return cli;
});
const call = (
cli: Effect.Success<typeof appsCli>,
target: string,
selection: ReadonlyArray<string> = [],
) =>
cli.cli([
"call",
"--host",
hosts.api,
...selection,
"--app",
target,
"--tool",
"import.records",
"--input",
input,
]);

// Both people approve their CLI's code at once; each approval waits for the next poll.
const [owner, member] = yield* Effect.all(
[signedIn(actors.owner), signedIn(actors.member)],
{ concurrency: 2 },
);

// The owner, who can use their own app, calls it. Sign-in prints the organization's slug,
// so people name it by slug as often as by ID; both select the signed-in organization.
const bySlug = ["--organization", actors.organization.slug];
const listed = yield* owner.cli(["list", "--host", hosts.api, ...bySlug]);
expect(listed.code, listed.stderr).toBe(0);
expect(listed.stdout).toContain(app.id);
for (const [label, selection] of [
["slug", bySlug],
["id", ["--organization", actors.organization.id]],
] as const) {
const called = yield* call(owner, app.slug, selection);
yield* evidence.json(`owner-device-call-by-${label}.json`, {
code: called.code,
stderr: called.stderr,
});
expect(called.code, called.stderr).toBe(0);
expect(yield* Schema.decodeUnknownEffect(Imported)(called.stdout)).toEqual({
count: 2,
first: "r-1",
last: "r-2",
noteChars: 10,
});
}
// An organization the session was not signed in to is the host's to refuse.
const elsewhere = yield* call(owner, app.id, [
"--organization",
`elsewhere-${randomUUID().slice(0, 8)}`,
]);
expect(elsewhere.code).toBe(1);
expect(elsewhere.stdout).not.toContain("r-2");
expect((yield* Schema.decodeUnknownEffect(Failure)(elsewhere.stdout)).error).toMatchObject({
code: "OrganizationForbidden",
status: 403,
});

// A member, calling without --organization, cannot use the owner's private app: the host
// refuses the call and the tool never runs.
const refused = yield* call(member, app.id);
yield* evidence.json("member-device-refused.json", refused);
expect(refused.code).toBe(1);
expect(refused.stdout).not.toContain("r-2");
expect((yield* Schema.decodeUnknownEffect(Failure)(refused.stdout)).error).toMatchObject({
code: "OrganizationForbidden",
status: 403,
});

// Once the owner shares the app with everyone, the member's same session may call it.
const access = yield* body(
Schema.Struct({ revision: Schema.String }),
yield* api.request(actors.owner, "GET", `${path}/access`),
);
const shared = yield* api.request(actors.owner, "PATCH", `${path}/access`, {
revision: access.revision,
audience: { kind: "everyone" },
});
expect(shared.status).toBe(200);
const allowed = yield* call(member, app.slug);
yield* evidence.json("member-device-call.json", {
code: allowed.code,
stderr: allowed.stderr,
});
expect(allowed.code, allowed.stderr).toBe(0);
expect((yield* Schema.decodeUnknownEffect(Imported)(allowed.stdout)).count).toBe(2);
}),
),
);
});
16 changes: 12 additions & 4 deletions packages/app-management/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ const host = Flag.String("host").pipe(
);
const organization = Flag.String("organization").pipe(
Flag.withDescription(
"Hosted organization. Required with EXECUTOR_ACCESS_TOKEN; after executor apps login it defaults to the signed-in organization",
"Hosted organization ID or slug. Required with EXECUTOR_ACCESS_TOKEN; after executor apps login it defaults to the signed-in organization",
),
Flag.optional,
);
Expand Down Expand Up @@ -153,9 +153,17 @@ const access = (host: string, organization: Option.Option<string>) =>
)
return { token: local.value, organization: undefined };
const session = Redacted.value(yield* registrySession(host));
if (Option.isSome(organization) && organization.value !== session.organization)
return yield* new AppClientError({ reason: "forbidden" });
return { token: Redacted.make(session.accessToken), organization: session.organization };
// Sign-in prints the organization's slug and saves its ID, so either names the session's own
// organization. Any other reference goes to the host, which refuses an organization the
// session's grant does not cover; the CLI never reports a refusal the host did not send.
const named = Option.getOrUndefined(organization);
return {
token: Redacted.make(session.accessToken),
organization:
named === undefined || named === session.organization || named === session.namespace
? session.organization
: named,
};
});
/** Authenticated requests to the host; redirects are refused so credentials never follow them. */
const transport = (host: string, token: Redacted.Redacted<string>, path = (url: string) => url) =>
Expand Down
Loading