Skip to content

Commit

Permalink
Inherit bindings in versions secret put
Browse files Browse the repository at this point in the history
  • Loading branch information
WillTaylorDev committed Feb 7, 2025
1 parent 0c0374c commit ad746b8
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/kind-sloths-speak.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": minor
---

Bugfix: Modified versions secret put to inherit all known bindings, which circumvents a limitation in the API which does not return all fields for all bindings.
5 changes: 5 additions & 0 deletions packages/wrangler/src/__tests__/versions/secrets/bulk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ describe("versions secret bulk", () => {
mockSetupApiCalls();
mockPostVersion((metadata) => {
expect(metadata.bindings).toStrictEqual([
{ type: "inherit", name: "do-binding" },
{ type: "secret_text", name: "SECRET_1", text: "secret-1" },
{ type: "secret_text", name: "SECRET_2", text: "secret-2" },
{ type: "secret_text", name: "SECRET_3", text: "secret-3" },
Expand Down Expand Up @@ -119,6 +120,7 @@ describe("versions secret bulk", () => {
mockSetupApiCalls();
mockPostVersion((metadata) => {
expect(metadata.bindings).toStrictEqual([
{ type: "inherit", name: "do-binding" },
{ type: "secret_text", name: "SECRET_1", text: "secret-1" },
{ type: "secret_text", name: "SECRET_2", text: "secret-2" },
{ type: "secret_text", name: "SECRET_3", text: "secret-3" },
Expand Down Expand Up @@ -164,6 +166,7 @@ describe("versions secret bulk", () => {
mockSetupApiCalls();
mockPostVersion((metadata) => {
expect(metadata.bindings).toStrictEqual([
{ type: "inherit", name: "do-binding" },
{ type: "secret_text", name: "SECRET_1", text: "secret-1" },
{ type: "secret_text", name: "SECRET_2", text: "secret-2" },
{ type: "secret_text", name: "SECRET_3", text: "secret-3" },
Expand Down Expand Up @@ -205,6 +208,7 @@ describe("versions secret bulk", () => {
mockSetupApiCalls();
mockPostVersion((metadata) => {
expect(metadata.bindings).toStrictEqual([
{ type: "inherit", name: "do-binding" },
{ type: "secret_text", name: "SECRET_1", text: "secret-1" },
{ type: "secret_text", name: "SECRET_2", text: "secret-2" },
{ type: "secret_text", name: "SECRET_3", text: "secret-3" },
Expand Down Expand Up @@ -249,6 +253,7 @@ describe("versions secret bulk", () => {
mockSetupApiCalls();
mockPostVersion((metadata) => {
expect(metadata.bindings).toStrictEqual([
{ type: "inherit", name: "do-binding" },
{ type: "secret_text", name: "SECRET_1", text: "secret-1" },
{ type: "secret_text", name: "SECRET_2", text: "secret-2" },
{ type: "secret_text", name: "SECRET_3", text: "secret-3" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe("versions secret delete", () => {
mockPostVersion((metadata) => {
// We should have all secrets except the one being deleted
expect(metadata.bindings).toStrictEqual([
{ type: "inherit", name: "do-binding" },
{ type: "inherit", name: "ANOTHER_SECRET" },
{ type: "inherit", name: "YET_ANOTHER_SECRET" },
]);
Expand All @@ -55,6 +56,7 @@ describe("versions secret delete", () => {
mockGetVersion();
mockPostVersion((metadata) => {
expect(metadata.bindings).toStrictEqual([
{ type: "inherit", name: "do-binding" },
{ type: "inherit", name: "ANOTHER_SECRET" },
{ type: "inherit", name: "YET_ANOTHER_SECRET" },
]);
Expand Down Expand Up @@ -82,6 +84,7 @@ describe("versions secret delete", () => {
mockGetVersion();
mockPostVersion((metadata) => {
expect(metadata.bindings).toStrictEqual([
{ type: "inherit", name: "do-binding" },
{ type: "inherit", name: "ANOTHER_SECRET" },
{ type: "inherit", name: "YET_ANOTHER_SECRET" },
]);
Expand Down
40 changes: 40 additions & 0 deletions packages/wrangler/src/__tests__/versions/secrets/put.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ describe("versions secret put", () => {
mockSetupApiCalls();
mockPostVersion((metadata) => {
expect(metadata.bindings).toStrictEqual([
{ type: "inherit", name: "do-binding" },
{ type: "secret_text", name: "NEW_SECRET", text: "the-secret" },
]);
expect(metadata.keep_bindings).toStrictEqual([
Expand Down Expand Up @@ -97,6 +98,7 @@ describe("versions secret put", () => {
mockSetupApiCalls();
mockPostVersion((metadata) => {
expect(metadata.bindings).toStrictEqual([
{ type: "inherit", name: "do-binding" },
{ type: "secret_text", name: "NEW_SECRET", text: "the-secret" },
]);
expect(metadata.keep_bindings).toStrictEqual([
Expand Down Expand Up @@ -138,6 +140,7 @@ describe("versions secret put", () => {
mockSetupApiCalls();
mockPostVersion((metadata) => {
expect(metadata.bindings).toStrictEqual([
{ type: "inherit", name: "do-binding" },
{ type: "secret_text", name: "NEW_SECRET", text: "the-secret" },
]);
expect(metadata.keep_bindings).toStrictEqual([
Expand Down Expand Up @@ -178,6 +181,7 @@ describe("versions secret put", () => {
mockSetupApiCalls();
mockPostVersion((metadata) => {
expect(metadata.bindings).toStrictEqual([
{ type: "inherit", name: "do-binding" },
{ type: "secret_text", name: "NEW_SECRET", text: "the-secret" },
]);
expect(metadata.keep_bindings).toStrictEqual([
Expand Down Expand Up @@ -208,6 +212,7 @@ describe("versions secret put", () => {
mockSetupApiCalls();
mockPostVersion((metadata) => {
expect(metadata.bindings).toStrictEqual([
{ type: "inherit", name: "do-binding" },
{ type: "secret_text", name: "NEW_SECRET", text: "the-secret" },
]);
expect(metadata.keep_bindings).toStrictEqual([
Expand Down Expand Up @@ -245,6 +250,7 @@ describe("versions secret put", () => {
mockSetupApiCalls();
mockPostVersion((metadata) => {
expect(metadata.bindings).toStrictEqual([
{ type: "inherit", name: "do-binding" },
{ type: "secret_text", name: "NEW_SECRET", text: "the-secret" },
]);
expect(metadata.keep_bindings).toStrictEqual([
Expand Down Expand Up @@ -273,6 +279,38 @@ describe("versions secret put", () => {
expect(std.err).toMatchInlineSnapshot(`""`);
});

test("all non-secret bindings are inherited", async () => {
setIsTTY(true);

mockSetupApiCalls();

mockPrompt({
text: "Enter a secret value:",
options: { isSecret: true },
result: "the-secret",
});

mockPostVersion((metadata) => {
expect(metadata.bindings).toStrictEqual([
{ type: "inherit", name: "do-binding" },
{ type: "secret_text", name: "SECRET", text: "the-secret" },
]);
expect(metadata.keep_bindings).toStrictEqual([
"secret_key",
"secret_text",
]);
expect(metadata.annotations).not.toBeUndefined();
});
await runWrangler("versions secret put SECRET --name script-name");

expect(std.out).toMatchInlineSnapshot(`
"🌀 Creating the secret for the Worker \\"script-name\\"
✨ Success! Created version id with secret SECRET.
➡️ To deploy this version with secret SECRET to production traffic use the command \\"wrangler versions deploy\\"."
`);
expect(std.err).toMatchInlineSnapshot(`""`);
});

test("can update an existing secret", async () => {
setIsTTY(true);

Expand All @@ -285,6 +323,7 @@ describe("versions secret put", () => {
mockSetupApiCalls();
mockPostVersion((metadata) => {
expect(metadata.bindings).toStrictEqual([
{ type: "inherit", name: "do-binding" },
{ type: "secret_text", name: "SECRET", text: "the-secret" },
]);
expect(metadata.keep_bindings).toStrictEqual([
Expand Down Expand Up @@ -361,6 +400,7 @@ describe("versions secret put", () => {
expect((formData.get("module.wasm") as File).size).equal(10);

expect(metadata.bindings).toStrictEqual([
{ type: "inherit", name: "do-binding" },
{ type: "secret_text", name: "SECRET", text: "the-secret" },
]);
expect(metadata.keep_bindings).toStrictEqual([
Expand Down
5 changes: 5 additions & 0 deletions packages/wrangler/src/__tests__/versions/secrets/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ export function mockGetVersion(versionInfo?: VersionDetails) {
name: "YET_ANOTHER_SECRET",
text: "Yet another secret shhhhh",
},
{
name: "do-binding",
type: "durable_object_namespace",
namespace_id: "some-namespace-id",
},
],
script: {
etag: "etag",
Expand Down
21 changes: 9 additions & 12 deletions packages/wrangler/src/versions/secrets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,15 @@ export async function copyWorkerVersionWithNewSecrets({
);

// Filter out secrets because we're gonna inherit them
const bindings: WorkerMetadataBinding[] =
versionInfo.resources.bindings.filter(
(binding) => binding.type !== "secret_text"
);

// We cannot upload a DO with a namespace_id so remove it
for (const binding of bindings) {
if (binding.type === "durable_object_namespace") {
// @ts-expect-error - it doesn't exist within wrangler but does in the API
delete binding.namespace_id;
}
}
let bindings: WorkerMetadataBinding[] = versionInfo.resources.bindings

Check failure on line 125 in packages/wrangler/src/versions/secrets/index.ts

View workflow job for this annotation

GitHub Actions / Checks

'bindings' is never reassigned. Use 'const' instead
.filter((binding) => binding.type !== "secret_text")
.map((binding) => {
// Inherit all of the existing bindings
return {
name: binding.name,
type: "inherit",
};
});

// Add the new secrets
for (const secret of secrets) {
Expand Down

0 comments on commit ad746b8

Please sign in to comment.