@caelo-cms/provisioning 0.11.9 → 0.11.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapter.d.ts +6 -0
- package/dist/adapter.d.ts.map +1 -1
- package/dist/admin-domain.d.ts +10 -1
- package/dist/admin-domain.d.ts.map +1 -1
- package/dist/admin-domain.js +13 -3
- package/dist/admin-domain.js.map +1 -1
- package/dist/adopt-existing.d.ts +12 -3
- package/dist/adopt-existing.d.ts.map +1 -1
- package/dist/adopt-existing.js +143 -0
- package/dist/adopt-existing.js.map +1 -1
- package/dist/agent/command.d.ts +23 -0
- package/dist/agent/command.d.ts.map +1 -1
- package/dist/agent/command.js +32 -9
- package/dist/agent/command.js.map +1 -1
- package/dist/agent/run.d.ts +2 -0
- package/dist/agent/run.d.ts.map +1 -1
- package/dist/agent/run.js +5 -1
- package/dist/agent/run.js.map +1 -1
- package/dist/agent-db.js +69 -10
- package/dist/aws-task-roles.d.ts +4 -2
- package/dist/aws-task-roles.d.ts.map +1 -1
- package/dist/aws-task-roles.js +4 -1
- package/dist/aws-task-roles.js.map +1 -1
- package/dist/caddy.d.ts +21 -4
- package/dist/caddy.d.ts.map +1 -1
- package/dist/caddy.js +27 -3
- package/dist/caddy.js.map +1 -1
- package/dist/cli-routing.js +1 -1
- package/dist/cli-routing.js.map +1 -1
- package/dist/cli.js +68 -7
- package/dist/cli.js.map +1 -1
- package/dist/cloudsql-tls.d.ts +93 -0
- package/dist/cloudsql-tls.d.ts.map +1 -0
- package/dist/cloudsql-tls.js +295 -0
- package/dist/cloudsql-tls.js.map +1 -0
- package/dist/compose.d.ts +43 -6
- package/dist/compose.d.ts.map +1 -1
- package/dist/compose.js +148 -23
- package/dist/compose.js.map +1 -1
- package/dist/converge-checkpoint.d.ts +1 -0
- package/dist/converge-checkpoint.d.ts.map +1 -1
- package/dist/converge-checkpoint.js +8 -1
- package/dist/converge-checkpoint.js.map +1 -1
- package/dist/converge-db-trust.d.ts +44 -0
- package/dist/converge-db-trust.d.ts.map +1 -0
- package/dist/converge-db-trust.js +158 -0
- package/dist/converge-db-trust.js.map +1 -0
- package/dist/converge-job-gcp.d.ts +5 -2
- package/dist/converge-job-gcp.d.ts.map +1 -1
- package/dist/converge-job-gcp.js +11 -13
- package/dist/converge-job-gcp.js.map +1 -1
- package/dist/converge-job-preflight.d.ts.map +1 -1
- package/dist/converge-job-preflight.js +8 -0
- package/dist/converge-job-preflight.js.map +1 -1
- package/dist/converge-job.d.ts +17 -0
- package/dist/converge-job.d.ts.map +1 -1
- package/dist/converge-job.js +12 -0
- package/dist/converge-job.js.map +1 -1
- package/dist/deployed-release.d.ts +4 -1
- package/dist/deployed-release.d.ts.map +1 -1
- package/dist/deployed-release.js +18 -8
- package/dist/deployed-release.js.map +1 -1
- package/dist/gcp-names.d.ts +34 -0
- package/dist/gcp-names.d.ts.map +1 -1
- package/dist/gcp-names.js +45 -0
- package/dist/gcp-names.js.map +1 -1
- package/dist/gcp-url-map.d.ts.map +1 -1
- package/dist/gcp-url-map.js +6 -2
- package/dist/gcp-url-map.js.map +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/infra-contract.d.ts +1 -1
- package/dist/infra-contract.d.ts.map +1 -1
- package/dist/infra-contract.js +45 -1
- package/dist/infra-contract.js.map +1 -1
- package/dist/install-job.d.ts +4 -2
- package/dist/install-job.d.ts.map +1 -1
- package/dist/install-job.js +4 -1
- package/dist/install-job.js.map +1 -1
- package/dist/install-state.d.ts +13 -0
- package/dist/install-state.d.ts.map +1 -1
- package/dist/install-state.js +17 -4
- package/dist/install-state.js.map +1 -1
- package/dist/lifecycle.d.ts.map +1 -1
- package/dist/lifecycle.js +20 -8
- package/dist/lifecycle.js.map +1 -1
- package/dist/mcp-auth-proxy-setup.d.ts +48 -0
- package/dist/mcp-auth-proxy-setup.d.ts.map +1 -0
- package/dist/mcp-auth-proxy-setup.js +280 -0
- package/dist/mcp-auth-proxy-setup.js.map +1 -0
- package/dist/migration-runner.d.ts.map +1 -1
- package/dist/migration-runner.js +8 -3
- package/dist/migration-runner.js.map +1 -1
- package/dist/migration-targets.d.ts +11 -0
- package/dist/migration-targets.d.ts.map +1 -0
- package/dist/migration-targets.js +12 -0
- package/dist/migration-targets.js.map +1 -0
- package/dist/operator-access.d.ts +10 -0
- package/dist/operator-access.d.ts.map +1 -1
- package/dist/operator-access.js +5 -0
- package/dist/operator-access.js.map +1 -1
- package/dist/pool-budget-stack.d.ts +13 -1
- package/dist/pool-budget-stack.d.ts.map +1 -1
- package/dist/pool-budget-stack.js +11 -1
- package/dist/pool-budget-stack.js.map +1 -1
- package/dist/public-role-isolation.d.ts +7 -3
- package/dist/public-role-isolation.d.ts.map +1 -1
- package/dist/public-role-isolation.js +25 -13
- package/dist/public-role-isolation.js.map +1 -1
- package/dist/release-contract.d.ts.map +1 -1
- package/dist/release-contract.js +3 -0
- package/dist/release-contract.js.map +1 -1
- package/dist/release-manifest.d.ts +25 -1
- package/dist/release-manifest.d.ts.map +1 -1
- package/dist/release-manifest.js +36 -3
- package/dist/release-manifest.js.map +1 -1
- package/dist/remote-mcp.d.ts +55 -18
- package/dist/remote-mcp.d.ts.map +1 -1
- package/dist/remote-mcp.js +307 -77
- package/dist/remote-mcp.js.map +1 -1
- package/dist/runtime-secrets.d.ts +11 -0
- package/dist/runtime-secrets.d.ts.map +1 -1
- package/dist/runtime-secrets.js +40 -25
- package/dist/runtime-secrets.js.map +1 -1
- package/dist/scheduler-setup.d.ts +75 -0
- package/dist/scheduler-setup.d.ts.map +1 -0
- package/dist/scheduler-setup.js +331 -0
- package/dist/scheduler-setup.js.map +1 -0
- package/dist/seal-key-rotation.d.ts +7 -7
- package/dist/seal-key-rotation.d.ts.map +1 -1
- package/dist/seal-key-rotation.js +22 -22
- package/dist/seal-key-rotation.js.map +1 -1
- package/dist/self-hosted-db-tls.d.ts +37 -0
- package/dist/self-hosted-db-tls.d.ts.map +1 -0
- package/dist/self-hosted-db-tls.js +220 -0
- package/dist/self-hosted-db-tls.js.map +1 -0
- package/dist/self-hosted-release.d.ts +3 -1
- package/dist/self-hosted-release.d.ts.map +1 -1
- package/dist/self-hosted-release.js +33 -9
- package/dist/self-hosted-release.js.map +1 -1
- package/dist/self-hosted-roles.d.ts +43 -2
- package/dist/self-hosted-roles.d.ts.map +1 -1
- package/dist/self-hosted-roles.js +87 -1
- package/dist/self-hosted-roles.js.map +1 -1
- package/dist/self-hosted-scheduler-interval.d.ts +21 -0
- package/dist/self-hosted-scheduler-interval.d.ts.map +1 -0
- package/dist/self-hosted-scheduler-interval.js +68 -0
- package/dist/self-hosted-scheduler-interval.js.map +1 -0
- package/dist/self-hosted-start.d.ts +0 -21
- package/dist/self-hosted-start.d.ts.map +1 -1
- package/dist/self-hosted-start.js +39 -1
- package/dist/self-hosted-start.js.map +1 -1
- package/dist/self-hosted-upgrade.d.ts +19 -7
- package/dist/self-hosted-upgrade.d.ts.map +1 -1
- package/dist/self-hosted-upgrade.js +78 -25
- package/dist/self-hosted-upgrade.js.map +1 -1
- package/dist/stack-contract.d.ts +234 -28
- package/dist/stack-contract.d.ts.map +1 -1
- package/dist/stack-contract.js +397 -39
- package/dist/stack-contract.js.map +1 -1
- package/dist/stack-converge.d.ts +31 -0
- package/dist/stack-converge.d.ts.map +1 -1
- package/dist/stack-converge.js +71 -30
- package/dist/stack-converge.js.map +1 -1
- package/dist/staging-database.d.ts +22 -0
- package/dist/staging-database.d.ts.map +1 -0
- package/dist/staging-database.js +117 -0
- package/dist/staging-database.js.map +1 -0
- package/dist/upgrade/app-roll.d.ts +25 -4
- package/dist/upgrade/app-roll.d.ts.map +1 -1
- package/dist/upgrade/app-roll.js +63 -17
- package/dist/upgrade/app-roll.js.map +1 -1
- package/dist/upgrade/converge.d.ts +5 -0
- package/dist/upgrade/converge.d.ts.map +1 -1
- package/dist/upgrade/converge.js +207 -20
- package/dist/upgrade/converge.js.map +1 -1
- package/dist/upgrade/health.d.ts +31 -7
- package/dist/upgrade/health.d.ts.map +1 -1
- package/dist/upgrade/health.js +41 -17
- package/dist/upgrade/health.js.map +1 -1
- package/dist/upgrade/image-forms.d.ts +1 -1
- package/dist/upgrade/image-forms.d.ts.map +1 -1
- package/dist/upgrade/image-forms.js +24 -7
- package/dist/upgrade/image-forms.js.map +1 -1
- package/dist/upgrade/release-images.d.ts +12 -3
- package/dist/upgrade/release-images.d.ts.map +1 -1
- package/dist/upgrade/release-images.js +13 -4
- package/dist/upgrade/release-images.js.map +1 -1
- package/dist/upgrade/upgrade.d.ts +19 -3
- package/dist/upgrade/upgrade.d.ts.map +1 -1
- package/dist/upgrade/upgrade.js +37 -2
- package/dist/upgrade/upgrade.js.map +1 -1
- package/dist/upgrader-gcp.d.ts +13 -0
- package/dist/upgrader-gcp.d.ts.map +1 -1
- package/dist/upgrader-gcp.js +50 -1
- package/dist/upgrader-gcp.js.map +1 -1
- package/dist/upgrader-iam-preflight.d.ts.map +1 -1
- package/dist/upgrader-iam-preflight.js +6 -1
- package/dist/upgrader-iam-preflight.js.map +1 -1
- package/dist/upgrader-job.d.ts +1 -1
- package/dist/upgrader-job.d.ts.map +1 -1
- package/dist/upgrader-job.js +20 -14
- package/dist/upgrader-job.js.map +1 -1
- package/dist/upgrader-records.d.ts +1 -1
- package/dist/upgrader-records.d.ts.map +1 -1
- package/dist/upgrader-records.js +26 -7
- package/dist/upgrader-records.js.map +1 -1
- package/dist/upgrader-setup.d.ts.map +1 -1
- package/dist/upgrader-setup.js +10 -5
- package/dist/upgrader-setup.js.map +1 -1
- package/dist/wizard.d.ts +7 -0
- package/dist/wizard.d.ts.map +1 -1
- package/dist/wizard.js +11 -0
- package/dist/wizard.js.map +1 -1
- package/dist/wizards/gcp-cost.d.ts +31 -0
- package/dist/wizards/gcp-cost.d.ts.map +1 -1
- package/dist/wizards/gcp-cost.js +103 -18
- package/dist/wizards/gcp-cost.js.map +1 -1
- package/dist/wizards/gcp.d.ts.map +1 -1
- package/dist/wizards/gcp.js +129 -27
- package/dist/wizards/gcp.js.map +1 -1
- package/package.json +2 -2
- package/src/adapter.ts +6 -0
- package/src/admin-domain.test.ts +9 -8
- package/src/admin-domain.ts +13 -3
- package/src/admin-memory.test.ts +2 -1
- package/src/adopt-existing.test.ts +85 -0
- package/src/adopt-existing.ts +141 -3
- package/src/agent/command.test.ts +49 -1
- package/src/agent/command.ts +39 -9
- package/src/agent/run.test.ts +19 -0
- package/src/agent/run.ts +11 -2
- package/src/aws-task-roles.ts +5 -2
- package/src/caddy-redirects.docker.test.ts +2 -0
- package/src/caddy-redirects.test.ts +4 -1
- package/src/caddy-validate.test.ts +7 -1
- package/src/caddy.test.ts +39 -1
- package/src/caddy.ts +36 -4
- package/src/checkpoint-secrets.test.ts +14 -6
- package/src/cli-routing.test.ts +1 -1
- package/src/cli-routing.ts +1 -1
- package/src/cli.ts +71 -5
- package/src/cloudsql-tls.test.ts +255 -0
- package/src/cloudsql-tls.ts +359 -0
- package/src/compose.ts +193 -24
- package/src/converge-checkpoint.ts +8 -1
- package/src/converge-db-trust.test.ts +237 -0
- package/src/converge-db-trust.ts +212 -0
- package/src/converge-job-gcp.ts +11 -12
- package/src/converge-job-preflight.test.ts +5 -0
- package/src/converge-job-preflight.ts +8 -0
- package/src/converge-job.test.ts +70 -0
- package/src/converge-job.ts +32 -0
- package/src/database-tls-builders.test.ts +56 -0
- package/src/deployed-release.test.ts +107 -40
- package/src/deployed-release.ts +30 -9
- package/src/gateway-env-isolation.test.ts +50 -12
- package/src/gcp-names.ts +56 -0
- package/src/gcp-url-map.test.ts +6 -8
- package/src/gcp-url-map.ts +6 -2
- package/src/index.test.ts +10 -4
- package/src/index.ts +7 -0
- package/src/infra-contract.test.ts +7 -3
- package/src/infra-contract.ts +45 -1
- package/src/install-job.ts +4 -1
- package/src/install-state.ts +33 -4
- package/src/lifecycle.ts +23 -11
- package/src/mcp-auth-proxy-setup.test.ts +305 -0
- package/src/mcp-auth-proxy-setup.ts +374 -0
- package/src/mcp-iap-stacks.test.ts +1 -1
- package/src/media-volume.test.ts +1 -1
- package/src/migration-runner.ts +14 -9
- package/src/migration-targets.ts +12 -0
- package/src/operator-access-stacks.test.ts +1 -1
- package/src/operator-access.test.ts +20 -4
- package/src/operator-access.ts +14 -0
- package/src/pool-budget-stack.ts +15 -0
- package/src/pool-budget-stacks.test.ts +39 -14
- package/src/public-role-isolation.test.ts +18 -8
- package/src/public-role-isolation.ts +31 -17
- package/src/release-contract.ts +3 -0
- package/src/release-manifest.test.ts +25 -0
- package/src/release-manifest.ts +59 -4
- package/src/remote-mcp-stack-config.test.ts +19 -9
- package/src/remote-mcp.test.ts +252 -57
- package/src/remote-mcp.ts +336 -77
- package/src/runtime-secrets.test.ts +37 -14
- package/src/runtime-secrets.ts +87 -27
- package/src/scheduler-setup.test.ts +201 -0
- package/src/scheduler-setup.ts +375 -0
- package/src/seal-key-rotation.test.ts +2 -2
- package/src/seal-key-rotation.ts +25 -25
- package/src/self-hosted-backup.test.ts +1 -0
- package/src/self-hosted-compose.test.ts +180 -6
- package/src/self-hosted-db-tls.test.ts +111 -0
- package/src/self-hosted-db-tls.ts +287 -0
- package/src/self-hosted-release-resolve.test.ts +71 -0
- package/src/self-hosted-release.ts +41 -11
- package/src/self-hosted-roles.integration.test.ts +2 -0
- package/src/self-hosted-roles.test.ts +87 -3
- package/src/self-hosted-roles.ts +101 -3
- package/src/self-hosted-scheduler-interval.test.ts +66 -0
- package/src/self-hosted-scheduler-interval.ts +79 -0
- package/src/self-hosted-start.test.ts +35 -2
- package/src/self-hosted-start.ts +20 -2
- package/src/self-hosted-upgrade.test.ts +104 -14
- package/src/self-hosted-upgrade.ts +123 -31
- package/src/stack-contract-parity.test.ts +79 -12
- package/src/stack-contract.test.ts +199 -31
- package/src/stack-contract.ts +472 -45
- package/src/stack-converge-secrets.test.ts +28 -13
- package/src/stack-converge.test.ts +203 -28
- package/src/stack-converge.ts +92 -30
- package/src/staging-database.test.ts +114 -0
- package/src/staging-database.ts +148 -0
- package/src/state-store.test.ts +2 -1
- package/src/upgrade/app-roll.ts +97 -19
- package/src/upgrade/converge.ts +249 -19
- package/src/upgrade/health.test.ts +63 -25
- package/src/upgrade/health.ts +54 -22
- package/src/upgrade/image-forms.test.ts +34 -0
- package/src/upgrade/image-forms.ts +23 -8
- package/src/upgrade/release-images.test.ts +5 -1
- package/src/upgrade/release-images.ts +22 -6
- package/src/upgrade/upgrade.test.ts +438 -46
- package/src/upgrade/upgrade.ts +64 -6
- package/src/upgrader-gcp.ts +66 -3
- package/src/upgrader-iam-preflight.test.ts +19 -0
- package/src/upgrader-iam-preflight.ts +6 -0
- package/src/upgrader-job.test.ts +101 -27
- package/src/upgrader-job.ts +27 -14
- package/src/upgrader-records.test.ts +97 -12
- package/src/upgrader-records.ts +28 -11
- package/src/upgrader-setup.test.ts +2 -0
- package/src/upgrader-setup.ts +10 -4
- package/src/wizard.ts +19 -0
- package/src/wizards/gcp-cost.test.ts +103 -0
- package/src/wizards/gcp-cost.ts +125 -18
- package/src/wizards/gcp.ts +164 -22
- package/stacks/aws/Pulumi.yaml +1 -1
- package/stacks/aws/index.ts +72 -14
- package/stacks/azure/index.ts +108 -21
- package/stacks/gcp/Pulumi.yaml +1 -1
- package/stacks/gcp/README.md +1 -0
- package/stacks/gcp/index.ts +133 -24
- package/stacks/gcp-firebase/Pulumi.yaml +1 -1
- package/stacks/gcp-firebase/index.ts +212 -34
- package/stacks/self-hosted/Pulumi.yaml +3 -0
- package/stacks/self-hosted/README.md +6 -1
- package/stacks/self-hosted/index.ts +40 -5
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* #1338 (gateway hardening L6) — converge a Cloud SQL install to
|
|
5
|
+
* `sslmode=verify-full` through gcloud, the way `cms-provision upgrade`
|
|
6
|
+
* converges everything else (stack-converge.ts; it never runs Pulumi).
|
|
7
|
+
*
|
|
8
|
+
* Verify-full checks the server certificate's chain AND that it names the
|
|
9
|
+
* host the client dialled. On Cloud SQL that needs three things the stacks
|
|
10
|
+
* declare for a new install (stacks/gcp, stacks/gcp-firebase) and this
|
|
11
|
+
* module brings an existing one to:
|
|
12
|
+
*
|
|
13
|
+
* 1. The shared server CA (`GOOGLE_MANAGED_CAS_CA`): "The SAN field
|
|
14
|
+
* contains the hostname (DNS name of the instance) for all types of
|
|
15
|
+
* instances", while the default per-instance CA names one "only for
|
|
16
|
+
* instances enabled with Private Service Connect"
|
|
17
|
+
* (docs.cloud.google.com/sql/docs/postgres/authorize-ssl). An instance
|
|
18
|
+
* may move from per-instance to shared, not back. After the move the
|
|
19
|
+
* instance needs a server certificate from the new CA: "create a new
|
|
20
|
+
* server certificate with `gcloud sql ssl server-certs create`", then
|
|
21
|
+
* "complete the rotation with `gcloud sql ssl server-certs rotate`"
|
|
22
|
+
* (docs.cloud.google.com/sql/docs/postgres/manage-ssl-instance). The
|
|
23
|
+
* rotation restarts nothing a client keeps: open connections drop and
|
|
24
|
+
* reconnect; every client already uses TLS (`sslmode=require` before).
|
|
25
|
+
* `sslMode=ENCRYPTED_ONLY` refuses plaintext from then on.
|
|
26
|
+
* 2. Resolution of the instance's DNS name (`dnsNames`, the
|
|
27
|
+
* PRIVATE_SERVICES_ACCESS entry, `….sql-psa.goog`) inside the VPC:
|
|
28
|
+
* "create the DNS record in a private DNS zone in the corresponding
|
|
29
|
+
* Virtual Private Cloud (VPC) network"
|
|
30
|
+
* (docs.cloud.google.com/sql/docs/postgres/configure-ssl-instance).
|
|
31
|
+
* 3. The services dialling that name. They read it from their own
|
|
32
|
+
* `*_DATABASE_URL`, and so does every job the upgrade derives from the
|
|
33
|
+
* admin's (migrations, upgrader, operator access): {@link
|
|
34
|
+
* moveServicesToDnsHost} switches the host of the live URLs first,
|
|
35
|
+
* keeping their current `sslmode`, so the old image keeps working until
|
|
36
|
+
* the roll sets verify-full with the image that carries the CA bundle.
|
|
37
|
+
*
|
|
38
|
+
* The CA the clients trust is Google's published shared-CA bundle, shipped
|
|
39
|
+
* in every image (shared database-tls.ts `CLOUD_SQL_CA_BUNDLE`): it covers
|
|
40
|
+
* the root and every regional CA, so a server-certificate rotation needs no
|
|
41
|
+
* change on the client side.
|
|
42
|
+
*
|
|
43
|
+
* Every step reads before it writes and is a no-op when already in shape,
|
|
44
|
+
* so a re-run after a failure picks up where the last one stopped.
|
|
45
|
+
*/
|
|
46
|
+
|
|
47
|
+
import { isIpLiteral } from "@caelo-cms/shared";
|
|
48
|
+
import { type GcloudRunner, realSleep, runWithRetry, type Sleep } from "./gcloud-retry.js";
|
|
49
|
+
import type { HttpFetch } from "./runtime-secrets.js";
|
|
50
|
+
import {
|
|
51
|
+
CLOUD_SQL_SERVER_CA_MODE,
|
|
52
|
+
CLOUD_SQL_SSL_MODE,
|
|
53
|
+
cloudSqlDnsZoneName,
|
|
54
|
+
cloudSqlPrivateDnsName,
|
|
55
|
+
} from "./stack-contract.js";
|
|
56
|
+
import type { LiveEnvValue } from "./stack-converge.js";
|
|
57
|
+
|
|
58
|
+
/** The install's Cloud SQL instance. */
|
|
59
|
+
export interface CloudSqlTlsTarget {
|
|
60
|
+
readonly projectId: string;
|
|
61
|
+
readonly env: string;
|
|
62
|
+
readonly sqlInstance: string;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface CloudSqlTlsDeps {
|
|
66
|
+
readonly run: GcloudRunner;
|
|
67
|
+
readonly http?: HttpFetch;
|
|
68
|
+
readonly sleep?: Sleep;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export type CloudSqlTlsResult =
|
|
72
|
+
| { readonly ok: true; readonly host: string; readonly done: string[] }
|
|
73
|
+
| { readonly ok: false; readonly error: string; readonly done: string[] };
|
|
74
|
+
|
|
75
|
+
/** The parts of `gcloud sql instances describe --format=json` this module and converge-db-trust.ts read. */
|
|
76
|
+
export interface InstanceDescription {
|
|
77
|
+
/** The CA of the instance's current server certificate (PEM in `cert`). */
|
|
78
|
+
readonly serverCaCert?: { readonly cert?: string };
|
|
79
|
+
readonly settings?: {
|
|
80
|
+
readonly ipConfiguration?: {
|
|
81
|
+
readonly serverCaMode?: string;
|
|
82
|
+
readonly sslMode?: string;
|
|
83
|
+
readonly privateNetwork?: string;
|
|
84
|
+
};
|
|
85
|
+
};
|
|
86
|
+
readonly ipAddresses?: readonly { readonly type?: string; readonly ipAddress?: string }[];
|
|
87
|
+
readonly dnsNames?: readonly { readonly name?: string; readonly connectionType?: string }[];
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const NOT_FOUND = /NOT_FOUND|not found|does not exist|404/i;
|
|
91
|
+
const SQL_ADMIN = "https://sqladmin.googleapis.com/v1";
|
|
92
|
+
|
|
93
|
+
/** `gcloud sql instances describe` of the install's instance, parsed. */
|
|
94
|
+
export async function describeInstance(
|
|
95
|
+
t: CloudSqlTlsTarget,
|
|
96
|
+
run: GcloudRunner,
|
|
97
|
+
): Promise<{ ok: true; d: InstanceDescription } | { ok: false; error: string }> {
|
|
98
|
+
const r = await run([
|
|
99
|
+
"sql",
|
|
100
|
+
"instances",
|
|
101
|
+
"describe",
|
|
102
|
+
t.sqlInstance,
|
|
103
|
+
`--project=${t.projectId}`,
|
|
104
|
+
"--format=json",
|
|
105
|
+
]);
|
|
106
|
+
if (!r.ok) return { ok: false, error: `describe ${t.sqlInstance}: ${r.stderr.trim()}` };
|
|
107
|
+
try {
|
|
108
|
+
return { ok: true, d: JSON.parse(r.stdout) as InstanceDescription };
|
|
109
|
+
} catch {
|
|
110
|
+
return { ok: false, error: `describe ${t.sqlInstance}: gcloud printed no JSON` };
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** The instance's server certificates and which one is active (Cloud SQL Admin API). */
|
|
115
|
+
async function listServerCertificates(
|
|
116
|
+
t: CloudSqlTlsTarget,
|
|
117
|
+
run: GcloudRunner,
|
|
118
|
+
http: HttpFetch,
|
|
119
|
+
): Promise<
|
|
120
|
+
{ ok: true; activeVersion: string | null; fingerprints: string[] } | { ok: false; error: string }
|
|
121
|
+
> {
|
|
122
|
+
const token = await run(["auth", "print-access-token"]);
|
|
123
|
+
const bearer = token.ok ? token.stdout.trim() : "";
|
|
124
|
+
if (!bearer)
|
|
125
|
+
return { ok: false, error: `gcloud auth print-access-token: ${token.stderr.trim()}` };
|
|
126
|
+
const res = await http(
|
|
127
|
+
`${SQL_ADMIN}/projects/${encodeURIComponent(t.projectId)}/instances/${encodeURIComponent(t.sqlInstance)}/listServerCertificates`,
|
|
128
|
+
{ method: "GET", headers: { Authorization: `Bearer ${bearer}` } },
|
|
129
|
+
);
|
|
130
|
+
const text = await res.text();
|
|
131
|
+
if (!res.ok) {
|
|
132
|
+
return {
|
|
133
|
+
ok: false,
|
|
134
|
+
error: `list server certificates: HTTP ${res.status} ${text.slice(0, 300)}`,
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
const body = JSON.parse(text) as {
|
|
138
|
+
activeVersion?: string;
|
|
139
|
+
serverCerts?: { sha1Fingerprint?: string }[];
|
|
140
|
+
};
|
|
141
|
+
return {
|
|
142
|
+
ok: true,
|
|
143
|
+
activeVersion: body.activeVersion ?? null,
|
|
144
|
+
fingerprints: (body.serverCerts ?? []).map((c) => c.sha1Fingerprint ?? "").filter(Boolean),
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Bring the instance to verify-full: shared CA with an active server
|
|
150
|
+
* certificate from it, encrypted connections only, its DNS name resolving
|
|
151
|
+
* in the VPC. Returns the host every client must dial.
|
|
152
|
+
*/
|
|
153
|
+
export async function ensureCloudSqlVerifyFull(
|
|
154
|
+
t: CloudSqlTlsTarget,
|
|
155
|
+
deps: CloudSqlTlsDeps,
|
|
156
|
+
): Promise<CloudSqlTlsResult> {
|
|
157
|
+
const sleep = deps.sleep ?? realSleep;
|
|
158
|
+
const run = (args: string[]) => runWithRetry(deps.run, sleep, args);
|
|
159
|
+
const http = deps.http ?? (fetch as unknown as HttpFetch);
|
|
160
|
+
const done: string[] = [];
|
|
161
|
+
const fail = (error: string): CloudSqlTlsResult => ({ ok: false, error, done });
|
|
162
|
+
const project = `--project=${t.projectId}`;
|
|
163
|
+
|
|
164
|
+
let described = await describeInstance(t, run);
|
|
165
|
+
if (!described.ok) return fail(described.error);
|
|
166
|
+
const ip = described.d.settings?.ipConfiguration;
|
|
167
|
+
const patch: string[] = [];
|
|
168
|
+
if (ip?.serverCaMode !== CLOUD_SQL_SERVER_CA_MODE) {
|
|
169
|
+
patch.push(`--server-ca-mode=${CLOUD_SQL_SERVER_CA_MODE}`);
|
|
170
|
+
}
|
|
171
|
+
if (ip?.sslMode !== CLOUD_SQL_SSL_MODE) patch.push(`--ssl-mode=${CLOUD_SQL_SSL_MODE}`);
|
|
172
|
+
if (patch.length > 0) {
|
|
173
|
+
const r = await run(["sql", "instances", "patch", t.sqlInstance, project, ...patch, "--quiet"]);
|
|
174
|
+
if (!r.ok) return fail(`patch ${t.sqlInstance} (${patch.join(" ")}): ${r.stderr.trim()}`);
|
|
175
|
+
done.push(`Cloud SQL ${t.sqlInstance}: ${patch.join(" ")}`);
|
|
176
|
+
// The instance reports its DNS name only once it uses the shared CA, so
|
|
177
|
+
// the description from before the patch cannot name the host.
|
|
178
|
+
described = await describeInstance(t, run);
|
|
179
|
+
if (!described.ok) return fail(described.error);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// The active server certificate must come from the shared CA. Before the
|
|
183
|
+
// move there is none in the list the shared CA keeps; after an
|
|
184
|
+
// interrupted run there may be an upcoming one that was never rotated in.
|
|
185
|
+
const certs = await listServerCertificates(t, run, http);
|
|
186
|
+
if (!certs.ok) return fail(certs.error);
|
|
187
|
+
const activeShared =
|
|
188
|
+
certs.activeVersion !== null && certs.fingerprints.includes(certs.activeVersion);
|
|
189
|
+
if (!activeShared) {
|
|
190
|
+
const upcoming = certs.fingerprints.filter((f) => f !== certs.activeVersion);
|
|
191
|
+
if (upcoming.length === 0) {
|
|
192
|
+
const c = await run([
|
|
193
|
+
"sql",
|
|
194
|
+
"ssl",
|
|
195
|
+
"server-certs",
|
|
196
|
+
"create",
|
|
197
|
+
`--instance=${t.sqlInstance}`,
|
|
198
|
+
project,
|
|
199
|
+
]);
|
|
200
|
+
if (!c.ok) return fail(`create a server certificate: ${c.stderr.trim()}`);
|
|
201
|
+
done.push(`Cloud SQL ${t.sqlInstance}: server certificate from the shared CA created`);
|
|
202
|
+
}
|
|
203
|
+
const rot = await run([
|
|
204
|
+
"sql",
|
|
205
|
+
"ssl",
|
|
206
|
+
"server-certs",
|
|
207
|
+
"rotate",
|
|
208
|
+
`--instance=${t.sqlInstance}`,
|
|
209
|
+
project,
|
|
210
|
+
"--quiet",
|
|
211
|
+
]);
|
|
212
|
+
if (!rot.ok) return fail(`rotate to the shared-CA server certificate: ${rot.stderr.trim()}`);
|
|
213
|
+
done.push(`Cloud SQL ${t.sqlInstance}: rotated onto the shared-CA server certificate`);
|
|
214
|
+
described = await describeInstance(t, run);
|
|
215
|
+
if (!described.ok) return fail(described.error);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const host = cloudSqlPrivateDnsName(described.d.dnsNames);
|
|
219
|
+
if (!host) {
|
|
220
|
+
return fail(
|
|
221
|
+
`${t.sqlInstance} reports no PRIVATE_SERVICES_ACCESS DNS name (dnsNames) although it uses the shared CA; verify-full needs it. Check the instance's private IP configuration and re-run.`,
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
const privateIp = described.d.ipAddresses?.find((a) => a.type === "PRIVATE")?.ipAddress;
|
|
225
|
+
if (!privateIp) return fail(`${t.sqlInstance} has no private IP address`);
|
|
226
|
+
const network = ip?.privateNetwork?.split("/").pop();
|
|
227
|
+
if (!network) return fail(`${t.sqlInstance} names no private network`);
|
|
228
|
+
|
|
229
|
+
const zone = cloudSqlDnsZoneName(t.env);
|
|
230
|
+
const fqdn = `${host}.`;
|
|
231
|
+
const z = await run(["dns", "managed-zones", "describe", zone, project, "--format=json"]);
|
|
232
|
+
if (z.ok) {
|
|
233
|
+
const existing = (JSON.parse(z.stdout) as { dnsName?: string }).dnsName?.toLowerCase();
|
|
234
|
+
if (existing !== fqdn) {
|
|
235
|
+
return fail(
|
|
236
|
+
`the private DNS zone ${zone} serves ${existing ?? "(no name)"}, not the instance's DNS name ${fqdn}. If the instance was replaced, delete the zone (gcloud dns managed-zones delete ${zone} ${project}) after removing its records, then re-run.`,
|
|
237
|
+
);
|
|
238
|
+
}
|
|
239
|
+
} else if (NOT_FOUND.test(z.stderr)) {
|
|
240
|
+
const c = await run([
|
|
241
|
+
"dns",
|
|
242
|
+
"managed-zones",
|
|
243
|
+
"create",
|
|
244
|
+
zone,
|
|
245
|
+
project,
|
|
246
|
+
`--dns-name=${fqdn}`,
|
|
247
|
+
"--visibility=private",
|
|
248
|
+
`--networks=${network}`,
|
|
249
|
+
`--description=Caelo ${t.env}: the Cloud SQL instance's DNS name inside the VPC (#1338)`,
|
|
250
|
+
]);
|
|
251
|
+
if (!c.ok) return fail(`create the private DNS zone ${zone}: ${c.stderr.trim()}`);
|
|
252
|
+
done.push(`private DNS zone ${zone} (${fqdn}) on network ${network}`);
|
|
253
|
+
} else {
|
|
254
|
+
return fail(`describe the private DNS zone ${zone}: ${z.stderr.trim()}`);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
const rec = ["dns", "record-sets"];
|
|
258
|
+
const where = [fqdn, `--zone=${zone}`, "--type=A", project];
|
|
259
|
+
const a = await run([...rec, "describe", ...where, "--format=json"]);
|
|
260
|
+
if (a.ok) {
|
|
261
|
+
const rrdatas = (JSON.parse(a.stdout) as { rrdatas?: string[] }).rrdatas ?? [];
|
|
262
|
+
if (rrdatas.length !== 1 || rrdatas[0] !== privateIp) {
|
|
263
|
+
const u = await run([...rec, "update", ...where, "--ttl=300", `--rrdatas=${privateIp}`]);
|
|
264
|
+
if (!u.ok) return fail(`update the A record of ${fqdn}: ${u.stderr.trim()}`);
|
|
265
|
+
done.push(`DNS ${fqdn} A ${rrdatas.join(",") || "(none)"} → ${privateIp}`);
|
|
266
|
+
}
|
|
267
|
+
} else if (NOT_FOUND.test(a.stderr)) {
|
|
268
|
+
const c = await run([...rec, "create", ...where, "--ttl=300", `--rrdatas=${privateIp}`]);
|
|
269
|
+
if (!c.ok) return fail(`create the A record of ${fqdn}: ${c.stderr.trim()}`);
|
|
270
|
+
done.push(`DNS ${fqdn} A ${privateIp}`);
|
|
271
|
+
} else {
|
|
272
|
+
return fail(`describe the A record of ${fqdn}: ${a.stderr.trim()}`);
|
|
273
|
+
}
|
|
274
|
+
return { ok: true, host, done };
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/** The plain values of `names` in a service's live env (secret refs and unset vars skipped). */
|
|
278
|
+
export function liveDatabaseUrls(
|
|
279
|
+
liveEnv: ReadonlyMap<string, LiveEnvValue>,
|
|
280
|
+
names: readonly string[],
|
|
281
|
+
): Record<string, string> {
|
|
282
|
+
const out: Record<string, string> = {};
|
|
283
|
+
for (const name of names) {
|
|
284
|
+
const v = liveEnv.get(name);
|
|
285
|
+
if (v?.kind === "value" && v.value) out[name] = v.value;
|
|
286
|
+
}
|
|
287
|
+
return out;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/** Whether a database URL dials an IP literal (an unparsable one does not). */
|
|
291
|
+
export function dialsIpAddress(url: string): boolean {
|
|
292
|
+
try {
|
|
293
|
+
return isIpLiteral(new URL(url).hostname);
|
|
294
|
+
} catch {
|
|
295
|
+
return false;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* `url` with its host replaced by `host`, everything else kept — or null
|
|
301
|
+
* when it already dials a host name (nothing to move).
|
|
302
|
+
*/
|
|
303
|
+
export function urlOnDnsHost(url: string, host: string): string | null {
|
|
304
|
+
const parsed = new URL(url);
|
|
305
|
+
if (!isIpLiteral(parsed.hostname)) return null;
|
|
306
|
+
parsed.hostname = host;
|
|
307
|
+
return parsed.toString();
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Switch the live database URLs of `services` from the instance's IP to
|
|
312
|
+
* `host`, keeping their `sslmode` (the running image may not carry the CA
|
|
313
|
+
* bundle yet; the roll sets verify-full with the image that does). Every
|
|
314
|
+
* reader of the database host — the rolls, the migration job, the upgrader
|
|
315
|
+
* and operator-access jobs — takes it from these URLs, so after this they
|
|
316
|
+
* all dial the name the server's certificate carries.
|
|
317
|
+
*
|
|
318
|
+
* @param liveUrls the plain URL values each service carries now
|
|
319
|
+
*/
|
|
320
|
+
export async function moveServicesToDnsHost(
|
|
321
|
+
target: { readonly projectId: string; readonly region: string },
|
|
322
|
+
services: readonly {
|
|
323
|
+
readonly serviceName: string;
|
|
324
|
+
readonly liveUrls: Readonly<Record<string, string>>;
|
|
325
|
+
}[],
|
|
326
|
+
host: string,
|
|
327
|
+
deps: CloudSqlTlsDeps,
|
|
328
|
+
): Promise<{ ok: true; done: string[] } | { ok: false; error: string; done: string[] }> {
|
|
329
|
+
const sleep = deps.sleep ?? realSleep;
|
|
330
|
+
const done: string[] = [];
|
|
331
|
+
for (const s of services) {
|
|
332
|
+
const moved = Object.entries(s.liveUrls)
|
|
333
|
+
.map(([name, url]) => [name, urlOnDnsHost(url, host)] as const)
|
|
334
|
+
.filter((e): e is readonly [string, string] => e[1] !== null);
|
|
335
|
+
if (moved.length === 0) continue;
|
|
336
|
+
const r = await runWithRetry(deps.run, sleep, [
|
|
337
|
+
"run",
|
|
338
|
+
"services",
|
|
339
|
+
"update",
|
|
340
|
+
s.serviceName,
|
|
341
|
+
"--region",
|
|
342
|
+
target.region,
|
|
343
|
+
`--project=${target.projectId}`,
|
|
344
|
+
// ^|^ — the values carry `&` and `=`; a custom delimiter keeps gcloud
|
|
345
|
+
// from splitting them.
|
|
346
|
+
`--update-env-vars=^|^${moved.map(([n, u]) => `${n}=${u}`).join("|")}`,
|
|
347
|
+
"--quiet",
|
|
348
|
+
]);
|
|
349
|
+
if (!r.ok) {
|
|
350
|
+
return {
|
|
351
|
+
ok: false,
|
|
352
|
+
error: `move ${s.serviceName} onto the database's DNS name: ${r.stderr.trim()}`,
|
|
353
|
+
done,
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
done.push(`${s.serviceName}: ${moved.map(([n]) => n).join(", ")} → ${host}`);
|
|
357
|
+
}
|
|
358
|
+
return { ok: true, done };
|
|
359
|
+
}
|