Skip to content
Closed
32 changes: 28 additions & 4 deletions packages/provider-revyl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ customers; a partner integration gets one through Revyl's partner program.
```sh
npm install -g agent-device
agent-device plugins add @agent-device/revyl
```

On the daemon host, configure the exact storage origins that may receive builds. If the daemon is
already running, stop it so the next command starts it with the new policy:

```sh
export REVYL_ARTIFACT_ORIGINS=https://your-revyl-storage.example
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
agent-device daemon stop
```

```sh
export REVYL_API_KEY=your-api-key
export AGENT_DEVICE_SESSION=checkout-ios
agent-device connect revyl --platform ios
Expand All @@ -18,19 +29,26 @@ agent-device disconnect
```

See the [Revyl guide](https://agent-device.dev/docs/revyl) for setup and supported operations.
See [SECURITY.md](./SECURITY.md) for the artifact storage trust boundary.
To update the plugin, run `agent-device plugins update @agent-device/revyl`.

After you add or update the plugin, close your sessions and run `agent-device daemon stop` before reconnecting.

## Use it from JavaScript

The package default-exports a plugin factory. Pass credentials as options, or leave them out to read
`REVYL_API_KEY` and the optional `REVYL_API_URL` from the environment:
`REVYL_API_KEY`, the optional `REVYL_API_URL`, and `REVYL_ARTIFACT_ORIGINS` from the environment.
The artifact value is a comma-separated list of exact HTTPS origins configured by the operator.
It is required before `install` uploads a local build. Obtain the origins from your Revyl
administrator or egress policy. Do not copy a full presigned URL into it.

```js
import revyl from '@agent-device/revyl';

const plugin = revyl({ apiKey: 'your-api-key' });
const plugin = revyl({
apiKey: 'your-api-key',
artifactOrigins: ['https://your-revyl-storage.example'],
});
```

Pass that object to a host you start yourself, and drive the device with its client:
Expand All @@ -41,11 +59,17 @@ import revyl from '@agent-device/revyl';

const host = await startAgentDevice({
stateDir: './.my-app/agent-device-state',
plugins: [revyl({ apiKey: 'your-api-key' })],
plugins: [
revyl({
apiKey: 'your-api-key',
artifactOrigins: ['https://your-revyl-storage.example'],
}),
],
});
// host.client drives the device; await host.close() when you are done.
```

Credentials passed as options are used instead of the environment variables, and the daemon does not
compare them with your shell. Each option replaces only its own variable: passing `apiUrl` alone still
reads `REVYL_API_KEY` from the environment.
reads `REVYL_API_KEY` from the environment. The `artifactOrigins` option replaces
`REVYL_ARTIFACT_ORIGINS`; an empty array admits no storage origin.
19 changes: 19 additions & 0 deletions packages/provider-revyl/SECURITY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Revyl artifact storage boundary

The operator selects each admitted artifact origin through `REVYL_ARTIFACT_ORIGINS` or the
plugin's `artifactOrigins` option. Do not derive this policy from a Revyl response.

The network stack resolves the configured host when the upload starts. The host can resolve
differently after admission. The operator must trust the configured origin's DNS and TLS identity.

The presigned PUT writes the first build byte only after the returned URL's origin matches the
configured set. The worker receives the download URL only after the same check.

| Case | Result |
| --- | --- |
| Returned HTTPS URL has an exact configured origin | Accept |
| Returned loopback HTTP URL exactly matches the configured origin and `REVYL_API_URL` | Accept for a local backend |
| No artifact origin is configured | Refuse before the PUT |
| Returned URL has another scheme, origin, credentials, or non-loopback literal IP host | Refuse before the PUT |
| Configured value has a path, query, fragment, credentials, cleartext non-loopback scheme, or non-loopback literal IP host | Refuse when the client is built |
| Upload response redirects | Refuse the redirect |
25 changes: 21 additions & 4 deletions packages/provider-revyl/src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import type { ProviderConnectionVerification } from '@agent-device/contracts/rem
import { AppError, type ReasonTable } from '@agent-device/kernel/errors';
import { REVYL_PROVIDER, REVYL_SERVICE, type RevylPlatform } from './device.ts';
import { requireRevylCredentials, type RevylCredentials } from './credentials.ts';
import { createRevylClient } from './revyl-client.ts';
import {
createRevylClient,
resolveRevylArtifactOrigins,
resolveRevylBackendUrl,
REVYL_DEFAULT_BACKEND_URL,
} from './revyl-client.ts';
import { REVYL_PROFILE_FIELDS } from './profile-fields.ts';
import { revylTextFlag } from './session-allocation.ts';

Expand All @@ -28,11 +33,22 @@ export type RevylConnection = ReturnType<typeof createRevylConnection>;
* before a device is rented. The verify call authenticates without starting anything, so a mistyped
* key costs one request rather than a session.
*/
export function createRevylConnection(host: ProviderPluginHost) {
export function createRevylConnection(
host: ProviderPluginHost,
artifactOrigins?: readonly string[],
) {
return {
resolve: ({ flags }: { flags: CliFlags; cwd: string }) => {
rejectRefusedProviderProfileFields(flags, REVYL_PROFILE_FIELDS);
requireRevylCredentials(host.env, `connect ${REVYL_PROVIDER}`);
const credentials = requireRevylCredentials(
host.env,
`connect ${REVYL_PROVIDER}`,
artifactOrigins,
);
resolveRevylArtifactOrigins(
credentials.artifactOrigins,
resolveRevylBackendUrl(credentials.apiUrl ?? REVYL_DEFAULT_BACKEND_URL),
);
const platform = requireConnectPlatform(flags);
const app = optionalAppUrl(flags.providerApp, host);
// Stored through the same canonicalizer allocation reads with, so the profile and the
Expand All @@ -58,7 +74,7 @@ export function createRevylConnection(host: ProviderPluginHost) {
const osVersion = revylTextFlag(flags.providerOsVersion);
const appUrl = revylTextFlag(flags.providerApp);
return await verifyRevylProfile(
requireRevylCredentials(host.env, `connect ${REVYL_PROVIDER}`),
requireRevylCredentials(host.env, `connect ${REVYL_PROVIDER}`, artifactOrigins),
host.clientVersion,
requireResolvedProfilePlatform(flags.platform, REVYL_SERVICE),
{
Expand Down Expand Up @@ -87,6 +103,7 @@ export async function verifyRevylProfile(
const client = createRevylClient({
apiKey: credentials.apiKey,
...(credentials.apiUrl === undefined ? {} : { backendUrl: credentials.apiUrl }),
artifactOrigins: credentials.artifactOrigins,
clientVersion,
});
await client.verifyCredentials();
Expand Down
23 changes: 21 additions & 2 deletions packages/provider-revyl/src/credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,47 @@ const REVYL_CREDENTIAL_REASONS = {

export const REVYL_API_KEY_VARIABLE = 'REVYL_API_KEY';
export const REVYL_API_URL_VARIABLE = 'REVYL_API_URL';
const REVYL_ARTIFACT_ORIGINS_VARIABLE = 'REVYL_ARTIFACT_ORIGINS';

export type RevylCredentials = Readonly<{
apiKey: string;
/** Overrides the Revyl backend origin; unset uses the public backend. */
apiUrl: string | undefined;
/** Exact storage origins that may receive session build bytes. */
artifactOrigins?: readonly string[];
}>;

type ResolvedRevylCredentials = RevylCredentials & Readonly<{ artifactOrigins: readonly string[] }>;

/**
* Reads the Revyl credentials from a host environment. `command` names the entry point in the
* refusal, so the caller who missed the key is told what to set and where to get it.
*/
export function requireRevylCredentials(
env: Readonly<Record<string, string | undefined>>,
command: string,
): RevylCredentials {
artifactOrigins?: readonly string[],
): ResolvedRevylCredentials {
const apiKey = env[REVYL_API_KEY_VARIABLE]?.trim();
if (apiKey) {
return { apiKey, apiUrl: env[REVYL_API_URL_VARIABLE]?.trim() || undefined };
return {
apiKey,
apiUrl: env[REVYL_API_URL_VARIABLE]?.trim() || undefined,
artifactOrigins:
artifactOrigins ?? splitArtifactOrigins(env[REVYL_ARTIFACT_ORIGINS_VARIABLE]),
};
}
throw new AppError('INVALID_ARGS', `${command} requires a Revyl API key.`, {
reason: REVYL_CREDENTIAL_REASONS.missingCredentials.reason,
hint: `Set ${REVYL_API_KEY_VARIABLE} to a Revyl API key. Revyl issues keys to its customers; a partner integration gets one through Revyl's partner program.`,
variables: [REVYL_API_KEY_VARIABLE],
});
}

function splitArtifactOrigins(value: string | undefined): readonly string[] {
if (!value?.trim()) return [];
return value
.split(',')
.map((origin) => origin.trim())
.filter(Boolean);
}
62 changes: 61 additions & 1 deletion packages/provider-revyl/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import { afterEach, test } from 'vitest';
import type { ProviderPluginHost } from 'agent-device/plugins';
import type { CliFlags } from '@agent-device/contracts/command';
Expand Down Expand Up @@ -44,7 +45,16 @@ async function verifyWith(
return await connection.verify({ flags: PROFILE_FLAGS, host: host(env) } as never);
}

test('the plugin declares only the variables it still reads from the environment', () => {
test('the credential declaration excludes daemon-owned storage policy', () => {
const manifest = JSON.parse(
readFileSync(new URL('../package.json', import.meta.url), 'utf8'),
) as {
agentDevicePlugin: { credentialVariables: string[] };
};
assert.deepEqual(manifest.agentDevicePlugin.credentialVariables, [
'REVYL_API_KEY',
'REVYL_API_URL',
]);
assert.deepEqual(revyl().credentials?.variables, ['REVYL_API_KEY', 'REVYL_API_URL']);
// A variable an option carries is no longer one the daemon compares against the shell, so a
// host-provided key is not reported as a rotated credential.
Expand All @@ -56,6 +66,56 @@ test('the plugin declares only the variables it still reads from the environment
revyl({ apiKey: 'k', apiUrl: 'https://backend.test' }).credentials?.variables,
[],
);
assert.deepEqual(
revyl({ apiKey: 'k', apiUrl: 'https://backend.test', artifactOrigins: [] }).credentials
?.variables,
[],
);
});

test('one artifactOrigins option entry cannot admit two storage origins', async () => {
const fixture = await backend();
const option = {
apiKey: 'option-key',
apiUrl: fixture.backendUrl,
artifactOrigins: ['https://storage.example,https://other.example'],
};
const connection = revyl(option).connection;
if (!connection?.resolve) throw new Error('Revyl declares a resolve callback');
await assert.rejects(
async () =>
await connection.resolve({ flags: PROFILE_FLAGS, cwd: '/tmp', host: host({}) } as never),
(error: unknown) =>
error instanceof AppError && error.details?.reason === 'invalid_provider_url',
);
await assert.rejects(
async () => await verifyWith(option, {}),
(error: unknown) =>
error instanceof AppError && error.details?.reason === 'invalid_provider_url',
);
await assert.rejects(
async () => await revyl(option).setup(host({})),
(error: unknown) =>
error instanceof AppError && error.details?.reason === 'invalid_provider_url',
);
assert.deepEqual(fixture.paths(), []);
const valid = await revyl({
...option,
artifactOrigins: ['https://storage.example', 'https://other.example'],
}).setup(host({}));
if (!('runtime' in valid)) throw new Error('Revyl sets up a runtime');
await valid.runtime.shutdown();
});

test('a key-only caller can verify while daemon storage policy stays daemon-owned', async () => {
const fixture = await backend();
await verifyWith({ apiUrl: fixture.backendUrl }, { REVYL_API_KEY: 'same-key' });
const setup = await revyl({ apiUrl: fixture.backendUrl }).setup(
host({ REVYL_API_KEY: 'same-key', REVYL_ARTIFACT_ORIGINS: 'https://storage.example' }),
);
if (!('runtime' in setup)) throw new Error('Revyl sets up a runtime');
assert.equal(setup.runtime.provider, 'revyl');
await setup.runtime.shutdown();
});

test('the plugin declares what a Revyl lease is and is not', () => {
Expand Down
24 changes: 15 additions & 9 deletions packages/provider-revyl/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,24 @@ export type RevylPluginOptions = {
apiKey?: string;
/** Used instead of `REVYL_API_URL` when set; the public backend is used when neither is set. */
apiUrl?: string;
/**
* Replaces `REVYL_ARTIFACT_ORIGINS` when set; an empty array admits no origin. Accepts exact HTTPS
* origins, or loopback HTTP matching the configured Revyl API URL.
*/
artifactOrigins?: readonly string[];
};

/**
Comment thread
thymikee marked this conversation as resolved.
Resolved
* Revyl cloud devices as a plugin: a device is rented from the Revyl backend and driven through its
* device-proxy actions, with no WebDriver or Appium session in between.
*
* The credentials come from `options`, with `REVYL_API_KEY` and the optional `REVYL_API_URL`
* environment variables as the fallback. The plugin declares only the variables it reads from the
* environment: a credential passed as an option is not part of the environment the daemon
* fingerprints.
* The configuration comes from `options`, with `REVYL_API_KEY`, optional `REVYL_API_URL`, and
* `REVYL_ARTIFACT_ORIGINS` environment variables as the fallback. Artifact origins are daemon
* policy and are not part of the credential fingerprint used when a client requests a lease.
*/
function revyl(options: RevylPluginOptions = {}): AgentDevicePlugin {
// Each option replaces its own variable and nothing else: an `apiUrl` with no `apiKey` still has
// the key read from the environment, and a `apiKey` with no `apiUrl` still reads the URL there.
// Each credential option replaces its own variable and nothing else: an `apiUrl` with no `apiKey`
// still has the key read from the environment, and an `apiKey` with no `apiUrl` still reads the URL.
const overridden: Record<string, string> = {};
if (options.apiKey !== undefined) overridden[REVYL_API_KEY_VARIABLE] = options.apiKey;
if (options.apiUrl !== undefined) overridden[REVYL_API_URL_VARIABLE] = options.apiUrl;
Expand Down Expand Up @@ -53,16 +57,18 @@ function revyl(options: RevylPluginOptions = {}): AgentDevicePlugin {
},
resolve: (context) =>
import('./connection.ts').then(({ createRevylConnection }) =>
createRevylConnection(withCredentials(context.host)).resolve(context),
createRevylConnection(withCredentials(context.host), options.artifactOrigins).resolve(
context,
),
),
verify: async (context) =>
await (
await import('./connection.ts')
)
.createRevylConnection(withCredentials(context.host))
.createRevylConnection(withCredentials(context.host), options.artifactOrigins)
.verify(context),
},
setup: async (host) => await setupRevylPlugin(withCredentials(host)),
setup: async (host) => await setupRevylPlugin(withCredentials(host), options.artifactOrigins),
};
}

Expand Down
4 changes: 3 additions & 1 deletion packages/provider-revyl/src/plugin-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ import type { RevylRuntimeRegistration } from './runtime.ts';
*/
export async function setupRevylPlugin(
host: ProviderPluginHost,
artifactOrigins?: readonly string[],
): Promise<RevylRuntimeRegistration> {
const { requireRevylCredentials } = await import('./credentials.ts');
const { createRevylRuntime } = await import('./runtime.ts');
const credentials = requireRevylCredentials(host.env, 'Revyl plugin setup');
const credentials = requireRevylCredentials(host.env, 'Revyl plugin setup', artifactOrigins);
return createRevylRuntime({
apiKey: credentials.apiKey,
...(credentials.apiUrl === undefined ? {} : { backendUrl: credentials.apiUrl }),
artifactOrigins: credentials.artifactOrigins,
clientVersion: host.clientVersion,
});
}
5 changes: 4 additions & 1 deletion packages/provider-revyl/src/revyl-backend.fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ export type RevylFixtureBackend = Readonly<{
/** The decoded body of the last request whose path ends with `action`. */
bodyFor(action: string): Record<string, unknown>;
createRuntime(
options?: Partial<Readonly<{ deviceModel: string; osVersion: string }>>,
options?: Partial<
Readonly<{ deviceModel: string; osVersion: string; artifactOrigins: readonly string[] }>
>,
): RevylRuntime;
close(): Promise<void>;
}>;
Expand Down Expand Up @@ -279,6 +281,7 @@ export async function startRevylFixtureBackend(
createRevylRuntime({
apiKey: 'test-key',
backendUrl,
artifactOrigins: runtimeOptions?.artifactOrigins ?? [backendUrl, 'https://storage.test'],
clientVersion: '0.0.0-test',
...(runtimeOptions?.deviceModel === undefined
? {}
Expand Down
Loading