Skip to content

Commit 3c92883

Browse files
author
Adrian Gracia
committed
SQC-354 add mtls fields to hyperdrive
1 parent c8fab4d commit 3c92883

File tree

6 files changed

+145
-9
lines changed

6 files changed

+145
-9
lines changed

packages/wrangler/src/__tests__/hyperdrive.test.ts

Lines changed: 105 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -540,18 +540,58 @@ describe("hyperdrive commands", () => {
540540
`);
541541
});
542542

543+
it("should create a hyperdrive with mtls config", async () => {
544+
const reqProm = mockHyperdriveCreate();
545+
await runWrangler(
546+
"hyperdrive create test123 --host=example.com --database=neondb --user=test --password=password --port=1234 --ca-certificate-uuid=12345 --mtls-certificate-uuid=1234"
547+
);
548+
await expect(reqProm).resolves.toMatchInlineSnapshot(`
549+
Object {
550+
"mtls": Object {
551+
"ca_certificate_uuid": "12345",
552+
"mtls_certificate_uuid": "1234",
553+
},
554+
"name": "test123",
555+
"origin": Object {
556+
"database": "neondb",
557+
"host": "example.com",
558+
"password": "password",
559+
"port": 1234,
560+
"scheme": "postgresql",
561+
"user": "test",
562+
},
563+
}
564+
`);
565+
expect(std.out).toMatchInlineSnapshot(`
566+
"🚧 Creating 'test123'
567+
✅ Created new Hyperdrive config: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
568+
📋 To start using your config from a Worker, add the following binding configuration to your Wrangler configuration file:
569+
570+
{
571+
\\"hyperdrive\\": [
572+
{
573+
\\"binding\\": \\"HYPERDRIVE\\",
574+
\\"id\\": \\"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\\"
575+
}
576+
]
577+
}"
578+
`);
579+
});
580+
543581
it("should handle listing configs", async () => {
544582
mockHyperdriveGetListOrDelete();
545583
await runWrangler("hyperdrive list");
546584
expect(std.out).toMatchInlineSnapshot(`
547585
"📋 Listing Hyperdrive configs
548-
┌──────────────────────────────────────┬─────────┬────────┬────────────────┬──────┬──────────┬───────────────────┐
549-
│ id │ name │ user │ host │ port │ database │ caching │
550-
├──────────────────────────────────────┼─────────┼────────┼────────────────┼──────┼──────────┼───────────────────┤
551-
│ xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx │ test123 │ test │ example.com │ 5432 │ neondb │ │
552-
├──────────────────────────────────────┼─────────┼────────┼────────────────┼──────┼──────────┼───────────────────┤
553-
│ yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy │ new-db │ dbuser │ www.google.com │ 3211 │ mydb │ {\\"disabled\\":true} │
554-
└──────────────────────────────────────┴─────────┴────────┴────────────────┴──────┴──────────┴───────────────────┘"
586+
┌──────────────────────────────────────┬─────────────┬─────────┬────────────────┬──────┬───────────┬───────────────────┬───────────────────────────────────────────────────────────────┐
587+
│ id │ name │ user │ host │ port │ database │ caching │ mtls │
588+
├──────────────────────────────────────┼─────────────┼─────────┼────────────────┼──────┼───────────┼───────────────────┼───────────────────────────────────────────────────────────────┤
589+
│ xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx │ test123 │ test │ example.com │ 5432 │ neondb │ │ │
590+
├──────────────────────────────────────┼─────────────┼─────────┼────────────────┼──────┼───────────┼───────────────────┼───────────────────────────────────────────────────────────────┤
591+
│ yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy │ new-db │ dbuser │ www.google.com │ 3211 │ mydb │ {\\"disabled\\":true} │ │
592+
├──────────────────────────────────────┼─────────────┼─────────┼────────────────┼──────┼───────────┼───────────────────┼───────────────────────────────────────────────────────────────┤
593+
│ zzzzzzzz-zzzz-zzzz-zzzz-zzzzzzzzzzzz │ new-db-mtls │ pg-mtls │ www.mtls.com │ 3212 │ mydb-mtls │ │ {\\"ca_certificate_uuid\\":\\"1234\\",\\"mtls_certificate_uuid\\":\\"1234\\"} │
594+
└──────────────────────────────────────┴─────────────┴─────────┴────────────────┴──────┴───────────┴───────────────────┴───────────────────────────────────────────────────────────────┘"
555595
`);
556596
});
557597

@@ -856,6 +896,41 @@ describe("hyperdrive commands", () => {
856896
"
857897
`);
858898
});
899+
900+
it("should handle updating a hyperdrive config's mtls configuration", async () => {
901+
const reqProm = mockHyperdriveUpdate();
902+
await runWrangler(
903+
"hyperdrive update xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx --ca-certificate-uuid=2345 --mtls-certificate-uuid=234"
904+
);
905+
await expect(reqProm).resolves.toMatchInlineSnapshot(`
906+
Object {
907+
"mtls": Object {
908+
"ca_certificate_uuid": "2345",
909+
"mtls_certificate_uuid": "234",
910+
},
911+
}
912+
`);
913+
expect(std.out).toMatchInlineSnapshot(`
914+
"🚧 Updating 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
915+
✅ Updated xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx Hyperdrive config
916+
{
917+
\\"id\\": \\"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\\",
918+
\\"name\\": \\"test123\\",
919+
\\"origin\\": {
920+
\\"scheme\\": \\"postgresql\\",
921+
\\"host\\": \\"example.com\\",
922+
\\"port\\": 5432,
923+
\\"database\\": \\"neondb\\",
924+
\\"user\\": \\"test\\"
925+
},
926+
\\"mtls\\": {
927+
\\"ca_certificate_uuid\\": \\"2345\\",
928+
\\"mtls_certificate_uuid\\": \\"234\\"
929+
}
930+
}"
931+
`);
932+
});
933+
859934
});
860935

861936
const defaultConfig: HyperdriveConfig = {
@@ -908,6 +983,21 @@ function mockHyperdriveGetListOrDelete() {
908983
disabled: true,
909984
},
910985
},
986+
{
987+
id: "zzzzzzzz-zzzz-zzzz-zzzz-zzzzzzzzzzzz",
988+
name: "new-db-mtls",
989+
origin: {
990+
host: "www.mtls.com",
991+
port: 3212,
992+
database: "mydb-mtls",
993+
user: "pg-mtls",
994+
scheme: "pg-mtls",
995+
},
996+
mtls: {
997+
ca_certificate_uuid: "1234",
998+
mtls_certificate_uuid: "1234",
999+
},
1000+
},
9111001
],
9121002
true
9131003
)
@@ -955,6 +1045,12 @@ function mockHyperdriveUpdate(): Promise<PatchHyperdriveBody> {
9551045
delete origin.port;
9561046
}
9571047
}
1048+
let mtls = defaultConfig.mtls;
1049+
if (mtls && reqBody.mtls) {
1050+
mtls.ca_certificate_uuid = reqBody.mtls.ca_certificate_uuid;
1051+
mtls.mtls_certificate_uuid = reqBody.mtls.mtls_certificate_uuid;
1052+
1053+
}
9581054

9591055
return HttpResponse.json(
9601056
createFetchResult(
@@ -963,6 +1059,7 @@ function mockHyperdriveUpdate(): Promise<PatchHyperdriveBody> {
9631059
name: reqBody.name ?? defaultConfig.name,
9641060
origin,
9651061
caching: reqBody.caching ?? defaultConfig.caching,
1062+
mtls: reqBody.mtls,
9661063
},
9671064
true
9681065
)
@@ -999,6 +1096,7 @@ function mockHyperdriveCreate(): Promise<CreateUpdateHyperdriveBody> {
9991096
access_client_id: reqBody.origin.access_client_id,
10001097
},
10011098
caching: reqBody.caching,
1099+
mtls: reqBody.mtls,
10021100
},
10031101
true
10041102
)

packages/wrangler/src/hyperdrive/client.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export type HyperdriveConfig = {
77
name: string;
88
origin: PublicOrigin;
99
caching?: CachingOptions;
10+
mtls?: Mtls;
1011
};
1112

1213
export type OriginDatabase = {
@@ -73,14 +74,21 @@ export type CreateUpdateHyperdriveBody = {
7374
name: string;
7475
origin: OriginWithSecrets;
7576
caching?: CachingOptions;
77+
mtls ?: Mtls;
7678
};
7779

7880
export type PatchHyperdriveBody = {
7981
name?: string;
8082
origin?: OriginWithSecretsPartial;
8183
caching?: CachingOptions;
84+
mtls?: Mtls;
8285
};
8386

87+
export type Mtls = {
88+
ca_certificate_uuid?: string;
89+
mtls_certificate_uuid?: string;
90+
}
91+
8492
export async function createConfig(
8593
config: Config,
8694
body: CreateUpdateHyperdriveBody

packages/wrangler/src/hyperdrive/create.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { configFileName, formatConfigSnippet, readConfig } from "../config";
22
import { logger } from "../logger";
33
import { createConfig } from "./client";
4-
import { getCacheOptionsFromArgs, getOriginFromArgs, upsertOptions } from ".";
4+
import { getCacheOptionsFromArgs, getOriginFromArgs, getMtlsFromArgs, upsertOptions } from ".";
55
import type {
66
CommonYargsArgv,
77
StrictYargsOptionsToInterface,
@@ -32,6 +32,7 @@ export async function handler(
3232
name: args.name,
3333
origin,
3434
caching: getCacheOptionsFromArgs(args),
35+
mtls: getMtlsFromArgs(args),
3536
});
3637
logger.log(`✅ Created new Hyperdrive config: ${database.id}`);
3738
logger.log(

packages/wrangler/src/hyperdrive/index.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type {
1212
} from "../yargs-types";
1313
import type {
1414
CachingOptions,
15+
Mtls,
1516
NetworkOriginWithSecrets,
1617
OriginDatabaseWithSecrets,
1718
OriginWithSecrets,
@@ -116,6 +117,16 @@ export function upsertOptions<T>(yargs: Argv<T>) {
116117
describe:
117118
"Indicates the number of seconds cache may serve the response after it becomes stale, cannot be set when caching is disabled",
118119
},
120+
"ca-certificate-uuid": {
121+
type: "string",
122+
describe:
123+
"Sets custom CA certificate when connecting to origin database. Must be valid UUID of already uploaded certificate via cert command.",
124+
},
125+
"mtls-certificate-uuid": {
126+
type: "string",
127+
describe:
128+
"Sets custom mTLS client certificates when connecting to origin database. Must be valid UUID of already uploaded certificates via cert command.",
129+
},
119130
})
120131
.group(
121132
["connection-string"],
@@ -297,3 +308,18 @@ export function getCacheOptionsFromArgs(
297308
return caching;
298309
}
299310
}
311+
312+
export function getMtlsFromArgs(
313+
args: StrictYargsOptionsToInterface<typeof upsertOptions>
314+
): Mtls | undefined {
315+
const mtls = {
316+
ca_certificate_uuid: args.caCertificateUuid,
317+
mtls_certificate_uuid: args.mtlsCertificateUuid,
318+
};
319+
320+
if (JSON.stringify(mtls) === "{}") {
321+
return undefined;
322+
} else {
323+
return mtls;
324+
}
325+
}

packages/wrangler/src/hyperdrive/list.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type {
55
CommonYargsArgv,
66
StrictYargsOptionsToInterface,
77
} from "../yargs-types";
8+
import { getMtlsFromArgs } from ".";
89

910
export function options(yargs: CommonYargsArgv) {
1011
return yargs;
@@ -26,6 +27,7 @@ export async function handler(
2627
port: database.origin.port?.toString() ?? "",
2728
database: database.origin.database ?? "",
2829
caching: JSON.stringify(database.caching),
30+
mtls: JSON.stringify(database.mtls),
2931
}))
3032
);
3133
}

packages/wrangler/src/hyperdrive/update.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { readConfig } from "../config";
22
import { logger } from "../logger";
33
import { patchConfig } from "./client";
4-
import { getCacheOptionsFromArgs, getOriginFromArgs, upsertOptions } from ".";
4+
import { getCacheOptionsFromArgs, getMtlsFromArgs, getOriginFromArgs, upsertOptions } from ".";
55
import type {
66
CommonYargsArgv,
77
StrictYargsOptionsToInterface,
@@ -32,6 +32,7 @@ export async function handler(
3232
name: args.name,
3333
origin,
3434
caching: getCacheOptionsFromArgs(args),
35+
mtls: getMtlsFromArgs(args),
3536
});
3637
logger.log(
3738
`✅ Updated ${updated.id} Hyperdrive config\n`,

0 commit comments

Comments
 (0)