-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(cli) Add command to Update a Secret #2990
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
AzamAbdul
merged 5 commits into
agent/browse-v4-7-context-names
from
feat/cli-update-secret-v4
Sep 23, 2026
+296
−12
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
106265f
Add encrypted secret update command
AzamAbdul 8a00eef
Clarify secrets command topic description
AzamAbdul bab1ec7
Update packages/cli/tests/cli-secrets-update-contract.test.ts
AzamAbdul 5a33940
docs(cli): add concrete secret ID examples
AzamAbdul 9a8a6f3
docs(cli): clarify secret ID argument descriptions
AzamAbdul File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "browse": minor | ||
| --- | ||
|
|
||
| Add `browse cloud secrets update` to replace a secret value by ID with local encryption and stdin, environment variable, or hidden prompt input. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import { Args } from "@oclif/core"; | ||
| import { BrowseCommand } from "../../../base.js"; | ||
| import { apiCommonFlags, toApiOptions } from "../../../lib/cloud/flags.js"; | ||
| import { updateSecret } from "../../../lib/secrets/api.js"; | ||
| import { readSecretValue } from "../../../lib/secrets/input.js"; | ||
| import { secretInputFlags } from "../../../lib/secrets/flags.js"; | ||
| import { outputJson } from "../../../lib/output.js"; | ||
|
|
||
| export default class SecretsUpdate extends BrowseCommand { | ||
| static override description = | ||
| "Replace a secret value, encrypting it locally with the current project public key."; | ||
| static override examples = [ | ||
| "browse cloud secrets update <secretId>", | ||
| "browse cloud secrets update d2c4f48f-38e9-4b82-a36a-2b373fd14a65", | ||
| "browse cloud secrets update <secretId> --env MY_SERVICE_TOKEN", | ||
| "browse cloud secrets update <secretId> --stdin < ./secret.txt", | ||
| ]; | ||
| static override args = { | ||
| secretId: Args.string({ | ||
| description: | ||
| "Project secret ID (e.g. d2c4f48f-38e9-4b82-a36a-2b373fd14a65).", | ||
| required: true, | ||
| }), | ||
| }; | ||
| static override flags = { ...apiCommonFlags, ...secretInputFlags }; | ||
| async run(): Promise<void> { | ||
| const { args, flags } = await this.parse(SecretsUpdate); | ||
| const options = toApiOptions(flags); | ||
| const value = await readSecretValue({ stdin: flags.stdin, env: flags.env }); | ||
| try { | ||
| outputJson(await updateSecret(options, args.secretId, value)); | ||
| } finally { | ||
| value.fill(0); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,217 @@ | ||
| import { Aes256Gcm, CipherSuite, HkdfSha256 } from "@hpke/core"; | ||
| import { DhkemX25519HkdfSha256 } from "@hpke/dhkem-x25519"; | ||
| import { afterEach, describe, expect, it } from "vitest"; | ||
| import { | ||
| jsonResponse, | ||
| startFakeBrowserbaseServer, | ||
| type FakeBrowserbaseServer, | ||
| } from "./helpers/fake-browserbase-server.js"; | ||
| import { runCli } from "./helpers/run-cli.js"; | ||
|
|
||
| const secretId = "d2c4f48f-38e9-4b82-a36a-2b373fd14a65"; | ||
| const metadata = { id: secretId, secretKey: "SERVICE_TOKEN" }; | ||
| const env = { | ||
| BROWSERBASE_API_KEY: "test-key", | ||
| BROWSE_LOAD_DOTENV: "0", | ||
| BROWSERBASE_TELEMETRY_DISABLED: "1", | ||
| }; | ||
| let server: FakeBrowserbaseServer | undefined; | ||
| afterEach(async () => { | ||
| await server?.close(); | ||
| server = undefined; | ||
| }); | ||
|
|
||
| function suite() { | ||
| return new CipherSuite({ | ||
| kem: new DhkemX25519HkdfSha256(), | ||
| kdf: new HkdfSha256(), | ||
| aead: new Aes256Gcm(), | ||
| }); | ||
| } | ||
|
|
||
| async function keypair() { | ||
| const crypto = suite(); | ||
| const pair = await crypto.kem.generateKeyPair(); | ||
| const publicKey = Buffer.from( | ||
| await crypto.kem.serializePublicKey(pair.publicKey), | ||
| ).toString("base64"); | ||
| return { crypto, pair, publicKey }; | ||
| } | ||
|
|
||
| describe("secret update CLI HTTP contracts", () => { | ||
| it.each(["stdin", "env"])( | ||
| "fetches the public key and encrypts exact %s bytes", | ||
| async (source) => { | ||
| const { crypto, pair, publicKey } = await keypair(); | ||
| const value = " token-🔑\nwith-whitespace\r\n"; | ||
| server = await startFakeBrowserbaseServer((request, response) => { | ||
| if (request.path === "/v1/secrets/keypair") | ||
| jsonResponse(response, 200, { id: "keypair-1", publicKey }); | ||
| else jsonResponse(response, 200, metadata); | ||
| }); | ||
| const result = await runCli( | ||
| [ | ||
| "cloud", | ||
| "secrets", | ||
| "update", | ||
| secretId, | ||
| ...(source === "stdin" | ||
| ? ["--stdin"] | ||
| : ["--env", "BROWSE_TEST_SECRET_VALUE"]), | ||
| "--base-url", | ||
| server.baseUrl, | ||
| ], | ||
| source === "stdin" | ||
| ? { env, stdin: value } | ||
| : { env: { ...env, BROWSE_TEST_SECRET_VALUE: value } }, | ||
| ); | ||
| expect(result.exitCode, result.stderr).toBe(0); | ||
| expect(JSON.parse(result.stdout)).toEqual(metadata); | ||
| expect(server.requests.map((r) => [r.method, r.path])).toEqual([ | ||
| ["GET", "/v1/secrets/keypair"], | ||
| ["PATCH", `/v1/secrets/${secretId}`], | ||
| ]); | ||
| for (const request of server.requests) | ||
| expect(request.headers["x-bb-api-key"]).toBe("test-key"); | ||
| const body = server.requests[1]!.jsonBody as { | ||
| keypairId: string; | ||
| sealedSecretValue: string; | ||
| secretKey?: string; | ||
| }; | ||
| expect(body.keypairId).toBe("keypair-1"); | ||
| expect(Object.keys(body).sort()).toEqual( | ||
| ["sealedSecretValue", "keypairId"].sort(), | ||
| ); | ||
| expect(body.secretKey).toBeUndefined(); | ||
| expect(server.requests[1]!.headers["content-type"]).toBe( | ||
| "application/json", | ||
| ); | ||
| const blob = Buffer.from(body.sealedSecretValue, "base64"); | ||
| const recipient = await crypto.createRecipientContext({ | ||
| recipientKey: pair.privateKey, | ||
| enc: new Uint8Array(blob.subarray(0, 32)).buffer, | ||
| }); | ||
| expect( | ||
| Buffer.from( | ||
| await recipient.open(new Uint8Array(blob.subarray(32)).buffer), | ||
| ).toString("utf8"), | ||
| ).toBe(value); | ||
| expect(server.requests[1]!.bodyText).not.toContain("token-🔑"); | ||
| expect(result.stdout + result.stderr).not.toContain("token-🔑"); | ||
| }, | ||
| ); | ||
|
|
||
| it("stops if key retrieval fails", async () => { | ||
| server = await startFakeBrowserbaseServer((_request, response) => | ||
| jsonResponse(response, 403, { message: "Forbidden" }), | ||
| ); | ||
| const result = await runCli( | ||
| [ | ||
| "cloud", | ||
| "secrets", | ||
| "update", | ||
| "TOKEN", | ||
| "--stdin", | ||
| "--base-url", | ||
| server.baseUrl, | ||
| ], | ||
| { env, stdin: "private-value" }, | ||
| ); | ||
| expect(result.exitCode).not.toBe(0); | ||
| expect(server.requests).toHaveLength(1); | ||
| expect(result.stderr).toContain("Forbidden"); | ||
| expect(result.stdout + result.stderr).not.toContain("private-value"); | ||
| }); | ||
|
|
||
| it("rejects a malformed public key without submitting a secret", async () => { | ||
| server = await startFakeBrowserbaseServer((_request, response) => | ||
| jsonResponse(response, 200, { id: "keypair-1", publicKey: "bad" }), | ||
| ); | ||
| const result = await runCli( | ||
| [ | ||
| "cloud", | ||
| "secrets", | ||
| "update", | ||
| "TOKEN", | ||
| "--stdin", | ||
| "--base-url", | ||
| server.baseUrl, | ||
| ], | ||
| { env, stdin: "private-value" }, | ||
| ); | ||
| expect(result.exitCode).not.toBe(0); | ||
| expect(server.requests).toHaveLength(1); | ||
| expect(result.stderr).toContain("invalid X25519 public key"); | ||
| expect(result.stdout + result.stderr).not.toContain("private-value"); | ||
| }); | ||
|
|
||
| it("requires --stdin for noninteractive input", async () => { | ||
| const result = await runCli(["cloud", "secrets", "update", "TOKEN"], { | ||
| env, | ||
| stdin: "private-value", | ||
| }); | ||
| expect(result.exitCode).not.toBe(0); | ||
| expect(result.stderr).toContain("Use --stdin"); | ||
| expect(result.stdout + result.stderr).not.toContain("private-value"); | ||
| }); | ||
|
|
||
| it("reports a missing secret without retrying the update", async () => { | ||
| const { publicKey } = await keypair(); | ||
| server = await startFakeBrowserbaseServer((request, response) => { | ||
| if (request.path === "/v1/secrets/keypair") { | ||
| jsonResponse(response, 200, { id: "keypair-1", publicKey }); | ||
| } else { | ||
| jsonResponse(response, 404, { message: "Secret not found" }); | ||
| } | ||
| }); | ||
| const result = await runCli( | ||
| [ | ||
| "cloud", | ||
| "secrets", | ||
| "update", | ||
| "TOKEN", | ||
| "--stdin", | ||
| "--base-url", | ||
| server.baseUrl, | ||
| ], | ||
| { env, stdin: "private-value" }, | ||
| ); | ||
| expect(result.exitCode).not.toBe(0); | ||
| expect(result.stderr).toContain("Secret not found"); | ||
| expect(server.requests).toHaveLength(2); | ||
| expect(result.stdout + result.stderr).not.toContain("private-value"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("environment secret input validation", () => { | ||
| it.each([ | ||
| ["--env", "BROWSE_TEST_MISSING_SECRET_VALUE"], | ||
| ["--env", ""], | ||
| ["--env", "BROWSE_TEST_SECRET_VALUE", "--stdin"], | ||
| ])("rejects invalid input flags %j before requesting", async (...flags) => { | ||
| server = await startFakeBrowserbaseServer((_request, response) => | ||
| jsonResponse(response, 200, {}), | ||
| ); | ||
| const result = await runCli( | ||
| [ | ||
| "cloud", | ||
| "secrets", | ||
| "update", | ||
| "TOKEN", | ||
| "--base-url", | ||
| server.baseUrl, | ||
| ...flags, | ||
| ], | ||
| { | ||
| env: { | ||
| ...env, | ||
| BROWSE_TEST_MISSING_SECRET_VALUE: undefined, | ||
| BROWSE_TEST_SECRET_VALUE: "private-value", | ||
| }, | ||
| }, | ||
| ); | ||
| expect(result.exitCode).not.toBe(0); | ||
| expect(server.requests).toHaveLength(0); | ||
| expect(result.stdout + result.stderr).not.toContain("private-value"); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.