@geekmidas/cli 10.0.0-alpha.13 → 10.0.0-alpha.130
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/README.md +313 -380
- package/bin/adjacent-tsconfig.mjs +46 -8
- package/bin/gkm.mjs +11 -3
- package/bin/owning-tsconfig-jsx-register.mjs +18 -0
- package/bin/owning-tsconfig-jsx.mjs +108 -0
- package/dist/EndpointGenerator-B99OGzbO.mjs +1035 -0
- package/dist/EndpointGenerator-B99OGzbO.mjs.map +1 -0
- package/dist/EndpointGenerator-De0lknKD.cjs +1094 -0
- package/dist/EndpointGenerator-De0lknKD.cjs.map +1 -0
- package/dist/GoDaddyProvider-D10Le3VX.mjs +388 -0
- package/dist/GoDaddyProvider-D10Le3VX.mjs.map +1 -0
- package/dist/GoDaddyProvider-DTfa47i3.cjs +388 -0
- package/dist/GoDaddyProvider-DTfa47i3.cjs.map +1 -0
- package/dist/{HostingerProvider-BY26bWwB.mjs → HostingerProvider-Cjh_2Ml6.mjs} +11 -6
- package/dist/HostingerProvider-Cjh_2Ml6.mjs.map +1 -0
- package/dist/{HostingerProvider-BZFGYYVA.cjs → HostingerProvider-OdQmKXE3.cjs} +11 -6
- package/dist/HostingerProvider-OdQmKXE3.cjs.map +1 -0
- package/dist/LegacyStateStore-C5c_zS4h.mjs +124 -0
- package/dist/LegacyStateStore-C5c_zS4h.mjs.map +1 -0
- package/dist/LegacyStateStore-CZSBR6ar.cjs +124 -0
- package/dist/LegacyStateStore-CZSBR6ar.cjs.map +1 -0
- package/dist/LocalStateStore-Ba0Y6q5C.mjs +167 -0
- package/dist/LocalStateStore-Ba0Y6q5C.mjs.map +1 -0
- package/dist/LocalStateStore-CgQtQix2.cjs +178 -0
- package/dist/LocalStateStore-CgQtQix2.cjs.map +1 -0
- package/dist/OpenApiTsGenerator-DMLzN-LU.cjs +541 -0
- package/dist/OpenApiTsGenerator-DMLzN-LU.cjs.map +1 -0
- package/dist/OpenApiTsGenerator-DNUCgkkx.mjs +536 -0
- package/dist/OpenApiTsGenerator-DNUCgkkx.mjs.map +1 -0
- package/dist/{Route53Provider-DVILBsvV.cjs → Route53Provider-Bllus2NR.cjs} +2 -2
- package/dist/Route53Provider-Bllus2NR.cjs.map +1 -0
- package/dist/{Route53Provider-Cvj40A8I.mjs → Route53Provider-QXxjkZO1.mjs} +2 -2
- package/dist/Route53Provider-QXxjkZO1.mjs.map +1 -0
- package/dist/S3StateStore-00mB2AG4.mjs +223 -0
- package/dist/S3StateStore-00mB2AG4.mjs.map +1 -0
- package/dist/S3StateStore-C6amZI39.cjs +222 -0
- package/dist/S3StateStore-C6amZI39.cjs.map +1 -0
- package/dist/SSMStateStore-CkYqOAzW.mjs +205 -0
- package/dist/SSMStateStore-CkYqOAzW.mjs.map +1 -0
- package/dist/SSMStateStore-Cnn5fHCg.cjs +204 -0
- package/dist/SSMStateStore-Cnn5fHCg.cjs.map +1 -0
- package/dist/SubscriberGenerator-0ruEmYXj.mjs +646 -0
- package/dist/SubscriberGenerator-0ruEmYXj.mjs.map +1 -0
- package/dist/SubscriberGenerator-CPjbWEOI.cjs +687 -0
- package/dist/SubscriberGenerator-CPjbWEOI.cjs.map +1 -0
- package/dist/add-CdMInvmN.cjs +719 -0
- package/dist/add-CdMInvmN.cjs.map +1 -0
- package/dist/add-DZSU5Tcc.mjs +717 -0
- package/dist/add-DZSU5Tcc.mjs.map +1 -0
- package/dist/appPorts-BoXMwolc.mjs +249 -0
- package/dist/appPorts-BoXMwolc.mjs.map +1 -0
- package/dist/appPorts-IW-OZmRi.cjs +296 -0
- package/dist/appPorts-IW-OZmRi.cjs.map +1 -0
- package/dist/aws-8QQ8n-t6.mjs +89 -0
- package/dist/aws-8QQ8n-t6.mjs.map +1 -0
- package/dist/aws-DY9NqQIT.cjs +100 -0
- package/dist/aws-DY9NqQIT.cjs.map +1 -0
- package/dist/awsStore-Bx5nCFnS.mjs +121 -0
- package/dist/awsStore-Bx5nCFnS.mjs.map +1 -0
- package/dist/awsStore-u3ehhMoY.cjs +138 -0
- package/dist/awsStore-u3ehhMoY.cjs.map +1 -0
- package/dist/{backup-provisioner-DIVCbfee.mjs → backup-provisioner-C8DnCmxK.mjs} +2 -2
- package/dist/backup-provisioner-C8DnCmxK.mjs.map +1 -0
- package/dist/{backup-provisioner-BmhIO_Yr.cjs → backup-provisioner-oDEsPj02.cjs} +2 -2
- package/dist/backup-provisioner-oDEsPj02.cjs.map +1 -0
- package/dist/backups/runner.cjs +519 -0
- package/dist/backups/runner.cjs.map +1 -0
- package/dist/backups/runner.d.cts +102 -0
- package/dist/backups/runner.d.cts.map +1 -0
- package/dist/backups/runner.d.mts +102 -0
- package/dist/backups/runner.d.mts.map +1 -0
- package/dist/backups/runner.mjs +506 -0
- package/dist/backups/runner.mjs.map +1 -0
- package/dist/{bundler-BodTL4kf.mjs → bundler-BzDMJbx8.mjs} +32 -11
- package/dist/bundler-BzDMJbx8.mjs.map +1 -0
- package/dist/{bundler-BeNDZdHQ.cjs → bundler-GKF4xpYI.cjs} +32 -11
- package/dist/bundler-GKF4xpYI.cjs.map +1 -0
- package/dist/cli-Ct6PQtc7.cjs +71 -0
- package/dist/cli-Ct6PQtc7.cjs.map +1 -0
- package/dist/cli-DXeLFtpA.mjs +71 -0
- package/dist/cli-DXeLFtpA.mjs.map +1 -0
- package/dist/compose-BfLDidYU.cjs +3977 -0
- package/dist/compose-BfLDidYU.cjs.map +1 -0
- package/dist/compose-CnanpQBB.mjs +3834 -0
- package/dist/compose-CnanpQBB.mjs.map +1 -0
- package/dist/config-DPsCfNpR.d.mts +268 -0
- package/dist/config-DPsCfNpR.d.mts.map +1 -0
- package/dist/config-azErNJSd.d.cts +268 -0
- package/dist/config-azErNJSd.d.cts.map +1 -0
- package/dist/config.cjs +420 -9
- package/dist/config.cjs.map +1 -0
- package/dist/config.d.cts +4 -117
- package/dist/config.d.mts +4 -117
- package/dist/config.mjs +406 -3
- package/dist/config.mjs.map +1 -0
- package/dist/credentialSchemas-CM0rxWmS.cjs +272 -0
- package/dist/credentialSchemas-CM0rxWmS.cjs.map +1 -0
- package/dist/credentialSchemas-DPOGqOrU.mjs +237 -0
- package/dist/credentialSchemas-DPOGqOrU.mjs.map +1 -0
- package/dist/credentials-D7XJypNd.cjs +520 -0
- package/dist/credentials-D7XJypNd.cjs.map +1 -0
- package/dist/credentials-DpfnUvIR.mjs +395 -0
- package/dist/credentials-DpfnUvIR.mjs.map +1 -0
- package/dist/databases-Db48ZIXL.mjs +588 -0
- package/dist/databases-Db48ZIXL.mjs.map +1 -0
- package/dist/databases-P824RTnx.cjs +703 -0
- package/dist/databases-P824RTnx.cjs.map +1 -0
- package/dist/define-6poNqm5O.mjs +106 -0
- package/dist/define-6poNqm5O.mjs.map +1 -0
- package/dist/define-crmMcXFS.cjs +153 -0
- package/dist/define-crmMcXFS.cjs.map +1 -0
- package/dist/deploy/public.cjs +211 -0
- package/dist/deploy/public.cjs.map +1 -0
- package/dist/deploy/public.d.cts +652 -0
- package/dist/deploy/public.d.cts.map +1 -0
- package/dist/deploy/public.d.mts +652 -0
- package/dist/deploy/public.d.mts.map +1 -0
- package/dist/deploy/public.mjs +144 -0
- package/dist/deploy/public.mjs.map +1 -0
- package/dist/deploy/sniffer-envparser-worker.cjs +54 -0
- package/dist/deploy/sniffer-envparser-worker.cjs.map +1 -0
- package/dist/deploy/sniffer-envparser-worker.d.cts +1 -0
- package/dist/deploy/sniffer-envparser-worker.d.mts +1 -0
- package/dist/deploy/sniffer-envparser-worker.mjs +55 -0
- package/dist/deploy/sniffer-envparser-worker.mjs.map +1 -0
- package/dist/deploy/sniffer-hooks.cjs +1 -1
- package/dist/deploy/sniffer-hooks.mjs +1 -1
- package/dist/deploy/sniffer-loader.cjs +2 -2
- package/dist/deploy/sniffer-loader.mjs +1 -1
- package/dist/deploy/sniffer-routes-worker.cjs +2 -2
- package/dist/deploy/sniffer-routes-worker.mjs +2 -2
- package/dist/deploy-B35L6CMr.mjs +3439 -0
- package/dist/deploy-B35L6CMr.mjs.map +1 -0
- package/dist/deploy-zFe_W4WS.cjs +3494 -0
- package/dist/deploy-zFe_W4WS.cjs.map +1 -0
- package/dist/derive-BtGidITm.cjs +161 -0
- package/dist/derive-BtGidITm.cjs.map +1 -0
- package/dist/derive-Dew0QoGe.mjs +150 -0
- package/dist/derive-Dew0QoGe.mjs.map +1 -0
- package/dist/discover-DEOspD9M.cjs +796 -0
- package/dist/discover-DEOspD9M.cjs.map +1 -0
- package/dist/discover-DOhYSKY_.mjs +693 -0
- package/dist/discover-DOhYSKY_.mjs.map +1 -0
- package/dist/dokploy-BGJxcEh0.cjs +38 -0
- package/dist/dokploy-BGJxcEh0.cjs.map +1 -0
- package/dist/dokploy-sDTohVj5.mjs +38 -0
- package/dist/dokploy-sDTohVj5.mjs.map +1 -0
- package/dist/drivers-C47nm-wB.cjs +311 -0
- package/dist/drivers-C47nm-wB.cjs.map +1 -0
- package/dist/drivers-CDjpjakg.mjs +252 -0
- package/dist/drivers-CDjpjakg.mjs.map +1 -0
- package/dist/eas-Bxb9pymo.mjs +334 -0
- package/dist/eas-Bxb9pymo.mjs.map +1 -0
- package/dist/eas-DogHTOxl.cjs +337 -0
- package/dist/eas-DogHTOxl.cjs.map +1 -0
- package/dist/{emit-BdoPFjZf.cjs → emit-CYloptRS.cjs} +44 -54
- package/dist/emit-CYloptRS.cjs.map +1 -0
- package/dist/{emit-Bzq7JsMP.mjs → emit-CiJGdy_f.mjs} +45 -43
- package/dist/emit-CiJGdy_f.mjs.map +1 -0
- package/dist/encryption-BZk_sxC-.mjs +89 -0
- package/dist/encryption-BZk_sxC-.mjs.map +1 -0
- package/dist/encryption-DRXgL9Lh.cjs +90 -0
- package/dist/encryption-DRXgL9Lh.cjs.map +1 -0
- package/dist/engine-BuPhWFVm.cjs +3328 -0
- package/dist/engine-BuPhWFVm.cjs.map +1 -0
- package/dist/engine-uQmNnvNQ.mjs +3215 -0
- package/dist/engine-uQmNnvNQ.mjs.map +1 -0
- package/dist/errors-D8Gwp5kU.mjs +23 -0
- package/dist/errors-D8Gwp5kU.mjs.map +1 -0
- package/dist/errors-DMC4Umsb.cjs +34 -0
- package/dist/errors-DMC4Umsb.cjs.map +1 -0
- package/dist/github-BCRbDSQa.cjs +901 -0
- package/dist/github-BCRbDSQa.cjs.map +1 -0
- package/dist/github-YknZQnYF.mjs +900 -0
- package/dist/github-YknZQnYF.mjs.map +1 -0
- package/dist/googlePlay-B_2dIDSO.mjs +931 -0
- package/dist/googlePlay-B_2dIDSO.mjs.map +1 -0
- package/dist/googlePlay-Cm2uipeZ.cjs +931 -0
- package/dist/googlePlay-Cm2uipeZ.cjs.map +1 -0
- package/dist/identity-CBXr4wNi.mjs +129 -0
- package/dist/identity-CBXr4wNi.mjs.map +1 -0
- package/dist/identity-CbLfgQNT.cjs +182 -0
- package/dist/identity-CbLfgQNT.cjs.map +1 -0
- package/dist/{index-D-iAVHx-.d.cts → index-D-gNc445.d.cts} +39 -15
- package/dist/index-D-gNc445.d.cts.map +1 -0
- package/dist/{index-Cd-qeOam.d.mts → index-Ur-ZIDYC.d.mts} +39 -15
- package/dist/index-Ur-ZIDYC.d.mts.map +1 -0
- package/dist/index.cjs +13461 -12270
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +13486 -12293
- package/dist/index.mjs.map +1 -1
- package/dist/local-B7Zv1NoA.mjs +224 -0
- package/dist/local-B7Zv1NoA.mjs.map +1 -0
- package/dist/local-OPC9BCZt.cjs +241 -0
- package/dist/local-OPC9BCZt.cjs.map +1 -0
- package/dist/migrate-BIl4kLb1.cjs +181 -0
- package/dist/migrate-BIl4kLb1.cjs.map +1 -0
- package/dist/migrate-Co3UnkrE.cjs +273 -0
- package/dist/migrate-Co3UnkrE.cjs.map +1 -0
- package/dist/migrate-D71REG0N.mjs +268 -0
- package/dist/migrate-D71REG0N.mjs.map +1 -0
- package/dist/migrate-zqsyod5I.mjs +181 -0
- package/dist/migrate-zqsyod5I.mjs.map +1 -0
- package/dist/openapi.cjs +214 -7
- package/dist/openapi.cjs.map +1 -0
- package/dist/openapi.d.cts +32 -8
- package/dist/openapi.d.cts.map +1 -1
- package/dist/openapi.d.mts +32 -8
- package/dist/openapi.d.mts.map +1 -1
- package/dist/openapi.mjs +209 -2
- package/dist/openapi.mjs.map +1 -0
- package/dist/projectBucket-BBbQ1lHF.cjs +263 -0
- package/dist/projectBucket-BBbQ1lHF.cjs.map +1 -0
- package/dist/projectBucket-C_DN4WzF.mjs +222 -0
- package/dist/projectBucket-C_DN4WzF.mjs.map +1 -0
- package/dist/provider-BqpjG9QM.cjs +57 -0
- package/dist/provider-BqpjG9QM.cjs.map +1 -0
- package/dist/provider-CEOKk8NS.mjs +46 -0
- package/dist/provider-CEOKk8NS.mjs.map +1 -0
- package/dist/publish-BUnnuLl_.cjs +124 -0
- package/dist/publish-BUnnuLl_.cjs.map +1 -0
- package/dist/publish-BtmLOhV9.mjs +123 -0
- package/dist/publish-BtmLOhV9.mjs.map +1 -0
- package/dist/ready-BwtLriAF.mjs +375 -0
- package/dist/ready-BwtLriAF.mjs.map +1 -0
- package/dist/ready-Cj4pI0dw.cjs +404 -0
- package/dist/ready-Cj4pI0dw.cjs.map +1 -0
- package/dist/reconcile/public.cjs +2 -4
- package/dist/reconcile/public.d.cts +8 -67
- package/dist/reconcile/public.d.cts.map +1 -1
- package/dist/reconcile/public.d.mts +8 -67
- package/dist/reconcile/public.d.mts.map +1 -1
- package/dist/reconcile/public.mjs +3 -3
- package/dist/{reconcile-DhQ00DpU.mjs → reconcile--I7V4Klj.mjs} +2 -2
- package/dist/{reconcile-DhQ00DpU.mjs.map → reconcile--I7V4Klj.mjs.map} +1 -1
- package/dist/{reconcile-Dd2iyILf.cjs → reconcile-CuCQBa5-.cjs} +3 -3
- package/dist/{reconcile-Dd2iyILf.cjs.map → reconcile-CuCQBa5-.cjs.map} +1 -1
- package/dist/resolve-B1zK9f8_.mjs +263 -0
- package/dist/resolve-B1zK9f8_.mjs.map +1 -0
- package/dist/resolve-BVVSf27Y.cjs +316 -0
- package/dist/resolve-BVVSf27Y.cjs.map +1 -0
- package/dist/rollback-CAPWtOm3.mjs +136 -0
- package/dist/rollback-CAPWtOm3.mjs.map +1 -0
- package/dist/rollback-hV8Kbbg_.cjs +165 -0
- package/dist/rollback-hV8Kbbg_.cjs.map +1 -0
- package/dist/runtime-C0okGPpK.d.cts +82 -0
- package/dist/runtime-C0okGPpK.d.cts.map +1 -0
- package/dist/runtime-DfwIeO8f.d.mts +82 -0
- package/dist/runtime-DfwIeO8f.d.mts.map +1 -0
- package/dist/s3-CkERdmxm.mjs +215 -0
- package/dist/s3-CkERdmxm.mjs.map +1 -0
- package/dist/s3-xJtWm6WN.cjs +215 -0
- package/dist/s3-xJtWm6WN.cjs.map +1 -0
- package/dist/sandbox/config-worker.cjs +43 -0
- package/dist/sandbox/config-worker.cjs.map +1 -0
- package/dist/sandbox/config-worker.d.cts +1 -0
- package/dist/sandbox/config-worker.d.mts +1 -0
- package/dist/sandbox/config-worker.mjs +44 -0
- package/dist/sandbox/config-worker.mjs.map +1 -0
- package/dist/sandbox/credentials-worker.cjs +54 -0
- package/dist/sandbox/credentials-worker.cjs.map +1 -0
- package/dist/sandbox/credentials-worker.d.cts +1 -0
- package/dist/sandbox/credentials-worker.d.mts +1 -0
- package/dist/sandbox/credentials-worker.mjs +55 -0
- package/dist/sandbox/credentials-worker.mjs.map +1 -0
- package/dist/sandbox/discover-worker.cjs +50 -0
- package/dist/sandbox/discover-worker.cjs.map +1 -0
- package/dist/sandbox/discover-worker.d.cts +1 -0
- package/dist/sandbox/discover-worker.d.mts +1 -0
- package/dist/sandbox/discover-worker.mjs +51 -0
- package/dist/sandbox/discover-worker.mjs.map +1 -0
- package/dist/sandbox/migrate-worker.cjs +76 -0
- package/dist/sandbox/migrate-worker.cjs.map +1 -0
- package/dist/sandbox/migrate-worker.d.cts +1 -0
- package/dist/sandbox/migrate-worker.d.mts +1 -0
- package/dist/sandbox/migrate-worker.mjs +77 -0
- package/dist/sandbox/migrate-worker.mjs.map +1 -0
- package/dist/sandbox-CHv3OYXg.cjs +165 -0
- package/dist/sandbox-CHv3OYXg.cjs.map +1 -0
- package/dist/sandbox-CbVB7uOD.mjs +118 -0
- package/dist/sandbox-CbVB7uOD.mjs.map +1 -0
- package/dist/{workspace-mFpI_ky_.cjs → schemas-BCwKKA42.mjs} +399 -1162
- package/dist/schemas-BCwKKA42.mjs.map +1 -0
- package/dist/{workspace-CgwmXEtc.mjs → schemas-BQ_v0lEX.cjs} +487 -971
- package/dist/schemas-BQ_v0lEX.cjs.map +1 -0
- package/dist/secretsManager-CQORubp9.mjs +97 -0
- package/dist/secretsManager-CQORubp9.mjs.map +1 -0
- package/dist/secretsManager-RAGcEDX1.cjs +108 -0
- package/dist/secretsManager-RAGcEDX1.cjs.map +1 -0
- package/dist/seeds-CNPdk_2f.mjs +9198 -0
- package/dist/seeds-CNPdk_2f.mjs.map +1 -0
- package/dist/seeds-MR13eS-F.cjs +10303 -0
- package/dist/seeds-MR13eS-F.cjs.map +1 -0
- package/dist/serverSetup-9kFFmA86.cjs +651 -0
- package/dist/serverSetup-9kFFmA86.cjs.map +1 -0
- package/dist/serverSetup-DgAERQ5s.mjs +651 -0
- package/dist/serverSetup-DgAERQ5s.mjs.map +1 -0
- package/dist/sniffer-Biw5SE1V.cjs +1998 -0
- package/dist/sniffer-Biw5SE1V.cjs.map +1 -0
- package/dist/sniffer-KS_EcbAk.mjs +1825 -0
- package/dist/sniffer-KS_EcbAk.mjs.map +1 -0
- package/dist/snsSubscriptions-Bl3CChIH.mjs +1472 -0
- package/dist/snsSubscriptions-Bl3CChIH.mjs.map +1 -0
- package/dist/snsSubscriptions-CQFfrCJf.cjs +1695 -0
- package/dist/snsSubscriptions-CQFfrCJf.cjs.map +1 -0
- package/dist/sst-DmVJRu5l.cjs +463 -0
- package/dist/sst-DmVJRu5l.cjs.map +1 -0
- package/dist/sst-fop6uZ25.mjs +440 -0
- package/dist/sst-fop6uZ25.mjs.map +1 -0
- package/dist/stage-BIKAMxNc.cjs +313 -0
- package/dist/stage-BIKAMxNc.cjs.map +1 -0
- package/dist/stage-tIMFTQtj.mjs +302 -0
- package/dist/stage-tIMFTQtj.mjs.map +1 -0
- package/dist/stages-C9uSkohw.cjs +190 -0
- package/dist/stages-C9uSkohw.cjs.map +1 -0
- package/dist/stages-DhmbURs_.mjs +190 -0
- package/dist/stages-DhmbURs_.mjs.map +1 -0
- package/dist/target/public.cjs +15 -0
- package/dist/target/public.d.cts +28 -0
- package/dist/target/public.d.cts.map +1 -0
- package/dist/target/public.d.mts +28 -0
- package/dist/target/public.d.mts.map +1 -0
- package/dist/target/public.mjs +4 -0
- package/dist/terminal-CebFDhWQ.mjs +85 -0
- package/dist/terminal-CebFDhWQ.mjs.map +1 -0
- package/dist/terminal-DBwedwSS.cjs +85 -0
- package/dist/terminal-DBwedwSS.cjs.map +1 -0
- package/dist/{types-BGo5TaNE.d.mts → types-Cjnqw1ta.d.cts} +2199 -462
- package/dist/types-Cjnqw1ta.d.cts.map +1 -0
- package/dist/{types-C5ZNpUad.d.cts → types-xBx-07__.d.mts} +2197 -462
- package/dist/types-xBx-07__.d.mts.map +1 -0
- package/dist/utils-D2Nk1Nv2.cjs +205 -0
- package/dist/utils-D2Nk1Nv2.cjs.map +1 -0
- package/dist/utils-D_fxUqvU.mjs +156 -0
- package/dist/utils-D_fxUqvU.mjs.map +1 -0
- package/dist/vitest.cjs +65 -0
- package/dist/vitest.cjs.map +1 -0
- package/dist/vitest.d.cts +29 -0
- package/dist/vitest.d.cts.map +1 -0
- package/dist/vitest.d.mts +29 -0
- package/dist/vitest.d.mts.map +1 -0
- package/dist/vitest.mjs +65 -0
- package/dist/vitest.mjs.map +1 -0
- package/dist/workerRuntime-BXfhbe13.cjs +108 -0
- package/dist/workerRuntime-BXfhbe13.cjs.map +1 -0
- package/dist/workerRuntime-CsOyBRx2.mjs +79 -0
- package/dist/workerRuntime-CsOyBRx2.mjs.map +1 -0
- package/dist/workspace/index.cjs +6 -2
- package/dist/workspace/index.d.cts +3 -3
- package/dist/workspace/index.d.mts +3 -3
- package/dist/workspace/index.mjs +2 -2
- package/dist/workspace-CjSfjfMK.mjs +2396 -0
- package/dist/workspace-CjSfjfMK.mjs.map +1 -0
- package/dist/workspace-DImrdet4.cjs +2808 -0
- package/dist/workspace-DImrdet4.cjs.map +1 -0
- package/package.json +55 -20
- package/dist/HostingerProvider-BY26bWwB.mjs.map +0 -1
- package/dist/HostingerProvider-BZFGYYVA.cjs.map +0 -1
- package/dist/LocalStateProvider-7HR6SS2t.cjs +0 -48
- package/dist/LocalStateProvider-7HR6SS2t.cjs.map +0 -1
- package/dist/LocalStateProvider-D7QyJoLM.mjs +0 -48
- package/dist/LocalStateProvider-D7QyJoLM.mjs.map +0 -1
- package/dist/Route53Provider-Cvj40A8I.mjs.map +0 -1
- package/dist/Route53Provider-DVILBsvV.cjs.map +0 -1
- package/dist/SSMStateProvider-B0Hu1MKZ.mjs +0 -75
- package/dist/SSMStateProvider-B0Hu1MKZ.mjs.map +0 -1
- package/dist/SSMStateProvider-VrQ-yeqd.cjs +0 -74
- package/dist/SSMStateProvider-VrQ-yeqd.cjs.map +0 -1
- package/dist/backup-provisioner-BmhIO_Yr.cjs.map +0 -1
- package/dist/backup-provisioner-DIVCbfee.mjs.map +0 -1
- package/dist/bundler-BeNDZdHQ.cjs.map +0 -1
- package/dist/bundler-BodTL4kf.mjs.map +0 -1
- package/dist/config-Q1rvKHvw.cjs +0 -467
- package/dist/config-Q1rvKHvw.cjs.map +0 -1
- package/dist/config-pqGD3hF9.mjs +0 -414
- package/dist/config-pqGD3hF9.mjs.map +0 -1
- package/dist/config.d.cts.map +0 -1
- package/dist/config.d.mts.map +0 -1
- package/dist/credentials-BekkKtY_.mjs +0 -2780
- package/dist/credentials-BekkKtY_.mjs.map +0 -1
- package/dist/credentials-Q6F0uqJq.cjs +0 -2917
- package/dist/credentials-Q6F0uqJq.cjs.map +0 -1
- package/dist/discover-BERGFg8v.cjs +0 -288
- package/dist/discover-BERGFg8v.cjs.map +0 -1
- package/dist/discover-CK-n3w6b.mjs +0 -244
- package/dist/discover-CK-n3w6b.mjs.map +0 -1
- package/dist/emit-BdoPFjZf.cjs.map +0 -1
- package/dist/emit-Bzq7JsMP.mjs.map +0 -1
- package/dist/fullstack-secrets-BET8gqwK.mjs +0 -81
- package/dist/fullstack-secrets-BET8gqwK.mjs.map +0 -1
- package/dist/fullstack-secrets-DXsWO-5m.cjs +0 -92
- package/dist/fullstack-secrets-DXsWO-5m.cjs.map +0 -1
- package/dist/generator-LfZU7NlU.mjs +0 -247
- package/dist/generator-LfZU7NlU.mjs.map +0 -1
- package/dist/generator-Wdfp0jCS.cjs +0 -282
- package/dist/generator-Wdfp0jCS.cjs.map +0 -1
- package/dist/github-BfflNPVe.cjs +0 -203
- package/dist/github-BfflNPVe.cjs.map +0 -1
- package/dist/github-CRwnxKmD.mjs +0 -203
- package/dist/github-CRwnxKmD.mjs.map +0 -1
- package/dist/index-Cd-qeOam.d.mts.map +0 -1
- package/dist/index-D-iAVHx-.d.cts.map +0 -1
- package/dist/keystore-B4PRlbgE.mjs +0 -81
- package/dist/keystore-B4PRlbgE.mjs.map +0 -1
- package/dist/keystore-DgGRj2Yy.cjs +0 -98
- package/dist/keystore-DgGRj2Yy.cjs.map +0 -1
- package/dist/openapi-Dcbme-Us.cjs +0 -2038
- package/dist/openapi-Dcbme-Us.cjs.map +0 -1
- package/dist/openapi-react-query.cjs +0 -158
- package/dist/openapi-react-query.cjs.map +0 -1
- package/dist/openapi-react-query.d.cts +0 -9
- package/dist/openapi-react-query.d.cts.map +0 -1
- package/dist/openapi-react-query.d.mts +0 -9
- package/dist/openapi-react-query.d.mts.map +0 -1
- package/dist/openapi-react-query.mjs +0 -158
- package/dist/openapi-react-query.mjs.map +0 -1
- package/dist/openapi-vy4JzhUv.mjs +0 -1991
- package/dist/openapi-vy4JzhUv.mjs.map +0 -1
- package/dist/storage-C-30BD1e.cjs +0 -272
- package/dist/storage-C-30BD1e.cjs.map +0 -1
- package/dist/storage-P_b3w7uj.mjs +0 -231
- package/dist/storage-P_b3w7uj.mjs.map +0 -1
- package/dist/sync-B3xxXtJT.mjs +0 -88
- package/dist/sync-B3xxXtJT.mjs.map +0 -1
- package/dist/sync-D7uTIIEi.cjs +0 -111
- package/dist/sync-D7uTIIEi.cjs.map +0 -1
- package/dist/types-BGo5TaNE.d.mts.map +0 -1
- package/dist/types-C5ZNpUad.d.cts.map +0 -1
- package/dist/workspace-CgwmXEtc.mjs.map +0 -1
- package/dist/workspace-mFpI_ky_.cjs.map +0 -1
|
@@ -0,0 +1,3439 @@
|
|
|
1
|
+
import { t as GkmError } from "./errors-D8Gwp5kU.mjs";
|
|
2
|
+
import { r as storedCredentials, x as gkmHome } from "./credentials-DpfnUvIR.mjs";
|
|
3
|
+
import { $ as IamUserNotOwned, G as checkS3Config, J as BucketNameUnavailable, K as AccessKeyLimit, L as resolveStageTelemetry, N as assertDeployedStage, Q as BucketRegionUnknown, U as stageProvider, W as S3_PROVISIONING, X as BucketProbeFailed, Y as BucketOwnedByAnotherProject, Z as BucketRegionMismatch, et as ProvisionedBucketUnreachable, ht as isReservedStageKey, nt as RotationInProgress, q as BucketKeySetElsewhere, rt as S3RegionRequired, tt as RecordedBucketUnreachable, u as getAppBuildOrder } from "./workspace-CjSfjfMK.mjs";
|
|
4
|
+
import { E as BACKUPS_URL_KEY, I as stageBackups, L as deploysWithCompose, M as backupExpiries, P as backupsPrefix, S as assertSitePasswords, T as BACKUPS_SERVICE, _ as stageDnsDomains, a as subscriptionKey, b as awsEndpoint, d as recordTtl, l as dnsCredentials, m as settleStackDns, o as subscriptionResource, x as awsProvisioningCredentials, y as awsClientConfig } from "./snsSubscriptions-Bl3CChIH.mjs";
|
|
5
|
+
import { o as builtinTarget } from "./define-6poNqm5O.mjs";
|
|
6
|
+
import { $ as ORIGIN_VERIFY_HEADER, En as mergeLifecycle, N as stageProviderNotes, P as PROVIDER_KINDS, Qt as initStageSecrets, Yt as secretsStoreFor, _t as appEnvKeys, br as resolveHost, ct as ExternalServicesNotConfigured, d as DeployJournal, dt as devServicesUsed, er as usesTelemetry, f as extractSubdomain, ft as externalServices, ht as stageServiceDeclarations, j as assertStageProvidersEnabled, m as normalizeDnsConfig, nt as originSecretKey, p as findRootDomain, pt as reportDevServices, rt as processorOriginHost, st as DevServicesNeedServerTarget, tt as newOriginSecret, v as createComposeState, vr as NoDomainForStage, wt as constructGlobs, y as createEmptyState, yr as isMainFrontendApp } from "./seeds-CNPdk_2f.mjs";
|
|
7
|
+
import { n as derivedApps, t as appKey } from "./derive-Dew0QoGe.mjs";
|
|
8
|
+
import { n as deployIdentity, t as applicationName } from "./identity-CBXr4wNi.mjs";
|
|
9
|
+
import { i as discover, m as eventsEdge, p as withOutput } from "./discover-DOhYSKY_.mjs";
|
|
10
|
+
import { c as withSandbox } from "./sandbox-CbVB7uOD.mjs";
|
|
11
|
+
import { findWorkspaceRoot, loadWorkspaceConfig } from "./config.mjs";
|
|
12
|
+
import { s as subscriberRoute } from "./SubscriberGenerator-0ruEmYXj.mjs";
|
|
13
|
+
import { a as parseTargetOptions, o as resolveTarget } from "./resolve-B1zK9f8_.mjs";
|
|
14
|
+
import { t as LocalSandbox } from "./local-B7Zv1NoA.mjs";
|
|
15
|
+
import { E as eventError, S as createStateStore, v as assertStateOutlivesRun, y as assertStateRoom } from "./sniffer-KS_EcbAk.mjs";
|
|
16
|
+
import { a as findProjectBucket, i as ensureProjectBucket, o as projectBucketName, r as callerAccountId, s as reconcileProjectBucketLifecycle } from "./projectBucket-C_DN4WzF.mjs";
|
|
17
|
+
import { n as assertStageCredentials } from "./credentialSchemas-DPOGqOrU.mjs";
|
|
18
|
+
import { resolve } from "node:path";
|
|
19
|
+
import { randomInt } from "node:crypto";
|
|
20
|
+
import { bucketExpiry, dependentsOf, kebabCase, provideKey, providedKeyFor } from "@geekmidas/manifest";
|
|
21
|
+
import * as s3Url from "@geekmidas/storage/s3-url";
|
|
22
|
+
//#region src/target/secrets.ts
|
|
23
|
+
/**
|
|
24
|
+
* A stage's secrets as a target sees them: read and written through the
|
|
25
|
+
* stage's own store, and masked in everything the run prints once read.
|
|
26
|
+
*/
|
|
27
|
+
/**
|
|
28
|
+
* Shorter values are left alone: what a progress line legitimately prints —
|
|
29
|
+
* a port, a stage, an app name — would otherwise come out as `***` wherever
|
|
30
|
+
* a secret happened to share it.
|
|
31
|
+
*/
|
|
32
|
+
const SHORTEST_MASKED = 6;
|
|
33
|
+
/** The values a run must never print, and the rewrite that keeps it so. */
|
|
34
|
+
var Redactor = class {
|
|
35
|
+
values = /* @__PURE__ */ new Set();
|
|
36
|
+
add(value) {
|
|
37
|
+
if (value && value.length >= SHORTEST_MASKED) this.values.add(value);
|
|
38
|
+
}
|
|
39
|
+
/** `text` with every masked value replaced by `***`, longest first. */
|
|
40
|
+
redact(text) {
|
|
41
|
+
if (this.values.size === 0) return text;
|
|
42
|
+
let out = text;
|
|
43
|
+
const longestFirst = [...this.values].sort((a, b) => b.length - a.length);
|
|
44
|
+
for (const value of longestFirst) out = out.replaceAll(value, "***");
|
|
45
|
+
return out;
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
/** Every value in `secrets` that is a secret rather than a label. */
|
|
49
|
+
function secretValues(secrets) {
|
|
50
|
+
return [
|
|
51
|
+
secrets.seed,
|
|
52
|
+
...Object.entries(secrets.custom ?? {}).filter(([key]) => !isReservedStageKey(key)).map(([, value]) => value),
|
|
53
|
+
...Object.values(secrets.urls ?? {}),
|
|
54
|
+
...Object.values(secrets.services ?? {}).map((service) => service?.password)
|
|
55
|
+
].filter((value) => typeof value === "string");
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* The stage's secrets through the store `secrets.store` names for it. Opening
|
|
59
|
+
* one builds a client and makes no request, so a target that never reads them
|
|
60
|
+
* costs nothing.
|
|
61
|
+
*/
|
|
62
|
+
async function deploySecrets(workspace, stage, redactor, options = {}) {
|
|
63
|
+
const store = await secretsStoreFor(workspace, stage, options);
|
|
64
|
+
const mask = (secrets) => {
|
|
65
|
+
for (const value of secretValues(secrets)) redactor.add(value);
|
|
66
|
+
};
|
|
67
|
+
return {
|
|
68
|
+
store: store.name,
|
|
69
|
+
async read() {
|
|
70
|
+
const secrets = await store.read(stage);
|
|
71
|
+
if (secrets) mask(secrets);
|
|
72
|
+
return secrets;
|
|
73
|
+
},
|
|
74
|
+
async write(secrets) {
|
|
75
|
+
mask(secrets);
|
|
76
|
+
await store.write(stage, secrets);
|
|
77
|
+
},
|
|
78
|
+
mask: (value) => redactor.add(value)
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
const SUFFIX_ALPHABET = "abcdefghijklmnopqrstuvwxyz0123456789";
|
|
82
|
+
/** A name that S3 would refuse — what the name builder never returns. */
|
|
83
|
+
var InvalidBucketName = class extends GkmError {
|
|
84
|
+
bucket;
|
|
85
|
+
reason;
|
|
86
|
+
constructor(bucket, reason) {
|
|
87
|
+
super(`'${bucket}' is not a valid S3 bucket name: ${reason}. Rename the construct or set deploy.namespace in gkm.config.ts to something that leaves room for it.`);
|
|
88
|
+
this.bucket = bucket;
|
|
89
|
+
this.reason = reason;
|
|
90
|
+
this.name = "InvalidBucketName";
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
/** Lowercase `[a-z0-9-]`, runs of anything else one `-`, no edge dashes. */
|
|
94
|
+
function part(value) {
|
|
95
|
+
return kebabCase(value).toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "");
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* `parts` joined by `-`, shortened to `max` characters by trimming the longest
|
|
99
|
+
* part one character at a time — so a long construct id gives way before a
|
|
100
|
+
* short stage name does, and every part keeps its start.
|
|
101
|
+
*/
|
|
102
|
+
function fitParts(parts, max) {
|
|
103
|
+
const kept = parts.map(part).filter(Boolean);
|
|
104
|
+
const length = () => kept.reduce((sum, p) => sum + p.length, 0) + kept.length - 1;
|
|
105
|
+
while (length() > max) {
|
|
106
|
+
let longest = 0;
|
|
107
|
+
for (let i = 1; i < kept.length; i++) if (kept[i].length > kept[longest].length) longest = i;
|
|
108
|
+
if (kept[longest].length <= 1) break;
|
|
109
|
+
kept[longest] = kept[longest].slice(0, -1).replace(/-+$/, "");
|
|
110
|
+
}
|
|
111
|
+
return kept.join("-");
|
|
112
|
+
}
|
|
113
|
+
/** Why `name` is not a valid bucket name, or undefined when it is. */
|
|
114
|
+
function bucketNameProblem(name) {
|
|
115
|
+
if (name.length < 3 || name.length > 63) return `it is ${name.length} characters, and S3 takes 3 to 63`;
|
|
116
|
+
if (!/^[a-z0-9-]+$/.test(name)) return "it may hold only lowercase letters, digits and -";
|
|
117
|
+
if (!/^[a-z0-9].*[a-z0-9]$/.test(name)) return "it must start and end with a letter or a digit";
|
|
118
|
+
if (name.startsWith("xn--") || name.startsWith("sthree-")) return "it starts with a prefix S3 reserves ('xn--', 'sthree-')";
|
|
119
|
+
if (name.endsWith("-s3alias") || name.endsWith("--ol-s3")) return "it ends with a suffix S3 reserves ('-s3alias', '--ol-s3')";
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* The good name: `<namespace>-<project>-<stage>-<bucket id>`, lowercased, no
|
|
123
|
+
* dots, shortened to leave room for a clash's suffix.
|
|
124
|
+
*
|
|
125
|
+
* @throws {InvalidBucketName} when nothing usable is left
|
|
126
|
+
*/
|
|
127
|
+
function bucketName(input) {
|
|
128
|
+
const name = fitParts([
|
|
129
|
+
input.scope,
|
|
130
|
+
input.stage,
|
|
131
|
+
input.id
|
|
132
|
+
], 56);
|
|
133
|
+
const problem = bucketNameProblem(name);
|
|
134
|
+
if (problem) throw new InvalidBucketName(name, problem);
|
|
135
|
+
return name;
|
|
136
|
+
}
|
|
137
|
+
/** Six random `[a-z0-9]` characters. */
|
|
138
|
+
function randomSuffix(random = randomInt) {
|
|
139
|
+
let suffix = "";
|
|
140
|
+
for (let i = 0; i < 6; i++) suffix += SUFFIX_ALPHABET[random(36)];
|
|
141
|
+
return suffix;
|
|
142
|
+
}
|
|
143
|
+
/** The good name with a clash's suffix. */
|
|
144
|
+
function suffixedBucketName(good, suffix) {
|
|
145
|
+
return `${good}-${suffix}`;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* The IAM user a bucket is reached with on a stage:
|
|
149
|
+
* `gkm-<project>-<stage>-<bucket id>`, at most 64 characters.
|
|
150
|
+
*/
|
|
151
|
+
function iamUserName(input) {
|
|
152
|
+
return fitParts([
|
|
153
|
+
"gkm",
|
|
154
|
+
input.scope,
|
|
155
|
+
input.stage,
|
|
156
|
+
input.id
|
|
157
|
+
], 64);
|
|
158
|
+
}
|
|
159
|
+
/** The path every user gkm creates is under, so it finds its own. */
|
|
160
|
+
const IAM_USER_PATH = "/gkm/";
|
|
161
|
+
/** The inline policy each user is given. */
|
|
162
|
+
const IAM_POLICY_NAME = "gkm-bucket";
|
|
163
|
+
//#endregion
|
|
164
|
+
//#region src/providers/s3/policy.ts
|
|
165
|
+
const bucketArn = (bucket) => `arn:aws:s3:::${bucket}`;
|
|
166
|
+
/**
|
|
167
|
+
* What the app's key may do: objects in this bucket, and listing it — and
|
|
168
|
+
* nothing else in the account.
|
|
169
|
+
*
|
|
170
|
+
* Multipart uploads are in it because a presigned upload of a large file is
|
|
171
|
+
* one, and an abandoned one is cleaned up by the app. Reading old versions is
|
|
172
|
+
* added only when the bucket keeps them.
|
|
173
|
+
*/
|
|
174
|
+
function userPolicy(bucket, options = {}) {
|
|
175
|
+
return {
|
|
176
|
+
Version: "2012-10-17",
|
|
177
|
+
Statement: [
|
|
178
|
+
{
|
|
179
|
+
Sid: "GkmObjects",
|
|
180
|
+
Effect: "Allow",
|
|
181
|
+
Action: [
|
|
182
|
+
"s3:GetObject",
|
|
183
|
+
"s3:PutObject",
|
|
184
|
+
"s3:DeleteObject",
|
|
185
|
+
"s3:AbortMultipartUpload",
|
|
186
|
+
"s3:ListMultipartUploadParts",
|
|
187
|
+
...options.versioning ? ["s3:GetObjectVersion"] : []
|
|
188
|
+
],
|
|
189
|
+
Resource: `${bucketArn(bucket)}/*`
|
|
190
|
+
},
|
|
191
|
+
{
|
|
192
|
+
Sid: "GkmBucket",
|
|
193
|
+
Effect: "Allow",
|
|
194
|
+
Action: [
|
|
195
|
+
"s3:ListBucket",
|
|
196
|
+
"s3:GetBucketLocation",
|
|
197
|
+
"s3:ListBucketMultipartUploads"
|
|
198
|
+
],
|
|
199
|
+
Resource: bucketArn(bucket)
|
|
200
|
+
},
|
|
201
|
+
...options.eventsTopicArn ? [{
|
|
202
|
+
Sid: "GkmEvents",
|
|
203
|
+
Effect: "Allow",
|
|
204
|
+
Action: "sns:ConfirmSubscription",
|
|
205
|
+
Resource: options.eventsTopicArn
|
|
206
|
+
}] : []
|
|
207
|
+
]
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* One open pattern as the resource it is public on. A `**` is S3's `*`; a
|
|
212
|
+
* single `*` is too, which crosses `/` where the construct's own check does
|
|
213
|
+
* not — the construct is the stricter of the two (see `bucketPolicy` in
|
|
214
|
+
* reconcile, which the local MinIO is given the same way).
|
|
215
|
+
*/
|
|
216
|
+
function openResource(bucket, pattern) {
|
|
217
|
+
const path = pattern.replace(/^\/+/, "").replace(/\*\*/g, "*");
|
|
218
|
+
return `${bucketArn(bucket)}/${path}`;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* The bucket's own policy: every request refused unless it came over TLS,
|
|
222
|
+
* and — only when a file server opens paths on it — `s3:GetObject` on
|
|
223
|
+
* exactly those prefixes: to anyone, or, when a CDN serves them, to
|
|
224
|
+
* CloudFront alone, for exactly the stage's distributions. Behind a CDN the
|
|
225
|
+
* bucket is never public: a key outside the open prefixes is refused to
|
|
226
|
+
* CloudFront too, so the edge answers it 403.
|
|
227
|
+
*/
|
|
228
|
+
function bucketPolicy(bucket, open = [], options = {}) {
|
|
229
|
+
const statements = [{
|
|
230
|
+
Sid: "GkmDenyInsecureTransport",
|
|
231
|
+
Effect: "Deny",
|
|
232
|
+
Principal: "*",
|
|
233
|
+
Action: "s3:*",
|
|
234
|
+
Resource: [bucketArn(bucket), `${bucketArn(bucket)}/*`],
|
|
235
|
+
Condition: { Bool: { "aws:SecureTransport": "false" } }
|
|
236
|
+
}];
|
|
237
|
+
const resources = [...new Set(open.map((p) => openResource(bucket, p)))].sort();
|
|
238
|
+
if (open.length > 0 && options.cdn) statements.push({
|
|
239
|
+
Sid: "GkmCdnRead",
|
|
240
|
+
Effect: "Allow",
|
|
241
|
+
Principal: { Service: "cloudfront.amazonaws.com" },
|
|
242
|
+
Action: "s3:GetObject",
|
|
243
|
+
Resource: resources,
|
|
244
|
+
Condition: { StringEquals: { "AWS:SourceArn": [...new Set(options.cdn.distributionArns)].sort() } }
|
|
245
|
+
});
|
|
246
|
+
else if (open.length > 0) statements.push({
|
|
247
|
+
Sid: "GkmOpenPaths",
|
|
248
|
+
Effect: "Allow",
|
|
249
|
+
Principal: "*",
|
|
250
|
+
Action: "s3:GetObject",
|
|
251
|
+
Resource: resources
|
|
252
|
+
});
|
|
253
|
+
return {
|
|
254
|
+
Version: "2012-10-17",
|
|
255
|
+
Statement: statements
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Block Public Access. All four flags on, unless the bucket has open paths:
|
|
260
|
+
* then a *policy* may make it public (`BlockPublicPolicy`,
|
|
261
|
+
* `RestrictPublicBuckets` off), and ACLs still may not.
|
|
262
|
+
*/
|
|
263
|
+
function publicAccessBlock(open) {
|
|
264
|
+
return {
|
|
265
|
+
BlockPublicAcls: true,
|
|
266
|
+
IgnorePublicAcls: true,
|
|
267
|
+
BlockPublicPolicy: !open,
|
|
268
|
+
RestrictPublicBuckets: !open
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
function corsRule(origins) {
|
|
272
|
+
return {
|
|
273
|
+
AllowedMethods: [
|
|
274
|
+
"GET",
|
|
275
|
+
"HEAD",
|
|
276
|
+
"POST",
|
|
277
|
+
"PUT"
|
|
278
|
+
],
|
|
279
|
+
AllowedOrigins: [...new Set(origins)].sort(),
|
|
280
|
+
AllowedHeaders: ["*"],
|
|
281
|
+
ExposeHeaders: ["ETag"],
|
|
282
|
+
MaxAgeSeconds: 3e3
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* A policy document in one canonical form — keys sorted, a lone string a
|
|
287
|
+
* one-element list, lists sorted — so one AWS reformatted reads as the same
|
|
288
|
+
* document rather than as drift.
|
|
289
|
+
*/
|
|
290
|
+
function canonicalPolicy(value) {
|
|
291
|
+
return JSON.stringify(canonical(value));
|
|
292
|
+
}
|
|
293
|
+
function canonical(value, key) {
|
|
294
|
+
if (Array.isArray(value)) {
|
|
295
|
+
const items = value.map((v) => canonical(v));
|
|
296
|
+
return key === "Statement" ? items : items.map((v) => JSON.stringify(v)).sort();
|
|
297
|
+
}
|
|
298
|
+
if (value && typeof value === "object") return Object.fromEntries(Object.keys(value).sort().map((k) => {
|
|
299
|
+
const v = value[k];
|
|
300
|
+
return [k, canonical(k === "Action" || k === "Resource" || k === "AWS" || k === "AWS:SourceArn" ? Array.isArray(v) ? v : [v] : v, k)];
|
|
301
|
+
}));
|
|
302
|
+
return value;
|
|
303
|
+
}
|
|
304
|
+
//#endregion
|
|
305
|
+
//#region src/providers/iam.ts
|
|
306
|
+
/**
|
|
307
|
+
* An IAM user gkm creates for one thing on a stage, its inline policy, and
|
|
308
|
+
* the access key written into the stage's secrets as an `s3://` URL — what
|
|
309
|
+
* the `s3` provider gives each bucket, and the deploy gives a stage's
|
|
310
|
+
* backups.
|
|
311
|
+
*
|
|
312
|
+
* - **The user** is under `/gkm/`, tagged with the deploy identity, the stage
|
|
313
|
+
* and what it is for. One that exists without those tags is someone
|
|
314
|
+
* else's, and is refused rather than taken over.
|
|
315
|
+
* - **Its policy** is read on every run and put back when it drifted.
|
|
316
|
+
* - **Its key**: one, written into the stage's secrets the moment IAM shows
|
|
317
|
+
* its secret. `--rotate-keys` issues a successor and keeps the old one
|
|
318
|
+
* active until the stage has deployed with the new one; the next run after
|
|
319
|
+
* that — or `--retire-old-keys` — deletes it.
|
|
320
|
+
* - **A new key** takes IAM seconds to become usable: the state records when
|
|
321
|
+
* it was issued, and a check made with it waits that out
|
|
322
|
+
* ({@link whileKeyIsNew}).
|
|
323
|
+
*/
|
|
324
|
+
/** The tags gkm finds its own buckets and users by. */
|
|
325
|
+
const TAG_PROJECT = "gkm:project";
|
|
326
|
+
const TAG_STAGE = "gkm:stage";
|
|
327
|
+
const TAG_CONSTRUCT = "gkm:construct";
|
|
328
|
+
/** The keys gkm tags a bucket and a user with. */
|
|
329
|
+
function identityTags(ctx, id) {
|
|
330
|
+
return [
|
|
331
|
+
{
|
|
332
|
+
Key: TAG_PROJECT,
|
|
333
|
+
Value: ctx.identity.key
|
|
334
|
+
},
|
|
335
|
+
{
|
|
336
|
+
Key: TAG_STAGE,
|
|
337
|
+
Value: ctx.stage
|
|
338
|
+
},
|
|
339
|
+
{
|
|
340
|
+
Key: TAG_CONSTRUCT,
|
|
341
|
+
Value: id
|
|
342
|
+
}
|
|
343
|
+
];
|
|
344
|
+
}
|
|
345
|
+
/** A read whose "there is none" answer is one of `missing`. */
|
|
346
|
+
async function readOr$3(read, missing) {
|
|
347
|
+
try {
|
|
348
|
+
return await read();
|
|
349
|
+
} catch (error) {
|
|
350
|
+
const e = error;
|
|
351
|
+
if (missing.includes(e?.Code ?? e?.name ?? "")) return void 0;
|
|
352
|
+
throw error;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
/** IAM returns a policy document URL-encoded; an emulator may not. */
|
|
356
|
+
function decodePolicy(document) {
|
|
357
|
+
try {
|
|
358
|
+
return decodeURIComponent(document);
|
|
359
|
+
} catch {
|
|
360
|
+
return document;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
/**
|
|
364
|
+
* The user and its inline policy: found — and refused when it is not this
|
|
365
|
+
* stage's — or created, and the policy put back when it drifted.
|
|
366
|
+
*
|
|
367
|
+
* @throws {IamUserNotOwned}
|
|
368
|
+
*/
|
|
369
|
+
async function ensureIamUser(ctx, clients, input) {
|
|
370
|
+
const { IAM, iam } = clients;
|
|
371
|
+
const { id, user, policy } = input;
|
|
372
|
+
const entry = {
|
|
373
|
+
key: `iam-user:${id}`,
|
|
374
|
+
type: "iam-user"
|
|
375
|
+
};
|
|
376
|
+
const found = await readOr$3(() => iam.send(new IAM.GetUserCommand({ UserName: user })), ["NoSuchEntity", "NoSuchEntityException"]);
|
|
377
|
+
if (found?.User) {
|
|
378
|
+
const tags = await iam.send(new IAM.ListUserTagsCommand({ UserName: user }));
|
|
379
|
+
const tag = (key) => tags.Tags?.find((t) => t.Key === key)?.Value;
|
|
380
|
+
if (found.User.Path !== "/gkm/" || tag("gkm:project") !== ctx.identity.key || tag("gkm:stage") !== ctx.stage) throw new IamUserNotOwned(user, ctx.identity.key, ctx.stage);
|
|
381
|
+
await ctx.state.ready(entry, user);
|
|
382
|
+
} else await ctx.change({
|
|
383
|
+
construct: id,
|
|
384
|
+
resource: `IAM user ${user}`,
|
|
385
|
+
change: `create under ${IAM_USER_PATH}`
|
|
386
|
+
}, async () => {
|
|
387
|
+
await ctx.state.pending(entry);
|
|
388
|
+
await iam.send(new IAM.CreateUserCommand({
|
|
389
|
+
UserName: user,
|
|
390
|
+
Path: IAM_USER_PATH,
|
|
391
|
+
Tags: identityTags(ctx, id)
|
|
392
|
+
}));
|
|
393
|
+
await ctx.state.ready(entry, user);
|
|
394
|
+
});
|
|
395
|
+
const exists = Boolean(found?.User) || !ctx.dryRun;
|
|
396
|
+
const current = exists ? await readOr$3(() => iam.send(new IAM.GetUserPolicyCommand({
|
|
397
|
+
UserName: user,
|
|
398
|
+
PolicyName: input.policyName
|
|
399
|
+
})), ["NoSuchEntity", "NoSuchEntityException"]) : void 0;
|
|
400
|
+
const document = current?.PolicyDocument ? JSON.parse(decodePolicy(current.PolicyDocument)) : void 0;
|
|
401
|
+
if (!document || canonicalPolicy(document) !== canonicalPolicy(policy)) await ctx.change({
|
|
402
|
+
construct: id,
|
|
403
|
+
resource: `IAM user ${user}`,
|
|
404
|
+
change: `policy ${input.policyName}: ${input.describe}`
|
|
405
|
+
}, async () => {
|
|
406
|
+
await iam.send(new IAM.PutUserPolicyCommand({
|
|
407
|
+
UserName: user,
|
|
408
|
+
PolicyName: input.policyName,
|
|
409
|
+
PolicyDocument: JSON.stringify(policy)
|
|
410
|
+
}));
|
|
411
|
+
});
|
|
412
|
+
return {
|
|
413
|
+
user,
|
|
414
|
+
exists
|
|
415
|
+
};
|
|
416
|
+
}
|
|
417
|
+
/**
|
|
418
|
+
* The user's access key, in the stage's secrets as the bucket's URL.
|
|
419
|
+
*
|
|
420
|
+
* - First run: one key, written into the stage's secrets.
|
|
421
|
+
* - `--rotate-keys`: a second key, written in place of the first; the first
|
|
422
|
+
* stays active until the stage has deployed with the second, and the next
|
|
423
|
+
* run after that deploy — or `--retire-old-keys` — deletes it.
|
|
424
|
+
* - A key the secrets no longer hold (a store emptied by hand): replaced the
|
|
425
|
+
* same way, since its secret cannot be read back.
|
|
426
|
+
*
|
|
427
|
+
* @throws {BucketKeySetElsewhere} when the secret names another bucket
|
|
428
|
+
* @throws {RotationInProgress} on `--rotate-keys` with the last rotation's old key still active
|
|
429
|
+
* @throws {AccessKeyLimit} when the user has two keys gkm did not record
|
|
430
|
+
*/
|
|
431
|
+
async function ensureAccessKey(ctx, clients, input) {
|
|
432
|
+
const { IAM, iam } = clients;
|
|
433
|
+
const { id, bucket, user, urlKey } = input;
|
|
434
|
+
const entryKey = `iam-access-key:${id}`;
|
|
435
|
+
const record = ctx.state.record(entryKey);
|
|
436
|
+
const given = ctx.secrets[urlKey];
|
|
437
|
+
let inUrl;
|
|
438
|
+
let parsed;
|
|
439
|
+
if (given !== void 0) {
|
|
440
|
+
try {
|
|
441
|
+
parsed = s3Url.parse(given);
|
|
442
|
+
} catch {
|
|
443
|
+
parsed = void 0;
|
|
444
|
+
}
|
|
445
|
+
if (parsed?.bucket !== bucket) throw new BucketKeySetElsewhere(urlKey, bucket, parsed?.bucket ?? given.replace(/\/\/[^@]*@/, "//"), ctx.stage);
|
|
446
|
+
inUrl = parsed.accessKeyId;
|
|
447
|
+
}
|
|
448
|
+
const listed = input.exists ? (await iam.send(new IAM.ListAccessKeysCommand({ UserName: user }))).AccessKeyMetadata?.map((k) => k.AccessKeyId) ?? [] : [];
|
|
449
|
+
let current = record?.status === "ready" ? record.id : void 0;
|
|
450
|
+
let previous = typeof record?.data?.previous === "string" ? record.data.previous : void 0;
|
|
451
|
+
const rotatedAt = typeof record?.data?.rotatedAt === "string" ? record.data.rotatedAt : void 0;
|
|
452
|
+
const entry = (data = {}) => ({
|
|
453
|
+
key: entryKey,
|
|
454
|
+
type: "iam-access-key",
|
|
455
|
+
data: {
|
|
456
|
+
user,
|
|
457
|
+
...data
|
|
458
|
+
}
|
|
459
|
+
});
|
|
460
|
+
if (previous && current) {
|
|
461
|
+
const deployed = rotatedAt !== void 0 && ctx.state.lastDeployedAt !== void 0 && ctx.state.lastDeployedAt > rotatedAt;
|
|
462
|
+
if (ctx.retireOldKeys || deployed) {
|
|
463
|
+
const old = previous;
|
|
464
|
+
const keep = current;
|
|
465
|
+
await ctx.change({
|
|
466
|
+
construct: id,
|
|
467
|
+
resource: `IAM user ${user}`,
|
|
468
|
+
change: `delete the rotated-out access key ${old}`
|
|
469
|
+
}, async () => {
|
|
470
|
+
if (listed.includes(old)) await iam.send(new IAM.DeleteAccessKeyCommand({
|
|
471
|
+
UserName: user,
|
|
472
|
+
AccessKeyId: old
|
|
473
|
+
}));
|
|
474
|
+
await ctx.state.ready(entry(), keep);
|
|
475
|
+
});
|
|
476
|
+
previous = void 0;
|
|
477
|
+
} else ctx.log(` ${id}: the old access key ${previous} stays active until '${ctx.stage}' is deployed with the new one; the next deploy after that deletes it (or pass --retire-old-keys)`);
|
|
478
|
+
}
|
|
479
|
+
if (!current && inUrl && listed.includes(inUrl)) {
|
|
480
|
+
await ctx.state.ready(entry(), inUrl);
|
|
481
|
+
current = inUrl;
|
|
482
|
+
}
|
|
483
|
+
if (current !== void 0 && listed.includes(current) && inUrl === current && !ctx.rotateKeys) {
|
|
484
|
+
if (parsed && parsed.region !== clients.region) {
|
|
485
|
+
const address = parsed;
|
|
486
|
+
await ctx.change({
|
|
487
|
+
construct: id,
|
|
488
|
+
resource: urlKey,
|
|
489
|
+
change: `name ${clients.region}, the bucket's region (it said ${address.region ?? "none"})`
|
|
490
|
+
}, () => ctx.writeSecrets({ [urlKey]: s3Url.build({
|
|
491
|
+
...address,
|
|
492
|
+
region: clients.region
|
|
493
|
+
}) }));
|
|
494
|
+
}
|
|
495
|
+
return;
|
|
496
|
+
}
|
|
497
|
+
if (ctx.rotateKeys && previous) throw new RotationInProgress(id, previous, ctx.stage);
|
|
498
|
+
const outgoing = current && listed.includes(current) ? current : void 0;
|
|
499
|
+
if (listed.length >= 2) throw new AccessKeyLimit(user, listed);
|
|
500
|
+
const why = !current ? "create an access key" : ctx.rotateKeys ? "rotate: issue a new access key" : "issue a new access key (the secrets no longer hold the recorded one)";
|
|
501
|
+
await ctx.change({
|
|
502
|
+
construct: id,
|
|
503
|
+
resource: `IAM user ${user}`,
|
|
504
|
+
change: why
|
|
505
|
+
}, async () => {
|
|
506
|
+
await ctx.state.pending(entry(outgoing ? { previous: outgoing } : {}));
|
|
507
|
+
const key = (await iam.send(new IAM.CreateAccessKeyCommand({ UserName: user }))).AccessKey;
|
|
508
|
+
await ctx.writeSecrets({ [urlKey]: s3Url.build({
|
|
509
|
+
bucket,
|
|
510
|
+
region: clients.region,
|
|
511
|
+
...clients.endpoint ? {
|
|
512
|
+
endpoint: clients.endpoint,
|
|
513
|
+
forcePathStyle: true
|
|
514
|
+
} : {},
|
|
515
|
+
accessKeyId: key.AccessKeyId,
|
|
516
|
+
secretAccessKey: key.SecretAccessKey
|
|
517
|
+
}) });
|
|
518
|
+
const issuedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
519
|
+
await ctx.state.ready(entry(outgoing ? {
|
|
520
|
+
previous: outgoing,
|
|
521
|
+
rotatedAt: issuedAt,
|
|
522
|
+
issuedAt
|
|
523
|
+
} : { issuedAt }), key.AccessKeyId);
|
|
524
|
+
});
|
|
525
|
+
if (outgoing) ctx.log(` ${urlKey} holds the new key. This deploy moves '${ctx.stage}' onto it; the next deploy after it deletes the old key ${outgoing}`);
|
|
526
|
+
}
|
|
527
|
+
/** How long a key issued this recently counts as new to IAM. */
|
|
528
|
+
const NEW_KEY_WINDOW_MS = 6e5;
|
|
529
|
+
/** The waits between asks while a new key becomes usable: about a minute. */
|
|
530
|
+
let newKeyWaits = [
|
|
531
|
+
2e3,
|
|
532
|
+
4e3,
|
|
533
|
+
8e3,
|
|
534
|
+
15e3,
|
|
535
|
+
15e3,
|
|
536
|
+
15e3
|
|
537
|
+
];
|
|
538
|
+
/**
|
|
539
|
+
* When the key in a URL was issued, if the stage's state says it was just
|
|
540
|
+
* now — the record of `id`'s key naming that very key.
|
|
541
|
+
*/
|
|
542
|
+
function issuedJustNow(resources, id, accessKeyId) {
|
|
543
|
+
const record = resources?.[`iam-access-key:${id}`];
|
|
544
|
+
const issuedAt = record?.data?.issuedAt;
|
|
545
|
+
if (record?.id !== accessKeyId || typeof issuedAt !== "string") return;
|
|
546
|
+
const age = Date.now() - Date.parse(issuedAt);
|
|
547
|
+
return age >= 0 && age < NEW_KEY_WINDOW_MS ? issuedAt : void 0;
|
|
548
|
+
}
|
|
549
|
+
/**
|
|
550
|
+
* `ask` again, for about a minute, while it is `refused` and the key was
|
|
551
|
+
* issued just now — IAM refuses a key for seconds after making it. Returns
|
|
552
|
+
* the last answer and how long was waited.
|
|
553
|
+
*/
|
|
554
|
+
async function whileKeyIsNew(first, options) {
|
|
555
|
+
let answer = first;
|
|
556
|
+
let waited = 0;
|
|
557
|
+
if (!options.issuedAt || !options.refused(answer)) return {
|
|
558
|
+
answer,
|
|
559
|
+
waited
|
|
560
|
+
};
|
|
561
|
+
options.onWait?.();
|
|
562
|
+
for (const wait of newKeyWaits) {
|
|
563
|
+
await new Promise((resolve) => setTimeout(resolve, wait));
|
|
564
|
+
waited += wait;
|
|
565
|
+
answer = await options.ask();
|
|
566
|
+
if (!options.refused(answer)) break;
|
|
567
|
+
}
|
|
568
|
+
return {
|
|
569
|
+
answer,
|
|
570
|
+
waited
|
|
571
|
+
};
|
|
572
|
+
}
|
|
573
|
+
//#endregion
|
|
574
|
+
//#region src/backups/provision.ts
|
|
575
|
+
/** What the backups user and its key are recorded and tagged as. */
|
|
576
|
+
const BACKUPS_ID = "backups";
|
|
577
|
+
/** The backups user's inline policy's name. */
|
|
578
|
+
const BACKUPS_POLICY_NAME = "gkm-backups";
|
|
579
|
+
/** Where a deploy's check puts its marker, under the backups prefix. */
|
|
580
|
+
const VERIFY_MARKER = ".gkm-verify";
|
|
581
|
+
/** The backups user's name: `gkm-<project>-<stage>-backups`. */
|
|
582
|
+
function backupsUserName(scope, stage) {
|
|
583
|
+
return iamUserName({
|
|
584
|
+
scope,
|
|
585
|
+
stage,
|
|
586
|
+
id: BACKUPS_ID
|
|
587
|
+
});
|
|
588
|
+
}
|
|
589
|
+
/**
|
|
590
|
+
* The backups key's policy: put objects under the stage's backups prefix.
|
|
591
|
+
* Uploading in parts is `s3:PutObject` too; aborting one is not, and the
|
|
592
|
+
* bucket's lifecycle does it instead.
|
|
593
|
+
*/
|
|
594
|
+
function backupsPolicy(bucket, prefix) {
|
|
595
|
+
return {
|
|
596
|
+
Version: "2012-10-17",
|
|
597
|
+
Statement: [{
|
|
598
|
+
Sid: "GkmBackupsPut",
|
|
599
|
+
Effect: "Allow",
|
|
600
|
+
Action: ["s3:PutObject"],
|
|
601
|
+
Resource: `arn:aws:s3:::${bucket}/${prefix}/*`
|
|
602
|
+
}]
|
|
603
|
+
};
|
|
604
|
+
}
|
|
605
|
+
/** A stage that backs up, and no AWS credentials to create its key with. */
|
|
606
|
+
var BackupsCredentialsMissing = class extends GkmError {
|
|
607
|
+
stage;
|
|
608
|
+
constructor(stage) {
|
|
609
|
+
super(`'${stage}' runs Postgres, so the deploy backs it up — and creates the IAM user and key it backs up with in the stage's AWS account — and it found no AWS credentials to do it with. On this machine, supply ${S3_PROVISIONING.describe}. In CI, the step that runs the deploy needs the stage's role (AWS_ROLE_ARN, from gkm deploy:github). To take no backups, set deploy.backups.${stage}: false.`);
|
|
610
|
+
this.stage = stage;
|
|
611
|
+
this.name = "BackupsCredentialsMissing";
|
|
612
|
+
}
|
|
613
|
+
};
|
|
614
|
+
/** No region to create the project bucket in. */
|
|
615
|
+
var BackupsRegionRequired = class extends GkmError {
|
|
616
|
+
stage;
|
|
617
|
+
constructor(stage) {
|
|
618
|
+
super(`'${stage}' is backed up to the project bucket, and there is no region to find it in: set state: { provider: 's3', region } in gkm.config.ts, or AWS_REGION.`);
|
|
619
|
+
this.stage = stage;
|
|
620
|
+
this.name = "BackupsRegionRequired";
|
|
621
|
+
}
|
|
622
|
+
};
|
|
623
|
+
/** A bucket construct whose IAM user would be the backups user. */
|
|
624
|
+
var BackupsUserNameTaken = class extends GkmError {
|
|
625
|
+
stage;
|
|
626
|
+
construct;
|
|
627
|
+
constructor(stage, construct) {
|
|
628
|
+
super(`The bucket '${construct}' is reached on '${stage}' with the IAM user the stage's backups are put with — both are named after 'backups'. Rename the construct, or set deploy.backups.${stage}: false.`);
|
|
629
|
+
this.stage = stage;
|
|
630
|
+
this.construct = construct;
|
|
631
|
+
this.name = "BackupsUserNameTaken";
|
|
632
|
+
}
|
|
633
|
+
};
|
|
634
|
+
/** The backups key could not put its marker: backups would fail too. */
|
|
635
|
+
var BackupsKeyRefused = class extends GkmError {
|
|
636
|
+
stage;
|
|
637
|
+
bucket;
|
|
638
|
+
key;
|
|
639
|
+
said;
|
|
640
|
+
constructor(stage, bucket, key, said) {
|
|
641
|
+
super(`The ${BACKUPS_URL_KEY} key of '${stage}' could not put s3://${bucket}/${key} (${said}), so its backups would fail the same way. Run the deploy with the stage's AWS credentials to put its user and policy back, or --rotate-keys to issue it a new key.`);
|
|
642
|
+
this.stage = stage;
|
|
643
|
+
this.bucket = bucket;
|
|
644
|
+
this.key = key;
|
|
645
|
+
this.said = said;
|
|
646
|
+
this.name = "BackupsKeyRefused";
|
|
647
|
+
}
|
|
648
|
+
};
|
|
649
|
+
/**
|
|
650
|
+
* The region the project bucket is in: the state's, when it is kept in S3,
|
|
651
|
+
* the secrets store's when that is in AWS, the credential's, `AWS_REGION`.
|
|
652
|
+
*/
|
|
653
|
+
function backupsRegion(workspace, stage, credential, env = process.env) {
|
|
654
|
+
const state = workspace.state;
|
|
655
|
+
const store = workspace.secrets?.store;
|
|
656
|
+
const region = (state && "region" in state ? state.region : void 0) ?? (store && typeof store === "object" && "region" in store ? store.region : void 0) ?? credential?.region ?? env.AWS_REGION ?? env.AWS_DEFAULT_REGION;
|
|
657
|
+
if (!region) throw new BackupsRegionRequired(stage);
|
|
658
|
+
return region;
|
|
659
|
+
}
|
|
660
|
+
/**
|
|
661
|
+
* The stage's backups bucket, lifecycle, user and key — or, for a stage set
|
|
662
|
+
* to `false`, a notice and nothing else. Returns what the stage's backups
|
|
663
|
+
* came to.
|
|
664
|
+
*
|
|
665
|
+
* @throws {BackupsCredentialsMissing} when there are no AWS credentials
|
|
666
|
+
*/
|
|
667
|
+
async function ensureStageBackups(input) {
|
|
668
|
+
const { workspace, manifest, stage, ctx } = input;
|
|
669
|
+
const env = input.env ?? process.env;
|
|
670
|
+
const choice = stageBackups(workspace, manifest, stage);
|
|
671
|
+
if (choice.mode === "disabled") {
|
|
672
|
+
ctx.log(`💾 backups: off — deploy.backups.${stage} is false. Nothing is backed up, and nothing already backed up is deleted.`);
|
|
673
|
+
return choice;
|
|
674
|
+
}
|
|
675
|
+
if (choice.mode !== "on") return choice;
|
|
676
|
+
if (stageProvider(workspace, "objects", stage).mode === "provider") {
|
|
677
|
+
const clash = Object.entries(manifest).find(([id, d]) => d.kind === "objects" && appKey(id) === "backups");
|
|
678
|
+
if (clash) throw new BackupsUserNameTaken(stage, clash[0]);
|
|
679
|
+
}
|
|
680
|
+
const credential = await awsProvisioningCredentials({
|
|
681
|
+
stage,
|
|
682
|
+
env,
|
|
683
|
+
...input.profile ? { profile: input.profile } : {},
|
|
684
|
+
...input.home ? { home: input.home } : {}
|
|
685
|
+
});
|
|
686
|
+
if (!credential) throw new BackupsCredentialsMissing(stage);
|
|
687
|
+
const region = backupsRegion(workspace, stage, credential, env);
|
|
688
|
+
const clients = await backupsClients(credential, region, env);
|
|
689
|
+
const bucket = projectBucketName(ctx.identity.scope, await callerAccountId(clients.sts));
|
|
690
|
+
const prefix = backupsPrefix(workspace, stage);
|
|
691
|
+
const expiries = backupExpiries(workspace, manifest);
|
|
692
|
+
ctx.log(`💾 backups: ${choice.backups.describe}, to s3://${bucket}/${prefix}/${ctx.dryRun ? " (dry run)" : ""}`);
|
|
693
|
+
if (await findProjectBucket(clients.s3, bucket) === "none") await ctx.change({
|
|
694
|
+
construct: BACKUPS_SERVICE,
|
|
695
|
+
resource: `bucket ${bucket}`,
|
|
696
|
+
change: `create in ${region}, expiring each stage's backups`
|
|
697
|
+
}, async () => {
|
|
698
|
+
await ensureProjectBucket(clients.s3, {
|
|
699
|
+
bucket,
|
|
700
|
+
region,
|
|
701
|
+
identity: ctx.identity,
|
|
702
|
+
expiries
|
|
703
|
+
});
|
|
704
|
+
});
|
|
705
|
+
else if (await reconcileProjectBucketLifecycle(clients.s3, bucket, expiries, { dryRun: true })) await ctx.change({
|
|
706
|
+
construct: BACKUPS_SERVICE,
|
|
707
|
+
resource: `bucket ${bucket}`,
|
|
708
|
+
change: `lifecycle: ${expiries.map((e) => `${e.prefix} after ${e.days} days`).join(", ")}`
|
|
709
|
+
}, async () => {
|
|
710
|
+
await reconcileProjectBucketLifecycle(clients.s3, bucket, expiries);
|
|
711
|
+
});
|
|
712
|
+
const user = await ensureIamUser(ctx, clients, {
|
|
713
|
+
id: BACKUPS_ID,
|
|
714
|
+
user: backupsUserName(ctx.identity.scope, stage),
|
|
715
|
+
policyName: BACKUPS_POLICY_NAME,
|
|
716
|
+
policy: backupsPolicy(bucket, prefix),
|
|
717
|
+
describe: `put objects under ${prefix}/ only`
|
|
718
|
+
});
|
|
719
|
+
await ensureAccessKey(ctx, clients, {
|
|
720
|
+
id: BACKUPS_ID,
|
|
721
|
+
bucket,
|
|
722
|
+
urlKey: BACKUPS_URL_KEY,
|
|
723
|
+
...user
|
|
724
|
+
});
|
|
725
|
+
return choice;
|
|
726
|
+
}
|
|
727
|
+
/** The clients a backups run creates through. */
|
|
728
|
+
async function backupsClients(credential, region, env) {
|
|
729
|
+
const S3 = await import("@aws-sdk/client-s3");
|
|
730
|
+
const IAM = await import("@aws-sdk/client-iam");
|
|
731
|
+
const { STSClient } = await import("@aws-sdk/client-sts");
|
|
732
|
+
const config = await awsClientConfig(credential, region);
|
|
733
|
+
const endpoint = awsEndpoint(env, "S3");
|
|
734
|
+
return {
|
|
735
|
+
IAM,
|
|
736
|
+
iam: new IAM.IAMClient(config),
|
|
737
|
+
s3: new S3.S3Client({
|
|
738
|
+
...config,
|
|
739
|
+
...endpoint ? {
|
|
740
|
+
endpoint,
|
|
741
|
+
forcePathStyle: true
|
|
742
|
+
} : {}
|
|
743
|
+
}),
|
|
744
|
+
sts: new STSClient(config),
|
|
745
|
+
region,
|
|
746
|
+
...endpoint ? { endpoint } : {}
|
|
747
|
+
};
|
|
748
|
+
}
|
|
749
|
+
/**
|
|
750
|
+
* The stage's backups key puts its marker under the prefix — the one thing
|
|
751
|
+
* it may do. A key issued by this deploy is waited for, as a bucket's is.
|
|
752
|
+
* Returns whether there was a key to check.
|
|
753
|
+
*
|
|
754
|
+
* @throws {BackupsKeyRefused}
|
|
755
|
+
*/
|
|
756
|
+
async function verifyStageBackups(input) {
|
|
757
|
+
const { stage } = input;
|
|
758
|
+
if (stageBackups(input.workspace, input.manifest, stage).mode !== "on") return false;
|
|
759
|
+
const url = input.secrets[BACKUPS_URL_KEY];
|
|
760
|
+
if (!url) return false;
|
|
761
|
+
const address = (await import("@geekmidas/storage/s3-url")).parse(url);
|
|
762
|
+
if (!address.accessKeyId || !address.secretAccessKey) return false;
|
|
763
|
+
const S3 = await import("@aws-sdk/client-s3");
|
|
764
|
+
const client = new S3.S3Client({
|
|
765
|
+
region: address.region ?? "us-east-1",
|
|
766
|
+
credentials: {
|
|
767
|
+
accessKeyId: address.accessKeyId,
|
|
768
|
+
secretAccessKey: address.secretAccessKey
|
|
769
|
+
},
|
|
770
|
+
...address.endpoint ? { endpoint: address.endpoint } : {},
|
|
771
|
+
...address.forcePathStyle ? { forcePathStyle: true } : {}
|
|
772
|
+
});
|
|
773
|
+
const Key = `${backupsPrefix(input.workspace, stage)}/${VERIFY_MARKER}`;
|
|
774
|
+
const put = async () => {
|
|
775
|
+
try {
|
|
776
|
+
await client.send(new S3.PutObjectCommand({
|
|
777
|
+
Bucket: address.bucket,
|
|
778
|
+
Key,
|
|
779
|
+
Body: (/* @__PURE__ */ new Date()).toISOString(),
|
|
780
|
+
ContentType: "text/plain"
|
|
781
|
+
}));
|
|
782
|
+
return {
|
|
783
|
+
ok: true,
|
|
784
|
+
said: ""
|
|
785
|
+
};
|
|
786
|
+
} catch (error) {
|
|
787
|
+
const e = error;
|
|
788
|
+
const status = e.$metadata?.httpStatusCode;
|
|
789
|
+
return {
|
|
790
|
+
ok: false,
|
|
791
|
+
...status !== void 0 ? { status } : {},
|
|
792
|
+
said: `HTTP ${status ?? "no response"} ${e.name ?? "Error"}: ${e.message ?? ""}`
|
|
793
|
+
};
|
|
794
|
+
}
|
|
795
|
+
};
|
|
796
|
+
try {
|
|
797
|
+
const { answer } = await whileKeyIsNew(await put(), {
|
|
798
|
+
issuedAt: issuedJustNow(input.resources, BACKUPS_ID, address.accessKeyId),
|
|
799
|
+
refused: (answer) => !answer.ok && answer.status === 403,
|
|
800
|
+
ask: put,
|
|
801
|
+
onWait: () => input.log?.(` backups: waiting for the new key ${address.accessKeyId} to become active…`)
|
|
802
|
+
});
|
|
803
|
+
if (!answer.ok) throw new BackupsKeyRefused(stage, address.bucket, Key, answer.said);
|
|
804
|
+
return true;
|
|
805
|
+
} finally {
|
|
806
|
+
client.destroy();
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
//#endregion
|
|
810
|
+
//#region src/providers/dnsWriter.ts
|
|
811
|
+
/**
|
|
812
|
+
* The DNS records a provider asks for — a certificate's validation record, a
|
|
813
|
+
* CDN's name — written through the stage's own DNS providers.
|
|
814
|
+
*
|
|
815
|
+
* The same machinery the deploy's DNS step uses for the stack's hosts
|
|
816
|
+
* (`settleStackDns`), handed a plan of CNAMEs instead of the server's
|
|
817
|
+
* addresses: a record holding its value is left alone, one with another value
|
|
818
|
+
* is replaced and printed, and what is written is read back. A host under no
|
|
819
|
+
* domain in `dns`, or under a `manual` one, is printed for the person to
|
|
820
|
+
* create and reported back, so the provider can say what it is waiting on.
|
|
821
|
+
*/
|
|
822
|
+
async function writeProviderDns(records, options) {
|
|
823
|
+
const { log } = options;
|
|
824
|
+
const dns = options.workspace.dns ? normalizeDnsConfig(options.workspace.dns) : {};
|
|
825
|
+
const manual = [];
|
|
826
|
+
const domains = /* @__PURE__ */ new Map();
|
|
827
|
+
for (const record of records) {
|
|
828
|
+
const host = record.host.toLowerCase().replace(/\.$/, "");
|
|
829
|
+
const value = record.value.replace(/\.$/, "");
|
|
830
|
+
const domain = findRootDomain(host, dns);
|
|
831
|
+
if (!domain) {
|
|
832
|
+
log(` create at its DNS host: ${host} CNAME ${value}`);
|
|
833
|
+
manual.push(host);
|
|
834
|
+
continue;
|
|
835
|
+
}
|
|
836
|
+
const config = dns[domain];
|
|
837
|
+
const plan = domains.get(domain) ?? {
|
|
838
|
+
domain,
|
|
839
|
+
provider: typeof config.provider === "string" ? config.provider : "custom",
|
|
840
|
+
config,
|
|
841
|
+
records: []
|
|
842
|
+
};
|
|
843
|
+
const planned = {
|
|
844
|
+
host,
|
|
845
|
+
domain,
|
|
846
|
+
name: extractSubdomain(host, domain),
|
|
847
|
+
type: record.type,
|
|
848
|
+
value,
|
|
849
|
+
ttl: recordTtl(config)
|
|
850
|
+
};
|
|
851
|
+
plan.records.push(planned);
|
|
852
|
+
domains.set(domain, plan);
|
|
853
|
+
}
|
|
854
|
+
if (domains.size === 0) return { manual };
|
|
855
|
+
const plans = [...domains.values()];
|
|
856
|
+
const tokens = await dnsCredentials(options.stage, plans.map(({ domain, config }) => ({
|
|
857
|
+
domain,
|
|
858
|
+
config
|
|
859
|
+
})), options.credentials);
|
|
860
|
+
const applied = await settleStackDns({
|
|
861
|
+
stage: options.stage,
|
|
862
|
+
server: { ipv4: "" },
|
|
863
|
+
domains: plans,
|
|
864
|
+
uncovered: []
|
|
865
|
+
}, {
|
|
866
|
+
...options.dryRun ? { dryRun: true } : { confirm: true },
|
|
867
|
+
log,
|
|
868
|
+
credentials: {
|
|
869
|
+
...tokens.godaddy ? { credential: tokens.godaddy } : {},
|
|
870
|
+
log
|
|
871
|
+
},
|
|
872
|
+
...tokens.hostinger ? { hostinger: tokens.hostinger } : {},
|
|
873
|
+
...options.providerFor ? { providerFor: options.providerFor } : {}
|
|
874
|
+
});
|
|
875
|
+
for (const change of applied.changes) {
|
|
876
|
+
if (change.action !== "manual") continue;
|
|
877
|
+
const host = plans.find((p) => p.domain === change.domain)?.records.find((r) => r.name === change.name)?.host;
|
|
878
|
+
if (host) manual.push(host);
|
|
879
|
+
}
|
|
880
|
+
return { manual: [...new Set(manual)].sort() };
|
|
881
|
+
}
|
|
882
|
+
//#endregion
|
|
883
|
+
//#region src/providers/s3/cdn.ts
|
|
884
|
+
/** AWS's managed `CachingOptimized` cache policy: path-keyed, gzip and brotli. */
|
|
885
|
+
const CACHING_OPTIMIZED = "658327ea-f89d-4fab-a63d-7e88639e58f6";
|
|
886
|
+
/** What an immutable path is served with. */
|
|
887
|
+
const IMMUTABLE_CACHE_CONTROL = "public, max-age=31536000, immutable";
|
|
888
|
+
/** The only region CloudFront reads a certificate from. */
|
|
889
|
+
const CDN_REGION = "us-east-1";
|
|
890
|
+
/** The distribution's bucket origin. */
|
|
891
|
+
const ORIGIN_ID = "bucket";
|
|
892
|
+
/** Its processors' origin: the stack's processor unit, at a host of its own. */
|
|
893
|
+
const PROCESSORS_ORIGIN_ID = "processors";
|
|
894
|
+
/**
|
|
895
|
+
* AWS's managed `CachingOptimizedForUncompressedObjects`: keyed on the path
|
|
896
|
+
* alone, and nothing re-compressed — a derivative is an image, already
|
|
897
|
+
* compressed, and its width is in the path, so nothing else may fragment it.
|
|
898
|
+
*/
|
|
899
|
+
const CACHING_OPTIMIZED_UNCOMPRESSED = "b2884449-e4de-46a7-ac36-70bc7f1ddd6d";
|
|
900
|
+
/** A CDN is a name on the stage's domain, and the stage has none. */
|
|
901
|
+
var FileServerCdnNeedsDomain = class extends GkmError {
|
|
902
|
+
server;
|
|
903
|
+
stage;
|
|
904
|
+
constructor(server, stage) {
|
|
905
|
+
super(`'${server}' is served through a CDN on '${stage}', which answers on <fileserver>.<domain> — and '${stage}' has no domain. Set domains.${stage} in gkm.config.ts, or set deploy.objects.${stage}.cdn to false to serve it from the bucket's own endpoint.`);
|
|
906
|
+
this.server = server;
|
|
907
|
+
this.stage = stage;
|
|
908
|
+
this.name = "FileServerCdnNeedsDomain";
|
|
909
|
+
}
|
|
910
|
+
};
|
|
911
|
+
/**
|
|
912
|
+
* The certificate is waiting on its validation record: one the person has to
|
|
913
|
+
* create, or one that has not propagated yet. Everything up to it is
|
|
914
|
+
* recorded, so the next deploy carries on from here.
|
|
915
|
+
*/
|
|
916
|
+
var CdnCertificatePending = class extends GkmError {
|
|
917
|
+
server;
|
|
918
|
+
host;
|
|
919
|
+
record;
|
|
920
|
+
reason;
|
|
921
|
+
constructor(server, host, record, reason) {
|
|
922
|
+
super((reason === "manual" ? `The certificate for ${host} ('${server}') is validated by a DNS record no DNS provider in gkm.config.ts can write. Create it at the domain's DNS host:
|
|
923
|
+
` : `The certificate for ${host} ('${server}') is not issued yet: ACM has not seen its validation record. It is written; DNS can take a while to answer with it. Check it resolves:
|
|
924
|
+
`) + ` ${record.name} CNAME ${record.value}\nthen deploy again — the certificate and everything before it are recorded, and the deploy carries on from there.`);
|
|
925
|
+
this.server = server;
|
|
926
|
+
this.host = host;
|
|
927
|
+
this.record = record;
|
|
928
|
+
this.reason = reason;
|
|
929
|
+
this.name = "CdnCertificatePending";
|
|
930
|
+
}
|
|
931
|
+
};
|
|
932
|
+
/** ACM gave up on the certificate. */
|
|
933
|
+
var CdnCertificateFailed = class extends GkmError {
|
|
934
|
+
server;
|
|
935
|
+
host;
|
|
936
|
+
status;
|
|
937
|
+
reason;
|
|
938
|
+
constructor(server, host, status, reason) {
|
|
939
|
+
super(`ACM did not issue the certificate for ${host} ('${server}'): ${status}` + (reason ? ` (${reason})` : "") + ". A CAA record on the domain that does not allow amazon.com, or a validation record removed before it was checked, are the usual causes. Fix it and deploy again: a new certificate is requested.");
|
|
940
|
+
this.server = server;
|
|
941
|
+
this.host = host;
|
|
942
|
+
this.status = status;
|
|
943
|
+
this.reason = reason;
|
|
944
|
+
this.name = "CdnCertificateFailed";
|
|
945
|
+
}
|
|
946
|
+
};
|
|
947
|
+
/** Where a file server answers: `<fileserver>.<domain>`. */
|
|
948
|
+
function cdnHost(serverId, domain) {
|
|
949
|
+
return `${appKey(serverId)}.${domain}`.toLowerCase();
|
|
950
|
+
}
|
|
951
|
+
/**
|
|
952
|
+
* The name the stage's CDN resources go by, within CloudFront's limits:
|
|
953
|
+
* letters, digits, `-` and `_`, and at most 64 characters (an origin access
|
|
954
|
+
* control's). Kept whole where it fits; otherwise shortened with a hash of
|
|
955
|
+
* the whole, so two long names never meet.
|
|
956
|
+
*/
|
|
957
|
+
function cdnName(scope, stage, serverId) {
|
|
958
|
+
const whole = `gkm-${scope}-${stage}-${appKey(serverId)}`.replace(/[^A-Za-z0-9_-]/g, "-");
|
|
959
|
+
if (whole.length <= 56) return whole;
|
|
960
|
+
return `${whole.slice(0, 47)}-${hash(whole)}`;
|
|
961
|
+
}
|
|
962
|
+
function hash(value) {
|
|
963
|
+
let h = 2166136261;
|
|
964
|
+
for (let i = 0; i < value.length; i++) {
|
|
965
|
+
h ^= value.charCodeAt(i);
|
|
966
|
+
h = Math.imul(h, 16777619);
|
|
967
|
+
}
|
|
968
|
+
return (h >>> 0).toString(36).padStart(8, "0").slice(0, 8);
|
|
969
|
+
}
|
|
970
|
+
/**
|
|
971
|
+
* An open or immutable glob as a CloudFront path pattern. CloudFront's `*`
|
|
972
|
+
* crosses `/`, so a single star is as wide as a double one — the bucket
|
|
973
|
+
* policy reads it the same way, and the construct's own check is the
|
|
974
|
+
* stricter of the three.
|
|
975
|
+
*/
|
|
976
|
+
function pathPattern(glob) {
|
|
977
|
+
return glob.replace(/^\/+/, "").replace(/\*\*/g, "*");
|
|
978
|
+
}
|
|
979
|
+
/** A CloudFront list: its `Quantity` and, when it has some, its `Items`. */
|
|
980
|
+
function list(items) {
|
|
981
|
+
return items.length > 0 ? {
|
|
982
|
+
Quantity: items.length,
|
|
983
|
+
Items: [...items]
|
|
984
|
+
} : { Quantity: 0 };
|
|
985
|
+
}
|
|
986
|
+
/** One of the stage's response headers policies. */
|
|
987
|
+
function headersPolicyConfig(input) {
|
|
988
|
+
return {
|
|
989
|
+
Name: input.name,
|
|
990
|
+
Comment: input.comment,
|
|
991
|
+
...input.origins.length > 0 ? { CorsConfig: {
|
|
992
|
+
AccessControlAllowOrigins: list([...input.origins].sort()),
|
|
993
|
+
AccessControlAllowHeaders: {
|
|
994
|
+
Quantity: 1,
|
|
995
|
+
Items: ["*"]
|
|
996
|
+
},
|
|
997
|
+
AccessControlAllowMethods: {
|
|
998
|
+
Quantity: 3,
|
|
999
|
+
Items: [
|
|
1000
|
+
"GET",
|
|
1001
|
+
"HEAD",
|
|
1002
|
+
"OPTIONS"
|
|
1003
|
+
]
|
|
1004
|
+
},
|
|
1005
|
+
AccessControlAllowCredentials: false,
|
|
1006
|
+
AccessControlMaxAgeSec: 86400,
|
|
1007
|
+
OriginOverride: false
|
|
1008
|
+
} } : {},
|
|
1009
|
+
...input.immutable ? { CustomHeadersConfig: {
|
|
1010
|
+
Quantity: 1,
|
|
1011
|
+
Items: [{
|
|
1012
|
+
Header: "Cache-Control",
|
|
1013
|
+
Value: IMMUTABLE_CACHE_CONTROL,
|
|
1014
|
+
Override: true
|
|
1015
|
+
}]
|
|
1016
|
+
} } : {}
|
|
1017
|
+
};
|
|
1018
|
+
}
|
|
1019
|
+
/** The parts of a distribution's config this provider owns. */
|
|
1020
|
+
function distributionConfig(input) {
|
|
1021
|
+
const behaviour = (policy) => ({
|
|
1022
|
+
TargetOriginId: ORIGIN_ID,
|
|
1023
|
+
ViewerProtocolPolicy: "redirect-to-https",
|
|
1024
|
+
AllowedMethods: {
|
|
1025
|
+
Quantity: 3,
|
|
1026
|
+
Items: [
|
|
1027
|
+
"GET",
|
|
1028
|
+
"HEAD",
|
|
1029
|
+
"OPTIONS"
|
|
1030
|
+
],
|
|
1031
|
+
CachedMethods: {
|
|
1032
|
+
Quantity: 2,
|
|
1033
|
+
Items: ["GET", "HEAD"]
|
|
1034
|
+
}
|
|
1035
|
+
},
|
|
1036
|
+
Compress: true,
|
|
1037
|
+
CachePolicyId: CACHING_OPTIMIZED,
|
|
1038
|
+
...policy ? { ResponseHeadersPolicyId: policy } : {}
|
|
1039
|
+
});
|
|
1040
|
+
const patterns = [...new Set(input.immutable.map(pathPattern))].sort();
|
|
1041
|
+
const processors = input.processors;
|
|
1042
|
+
const processorPatterns = processors ? [...new Set(processors.prefixes.map((p) => `${pathPattern(p)}*`))].sort() : [];
|
|
1043
|
+
return {
|
|
1044
|
+
CallerReference: input.callerReference,
|
|
1045
|
+
Comment: input.comment,
|
|
1046
|
+
Enabled: true,
|
|
1047
|
+
Aliases: {
|
|
1048
|
+
Quantity: 1,
|
|
1049
|
+
Items: [input.host]
|
|
1050
|
+
},
|
|
1051
|
+
Origins: list([{
|
|
1052
|
+
Id: ORIGIN_ID,
|
|
1053
|
+
DomainName: input.bucketDomain,
|
|
1054
|
+
OriginPath: "",
|
|
1055
|
+
S3OriginConfig: { OriginAccessIdentity: "" },
|
|
1056
|
+
OriginAccessControlId: input.originAccessControlId
|
|
1057
|
+
}, ...processors ? [{
|
|
1058
|
+
Id: PROCESSORS_ORIGIN_ID,
|
|
1059
|
+
DomainName: processors.host,
|
|
1060
|
+
OriginPath: "",
|
|
1061
|
+
CustomOriginConfig: {
|
|
1062
|
+
HTTPPort: 80,
|
|
1063
|
+
HTTPSPort: 443,
|
|
1064
|
+
OriginProtocolPolicy: "https-only",
|
|
1065
|
+
OriginSslProtocols: {
|
|
1066
|
+
Quantity: 1,
|
|
1067
|
+
Items: ["TLSv1.2"]
|
|
1068
|
+
},
|
|
1069
|
+
OriginReadTimeout: 30,
|
|
1070
|
+
OriginKeepaliveTimeout: 5
|
|
1071
|
+
},
|
|
1072
|
+
CustomHeaders: {
|
|
1073
|
+
Quantity: 1,
|
|
1074
|
+
Items: [{
|
|
1075
|
+
HeaderName: ORIGIN_VERIFY_HEADER,
|
|
1076
|
+
HeaderValue: processors.secret
|
|
1077
|
+
}]
|
|
1078
|
+
}
|
|
1079
|
+
}] : []]),
|
|
1080
|
+
DefaultCacheBehavior: behaviour(input.corsPolicyId),
|
|
1081
|
+
CacheBehaviors: list([...processorPatterns.map((PathPattern) => ({
|
|
1082
|
+
...behaviour(input.corsPolicyId),
|
|
1083
|
+
TargetOriginId: PROCESSORS_ORIGIN_ID,
|
|
1084
|
+
Compress: false,
|
|
1085
|
+
CachePolicyId: CACHING_OPTIMIZED_UNCOMPRESSED,
|
|
1086
|
+
PathPattern
|
|
1087
|
+
})), ...patterns.map((PathPattern) => ({
|
|
1088
|
+
...behaviour(input.immutablePolicyId),
|
|
1089
|
+
PathPattern
|
|
1090
|
+
}))]),
|
|
1091
|
+
ViewerCertificate: {
|
|
1092
|
+
ACMCertificateArn: input.certificateArn,
|
|
1093
|
+
SSLSupportMethod: "sni-only",
|
|
1094
|
+
MinimumProtocolVersion: "TLSv1.2_2021",
|
|
1095
|
+
CloudFrontDefaultCertificate: false
|
|
1096
|
+
},
|
|
1097
|
+
HttpVersion: "http2and3",
|
|
1098
|
+
IsIPV6Enabled: true,
|
|
1099
|
+
DefaultRootObject: ""
|
|
1100
|
+
};
|
|
1101
|
+
}
|
|
1102
|
+
/**
|
|
1103
|
+
* Whether `current` already holds everything in `wanted`: each key `wanted`
|
|
1104
|
+
* names, recursively, with lists compared as sets. What else `current` holds
|
|
1105
|
+
* — logging, a WAF, geo restrictions set by hand — is left alone, and a
|
|
1106
|
+
* `CallerReference`, fixed at creation, is never compared.
|
|
1107
|
+
*/
|
|
1108
|
+
function holds(current, wanted, key) {
|
|
1109
|
+
if (key === "CallerReference") return true;
|
|
1110
|
+
if (Array.isArray(wanted)) return Array.isArray(current) && current.length === wanted.length && wanted.every((w) => current.some((c) => holds(c, w)));
|
|
1111
|
+
if (wanted && typeof wanted === "object") {
|
|
1112
|
+
if (!current || typeof current !== "object") return false;
|
|
1113
|
+
return Object.entries(wanted).every(([k, v]) => holds(current[k], v, k));
|
|
1114
|
+
}
|
|
1115
|
+
return current === wanted;
|
|
1116
|
+
}
|
|
1117
|
+
/** Header configs a policy may hold that this provider never sets. */
|
|
1118
|
+
const FOREIGN_HEADER_CONFIGS = [
|
|
1119
|
+
"CorsConfig",
|
|
1120
|
+
"CustomHeadersConfig",
|
|
1121
|
+
"SecurityHeadersConfig",
|
|
1122
|
+
"RemoveHeadersConfig",
|
|
1123
|
+
"ServerTimingHeadersConfig"
|
|
1124
|
+
];
|
|
1125
|
+
/** Whether a policy is exactly what is wanted: nothing missing, nothing more. */
|
|
1126
|
+
function sameHeadersPolicy(current, wanted) {
|
|
1127
|
+
if (!current || !holds(current, wanted)) return false;
|
|
1128
|
+
return FOREIGN_HEADER_CONFIGS.every((k) => {
|
|
1129
|
+
if (k in wanted) return true;
|
|
1130
|
+
const extra = current[k];
|
|
1131
|
+
return !extra || extra.Quantity === 0;
|
|
1132
|
+
});
|
|
1133
|
+
}
|
|
1134
|
+
const defaultCdnClients = async (credential) => {
|
|
1135
|
+
const CF = await import("@aws-sdk/client-cloudfront");
|
|
1136
|
+
const ACM = await import("@aws-sdk/client-acm");
|
|
1137
|
+
const config = await awsClientConfig(credential, CDN_REGION);
|
|
1138
|
+
const cloudfront = awsEndpoint(process.env, "CLOUDFRONT");
|
|
1139
|
+
const acm = awsEndpoint(process.env, "ACM");
|
|
1140
|
+
return {
|
|
1141
|
+
CF,
|
|
1142
|
+
ACM,
|
|
1143
|
+
cloudfront: new CF.CloudFrontClient({
|
|
1144
|
+
...config,
|
|
1145
|
+
...cloudfront ? { endpoint: cloudfront } : {}
|
|
1146
|
+
}),
|
|
1147
|
+
acm: new ACM.ACMClient({
|
|
1148
|
+
...config,
|
|
1149
|
+
...acm ? { endpoint: acm } : {}
|
|
1150
|
+
}),
|
|
1151
|
+
wait: {
|
|
1152
|
+
timeoutMs: 9e5,
|
|
1153
|
+
intervalMs: 1e4
|
|
1154
|
+
}
|
|
1155
|
+
};
|
|
1156
|
+
};
|
|
1157
|
+
let makeCdnClients = defaultCdnClients;
|
|
1158
|
+
/** What an AWS SDK error is called, whichever field carries it. */
|
|
1159
|
+
function errorName$2(error) {
|
|
1160
|
+
const e = error;
|
|
1161
|
+
return e?.Code ?? e?.name ?? "";
|
|
1162
|
+
}
|
|
1163
|
+
async function readOr$2(read, missing) {
|
|
1164
|
+
try {
|
|
1165
|
+
return await read();
|
|
1166
|
+
} catch (error) {
|
|
1167
|
+
if (missing.includes(errorName$2(error))) return void 0;
|
|
1168
|
+
throw error;
|
|
1169
|
+
}
|
|
1170
|
+
}
|
|
1171
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
1172
|
+
/** What stands in for an id a dry run would create. */
|
|
1173
|
+
const PLANNED = "(created by this deploy)";
|
|
1174
|
+
/** Each file server over a bucket, behind its own distribution. */
|
|
1175
|
+
async function ensureBucketCdn(ctx, input) {
|
|
1176
|
+
const domain = ctx.workspace.domains?.[ctx.stage];
|
|
1177
|
+
if (!domain) throw new FileServerCdnNeedsDomain(input.servers[0].id, ctx.stage);
|
|
1178
|
+
const clients = await makeCdnClients(ctx.credential);
|
|
1179
|
+
const out = {
|
|
1180
|
+
distributionArns: [],
|
|
1181
|
+
urls: {}
|
|
1182
|
+
};
|
|
1183
|
+
for (const server of input.servers) {
|
|
1184
|
+
const { arn, url } = await ensureServerCdn(ctx, clients, {
|
|
1185
|
+
...input,
|
|
1186
|
+
server,
|
|
1187
|
+
host: cdnHost(server.id, domain),
|
|
1188
|
+
processorHost: processorOriginHost(server.id, domain)
|
|
1189
|
+
});
|
|
1190
|
+
out.distributionArns.push(arn);
|
|
1191
|
+
out.urls[server.id] = url;
|
|
1192
|
+
}
|
|
1193
|
+
return out;
|
|
1194
|
+
}
|
|
1195
|
+
async function ensureServerCdn(ctx, clients, input) {
|
|
1196
|
+
const { server, host } = input;
|
|
1197
|
+
const name = cdnName(ctx.identity.scope, ctx.stage, server.id);
|
|
1198
|
+
const comment = `gkm ${ctx.identity.key} ${ctx.stage} ${server.id}`.slice(0, 128);
|
|
1199
|
+
const certificateArn = await ensureCertificate(ctx, clients, {
|
|
1200
|
+
id: server.id,
|
|
1201
|
+
host,
|
|
1202
|
+
name
|
|
1203
|
+
});
|
|
1204
|
+
const originAccessControlId = await ensureOriginAccessControl(ctx, clients, {
|
|
1205
|
+
id: server.id,
|
|
1206
|
+
name,
|
|
1207
|
+
bucket: input.bucket
|
|
1208
|
+
});
|
|
1209
|
+
const corsPolicyId = input.origins.length > 0 ? await ensureHeadersPolicy(ctx, clients, {
|
|
1210
|
+
id: server.id,
|
|
1211
|
+
kind: "cors",
|
|
1212
|
+
config: headersPolicyConfig({
|
|
1213
|
+
name: `${name}-cors`,
|
|
1214
|
+
comment: `${comment}: CORS`,
|
|
1215
|
+
origins: input.origins,
|
|
1216
|
+
immutable: false
|
|
1217
|
+
})
|
|
1218
|
+
}) : void 0;
|
|
1219
|
+
const immutablePolicyId = server.immutable.length > 0 ? await ensureHeadersPolicy(ctx, clients, {
|
|
1220
|
+
id: server.id,
|
|
1221
|
+
kind: "immutable",
|
|
1222
|
+
config: headersPolicyConfig({
|
|
1223
|
+
name: `${name}-immutable`,
|
|
1224
|
+
comment: `${comment}: CORS and Cache-Control immutable`,
|
|
1225
|
+
origins: input.origins,
|
|
1226
|
+
immutable: true
|
|
1227
|
+
})
|
|
1228
|
+
}) : void 0;
|
|
1229
|
+
const processors = server.processors?.length ? {
|
|
1230
|
+
prefixes: server.processors,
|
|
1231
|
+
host: input.processorHost,
|
|
1232
|
+
secret: await ensureOriginSecret(ctx, server.id)
|
|
1233
|
+
} : void 0;
|
|
1234
|
+
const distribution = await ensureDistribution(ctx, clients, {
|
|
1235
|
+
id: server.id,
|
|
1236
|
+
comment,
|
|
1237
|
+
config: distributionConfig({
|
|
1238
|
+
comment,
|
|
1239
|
+
callerReference: name,
|
|
1240
|
+
host,
|
|
1241
|
+
certificateArn,
|
|
1242
|
+
bucketDomain: `${input.bucket}.s3.${input.region}.amazonaws.com`,
|
|
1243
|
+
originAccessControlId,
|
|
1244
|
+
...corsPolicyId ? { corsPolicyId } : {},
|
|
1245
|
+
...immutablePolicyId ? { immutablePolicyId } : {},
|
|
1246
|
+
immutable: server.immutable,
|
|
1247
|
+
...processors ? { processors } : {}
|
|
1248
|
+
})
|
|
1249
|
+
});
|
|
1250
|
+
if (distribution.domainName !== PLANNED) {
|
|
1251
|
+
if ((await ctx.writeDns([{
|
|
1252
|
+
host,
|
|
1253
|
+
type: "CNAME",
|
|
1254
|
+
value: distribution.domainName
|
|
1255
|
+
}])).manual.includes(host)) ctx.log(` ${host} answers once that record exists — ${server.id}'s URL is https://${host}`);
|
|
1256
|
+
}
|
|
1257
|
+
return {
|
|
1258
|
+
arn: distribution.arn,
|
|
1259
|
+
url: `https://${host}`
|
|
1260
|
+
};
|
|
1261
|
+
}
|
|
1262
|
+
/**
|
|
1263
|
+
* The secret the CDN sends a file server's processors: the one in the stage's
|
|
1264
|
+
* secrets, or a new one written there — read by the stack, whose edge checks
|
|
1265
|
+
* it, when it composes.
|
|
1266
|
+
*/
|
|
1267
|
+
async function ensureOriginSecret(ctx, serverId) {
|
|
1268
|
+
const key = originSecretKey(serverId);
|
|
1269
|
+
const kept = ctx.secrets[key];
|
|
1270
|
+
if (kept) return kept;
|
|
1271
|
+
const secret = newOriginSecret();
|
|
1272
|
+
ctx.log(` ${serverId}: a new processor origin secret, kept as ${key}`);
|
|
1273
|
+
await ctx.writeSecrets({ [key]: secret });
|
|
1274
|
+
return secret;
|
|
1275
|
+
}
|
|
1276
|
+
/** The certificate for `host`: recorded, adopted, or requested — then issued. */
|
|
1277
|
+
async function ensureCertificate(ctx, clients, input) {
|
|
1278
|
+
const { ACM, acm } = clients;
|
|
1279
|
+
const { id, host } = input;
|
|
1280
|
+
const entry = {
|
|
1281
|
+
key: `acm-certificate:${id}`,
|
|
1282
|
+
type: "acm-certificate",
|
|
1283
|
+
data: { host }
|
|
1284
|
+
};
|
|
1285
|
+
const describe = (arn) => readOr$2(() => acm.send(new ACM.DescribeCertificateCommand({ CertificateArn: arn })), ["ResourceNotFoundException"]).then((out) => out?.Certificate);
|
|
1286
|
+
let arn;
|
|
1287
|
+
const recorded = ctx.state.record(entry.key);
|
|
1288
|
+
if (recorded?.id && recorded.data?.host === host) {
|
|
1289
|
+
const found = await describe(recorded.id);
|
|
1290
|
+
if (found && found.Status !== "FAILED" && found.Status !== "VALIDATION_TIMED_OUT" && found.Status !== "REVOKED" && found.Status !== "EXPIRED") arn = recorded.id;
|
|
1291
|
+
}
|
|
1292
|
+
if (!arn) {
|
|
1293
|
+
arn = await findCertificate(clients, host);
|
|
1294
|
+
if (arn) ctx.log(` adopting the certificate for ${host} — ${arn}`);
|
|
1295
|
+
}
|
|
1296
|
+
if (!arn) {
|
|
1297
|
+
if (ctx.dryRun) {
|
|
1298
|
+
await ctx.change({
|
|
1299
|
+
construct: id,
|
|
1300
|
+
resource: `certificate ${host}`,
|
|
1301
|
+
change: "request (ACM, us-east-1, DNS-validated)"
|
|
1302
|
+
}, async () => {});
|
|
1303
|
+
return PLANNED;
|
|
1304
|
+
}
|
|
1305
|
+
await ctx.state.pending(entry);
|
|
1306
|
+
arn = (await acm.send(new ACM.RequestCertificateCommand({
|
|
1307
|
+
DomainName: host,
|
|
1308
|
+
ValidationMethod: "DNS",
|
|
1309
|
+
IdempotencyToken: input.name.replace(/[^A-Za-z0-9]/g, "").slice(0, 32),
|
|
1310
|
+
Tags: identityTags(ctx, id).map((t) => ({
|
|
1311
|
+
Key: t.Key,
|
|
1312
|
+
Value: t.Value
|
|
1313
|
+
}))
|
|
1314
|
+
}))).CertificateArn;
|
|
1315
|
+
await ctx.change({
|
|
1316
|
+
construct: id,
|
|
1317
|
+
resource: `certificate ${host}`,
|
|
1318
|
+
change: "request (ACM, us-east-1, DNS-validated)"
|
|
1319
|
+
}, async () => {});
|
|
1320
|
+
}
|
|
1321
|
+
if (!ctx.dryRun) await ctx.state.ready(entry, arn);
|
|
1322
|
+
let certificate = await describe(arn);
|
|
1323
|
+
let record = certificate?.DomainValidationOptions?.[0]?.ResourceRecord;
|
|
1324
|
+
for (let i = 0; !record && i < 15; i++) {
|
|
1325
|
+
await sleep(Math.min(clients.wait.intervalMs, 2e3));
|
|
1326
|
+
certificate = await describe(arn);
|
|
1327
|
+
record = certificate?.DomainValidationOptions?.[0]?.ResourceRecord;
|
|
1328
|
+
}
|
|
1329
|
+
const validation = record ? {
|
|
1330
|
+
name: record.Name.replace(/\.$/, ""),
|
|
1331
|
+
value: record.Value.replace(/\.$/, "")
|
|
1332
|
+
} : void 0;
|
|
1333
|
+
const written = validation ? await ctx.writeDns([{
|
|
1334
|
+
host: validation.name,
|
|
1335
|
+
type: "CNAME",
|
|
1336
|
+
value: validation.value
|
|
1337
|
+
}]) : { manual: [] };
|
|
1338
|
+
if (certificate?.Status === "ISSUED" || ctx.dryRun) return arn;
|
|
1339
|
+
if (validation && written.manual.includes(validation.name.toLowerCase())) throw new CdnCertificatePending(id, host, validation, "manual");
|
|
1340
|
+
ctx.log(` waiting for ACM to issue the certificate for ${host}…`);
|
|
1341
|
+
const deadline = Date.now() + clients.wait.timeoutMs;
|
|
1342
|
+
while (certificate?.Status === "PENDING_VALIDATION" && Date.now() < deadline) {
|
|
1343
|
+
await sleep(clients.wait.intervalMs);
|
|
1344
|
+
certificate = await describe(arn);
|
|
1345
|
+
}
|
|
1346
|
+
if (certificate?.Status === "ISSUED") return arn;
|
|
1347
|
+
if (certificate?.Status === "PENDING_VALIDATION" || !certificate) throw new CdnCertificatePending(id, host, validation ?? {
|
|
1348
|
+
name: `(not published yet) ${host}`,
|
|
1349
|
+
value: ""
|
|
1350
|
+
}, "timeout");
|
|
1351
|
+
throw new CdnCertificateFailed(id, host, certificate.Status ?? "unknown", certificate.FailureReason);
|
|
1352
|
+
}
|
|
1353
|
+
/** An issued or pending certificate for exactly `host`, if the account has one. */
|
|
1354
|
+
async function findCertificate(clients, host) {
|
|
1355
|
+
const { ACM, acm } = clients;
|
|
1356
|
+
let NextToken;
|
|
1357
|
+
do {
|
|
1358
|
+
const page = await acm.send(new ACM.ListCertificatesCommand({
|
|
1359
|
+
CertificateStatuses: ["ISSUED", "PENDING_VALIDATION"],
|
|
1360
|
+
...NextToken ? { NextToken } : {}
|
|
1361
|
+
}));
|
|
1362
|
+
const match = page.CertificateSummaryList?.find((c) => c.DomainName?.toLowerCase() === host);
|
|
1363
|
+
if (match?.CertificateArn) return match.CertificateArn;
|
|
1364
|
+
NextToken = page.NextToken;
|
|
1365
|
+
} while (NextToken);
|
|
1366
|
+
}
|
|
1367
|
+
/** The origin access control CloudFront signs its bucket requests with. */
|
|
1368
|
+
async function ensureOriginAccessControl(ctx, clients, input) {
|
|
1369
|
+
const { CF, cloudfront } = clients;
|
|
1370
|
+
const entry = {
|
|
1371
|
+
key: `cloudfront-oac:${input.id}`,
|
|
1372
|
+
type: "cloudfront-oac",
|
|
1373
|
+
data: { name: input.name }
|
|
1374
|
+
};
|
|
1375
|
+
const recorded = ctx.state.record(entry.key)?.id;
|
|
1376
|
+
if (recorded) {
|
|
1377
|
+
if (await readOr$2(() => cloudfront.send(new CF.GetOriginAccessControlCommand({ Id: recorded })), ["NoSuchOriginAccessControl"])) return recorded;
|
|
1378
|
+
}
|
|
1379
|
+
let Marker;
|
|
1380
|
+
do {
|
|
1381
|
+
const page = await cloudfront.send(new CF.ListOriginAccessControlsCommand({ ...Marker ? { Marker } : {} }));
|
|
1382
|
+
const match = page.OriginAccessControlList?.Items?.find((o) => o.Name === input.name);
|
|
1383
|
+
if (match?.Id) {
|
|
1384
|
+
if (!ctx.dryRun) await ctx.state.ready(entry, match.Id);
|
|
1385
|
+
return match.Id;
|
|
1386
|
+
}
|
|
1387
|
+
Marker = page.OriginAccessControlList?.NextMarker;
|
|
1388
|
+
} while (Marker);
|
|
1389
|
+
let id = PLANNED;
|
|
1390
|
+
await ctx.change({
|
|
1391
|
+
construct: input.id,
|
|
1392
|
+
resource: `origin access control ${input.name}`,
|
|
1393
|
+
change: `create (CloudFront signs its reads of ${input.bucket})`
|
|
1394
|
+
}, async () => {
|
|
1395
|
+
await ctx.state.pending(entry);
|
|
1396
|
+
id = (await cloudfront.send(new CF.CreateOriginAccessControlCommand({ OriginAccessControlConfig: {
|
|
1397
|
+
Name: input.name,
|
|
1398
|
+
Description: `gkm: CloudFront's reads of ${input.bucket}`,
|
|
1399
|
+
OriginAccessControlOriginType: "s3",
|
|
1400
|
+
SigningBehavior: "always",
|
|
1401
|
+
SigningProtocol: "sigv4"
|
|
1402
|
+
} }))).OriginAccessControl.Id;
|
|
1403
|
+
await ctx.state.ready(entry, id);
|
|
1404
|
+
});
|
|
1405
|
+
return id;
|
|
1406
|
+
}
|
|
1407
|
+
/** One response headers policy, as wanted. */
|
|
1408
|
+
async function ensureHeadersPolicy(ctx, clients, input) {
|
|
1409
|
+
const { CF, cloudfront } = clients;
|
|
1410
|
+
const { config } = input;
|
|
1411
|
+
const entry = {
|
|
1412
|
+
key: `cloudfront-headers:${input.id}:${input.kind}`,
|
|
1413
|
+
type: "cloudfront-headers",
|
|
1414
|
+
data: { name: config.Name }
|
|
1415
|
+
};
|
|
1416
|
+
const read = (Id) => readOr$2(() => cloudfront.send(new CF.GetResponseHeadersPolicyConfigCommand({ Id })), ["NoSuchResponseHeadersPolicy"]);
|
|
1417
|
+
let id = ctx.state.record(entry.key)?.id;
|
|
1418
|
+
let current = id ? await read(id) : void 0;
|
|
1419
|
+
if (!current) {
|
|
1420
|
+
id = void 0;
|
|
1421
|
+
let Marker;
|
|
1422
|
+
do {
|
|
1423
|
+
const page = await cloudfront.send(new CF.ListResponseHeadersPoliciesCommand({
|
|
1424
|
+
Type: "custom",
|
|
1425
|
+
...Marker ? { Marker } : {}
|
|
1426
|
+
}));
|
|
1427
|
+
const match = page.ResponseHeadersPolicyList?.Items?.find((p) => p.ResponseHeadersPolicy?.ResponseHeadersPolicyConfig?.Name === config.Name);
|
|
1428
|
+
if (match?.ResponseHeadersPolicy?.Id) {
|
|
1429
|
+
id = match.ResponseHeadersPolicy.Id;
|
|
1430
|
+
current = await read(id);
|
|
1431
|
+
}
|
|
1432
|
+
Marker = page.ResponseHeadersPolicyList?.NextMarker;
|
|
1433
|
+
} while (!id && Marker);
|
|
1434
|
+
}
|
|
1435
|
+
if (id && current) {
|
|
1436
|
+
if (!ctx.dryRun) await ctx.state.ready(entry, id);
|
|
1437
|
+
if (sameHeadersPolicy(current.ResponseHeadersPolicyConfig, config)) return id;
|
|
1438
|
+
const existing = id;
|
|
1439
|
+
await ctx.change({
|
|
1440
|
+
construct: input.id,
|
|
1441
|
+
resource: `response headers policy ${config.Name}`,
|
|
1442
|
+
change: "put back what drifted"
|
|
1443
|
+
}, async () => {
|
|
1444
|
+
await cloudfront.send(new CF.UpdateResponseHeadersPolicyCommand({
|
|
1445
|
+
Id: existing,
|
|
1446
|
+
IfMatch: current.ETag,
|
|
1447
|
+
ResponseHeadersPolicyConfig: config
|
|
1448
|
+
}));
|
|
1449
|
+
});
|
|
1450
|
+
return existing;
|
|
1451
|
+
}
|
|
1452
|
+
let created = PLANNED;
|
|
1453
|
+
await ctx.change({
|
|
1454
|
+
construct: input.id,
|
|
1455
|
+
resource: `response headers policy ${config.Name}`,
|
|
1456
|
+
change: "create"
|
|
1457
|
+
}, async () => {
|
|
1458
|
+
await ctx.state.pending(entry);
|
|
1459
|
+
created = (await cloudfront.send(new CF.CreateResponseHeadersPolicyCommand({ ResponseHeadersPolicyConfig: config }))).ResponseHeadersPolicy.Id;
|
|
1460
|
+
await ctx.state.ready(entry, created);
|
|
1461
|
+
});
|
|
1462
|
+
return created;
|
|
1463
|
+
}
|
|
1464
|
+
/** The distribution: recorded, found by its comment, or created — as wanted. */
|
|
1465
|
+
async function ensureDistribution(ctx, clients, input) {
|
|
1466
|
+
const { CF, cloudfront } = clients;
|
|
1467
|
+
const entry = {
|
|
1468
|
+
key: `cloudfront-distribution:${input.id}`,
|
|
1469
|
+
type: "cloudfront-distribution"
|
|
1470
|
+
};
|
|
1471
|
+
const get = (Id) => readOr$2(() => cloudfront.send(new CF.GetDistributionCommand({ Id })), ["NoSuchDistribution"]);
|
|
1472
|
+
let found = ctx.state.record(entry.key)?.id ? await get(ctx.state.record(entry.key).id) : void 0;
|
|
1473
|
+
if (!found) {
|
|
1474
|
+
let Marker;
|
|
1475
|
+
do {
|
|
1476
|
+
const page = await cloudfront.send(new CF.ListDistributionsCommand({ ...Marker ? { Marker } : {} }));
|
|
1477
|
+
const match = page.DistributionList?.Items?.find((d) => d.Comment === input.comment);
|
|
1478
|
+
if (match?.Id) found = await get(match.Id);
|
|
1479
|
+
Marker = page.DistributionList?.NextMarker;
|
|
1480
|
+
} while (!found && Marker);
|
|
1481
|
+
}
|
|
1482
|
+
if (found?.Distribution) {
|
|
1483
|
+
const distribution = found.Distribution;
|
|
1484
|
+
const id = distribution.Id;
|
|
1485
|
+
const located = {
|
|
1486
|
+
arn: distribution.ARN,
|
|
1487
|
+
domainName: distribution.DomainName
|
|
1488
|
+
};
|
|
1489
|
+
if (!ctx.dryRun) await ctx.state.ready({
|
|
1490
|
+
...entry,
|
|
1491
|
+
data: located
|
|
1492
|
+
}, id);
|
|
1493
|
+
const current = await cloudfront.send(new CF.GetDistributionConfigCommand({ Id: id }));
|
|
1494
|
+
if (holds(current.DistributionConfig, input.config)) return located;
|
|
1495
|
+
await ctx.change({
|
|
1496
|
+
construct: input.id,
|
|
1497
|
+
resource: `distribution ${id}`,
|
|
1498
|
+
change: "put back what drifted"
|
|
1499
|
+
}, async () => {
|
|
1500
|
+
await cloudfront.send(new CF.UpdateDistributionCommand({
|
|
1501
|
+
Id: id,
|
|
1502
|
+
IfMatch: current.ETag,
|
|
1503
|
+
DistributionConfig: {
|
|
1504
|
+
...current.DistributionConfig,
|
|
1505
|
+
...input.config,
|
|
1506
|
+
CallerReference: current.DistributionConfig.CallerReference
|
|
1507
|
+
}
|
|
1508
|
+
}));
|
|
1509
|
+
});
|
|
1510
|
+
return located;
|
|
1511
|
+
}
|
|
1512
|
+
let created = {
|
|
1513
|
+
arn: `arn:aws:cloudfront::${PLANNED}`,
|
|
1514
|
+
domainName: PLANNED
|
|
1515
|
+
};
|
|
1516
|
+
const host = input.config.Aliases?.Items?.[0] ?? input.id;
|
|
1517
|
+
await ctx.change({
|
|
1518
|
+
construct: input.id,
|
|
1519
|
+
resource: `distribution ${host}`,
|
|
1520
|
+
change: "create (CloudFront, takes minutes to reach every edge)"
|
|
1521
|
+
}, async () => {
|
|
1522
|
+
await ctx.state.pending(entry);
|
|
1523
|
+
const out = await cloudfront.send(new CF.CreateDistributionWithTagsCommand({ DistributionConfigWithTags: {
|
|
1524
|
+
DistributionConfig: input.config,
|
|
1525
|
+
Tags: { Items: identityTags(ctx, input.id).map((t) => ({
|
|
1526
|
+
Key: t.Key,
|
|
1527
|
+
Value: t.Value
|
|
1528
|
+
})) }
|
|
1529
|
+
} }));
|
|
1530
|
+
created = {
|
|
1531
|
+
arn: out.Distribution.ARN,
|
|
1532
|
+
domainName: out.Distribution.DomainName
|
|
1533
|
+
};
|
|
1534
|
+
await ctx.state.ready({
|
|
1535
|
+
...entry,
|
|
1536
|
+
data: created
|
|
1537
|
+
}, out.Distribution.Id);
|
|
1538
|
+
});
|
|
1539
|
+
return created;
|
|
1540
|
+
}
|
|
1541
|
+
//#endregion
|
|
1542
|
+
//#region src/providers/s3/events.ts
|
|
1543
|
+
/** What S3 tells the topic about: every object written, and every removed. */
|
|
1544
|
+
const BUCKET_EVENTS = ["s3:ObjectCreated:*", "s3:ObjectRemoved:*"];
|
|
1545
|
+
/**
|
|
1546
|
+
* Each bucket a subscriber watches, with the subscribers discovery could name.
|
|
1547
|
+
*
|
|
1548
|
+
* A bucket is watched when a process consumes its events — discovery records
|
|
1549
|
+
* the edge, and the subscriber by its export — or when a build put a
|
|
1550
|
+
* subscriber in its declaration. Only a named subscriber can be subscribed: a
|
|
1551
|
+
* route is named after its export.
|
|
1552
|
+
*/
|
|
1553
|
+
function bucketWatchers(manifest, runnables, subscribers = {}) {
|
|
1554
|
+
const watched = /* @__PURE__ */ new Map();
|
|
1555
|
+
for (const [id, declaration] of Object.entries(manifest)) {
|
|
1556
|
+
if (declaration.kind !== "objects") continue;
|
|
1557
|
+
const key = provideKey(id, "eventsConnectionString");
|
|
1558
|
+
const named = Object.entries(subscribers).flatMap(([worker, bound]) => bound.filter((s) => s.connectionKey === key).map((s) => ({
|
|
1559
|
+
worker,
|
|
1560
|
+
name: s.name
|
|
1561
|
+
}))).sort((a, b) => `${a.worker}/${a.name}`.localeCompare(`${b.worker}/${b.name}`));
|
|
1562
|
+
const edged = Object.values(runnables).some((edges) => edges.includes(eventsEdge(id)));
|
|
1563
|
+
const declared = (declaration.subscribers?.length ?? 0) > 0;
|
|
1564
|
+
if (named.length > 0 || edged || declared) watched.set(id, named);
|
|
1565
|
+
}
|
|
1566
|
+
return watched;
|
|
1567
|
+
}
|
|
1568
|
+
/** The topic a bucket's changes are published to. */
|
|
1569
|
+
function eventsTopicName(bucket) {
|
|
1570
|
+
return `${bucket.replaceAll(".", "-")}-events`;
|
|
1571
|
+
}
|
|
1572
|
+
/** What SNS gives up delivering, kept. */
|
|
1573
|
+
function eventsDeadLetterName(bucket) {
|
|
1574
|
+
return `${eventsTopicName(bucket)}-dlq`;
|
|
1575
|
+
}
|
|
1576
|
+
/** Only this bucket, in this account, may publish to its topic. */
|
|
1577
|
+
function eventsTopicPolicy(topicArn, bucket, account) {
|
|
1578
|
+
return {
|
|
1579
|
+
Version: "2012-10-17",
|
|
1580
|
+
Statement: [{
|
|
1581
|
+
Sid: "GkmBucketPublishes",
|
|
1582
|
+
Effect: "Allow",
|
|
1583
|
+
Principal: { Service: "s3.amazonaws.com" },
|
|
1584
|
+
Action: "sns:Publish",
|
|
1585
|
+
Resource: topicArn,
|
|
1586
|
+
Condition: {
|
|
1587
|
+
ArnLike: { "aws:SourceArn": `arn:aws:s3:::${bucket}` },
|
|
1588
|
+
StringEquals: { "aws:SourceAccount": account }
|
|
1589
|
+
}
|
|
1590
|
+
}]
|
|
1591
|
+
};
|
|
1592
|
+
}
|
|
1593
|
+
/** Only the bucket's topic may send to its dead-letter queue. */
|
|
1594
|
+
function eventsDeadLetterPolicy(queueArn, topicArn) {
|
|
1595
|
+
return {
|
|
1596
|
+
Version: "2012-10-17",
|
|
1597
|
+
Statement: [{
|
|
1598
|
+
Sid: "GkmTopicDeadLetters",
|
|
1599
|
+
Effect: "Allow",
|
|
1600
|
+
Principal: { Service: "sns.amazonaws.com" },
|
|
1601
|
+
Action: "sqs:SendMessage",
|
|
1602
|
+
Resource: queueArn,
|
|
1603
|
+
Condition: { ArnEquals: { "aws:SourceArn": topicArn } }
|
|
1604
|
+
}]
|
|
1605
|
+
};
|
|
1606
|
+
}
|
|
1607
|
+
/** The id this provider's notification entry goes under. */
|
|
1608
|
+
const notificationId = (id) => `gkm-${id}`;
|
|
1609
|
+
/**
|
|
1610
|
+
* The bucket's notifications with this provider's entry put back, and whether
|
|
1611
|
+
* that changed anything. Every other entry — a queue, a function, a topic
|
|
1612
|
+
* somebody added by hand — is kept exactly as it was.
|
|
1613
|
+
*/
|
|
1614
|
+
function mergedNotifications(current, id, topicArn) {
|
|
1615
|
+
const own = notificationId(id);
|
|
1616
|
+
const wanted = {
|
|
1617
|
+
Id: own,
|
|
1618
|
+
TopicArn: topicArn,
|
|
1619
|
+
Events: [...BUCKET_EVENTS]
|
|
1620
|
+
};
|
|
1621
|
+
const topics = current.TopicConfigurations ?? [];
|
|
1622
|
+
const existing = topics.find((t) => t.Id === own);
|
|
1623
|
+
const same = existing !== void 0 && existing.TopicArn === topicArn && JSON.stringify([...existing.Events ?? []].sort()) === JSON.stringify([...BUCKET_EVENTS].sort()) && existing.Filter === void 0;
|
|
1624
|
+
const { ResponseMetadata: _, ...rest } = current;
|
|
1625
|
+
return {
|
|
1626
|
+
config: {
|
|
1627
|
+
...rest,
|
|
1628
|
+
TopicConfigurations: [...topics.filter((t) => t.Id !== own), wanted]
|
|
1629
|
+
},
|
|
1630
|
+
changed: !same
|
|
1631
|
+
};
|
|
1632
|
+
}
|
|
1633
|
+
/** S3 refused the entry because another one already covers these events. */
|
|
1634
|
+
var BucketNotificationsOverlap = class extends GkmError {
|
|
1635
|
+
bucket;
|
|
1636
|
+
detail;
|
|
1637
|
+
constructor(bucket, detail) {
|
|
1638
|
+
super(`'${bucket}' already sends object creates or deletes somewhere else, and S3 sends each event to one place only, so the deploy cannot add its own entry. Remove the other notification from the bucket, or give it a prefix, and deploy again. S3 said: ${detail}`);
|
|
1639
|
+
this.bucket = bucket;
|
|
1640
|
+
this.detail = detail;
|
|
1641
|
+
this.name = "BucketNotificationsOverlap";
|
|
1642
|
+
}
|
|
1643
|
+
};
|
|
1644
|
+
const defaultEventsClients = async (credential, region) => {
|
|
1645
|
+
const SNS = await import("@aws-sdk/client-sns");
|
|
1646
|
+
const SQS = await import("@aws-sdk/client-sqs");
|
|
1647
|
+
const { STSClient, GetCallerIdentityCommand } = await import("@aws-sdk/client-sts");
|
|
1648
|
+
const config = await awsClientConfig(credential, region);
|
|
1649
|
+
const endpoint = awsEndpoint(process.env, "SNS");
|
|
1650
|
+
const sts = new STSClient({
|
|
1651
|
+
...config,
|
|
1652
|
+
...awsEndpoint(process.env, "STS") ? { endpoint: awsEndpoint(process.env, "STS") } : {}
|
|
1653
|
+
});
|
|
1654
|
+
const { Account } = await sts.send(new GetCallerIdentityCommand({}));
|
|
1655
|
+
sts.destroy();
|
|
1656
|
+
return {
|
|
1657
|
+
SNS,
|
|
1658
|
+
SQS,
|
|
1659
|
+
sns: new SNS.SNSClient({
|
|
1660
|
+
...config,
|
|
1661
|
+
...endpoint ? { endpoint } : {}
|
|
1662
|
+
}),
|
|
1663
|
+
sqs: new SQS.SQSClient({
|
|
1664
|
+
...config,
|
|
1665
|
+
...awsEndpoint(process.env, "SQS") ? { endpoint: awsEndpoint(process.env, "SQS") } : {}
|
|
1666
|
+
}),
|
|
1667
|
+
account: Account,
|
|
1668
|
+
region,
|
|
1669
|
+
...endpoint ? { endpoint } : {}
|
|
1670
|
+
};
|
|
1671
|
+
};
|
|
1672
|
+
let makeEventsClients = defaultEventsClients;
|
|
1673
|
+
function errorName$1(error) {
|
|
1674
|
+
const e = error;
|
|
1675
|
+
return e?.name ?? e?.Code ?? e?.code ?? "";
|
|
1676
|
+
}
|
|
1677
|
+
async function readOr$1(read, missing) {
|
|
1678
|
+
try {
|
|
1679
|
+
return await read();
|
|
1680
|
+
} catch (error) {
|
|
1681
|
+
if (missing.includes(errorName$1(error))) return void 0;
|
|
1682
|
+
throw error;
|
|
1683
|
+
}
|
|
1684
|
+
}
|
|
1685
|
+
/**
|
|
1686
|
+
* The topic, its policy, the dead-letter queue and the bucket's notification
|
|
1687
|
+
* entry for one watched bucket — created where missing, put back where they
|
|
1688
|
+
* drifted.
|
|
1689
|
+
*/
|
|
1690
|
+
async function ensureBucketEvents(ctx, s3, input) {
|
|
1691
|
+
const { id, bucket, region } = input;
|
|
1692
|
+
const { SNS, SQS, sns, sqs, account } = await makeEventsClients(ctx.credential, region);
|
|
1693
|
+
const topicName = eventsTopicName(bucket);
|
|
1694
|
+
const topicArn = `arn:aws:sns:${region}:${account}:${topicName}`;
|
|
1695
|
+
const topicEntry = {
|
|
1696
|
+
key: `sns-topic:${id}`,
|
|
1697
|
+
type: "sns-topic"
|
|
1698
|
+
};
|
|
1699
|
+
const topic = await readOr$1(() => sns.send(new SNS.GetTopicAttributesCommand({ TopicArn: topicArn })), ["NotFound", "NotFoundException"]);
|
|
1700
|
+
if (topic) await ctx.state.ready(topicEntry, topicArn);
|
|
1701
|
+
else await ctx.change({
|
|
1702
|
+
construct: id,
|
|
1703
|
+
resource: `SNS topic ${topicName}`,
|
|
1704
|
+
change: "create"
|
|
1705
|
+
}, async () => {
|
|
1706
|
+
await ctx.state.pending(topicEntry);
|
|
1707
|
+
await sns.send(new SNS.CreateTopicCommand({
|
|
1708
|
+
Name: topicName,
|
|
1709
|
+
Tags: identityTags(ctx, id)
|
|
1710
|
+
}));
|
|
1711
|
+
await ctx.state.ready(topicEntry, topicArn);
|
|
1712
|
+
});
|
|
1713
|
+
const topicPolicy = eventsTopicPolicy(topicArn, bucket, account);
|
|
1714
|
+
const currentTopicPolicy = topic?.Attributes?.Policy;
|
|
1715
|
+
if (!currentTopicPolicy || canonicalPolicy(JSON.parse(currentTopicPolicy)) !== canonicalPolicy(topicPolicy)) await ctx.change({
|
|
1716
|
+
construct: id,
|
|
1717
|
+
resource: `SNS topic ${topicName}`,
|
|
1718
|
+
change: `publishable by '${bucket}' only`
|
|
1719
|
+
}, async () => {
|
|
1720
|
+
await sns.send(new SNS.SetTopicAttributesCommand({
|
|
1721
|
+
TopicArn: topicArn,
|
|
1722
|
+
AttributeName: "Policy",
|
|
1723
|
+
AttributeValue: JSON.stringify(topicPolicy)
|
|
1724
|
+
}));
|
|
1725
|
+
});
|
|
1726
|
+
const queueName = eventsDeadLetterName(bucket);
|
|
1727
|
+
const deadLetterArn = `arn:aws:sqs:${region}:${account}:${queueName}`;
|
|
1728
|
+
const queueEntry = {
|
|
1729
|
+
key: `sqs-queue:${id}-events-dlq`,
|
|
1730
|
+
type: "sqs-queue"
|
|
1731
|
+
};
|
|
1732
|
+
const queue = await readOr$1(() => sqs.send(new SQS.GetQueueUrlCommand({ QueueName: queueName })), ["QueueDoesNotExist", "AWS.SimpleQueueService.NonExistentQueue"]);
|
|
1733
|
+
let queueUrl = queue?.QueueUrl;
|
|
1734
|
+
if (queueUrl) await ctx.state.ready(queueEntry, deadLetterArn);
|
|
1735
|
+
else await ctx.change({
|
|
1736
|
+
construct: id,
|
|
1737
|
+
resource: `SQS queue ${queueName}`,
|
|
1738
|
+
change: "create"
|
|
1739
|
+
}, async () => {
|
|
1740
|
+
await ctx.state.pending(queueEntry);
|
|
1741
|
+
queueUrl = (await sqs.send(new SQS.CreateQueueCommand({
|
|
1742
|
+
QueueName: queueName,
|
|
1743
|
+
Attributes: { MessageRetentionPeriod: "1209600" },
|
|
1744
|
+
tags: Object.fromEntries(identityTags(ctx, id).map((t) => [t.Key, t.Value]))
|
|
1745
|
+
}))).QueueUrl;
|
|
1746
|
+
await ctx.state.ready(queueEntry, deadLetterArn);
|
|
1747
|
+
});
|
|
1748
|
+
const queuePolicy = eventsDeadLetterPolicy(deadLetterArn, topicArn);
|
|
1749
|
+
const currentQueuePolicy = queue?.QueueUrl ? (await sqs.send(new SQS.GetQueueAttributesCommand({
|
|
1750
|
+
QueueUrl: queue.QueueUrl,
|
|
1751
|
+
AttributeNames: ["Policy"]
|
|
1752
|
+
}))).Attributes?.Policy : void 0;
|
|
1753
|
+
if (!currentQueuePolicy || canonicalPolicy(JSON.parse(currentQueuePolicy)) !== canonicalPolicy(queuePolicy)) await ctx.change({
|
|
1754
|
+
construct: id,
|
|
1755
|
+
resource: `SQS queue ${queueName}`,
|
|
1756
|
+
change: `receives what ${topicName} cannot deliver`
|
|
1757
|
+
}, async () => {
|
|
1758
|
+
await sqs.send(new SQS.SetQueueAttributesCommand({
|
|
1759
|
+
QueueUrl: queueUrl,
|
|
1760
|
+
Attributes: { Policy: JSON.stringify(queuePolicy) }
|
|
1761
|
+
}));
|
|
1762
|
+
});
|
|
1763
|
+
const merged = mergedNotifications(await readOr$1(() => s3.s3.send(new s3.S3.GetBucketNotificationConfigurationCommand({ Bucket: bucket })), ["NoSuchBucket"]) ?? {}, id, topicArn);
|
|
1764
|
+
if (merged.changed) await ctx.change({
|
|
1765
|
+
construct: id,
|
|
1766
|
+
resource: `bucket ${bucket}`,
|
|
1767
|
+
change: `send object creates and deletes to ${topicName}`
|
|
1768
|
+
}, async () => {
|
|
1769
|
+
try {
|
|
1770
|
+
await s3.s3.send(new s3.S3.PutBucketNotificationConfigurationCommand({
|
|
1771
|
+
Bucket: bucket,
|
|
1772
|
+
NotificationConfiguration: merged.config
|
|
1773
|
+
}));
|
|
1774
|
+
} catch (error) {
|
|
1775
|
+
const message = error.message ?? "";
|
|
1776
|
+
if (/overlap/i.test(message)) throw new BucketNotificationsOverlap(bucket, message);
|
|
1777
|
+
throw error;
|
|
1778
|
+
}
|
|
1779
|
+
});
|
|
1780
|
+
sns.destroy();
|
|
1781
|
+
sqs.destroy();
|
|
1782
|
+
return {
|
|
1783
|
+
topicArn,
|
|
1784
|
+
deadLetterArn
|
|
1785
|
+
};
|
|
1786
|
+
}
|
|
1787
|
+
/**
|
|
1788
|
+
* The events key: the topic's address, signed by the bucket's own access key
|
|
1789
|
+
* — read off the bucket's URL, so a rotated key rotates this one with it.
|
|
1790
|
+
*/
|
|
1791
|
+
async function ensureEventsKey(ctx, input) {
|
|
1792
|
+
const { id, topicArn, region } = input;
|
|
1793
|
+
const key = provideKey(id, "eventsConnectionString");
|
|
1794
|
+
const url = ctx.secrets[provideKey(id, "url")];
|
|
1795
|
+
let parsed;
|
|
1796
|
+
try {
|
|
1797
|
+
parsed = url ? s3Url.parse(url) : void 0;
|
|
1798
|
+
} catch {
|
|
1799
|
+
parsed = void 0;
|
|
1800
|
+
}
|
|
1801
|
+
if (!parsed?.accessKeyId || !parsed.secretAccessKey) {
|
|
1802
|
+
await ctx.change({
|
|
1803
|
+
construct: id,
|
|
1804
|
+
resource: key,
|
|
1805
|
+
change: "set to the topic’s address"
|
|
1806
|
+
}, async () => {});
|
|
1807
|
+
return;
|
|
1808
|
+
}
|
|
1809
|
+
const { snsUrl } = await import("@geekmidas/events/sns");
|
|
1810
|
+
const endpoint = awsEndpoint(process.env, "SNS");
|
|
1811
|
+
const address = new URL(snsUrl.build({
|
|
1812
|
+
topicArn,
|
|
1813
|
+
region,
|
|
1814
|
+
...endpoint ? { endpoint } : {}
|
|
1815
|
+
}));
|
|
1816
|
+
address.searchParams.set("accessKeyId", parsed.accessKeyId);
|
|
1817
|
+
address.searchParams.set("secretAccessKey", parsed.secretAccessKey);
|
|
1818
|
+
const value = address.toString();
|
|
1819
|
+
const entry = {
|
|
1820
|
+
key: `events-key:${id}`,
|
|
1821
|
+
type: "url"
|
|
1822
|
+
};
|
|
1823
|
+
if (ctx.secrets[key] === value) {
|
|
1824
|
+
await ctx.state.ready(entry, topicArn);
|
|
1825
|
+
return;
|
|
1826
|
+
}
|
|
1827
|
+
await ctx.change({
|
|
1828
|
+
construct: id,
|
|
1829
|
+
resource: key,
|
|
1830
|
+
change: `set to ${topicArn}`
|
|
1831
|
+
}, async () => {
|
|
1832
|
+
await ctx.writeSecrets({ [key]: value });
|
|
1833
|
+
await ctx.state.ready(entry, topicArn);
|
|
1834
|
+
});
|
|
1835
|
+
}
|
|
1836
|
+
/** Where SNS pushes to a worker: its own host on the stage's domain. */
|
|
1837
|
+
function workerPushHost(worker, domain) {
|
|
1838
|
+
return `${appKey(worker)}.${domain}`;
|
|
1839
|
+
}
|
|
1840
|
+
/**
|
|
1841
|
+
* One HTTP subscription per subscriber to the bucket's topic, at its route on
|
|
1842
|
+
* its worker's host, with the bucket's dead-letter queue — and each ARN
|
|
1843
|
+
* recorded, which is what the worker accepts deliveries under.
|
|
1844
|
+
*
|
|
1845
|
+
* Unfiltered: a bucket's notifications carry no `type` attribute, and the
|
|
1846
|
+
* subscriber's own filter — events, prefix, suffix — is what decides. Only a
|
|
1847
|
+
* compose stage pushes to a worker; any other is told so and left alone.
|
|
1848
|
+
*/
|
|
1849
|
+
async function ensurePushSubscriptions(ctx, input) {
|
|
1850
|
+
const { id, events, region, watchers } = input;
|
|
1851
|
+
if (!deploysWithCompose(ctx.workspace)) {
|
|
1852
|
+
ctx.log(` '${id}' is watched, and only a compose stage pushes a bucket's changes to a worker: its subscribers receive nothing on '${ctx.stage}'`);
|
|
1853
|
+
return;
|
|
1854
|
+
}
|
|
1855
|
+
if (watchers.length === 0) return;
|
|
1856
|
+
const domain = ctx.workspace.domains?.[ctx.stage];
|
|
1857
|
+
if (!domain) throw new NoDomainForStage(ctx.stage);
|
|
1858
|
+
const { SNSConnection, reconcileHttpSubscriptions } = await import("@geekmidas/events/sns");
|
|
1859
|
+
const { credentials } = await awsClientConfig(ctx.credential, region);
|
|
1860
|
+
const endpoint = awsEndpoint(process.env, "SNS");
|
|
1861
|
+
const byWorker = /* @__PURE__ */ new Map();
|
|
1862
|
+
for (const watcher of watchers) byWorker.set(watcher.worker, [...byWorker.get(watcher.worker) ?? [], watcher]);
|
|
1863
|
+
for (const [worker, own] of byWorker) {
|
|
1864
|
+
const host = workerPushHost(worker, domain);
|
|
1865
|
+
const endpoints = own.map(({ name }) => ({
|
|
1866
|
+
name,
|
|
1867
|
+
endpoint: `https://${host}${subscriberRoute(name)}`,
|
|
1868
|
+
deadLetterQueueArn: events.deadLetterArn
|
|
1869
|
+
}));
|
|
1870
|
+
const unrecorded = own.filter(({ name }) => ctx.state.record(subscriptionKey(worker, name))?.status !== "ready");
|
|
1871
|
+
if (ctx.dryRun) {
|
|
1872
|
+
if (unrecorded.length > 0) await ctx.change({
|
|
1873
|
+
construct: id,
|
|
1874
|
+
resource: `SNS subscriptions on ${host}`,
|
|
1875
|
+
change: `subscribe ${unrecorded.map((w) => w.name).join(", ")}`
|
|
1876
|
+
}, async () => {});
|
|
1877
|
+
continue;
|
|
1878
|
+
}
|
|
1879
|
+
const connection = new SNSConnection({
|
|
1880
|
+
topicArn: events.topicArn,
|
|
1881
|
+
region,
|
|
1882
|
+
...endpoint ? { endpoint } : {},
|
|
1883
|
+
...credentials ? { credentials } : {}
|
|
1884
|
+
});
|
|
1885
|
+
const result = await reconcileHttpSubscriptions(connection, {
|
|
1886
|
+
endpoints,
|
|
1887
|
+
ownedHost: host
|
|
1888
|
+
});
|
|
1889
|
+
await connection.close();
|
|
1890
|
+
for (const spec of endpoints) {
|
|
1891
|
+
const arn = result.subscriptions[spec.name];
|
|
1892
|
+
const pending = result.pending.includes(spec.name);
|
|
1893
|
+
if (ctx.state.record(subscriptionKey(worker, spec.name))?.id !== arn) ctx.log(` ✓ '${spec.name}' subscribed to ${events.topicArn.split(":").pop()} at ${spec.endpoint}`);
|
|
1894
|
+
const { key, type, data } = subscriptionResource({
|
|
1895
|
+
worker,
|
|
1896
|
+
name: spec.name,
|
|
1897
|
+
topicArn: events.topicArn,
|
|
1898
|
+
endpoint: spec.endpoint,
|
|
1899
|
+
deadLetterQueueArn: events.deadLetterArn,
|
|
1900
|
+
pending
|
|
1901
|
+
}, arn);
|
|
1902
|
+
await ctx.state.ready({
|
|
1903
|
+
key,
|
|
1904
|
+
type,
|
|
1905
|
+
...data ? { data } : {}
|
|
1906
|
+
}, arn);
|
|
1907
|
+
}
|
|
1908
|
+
for (const arn of result.removed) ctx.log(` – unsubscribed ${arn}: no subscriber wants it any more`);
|
|
1909
|
+
}
|
|
1910
|
+
}
|
|
1911
|
+
//#endregion
|
|
1912
|
+
//#region src/providers/s3/origins.ts
|
|
1913
|
+
/**
|
|
1914
|
+
* Which browser origins may call a bucket: the stage's sites that depend on
|
|
1915
|
+
* an API using it.
|
|
1916
|
+
*
|
|
1917
|
+
* The same reading of the graph an API's own CORS origins are — its callers —
|
|
1918
|
+
* taken one step further: a site that calls an API which hands it a presigned
|
|
1919
|
+
* upload URL is a site whose browser PUTs to the bucket. A site that reads the
|
|
1920
|
+
* bucket (or a file server over it) itself is one too. Nothing is written
|
|
1921
|
+
* down: add a site that calls the API and the next provision run allows it.
|
|
1922
|
+
*/
|
|
1923
|
+
/** The keys a reader of this bucket holds: its URL, and each server's over it. */
|
|
1924
|
+
function bucketKeys(manifest, bucket) {
|
|
1925
|
+
return [provideKey(bucket, "url"), ...Object.entries(manifest).filter(([, d]) => d.kind === "file-server" && d.of === bucket).map(([id]) => provideKey(id, "url"))];
|
|
1926
|
+
}
|
|
1927
|
+
function bucketOrigins(input) {
|
|
1928
|
+
const { manifest, workspace, stage } = input;
|
|
1929
|
+
const keys = bucketKeys(manifest, input.bucket);
|
|
1930
|
+
const readers = Object.entries(manifest).filter(([, d]) => d.kind === "rest-api" || d.kind === "websocket-api" || d.kind === "site").filter(([id]) => {
|
|
1931
|
+
const env = appEnvKeys(manifest, appKey(id), input.runnables);
|
|
1932
|
+
return env !== void 0 && keys.some((key) => env.has(key));
|
|
1933
|
+
});
|
|
1934
|
+
const sites = /* @__PURE__ */ new Set();
|
|
1935
|
+
for (const [id, declaration] of readers) {
|
|
1936
|
+
if (declaration.kind === "site") {
|
|
1937
|
+
sites.add(id);
|
|
1938
|
+
continue;
|
|
1939
|
+
}
|
|
1940
|
+
for (const caller of dependentsOf(manifest, id)) if (manifest[caller]?.kind === "site") sites.add(caller);
|
|
1941
|
+
}
|
|
1942
|
+
const origins = /* @__PURE__ */ new Set();
|
|
1943
|
+
const unresolved = [];
|
|
1944
|
+
for (const id of [...sites].sort()) {
|
|
1945
|
+
const name = appKey(id);
|
|
1946
|
+
const app = workspace.apps[name];
|
|
1947
|
+
if (!app) {
|
|
1948
|
+
unresolved.push(name);
|
|
1949
|
+
continue;
|
|
1950
|
+
}
|
|
1951
|
+
try {
|
|
1952
|
+
const host = resolveHost(name, app, stage, workspace.domains, isMainFrontendApp(name, app, workspace.apps));
|
|
1953
|
+
origins.add(`https://${host}`);
|
|
1954
|
+
} catch (error) {
|
|
1955
|
+
if (!(error instanceof NoDomainForStage)) throw error;
|
|
1956
|
+
unresolved.push(name);
|
|
1957
|
+
}
|
|
1958
|
+
}
|
|
1959
|
+
return {
|
|
1960
|
+
origins: [...origins].sort(),
|
|
1961
|
+
unresolved
|
|
1962
|
+
};
|
|
1963
|
+
}
|
|
1964
|
+
//#endregion
|
|
1965
|
+
//#region src/providers/s3/index.ts
|
|
1966
|
+
function bucketConstructs(manifest) {
|
|
1967
|
+
return Object.entries(manifest).filter(([, d]) => d.kind === "objects").map(([id, d]) => ({
|
|
1968
|
+
id,
|
|
1969
|
+
versioned: d.versioned === true,
|
|
1970
|
+
...bucketExpiry(manifest, id) ? { expire: bucketExpiry(manifest, id) } : {},
|
|
1971
|
+
servers: Object.entries(manifest).filter(([, s]) => s.kind === "file-server" && s.of === id).map(([sid, s]) => ({
|
|
1972
|
+
id: sid,
|
|
1973
|
+
open: s.open ?? [],
|
|
1974
|
+
immutable: s.immutable ?? [],
|
|
1975
|
+
...s.processors?.length ? { processors: [...new Set(s.processors.map((p) => p.prefix))].sort() } : {}
|
|
1976
|
+
})).sort((a, b) => a.id.localeCompare(b.id))
|
|
1977
|
+
})).sort((a, b) => a.id.localeCompare(b.id));
|
|
1978
|
+
}
|
|
1979
|
+
/**
|
|
1980
|
+
* The region the stage's buckets are created in: the entry's, the secrets
|
|
1981
|
+
* store's when it is in AWS, the credential's, `AWS_REGION`.
|
|
1982
|
+
*/
|
|
1983
|
+
function s3Region(ctx, env = process.env) {
|
|
1984
|
+
const store = ctx.workspace.secrets?.store;
|
|
1985
|
+
const storeRegion = store && typeof store === "object" && "region" in store ? store.region : void 0;
|
|
1986
|
+
const region = ctx.config.region ?? storeRegion ?? ctx.credential?.region ?? env.AWS_REGION ?? env.AWS_DEFAULT_REGION;
|
|
1987
|
+
if (!region) throw new S3RegionRequired(ctx.stage);
|
|
1988
|
+
return region;
|
|
1989
|
+
}
|
|
1990
|
+
/**
|
|
1991
|
+
* The address a file server over `bucket` serves on: the bucket's regional
|
|
1992
|
+
* virtual-host endpoint, which is what `FileServer.url(key)` appends a key to
|
|
1993
|
+
* — `https://<bucket>.s3.<region>.amazonaws.com/<key>`. Against an emulator,
|
|
1994
|
+
* its path-style address.
|
|
1995
|
+
*/
|
|
1996
|
+
function bucketPublicUrl(bucket, region, endpoint) {
|
|
1997
|
+
if (endpoint) return `${endpoint.replace(/\/+$/, "")}/${bucket}`;
|
|
1998
|
+
return `https://${bucket}.s3.${region}.amazonaws.com`;
|
|
1999
|
+
}
|
|
2000
|
+
/** What an AWS SDK error is called, whichever field carries it. */
|
|
2001
|
+
function errorName(error) {
|
|
2002
|
+
const e = error;
|
|
2003
|
+
return e?.Code ?? e?.name ?? "";
|
|
2004
|
+
}
|
|
2005
|
+
/** The answer an SDK call failed with. */
|
|
2006
|
+
function s3Answer(error) {
|
|
2007
|
+
const e = error;
|
|
2008
|
+
const headers = Object.fromEntries(Object.entries(e?.$response?.headers ?? {}).map(([k, v]) => [k.toLowerCase(), v]));
|
|
2009
|
+
const status = e?.$metadata?.httpStatusCode ?? e?.$response?.statusCode;
|
|
2010
|
+
const region = headers["x-amz-bucket-region"];
|
|
2011
|
+
const requestId = e?.$metadata?.requestId ?? headers["x-amz-request-id"];
|
|
2012
|
+
const id2 = e?.$metadata?.extendedRequestId ?? headers["x-amz-id-2"];
|
|
2013
|
+
return {
|
|
2014
|
+
...status !== void 0 ? { status } : {},
|
|
2015
|
+
name: errorName(error) || "Error",
|
|
2016
|
+
message: e?.message ?? String(error),
|
|
2017
|
+
...region ? { region } : {},
|
|
2018
|
+
...requestId ? { requestId } : {},
|
|
2019
|
+
...id2 ? { id2 } : {}
|
|
2020
|
+
};
|
|
2021
|
+
}
|
|
2022
|
+
/**
|
|
2023
|
+
* `HTTP 403 Unknown: UnknownError [x-amz-bucket-region: eu-west-1,
|
|
2024
|
+
* x-amz-request-id: …, x-amz-id-2: …]` — everything S3 said, for a message.
|
|
2025
|
+
*/
|
|
2026
|
+
function describeAnswer(answer) {
|
|
2027
|
+
const said = answer.message && answer.message !== answer.name ? `${answer.name}: ${answer.message}` : answer.name;
|
|
2028
|
+
const headers = [
|
|
2029
|
+
answer.region && `x-amz-bucket-region: ${answer.region}`,
|
|
2030
|
+
answer.requestId && `x-amz-request-id: ${answer.requestId}`,
|
|
2031
|
+
answer.id2 && `x-amz-id-2: ${answer.id2}`
|
|
2032
|
+
].filter(Boolean);
|
|
2033
|
+
return `${answer.status === void 0 ? "no response" : `HTTP ${answer.status}`} ${said}` + (headers.length > 0 ? ` [${headers.join(", ")}]` : "");
|
|
2034
|
+
}
|
|
2035
|
+
/** `HeadBucket`: the bucket's region when it answers, else what S3 said. */
|
|
2036
|
+
async function headBucket(S3, client, bucket) {
|
|
2037
|
+
try {
|
|
2038
|
+
const out = await client.send(new S3.HeadBucketCommand({ Bucket: bucket }));
|
|
2039
|
+
return {
|
|
2040
|
+
ok: true,
|
|
2041
|
+
...out.BucketRegion ? { region: out.BucketRegion } : {}
|
|
2042
|
+
};
|
|
2043
|
+
} catch (error) {
|
|
2044
|
+
return {
|
|
2045
|
+
ok: false,
|
|
2046
|
+
...s3Answer(error)
|
|
2047
|
+
};
|
|
2048
|
+
}
|
|
2049
|
+
}
|
|
2050
|
+
/** `GetBucketLocation`'s answer as a region: none is us-east-1, `EU` eu-west-1. */
|
|
2051
|
+
async function bucketLocation(S3, client, bucket) {
|
|
2052
|
+
try {
|
|
2053
|
+
const location = (await client.send(new S3.GetBucketLocationCommand({ Bucket: bucket }))).LocationConstraint;
|
|
2054
|
+
return { region: !location ? "us-east-1" : location === "EU" ? "eu-west-1" : location };
|
|
2055
|
+
} catch (error) {
|
|
2056
|
+
return { failed: s3Answer(error) };
|
|
2057
|
+
}
|
|
2058
|
+
}
|
|
2059
|
+
/**
|
|
2060
|
+
* Where `bucket` is: `HeadBucket`'s `x-amz-bucket-region` — on a 200, a 301
|
|
2061
|
+
* or a 400 alike — else `GetBucketLocation`.
|
|
2062
|
+
*
|
|
2063
|
+
* @throws {BucketRegionUnknown}
|
|
2064
|
+
*/
|
|
2065
|
+
async function bucketRegion(clients, bucket) {
|
|
2066
|
+
const { S3, s3 } = clients;
|
|
2067
|
+
const head = await headBucket(S3, s3, bucket);
|
|
2068
|
+
if (head.region) return head.region;
|
|
2069
|
+
const location = await bucketLocation(S3, s3, bucket);
|
|
2070
|
+
if ("region" in location) return location.region;
|
|
2071
|
+
throw new BucketRegionUnknown(bucket, head.ok ? "HTTP 200 without x-amz-bucket-region" : describeAnswer(head), describeAnswer(location.failed));
|
|
2072
|
+
}
|
|
2073
|
+
/** A read whose "there is none" answer is one of `missing`. */
|
|
2074
|
+
async function readOr(read, missing) {
|
|
2075
|
+
try {
|
|
2076
|
+
return await read();
|
|
2077
|
+
} catch (error) {
|
|
2078
|
+
if (missing.includes(errorName(error))) return void 0;
|
|
2079
|
+
throw error;
|
|
2080
|
+
}
|
|
2081
|
+
}
|
|
2082
|
+
const defaultClients = async (credential, region) => {
|
|
2083
|
+
const S3 = await import("@aws-sdk/client-s3");
|
|
2084
|
+
const IAM = await import("@aws-sdk/client-iam");
|
|
2085
|
+
const config = await awsClientConfig(credential, region);
|
|
2086
|
+
const endpoint = awsEndpoint(process.env, "S3");
|
|
2087
|
+
return {
|
|
2088
|
+
S3,
|
|
2089
|
+
IAM,
|
|
2090
|
+
s3: new S3.S3Client({
|
|
2091
|
+
...config,
|
|
2092
|
+
...endpoint ? {
|
|
2093
|
+
endpoint,
|
|
2094
|
+
forcePathStyle: true
|
|
2095
|
+
} : {}
|
|
2096
|
+
}),
|
|
2097
|
+
iam: new IAM.IAMClient(config),
|
|
2098
|
+
region,
|
|
2099
|
+
...endpoint ? { endpoint } : {}
|
|
2100
|
+
};
|
|
2101
|
+
};
|
|
2102
|
+
/** For tests: what makes the clients. */
|
|
2103
|
+
let makeClients = defaultClients;
|
|
2104
|
+
async function ensureS3(ctx) {
|
|
2105
|
+
const region = s3Region(ctx);
|
|
2106
|
+
const clients = await makeClients(ctx.credential, region);
|
|
2107
|
+
const watched = bucketWatchers(ctx.manifest, ctx.runnables, ctx.subscribers);
|
|
2108
|
+
for (const construct of bucketConstructs(ctx.manifest)) await ensureBucket(ctx, clients, construct, watched.get(construct.id));
|
|
2109
|
+
}
|
|
2110
|
+
async function ensureBucket(ctx, stageClients, construct, watchers) {
|
|
2111
|
+
const { id } = construct;
|
|
2112
|
+
const nameInput = {
|
|
2113
|
+
scope: ctx.identity.scope,
|
|
2114
|
+
stage: ctx.stage,
|
|
2115
|
+
id: appKey(id)
|
|
2116
|
+
};
|
|
2117
|
+
const versioning = ctx.config.versioning ?? construct.versioned;
|
|
2118
|
+
const open = [...new Set(construct.servers.flatMap((s) => s.open))].sort();
|
|
2119
|
+
const located = await ensureBucketName(ctx, stageClients, id, bucketName(nameInput));
|
|
2120
|
+
const { bucket, exists } = located;
|
|
2121
|
+
let clients = stageClients;
|
|
2122
|
+
if (located.region !== stageClients.region) {
|
|
2123
|
+
clients = await makeClients(ctx.credential, located.region);
|
|
2124
|
+
ctx.log(` '${bucket}' is in ${located.region}, not ${stageClients.region}: '${id}' is reached and addressed there`);
|
|
2125
|
+
}
|
|
2126
|
+
const cdn = ctx.config.cdn !== false && construct.servers.length > 0 ? await ensureBucketCdn(ctx, {
|
|
2127
|
+
bucket,
|
|
2128
|
+
region: clients.region,
|
|
2129
|
+
servers: construct.servers,
|
|
2130
|
+
origins: bucketOrigins({
|
|
2131
|
+
workspace: ctx.workspace,
|
|
2132
|
+
manifest: ctx.manifest,
|
|
2133
|
+
runnables: ctx.runnables,
|
|
2134
|
+
stage: ctx.stage,
|
|
2135
|
+
bucket: id
|
|
2136
|
+
}).origins
|
|
2137
|
+
}) : void 0;
|
|
2138
|
+
await ensureBucketSettings(ctx, clients, {
|
|
2139
|
+
id,
|
|
2140
|
+
bucket,
|
|
2141
|
+
exists,
|
|
2142
|
+
versioning,
|
|
2143
|
+
open,
|
|
2144
|
+
...cdn ? { cdn } : {},
|
|
2145
|
+
...construct.expire ? { expire: construct.expire } : {}
|
|
2146
|
+
});
|
|
2147
|
+
const events = watchers ? await ensureBucketEvents(ctx, clients, {
|
|
2148
|
+
id,
|
|
2149
|
+
bucket,
|
|
2150
|
+
region: clients.region
|
|
2151
|
+
}) : void 0;
|
|
2152
|
+
const user = await ensureIamUser(ctx, clients, {
|
|
2153
|
+
id,
|
|
2154
|
+
user: iamUserName(nameInput),
|
|
2155
|
+
policyName: IAM_POLICY_NAME,
|
|
2156
|
+
policy: userPolicy(bucket, {
|
|
2157
|
+
versioning,
|
|
2158
|
+
...events ? { eventsTopicArn: events.topicArn } : {}
|
|
2159
|
+
}),
|
|
2160
|
+
describe: "this bucket only"
|
|
2161
|
+
});
|
|
2162
|
+
await ensureAccessKey(ctx, clients, {
|
|
2163
|
+
id,
|
|
2164
|
+
bucket,
|
|
2165
|
+
urlKey: provideKey(id, "url"),
|
|
2166
|
+
...user
|
|
2167
|
+
});
|
|
2168
|
+
if (events && watchers) {
|
|
2169
|
+
await ensureEventsKey(ctx, {
|
|
2170
|
+
id,
|
|
2171
|
+
topicArn: events.topicArn,
|
|
2172
|
+
region: clients.region
|
|
2173
|
+
});
|
|
2174
|
+
await ensurePushSubscriptions(ctx, {
|
|
2175
|
+
id,
|
|
2176
|
+
events,
|
|
2177
|
+
region: clients.region,
|
|
2178
|
+
watchers
|
|
2179
|
+
});
|
|
2180
|
+
}
|
|
2181
|
+
const bucketUrl = bucketPublicUrl(bucket, clients.region, clients.endpoint);
|
|
2182
|
+
for (const server of construct.servers) {
|
|
2183
|
+
const publicUrl = cdn?.urls[server.id] ?? bucketUrl;
|
|
2184
|
+
const key = provideKey(server.id, "url");
|
|
2185
|
+
const entry = {
|
|
2186
|
+
key: `file-server:${server.id}`,
|
|
2187
|
+
type: "url"
|
|
2188
|
+
};
|
|
2189
|
+
const recorded = ctx.state.record(entry.key)?.id;
|
|
2190
|
+
const current = ctx.secrets[key];
|
|
2191
|
+
if (current === publicUrl) {
|
|
2192
|
+
await ctx.state.ready(entry, publicUrl);
|
|
2193
|
+
continue;
|
|
2194
|
+
}
|
|
2195
|
+
if (current !== void 0 && current !== recorded) {
|
|
2196
|
+
ctx.log(` ${key} is set to ${current} by you, and kept — the bucket's own address is ${publicUrl}`);
|
|
2197
|
+
continue;
|
|
2198
|
+
}
|
|
2199
|
+
await ctx.change({
|
|
2200
|
+
construct: server.id,
|
|
2201
|
+
resource: key,
|
|
2202
|
+
change: `set to ${publicUrl}`
|
|
2203
|
+
}, async () => {
|
|
2204
|
+
await ctx.writeSecrets({ [key]: publicUrl });
|
|
2205
|
+
await ctx.state.ready(entry, publicUrl);
|
|
2206
|
+
});
|
|
2207
|
+
}
|
|
2208
|
+
}
|
|
2209
|
+
/**
|
|
2210
|
+
* The bucket's name: the recorded one, else the good name, else the good
|
|
2211
|
+
* name with a random suffix — up to {@link SUFFIX_ATTEMPTS} times.
|
|
2212
|
+
*/
|
|
2213
|
+
async function ensureBucketName(ctx, clients, id, good) {
|
|
2214
|
+
const { S3, s3, region } = clients;
|
|
2215
|
+
const key = `s3-bucket:${id}`;
|
|
2216
|
+
const record = ctx.state.record(key);
|
|
2217
|
+
const entry = (name, where) => ({
|
|
2218
|
+
key,
|
|
2219
|
+
type: "s3-bucket",
|
|
2220
|
+
data: {
|
|
2221
|
+
name,
|
|
2222
|
+
region: where
|
|
2223
|
+
}
|
|
2224
|
+
});
|
|
2225
|
+
/**
|
|
2226
|
+
* Whether this account holds `name`: yes, no, or someone else does. Read
|
|
2227
|
+
* by status — a `HEAD` has no body, so its error is named `Unknown` — and,
|
|
2228
|
+
* when S3 says the bucket is in another region, asked again there.
|
|
2229
|
+
*/
|
|
2230
|
+
const probe = async (name) => {
|
|
2231
|
+
let head = await headBucket(S3, s3, name);
|
|
2232
|
+
if (!head.ok && head.region && head.region !== region && (head.status === 301 || head.status === 400)) {
|
|
2233
|
+
const there = await makeClients(ctx.credential, head.region);
|
|
2234
|
+
head = await headBucket(there.S3, there.s3, name);
|
|
2235
|
+
}
|
|
2236
|
+
if (head.ok) return "ours";
|
|
2237
|
+
if (head.status === 404) return "none";
|
|
2238
|
+
if (head.status === 403) return "taken";
|
|
2239
|
+
throw new BucketProbeFailed(name, describeAnswer(head));
|
|
2240
|
+
};
|
|
2241
|
+
/** Where the bucket is now — read, not assumed: it may be adopted. */
|
|
2242
|
+
const located = async (name, made) => ctx.dryRun && made === "created" ? region : bucketRegion(clients, name);
|
|
2243
|
+
const create = async (name) => {
|
|
2244
|
+
if (ctx.dryRun) {
|
|
2245
|
+
const found = await probe(name);
|
|
2246
|
+
if (found === "none") {
|
|
2247
|
+
await ctx.change({
|
|
2248
|
+
construct: id,
|
|
2249
|
+
resource: `bucket ${name}`,
|
|
2250
|
+
change: `create in ${region}`
|
|
2251
|
+
}, async () => {});
|
|
2252
|
+
return "created";
|
|
2253
|
+
}
|
|
2254
|
+
return found;
|
|
2255
|
+
}
|
|
2256
|
+
await ctx.state.pending(entry(name, region));
|
|
2257
|
+
try {
|
|
2258
|
+
await s3.send(new S3.CreateBucketCommand({
|
|
2259
|
+
Bucket: name,
|
|
2260
|
+
...region === "us-east-1" ? {} : { CreateBucketConfiguration: { LocationConstraint: region } }
|
|
2261
|
+
}));
|
|
2262
|
+
} catch (error) {
|
|
2263
|
+
const code = errorName(error);
|
|
2264
|
+
if (code === "BucketAlreadyOwnedByYou") return "ours";
|
|
2265
|
+
if (code === "BucketAlreadyExists") return "taken";
|
|
2266
|
+
throw error;
|
|
2267
|
+
}
|
|
2268
|
+
await ctx.change({
|
|
2269
|
+
construct: id,
|
|
2270
|
+
resource: `bucket ${name}`,
|
|
2271
|
+
change: `create in ${region}`
|
|
2272
|
+
}, async () => {});
|
|
2273
|
+
return "created";
|
|
2274
|
+
};
|
|
2275
|
+
const adopt = async (name, where) => {
|
|
2276
|
+
const there = where === region ? clients : await makeClients(ctx.credential, where);
|
|
2277
|
+
const owner = (await readOr(() => there.s3.send(new there.S3.GetBucketTaggingCommand({ Bucket: name })), ["NoSuchTagSet", "NoSuchTagSetError"]))?.TagSet?.find((t) => t.Key === TAG_PROJECT)?.Value;
|
|
2278
|
+
if (owner && owner !== ctx.identity.key) throw new BucketOwnedByAnotherProject(name, owner, ctx.identity.key);
|
|
2279
|
+
};
|
|
2280
|
+
if (record?.status === "ready" && record.id) {
|
|
2281
|
+
const found = await probe(record.id);
|
|
2282
|
+
if (found === "taken") throw new RecordedBucketUnreachable(id, record.id);
|
|
2283
|
+
const made = found === "ours" ? "ours" : await create(record.id);
|
|
2284
|
+
if (made === "taken") throw new RecordedBucketUnreachable(id, record.id);
|
|
2285
|
+
const where = await located(record.id, made);
|
|
2286
|
+
if (!ctx.dryRun) await ctx.state.ready(entry(record.id, where), record.id);
|
|
2287
|
+
return {
|
|
2288
|
+
bucket: record.id,
|
|
2289
|
+
exists: made === "ours",
|
|
2290
|
+
region: where
|
|
2291
|
+
};
|
|
2292
|
+
}
|
|
2293
|
+
const pending = record?.data?.name;
|
|
2294
|
+
const names = [
|
|
2295
|
+
...typeof pending === "string" && pending !== good ? [pending] : [],
|
|
2296
|
+
good,
|
|
2297
|
+
...Array.from({ length: 5 }, () => suffixedBucketName(good, randomSuffix()))
|
|
2298
|
+
];
|
|
2299
|
+
const tried = [];
|
|
2300
|
+
for (const name of names) {
|
|
2301
|
+
tried.push(name);
|
|
2302
|
+
const made = await create(name);
|
|
2303
|
+
if (made === "taken") continue;
|
|
2304
|
+
const where = await located(name, made);
|
|
2305
|
+
if (made === "ours") await adopt(name, where);
|
|
2306
|
+
if (!ctx.dryRun) await ctx.state.ready(entry(name, where), name);
|
|
2307
|
+
if (name !== good) ctx.log(` '${good}' is taken by another account; '${id}' is '${name}', recorded in the stage's state`);
|
|
2308
|
+
return {
|
|
2309
|
+
bucket: name,
|
|
2310
|
+
exists: made === "ours",
|
|
2311
|
+
region: where
|
|
2312
|
+
};
|
|
2313
|
+
}
|
|
2314
|
+
throw new BucketNameUnavailable(id, tried.filter((n) => n !== pending));
|
|
2315
|
+
}
|
|
2316
|
+
/**
|
|
2317
|
+
* Tags, Block Public Access, encryption, policy, versioning, lifecycle and
|
|
2318
|
+
* CORS.
|
|
2319
|
+
*/
|
|
2320
|
+
async function ensureBucketSettings(ctx, clients, input) {
|
|
2321
|
+
const { S3, s3 } = clients;
|
|
2322
|
+
const { id, bucket } = input;
|
|
2323
|
+
const Bucket = bucket;
|
|
2324
|
+
const fresh = ctx.dryRun && !input.exists;
|
|
2325
|
+
const read = async (fn, missing) => fresh ? void 0 : readOr(fn, missing);
|
|
2326
|
+
const set = (change, apply) => ctx.change({
|
|
2327
|
+
construct: id,
|
|
2328
|
+
resource: `bucket ${bucket}`,
|
|
2329
|
+
change
|
|
2330
|
+
}, () => apply().then(() => void 0));
|
|
2331
|
+
const tagged = await read(() => s3.send(new S3.GetBucketTaggingCommand({ Bucket })), ["NoSuchTagSet", "NoSuchTagSetError"]);
|
|
2332
|
+
const ours = identityTags(ctx, id);
|
|
2333
|
+
const others = (tagged?.TagSet ?? []).filter((t) => !ours.some((o) => o.Key === t.Key));
|
|
2334
|
+
if (ours.some((o) => !tagged?.TagSet?.some((t) => t.Key === o.Key && t.Value === o.Value))) await set("tag", () => s3.send(new S3.PutBucketTaggingCommand({
|
|
2335
|
+
Bucket,
|
|
2336
|
+
Tagging: { TagSet: [...others, ...ours].map((t) => ({
|
|
2337
|
+
Key: t.Key,
|
|
2338
|
+
Value: t.Value
|
|
2339
|
+
})) }
|
|
2340
|
+
})));
|
|
2341
|
+
const isOpen = input.open.length > 0 && !input.cdn;
|
|
2342
|
+
const block = publicAccessBlock(isOpen);
|
|
2343
|
+
const ensureBlock = async () => {
|
|
2344
|
+
const now = (await read(() => s3.send(new S3.GetPublicAccessBlockCommand({ Bucket })), ["NoSuchPublicAccessBlockConfiguration"]))?.PublicAccessBlockConfiguration;
|
|
2345
|
+
if (!(now && Object.keys(block).every((k) => (now[k] ?? false) === block[k]))) await set(isOpen ? "block public access (ACLs; the policy opens the open paths)" : "block all public access", () => s3.send(new S3.PutPublicAccessBlockCommand({
|
|
2346
|
+
Bucket,
|
|
2347
|
+
PublicAccessBlockConfiguration: block
|
|
2348
|
+
})));
|
|
2349
|
+
};
|
|
2350
|
+
const policy = bucketPolicy(bucket, input.open, input.cdn ? { cdn: input.cdn } : {});
|
|
2351
|
+
const ensurePolicy = async () => {
|
|
2352
|
+
const current = await read(() => s3.send(new S3.GetBucketPolicyCommand({ Bucket })), ["NoSuchBucketPolicy"]);
|
|
2353
|
+
if (!(current?.Policy !== void 0 && canonicalPolicy(JSON.parse(current.Policy)) === canonicalPolicy(policy))) await set(isOpen ? `policy: TLS only, public read on ${input.open.join(", ")}` : input.cdn && input.open.length > 0 ? `policy: TLS only, CloudFront reads ${input.open.join(", ")}` : "policy: TLS only", () => s3.send(new S3.PutBucketPolicyCommand({
|
|
2354
|
+
Bucket,
|
|
2355
|
+
Policy: JSON.stringify(policy)
|
|
2356
|
+
})));
|
|
2357
|
+
};
|
|
2358
|
+
if (isOpen) {
|
|
2359
|
+
await ensureBlock();
|
|
2360
|
+
await ensurePolicy();
|
|
2361
|
+
} else {
|
|
2362
|
+
await ensurePolicy();
|
|
2363
|
+
await ensureBlock();
|
|
2364
|
+
}
|
|
2365
|
+
if ((await read(() => s3.send(new S3.GetBucketEncryptionCommand({ Bucket })), ["ServerSideEncryptionConfigurationNotFoundError"]))?.ServerSideEncryptionConfiguration?.Rules?.[0]?.ApplyServerSideEncryptionByDefault?.SSEAlgorithm !== "AES256" && !fresh) await set("encrypt by default (SSE-S3)", () => s3.send(new S3.PutBucketEncryptionCommand({
|
|
2366
|
+
Bucket,
|
|
2367
|
+
ServerSideEncryptionConfiguration: { Rules: [{ ApplyServerSideEncryptionByDefault: { SSEAlgorithm: "AES256" } }] }
|
|
2368
|
+
})));
|
|
2369
|
+
const status = (await read(() => s3.send(new S3.GetBucketVersioningCommand({ Bucket })), []))?.Status;
|
|
2370
|
+
if (input.versioning && status !== "Enabled") await set("enable versioning", () => s3.send(new S3.PutBucketVersioningCommand({
|
|
2371
|
+
Bucket,
|
|
2372
|
+
VersioningConfiguration: { Status: "Enabled" }
|
|
2373
|
+
})));
|
|
2374
|
+
else if (!input.versioning && status === "Enabled") await set("suspend versioning", () => s3.send(new S3.PutBucketVersioningCommand({
|
|
2375
|
+
Bucket,
|
|
2376
|
+
VersioningConfiguration: { Status: "Suspended" }
|
|
2377
|
+
})));
|
|
2378
|
+
const lifecycle = await read(() => s3.send(new S3.GetBucketLifecycleConfigurationCommand({ Bucket })), ["NoSuchLifecycleConfiguration"]);
|
|
2379
|
+
const merge = mergeLifecycle(lifecycle?.Rules, input.expire);
|
|
2380
|
+
if (merge.changed) await set(merge.describe, () => merge.rules.length > 0 ? s3.send(new S3.PutBucketLifecycleConfigurationCommand({
|
|
2381
|
+
Bucket,
|
|
2382
|
+
LifecycleConfiguration: { Rules: merge.rules }
|
|
2383
|
+
})) : s3.send(new S3.DeleteBucketLifecycleCommand({ Bucket })));
|
|
2384
|
+
const { origins, unresolved } = bucketOrigins({
|
|
2385
|
+
workspace: ctx.workspace,
|
|
2386
|
+
manifest: ctx.manifest,
|
|
2387
|
+
runnables: ctx.runnables,
|
|
2388
|
+
stage: ctx.stage,
|
|
2389
|
+
bucket: id
|
|
2390
|
+
});
|
|
2391
|
+
if (unresolved.length > 0) ctx.log(` ${unresolved.join(", ")} ${unresolved.length === 1 ? "has" : "have"} no address on '${ctx.stage}' (domains.${ctx.stage}), so ${unresolved.length === 1 ? "is" : "are"} not in '${bucket}''s CORS`);
|
|
2392
|
+
if (origins.length === 0) return;
|
|
2393
|
+
const rule = corsRule(origins);
|
|
2394
|
+
const cors = await read(() => s3.send(new S3.GetBucketCorsCommand({ Bucket })), ["NoSuchCORSConfiguration"]);
|
|
2395
|
+
if (!(cors?.CORSRules?.length === 1 && canonicalCors(cors.CORSRules[0]) === canonicalCors(rule))) await set(`CORS for ${origins.join(", ")}`, () => s3.send(new S3.PutBucketCorsCommand({
|
|
2396
|
+
Bucket,
|
|
2397
|
+
CORSConfiguration: { CORSRules: [rule] }
|
|
2398
|
+
})));
|
|
2399
|
+
}
|
|
2400
|
+
function canonicalCors(rule) {
|
|
2401
|
+
const sorted = (list) => [...list ?? []].sort();
|
|
2402
|
+
return JSON.stringify({
|
|
2403
|
+
AllowedMethods: sorted(rule.AllowedMethods),
|
|
2404
|
+
AllowedOrigins: sorted(rule.AllowedOrigins),
|
|
2405
|
+
AllowedHeaders: sorted(rule.AllowedHeaders),
|
|
2406
|
+
ExposeHeaders: sorted(rule.ExposeHeaders),
|
|
2407
|
+
MaxAgeSeconds: rule.MaxAgeSeconds ?? null
|
|
2408
|
+
});
|
|
2409
|
+
}
|
|
2410
|
+
/**
|
|
2411
|
+
* Each provisioned bucket answers `HeadBucket` to the key in the stage's
|
|
2412
|
+
* secrets — the one the apps will use. A bucket whose URL carries no key is
|
|
2413
|
+
* not one this provider wrote, and the deploy's own check covers a URL that
|
|
2414
|
+
* is missing.
|
|
2415
|
+
*
|
|
2416
|
+
* A failure is read by its status, never its name — a `HEAD` has no body,
|
|
2417
|
+
* so SDK v3 calls a 301, 400 or 403 `Unknown` — and the message carries
|
|
2418
|
+
* everything S3 said: status, error, region, request ids.
|
|
2419
|
+
*
|
|
2420
|
+
* @throws {BucketRegionMismatch} the bucket is in another region than its URL says
|
|
2421
|
+
* @throws {ProvisionedBucketUnreachable}
|
|
2422
|
+
*/
|
|
2423
|
+
async function verifyS3(ctx) {
|
|
2424
|
+
const S3 = await import("@aws-sdk/client-s3");
|
|
2425
|
+
for (const { id } of bucketConstructs(ctx.manifest)) {
|
|
2426
|
+
const urlKey = provideKey(id, "url");
|
|
2427
|
+
const url = ctx.secrets[urlKey];
|
|
2428
|
+
if (url === void 0) continue;
|
|
2429
|
+
const address = s3Url.parse(url);
|
|
2430
|
+
if (!address.accessKeyId || !address.secretAccessKey) continue;
|
|
2431
|
+
const asked = address.region ?? ctx.config.region ?? "us-east-1";
|
|
2432
|
+
const client = new S3.S3Client({
|
|
2433
|
+
region: asked,
|
|
2434
|
+
credentials: {
|
|
2435
|
+
accessKeyId: address.accessKeyId,
|
|
2436
|
+
secretAccessKey: address.secretAccessKey
|
|
2437
|
+
},
|
|
2438
|
+
...address.endpoint ? { endpoint: address.endpoint } : {},
|
|
2439
|
+
...address.forcePathStyle ? { forcePathStyle: true } : {}
|
|
2440
|
+
});
|
|
2441
|
+
try {
|
|
2442
|
+
const issuedAt = issuedJustNow(ctx.resources, id, address.accessKeyId);
|
|
2443
|
+
const { answer: head, waited } = await whileKeyIsNew(await headBucket(S3, client, address.bucket), {
|
|
2444
|
+
issuedAt,
|
|
2445
|
+
refused: (answer) => !answer.ok && answer.status === 403,
|
|
2446
|
+
ask: () => headBucket(S3, client, address.bucket),
|
|
2447
|
+
onWait: () => ctx.log?.(` ${id}: waiting for the new key ${address.accessKeyId} to become active…`)
|
|
2448
|
+
});
|
|
2449
|
+
if (head.ok) continue;
|
|
2450
|
+
const fail = (reason) => new ProvisionedBucketUnreachable(id, address.bucket, ctx.stage, reason);
|
|
2451
|
+
const said = describeAnswer(head);
|
|
2452
|
+
if (head.status === 301 || head.status === 400 && head.region && head.region !== asked) {
|
|
2453
|
+
let where = head.region;
|
|
2454
|
+
let located = "";
|
|
2455
|
+
if (!where) {
|
|
2456
|
+
const location = await bucketLocation(S3, client, address.bucket);
|
|
2457
|
+
if ("region" in location) where = location.region;
|
|
2458
|
+
else located = `; GetBucketLocation answered ${describeAnswer(location.failed)}`;
|
|
2459
|
+
}
|
|
2460
|
+
if (where && where !== asked) throw new BucketRegionMismatch(id, address.bucket, ctx.stage, urlKey, where, address.region, asked, said);
|
|
2461
|
+
throw fail(`S3 redirected the request (${said}${located})`);
|
|
2462
|
+
}
|
|
2463
|
+
if (head.status === 404) throw fail(`it does not exist (${said})`);
|
|
2464
|
+
if (head.status === 403) throw fail(`the key is refused — AccessDenied, or an invalid or not-yet-active key (${said})` + (issuedAt ? `. The key was issued at ${issuedAt} by this deploy, and IAM keys take seconds to become usable: it was still refused after waiting ${Math.round(waited / 1e3)}s` : ""));
|
|
2465
|
+
throw fail(said);
|
|
2466
|
+
} finally {
|
|
2467
|
+
client.destroy();
|
|
2468
|
+
}
|
|
2469
|
+
}
|
|
2470
|
+
}
|
|
2471
|
+
//#endregion
|
|
2472
|
+
//#region src/providers/registry.ts
|
|
2473
|
+
/**
|
|
2474
|
+
* The providers gkm ships, by kind and name — registered the way a deploy
|
|
2475
|
+
* target's built-ins are (`target/builtins.ts`). A provider from a package is
|
|
2476
|
+
* one more entry here once there is a loading story for it.
|
|
2477
|
+
*/
|
|
2478
|
+
const BUILTIN_PROVIDERS = { objects: { s3: {
|
|
2479
|
+
kind: "objects",
|
|
2480
|
+
name: "s3",
|
|
2481
|
+
check: checkS3Config,
|
|
2482
|
+
provisioning: S3_PROVISIONING,
|
|
2483
|
+
credentials: awsProvisioningCredentials,
|
|
2484
|
+
runtimeKeys(manifest, runnables = {}) {
|
|
2485
|
+
const watched = bucketWatchers(manifest, runnables);
|
|
2486
|
+
return bucketConstructs(manifest).flatMap((b) => [
|
|
2487
|
+
provideKey(b.id, "url"),
|
|
2488
|
+
...watched.has(b.id) ? [provideKey(b.id, "eventsConnectionString")] : [],
|
|
2489
|
+
...b.servers.map((s) => provideKey(s.id, "url"))
|
|
2490
|
+
]);
|
|
2491
|
+
},
|
|
2492
|
+
ensure: ensureS3,
|
|
2493
|
+
verify: verifyS3
|
|
2494
|
+
} } };
|
|
2495
|
+
/** The provider `kind` names `name`, if gkm has one. */
|
|
2496
|
+
function builtinProvider(kind, name) {
|
|
2497
|
+
return Object.hasOwn(BUILTIN_PROVIDERS[kind], name) ? BUILTIN_PROVIDERS[kind][name] : void 0;
|
|
2498
|
+
}
|
|
2499
|
+
//#endregion
|
|
2500
|
+
//#region src/providers/index.ts
|
|
2501
|
+
/** The constructs of a kind: `objects` is every bucket. */
|
|
2502
|
+
function constructsOf(manifest, kind) {
|
|
2503
|
+
return Object.entries(manifest).filter(([, d]) => d.kind === kind).map(([id]) => id).sort();
|
|
2504
|
+
}
|
|
2505
|
+
/**
|
|
2506
|
+
* What every deploy runs: each provider's `verify()` — the cheap check that
|
|
2507
|
+
* what it created is still there and the stage's key reaches it. Nothing on
|
|
2508
|
+
* the local stage, and nothing for a kind on `external` or `false` — a kind
|
|
2509
|
+
* the stage has none of is the deploy's readiness check's to refuse.
|
|
2510
|
+
*/
|
|
2511
|
+
async function verifyStageProviders(input) {
|
|
2512
|
+
const verified = [];
|
|
2513
|
+
for (const kind of PROVIDER_KINDS) {
|
|
2514
|
+
if (constructsOf(input.manifest, kind).length === 0) continue;
|
|
2515
|
+
const choice = stageProvider(input.workspace, kind, input.stage);
|
|
2516
|
+
if (choice.mode !== "provider") continue;
|
|
2517
|
+
const provider = builtinProvider(kind, choice.name);
|
|
2518
|
+
await provider.verify({
|
|
2519
|
+
workspace: input.workspace,
|
|
2520
|
+
manifest: input.manifest,
|
|
2521
|
+
stage: input.stage,
|
|
2522
|
+
config: choice.config,
|
|
2523
|
+
secrets: input.secrets,
|
|
2524
|
+
...input.resources ? { resources: input.resources } : {},
|
|
2525
|
+
...input.log ? { log: input.log } : {}
|
|
2526
|
+
});
|
|
2527
|
+
verified.push(`${kind}: ${provider.name}`);
|
|
2528
|
+
}
|
|
2529
|
+
if (await verifyStageBackups(input)) verified.push("backups: s3");
|
|
2530
|
+
return verified;
|
|
2531
|
+
}
|
|
2532
|
+
/**
|
|
2533
|
+
* A stage whose provider creates its resources, and no credentials to create
|
|
2534
|
+
* them with. Raised before anything is created, built or started.
|
|
2535
|
+
*/
|
|
2536
|
+
var ProviderCredentialsMissing = class extends GkmError {
|
|
2537
|
+
stage;
|
|
2538
|
+
kind;
|
|
2539
|
+
provider;
|
|
2540
|
+
ids;
|
|
2541
|
+
env;
|
|
2542
|
+
describe;
|
|
2543
|
+
login;
|
|
2544
|
+
constructor(stage, kind, provider, ids, env, describe, login) {
|
|
2545
|
+
super(`deploy.${kind}.${stage} is ${provider}, so the deploy creates what ${ids.join(", ")} ${ids.length === 1 ? "is" : "are"} backed by on '${stage}' and writes ${ids.length === 1 ? "its key" : "their keys"} into the stage's secrets — and it found no ${provider} credentials to do it with. On this machine, supply ${describe}` + (login ? `, or run gkm login --provider ${login}` : "") + `. In CI, the step that runs the deploy needs them: the '${stage}' environment's role (AWS_ROLE_ARN, from gkm deploy:github), assumed before that step. With the keys set by hand instead, set deploy.${kind}.${stage} to 'external'.`);
|
|
2546
|
+
this.stage = stage;
|
|
2547
|
+
this.kind = kind;
|
|
2548
|
+
this.provider = provider;
|
|
2549
|
+
this.ids = ids;
|
|
2550
|
+
this.env = env;
|
|
2551
|
+
this.describe = describe;
|
|
2552
|
+
this.login = login;
|
|
2553
|
+
this.name = "ProviderCredentialsMissing";
|
|
2554
|
+
}
|
|
2555
|
+
};
|
|
2556
|
+
/**
|
|
2557
|
+
* `ensure()` for every provider the stage configures — find or create, fix
|
|
2558
|
+
* drift, record in the stage's state, write the runtime keys into its
|
|
2559
|
+
* secrets. Run by the deploy, which holds the stage's lock. A provider with
|
|
2560
|
+
* no provisioning credentials stops the run with
|
|
2561
|
+
* {@link ProviderCredentialsMissing} before anything is created.
|
|
2562
|
+
*/
|
|
2563
|
+
async function provisionStage(options) {
|
|
2564
|
+
const { workspace, stage, manifest } = options;
|
|
2565
|
+
const dryRun = options.dryRun === true;
|
|
2566
|
+
const env = options.env ?? process.env;
|
|
2567
|
+
const log = options.log ?? ((line) => console.log(line));
|
|
2568
|
+
const runnables = options.runnables ?? {};
|
|
2569
|
+
const reports = [];
|
|
2570
|
+
const configured = [];
|
|
2571
|
+
for (const kind of PROVIDER_KINDS) {
|
|
2572
|
+
const choice = stageProvider(workspace, kind, stage);
|
|
2573
|
+
if (choice.mode !== "provider") {
|
|
2574
|
+
reports.push({
|
|
2575
|
+
kind,
|
|
2576
|
+
mode: choice.mode,
|
|
2577
|
+
actions: [],
|
|
2578
|
+
planned: []
|
|
2579
|
+
});
|
|
2580
|
+
continue;
|
|
2581
|
+
}
|
|
2582
|
+
const provider = builtinProvider(kind, choice.name);
|
|
2583
|
+
const report = {
|
|
2584
|
+
kind,
|
|
2585
|
+
mode: "provider",
|
|
2586
|
+
provider: provider.name,
|
|
2587
|
+
actions: [],
|
|
2588
|
+
planned: []
|
|
2589
|
+
};
|
|
2590
|
+
reports.push(report);
|
|
2591
|
+
const ids = constructsOf(manifest, kind);
|
|
2592
|
+
if (ids.length === 0) continue;
|
|
2593
|
+
const credential = await provider.credentials({
|
|
2594
|
+
stage,
|
|
2595
|
+
env,
|
|
2596
|
+
...options.profile ? { profile: options.profile } : {},
|
|
2597
|
+
...options.home ? { home: options.home } : {}
|
|
2598
|
+
});
|
|
2599
|
+
if (credential === void 0) throw new ProviderCredentialsMissing(stage, kind, provider.name, ids, provider.provisioning.env, provider.provisioning.describe, provider.provisioning.login);
|
|
2600
|
+
configured.push({
|
|
2601
|
+
kind,
|
|
2602
|
+
provider,
|
|
2603
|
+
config: choice.config,
|
|
2604
|
+
credential,
|
|
2605
|
+
report
|
|
2606
|
+
});
|
|
2607
|
+
}
|
|
2608
|
+
const backups = stageBackups(workspace, manifest, stage);
|
|
2609
|
+
if (configured.length === 0 && backups.mode !== "on") {
|
|
2610
|
+
if (backups.mode === "disabled") log(`💾 backups: off — deploy.backups.${stage} is false. Nothing is backed up, and nothing already backed up is deleted.`);
|
|
2611
|
+
return reports;
|
|
2612
|
+
}
|
|
2613
|
+
const identity = deployIdentity(workspace, stage);
|
|
2614
|
+
let secrets = (await options.secrets.read())?.custom ?? {};
|
|
2615
|
+
for (const entry of configured) entry.report.planned = entry.provider.runtimeKeys(manifest, runnables).filter((key) => secrets[key] === void 0);
|
|
2616
|
+
const state = dryRun ? await readOnlyState(options.state, stage) : journalState(await DeployJournal.open(options.state, stage, () => deploysWithCompose(workspace) ? createComposeState(stage) : createEmptyState(stage, "", "")));
|
|
2617
|
+
const writeSecrets = async (values) => {
|
|
2618
|
+
if (dryRun) return;
|
|
2619
|
+
const stored = await options.secrets.read() ?? initStageSecrets(stage);
|
|
2620
|
+
const next = {
|
|
2621
|
+
...stored,
|
|
2622
|
+
custom: {
|
|
2623
|
+
...stored.custom,
|
|
2624
|
+
...values
|
|
2625
|
+
},
|
|
2626
|
+
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
2627
|
+
};
|
|
2628
|
+
await options.secrets.write(next);
|
|
2629
|
+
secrets = next.custom;
|
|
2630
|
+
};
|
|
2631
|
+
for (const entry of configured) {
|
|
2632
|
+
log(`☁️ ${entry.kind}: ${entry.provider.name}${dryRun ? " (dry run)" : ""}`);
|
|
2633
|
+
await entry.provider.ensure({
|
|
2634
|
+
workspace,
|
|
2635
|
+
manifest,
|
|
2636
|
+
runnables,
|
|
2637
|
+
...options.subscribers ? { subscribers: options.subscribers } : {},
|
|
2638
|
+
stage,
|
|
2639
|
+
identity,
|
|
2640
|
+
config: entry.config,
|
|
2641
|
+
credential: entry.credential,
|
|
2642
|
+
get secrets() {
|
|
2643
|
+
return secrets;
|
|
2644
|
+
},
|
|
2645
|
+
state,
|
|
2646
|
+
dryRun,
|
|
2647
|
+
rotateKeys: options.rotateKeys === true,
|
|
2648
|
+
retireOldKeys: options.retireOldKeys === true,
|
|
2649
|
+
async change(action, apply) {
|
|
2650
|
+
if (!dryRun) await apply();
|
|
2651
|
+
entry.report.actions.push(action);
|
|
2652
|
+
log(` ${dryRun ? "would " : ""}${action.change} — ${action.resource}`);
|
|
2653
|
+
},
|
|
2654
|
+
writeSecrets,
|
|
2655
|
+
writeDns: (records) => writeProviderDns(records, {
|
|
2656
|
+
workspace,
|
|
2657
|
+
stage,
|
|
2658
|
+
dryRun,
|
|
2659
|
+
credentials: options.credentials ?? storedCredentials({
|
|
2660
|
+
env,
|
|
2661
|
+
...options.home ? { home: options.home } : {}
|
|
2662
|
+
}),
|
|
2663
|
+
...options.dnsProviderFor ? { providerFor: options.dnsProviderFor } : {},
|
|
2664
|
+
log
|
|
2665
|
+
}),
|
|
2666
|
+
log
|
|
2667
|
+
});
|
|
2668
|
+
if (entry.report.actions.length === 0) log(" up to date");
|
|
2669
|
+
}
|
|
2670
|
+
if (backups.mode === "on" || backups.mode === "disabled") {
|
|
2671
|
+
const report = {
|
|
2672
|
+
kind: "backups",
|
|
2673
|
+
mode: backups.mode === "on" ? "provider" : "none",
|
|
2674
|
+
actions: [],
|
|
2675
|
+
planned: backups.mode === "on" && secrets["BACKUPS_URL"] === void 0 ? [BACKUPS_URL_KEY] : []
|
|
2676
|
+
};
|
|
2677
|
+
reports.push(report);
|
|
2678
|
+
await ensureStageBackups({
|
|
2679
|
+
workspace,
|
|
2680
|
+
manifest,
|
|
2681
|
+
stage,
|
|
2682
|
+
...options.profile ? { profile: options.profile } : {},
|
|
2683
|
+
...options.home ? { home: options.home } : {},
|
|
2684
|
+
env,
|
|
2685
|
+
ctx: {
|
|
2686
|
+
identity,
|
|
2687
|
+
stage,
|
|
2688
|
+
state,
|
|
2689
|
+
dryRun,
|
|
2690
|
+
get secrets() {
|
|
2691
|
+
return secrets;
|
|
2692
|
+
},
|
|
2693
|
+
rotateKeys: options.rotateKeys === true,
|
|
2694
|
+
retireOldKeys: options.retireOldKeys === true,
|
|
2695
|
+
async change(action, apply) {
|
|
2696
|
+
if (!dryRun) await apply();
|
|
2697
|
+
report.actions.push(action);
|
|
2698
|
+
log(` ${dryRun ? "would " : ""}${action.change} — ${action.resource}`);
|
|
2699
|
+
},
|
|
2700
|
+
writeSecrets,
|
|
2701
|
+
log
|
|
2702
|
+
}
|
|
2703
|
+
});
|
|
2704
|
+
if (backups.mode === "on" && report.actions.length === 0) log(" up to date");
|
|
2705
|
+
}
|
|
2706
|
+
return reports;
|
|
2707
|
+
}
|
|
2708
|
+
/** The journal as `ensure()` sees it. */
|
|
2709
|
+
function journalState(journal) {
|
|
2710
|
+
return {
|
|
2711
|
+
record: (key) => journal.record(key),
|
|
2712
|
+
pending: (entry) => journal.pending(entry),
|
|
2713
|
+
ready: (entry, id) => journal.ready(entry, id),
|
|
2714
|
+
lastDeployedAt: journal.existed ? journal.state.lastDeployedAt : void 0
|
|
2715
|
+
};
|
|
2716
|
+
}
|
|
2717
|
+
/** What a dry run reads, and the writes it does not make. */
|
|
2718
|
+
async function readOnlyState(store, stage) {
|
|
2719
|
+
const stored = await store.read(stage);
|
|
2720
|
+
return {
|
|
2721
|
+
record: (key) => stored?.resources[key],
|
|
2722
|
+
pending: async () => {},
|
|
2723
|
+
ready: async () => {},
|
|
2724
|
+
...stored ? { lastDeployedAt: stored.state.lastDeployedAt } : {}
|
|
2725
|
+
};
|
|
2726
|
+
}
|
|
2727
|
+
//#endregion
|
|
2728
|
+
//#region src/secrets/stale.ts
|
|
2729
|
+
/**
|
|
2730
|
+
* Stored secrets an older gkm generated that now shadow a derived value.
|
|
2731
|
+
*
|
|
2732
|
+
* Before constructs, initialising a stage wrote an address for each app into
|
|
2733
|
+
* its custom secrets: `<APP>_DATABASE_URL` as a `postgresql://…@localhost`
|
|
2734
|
+
* URL with a password of its own, and `http://localhost:<port>` for a site or
|
|
2735
|
+
* an auth server. Those keys are now a construct's — `database.schema('AuthDatabase')`
|
|
2736
|
+
* provides `AUTH_DATABASE_URL`, `new BetterAuth('Auth')` provides `AUTH_URL`
|
|
2737
|
+
* — and a deploy derives them. A value the stage holds is a value set by hand,
|
|
2738
|
+
* and set by hand wins: the container is handed `localhost`, where nothing
|
|
2739
|
+
* answers, with a password no role has.
|
|
2740
|
+
*
|
|
2741
|
+
* So a deploy refuses such a stage rather than run it, naming each key and
|
|
2742
|
+
* the command that removes it. Nothing is deleted for the user: the value is
|
|
2743
|
+
* theirs, and only they can say it is stale. A URL that names a real host — a
|
|
2744
|
+
* managed database — is a deliberate choice and still wins.
|
|
2745
|
+
*/
|
|
2746
|
+
/** The kinds whose URL an older gkm wrote into a stage's secrets. */
|
|
2747
|
+
const ADDRESSED = /* @__PURE__ */ new Set([
|
|
2748
|
+
"database",
|
|
2749
|
+
"database-reader",
|
|
2750
|
+
"database-schema",
|
|
2751
|
+
"rest-api",
|
|
2752
|
+
"site"
|
|
2753
|
+
]);
|
|
2754
|
+
/** Hosts that are this machine — never anything, inside a container. */
|
|
2755
|
+
const LOOPBACK = /* @__PURE__ */ new Set([
|
|
2756
|
+
"localhost",
|
|
2757
|
+
"127.0.0.1",
|
|
2758
|
+
"[::1]",
|
|
2759
|
+
"::1"
|
|
2760
|
+
]);
|
|
2761
|
+
/**
|
|
2762
|
+
* A stage holds a construct's address as a custom secret, pointing at
|
|
2763
|
+
* `localhost` — what an older `gkm secrets:init`, `gkm setup` or `gkm init`
|
|
2764
|
+
* generated. Set by hand wins over derived, so the app would be handed it.
|
|
2765
|
+
*/
|
|
2766
|
+
var StaleStageSecrets = class extends GkmError {
|
|
2767
|
+
stage;
|
|
2768
|
+
stale;
|
|
2769
|
+
constructor(stage, stale) {
|
|
2770
|
+
const one = stale.length === 1;
|
|
2771
|
+
super(`The stage '${stage}' holds ${one ? "a value" : "values"} an older gkm generated for ${one ? "an address a construct" : "addresses constructs"} now ${one ? "provides" : "provide"}:\n` + stale.map((s) => ` ${s.key}: ${s.construct}'s, stored pointing at ${s.host}`).join("\n") + `
|
|
2772
|
+
A stored value wins over the derived one, so the app would be handed an address where nothing answers inside its container. Remove ${one ? "it" : "each"}, and the construct's own value is used:\n` + stale.map((s) => ` gkm secrets:unset ${s.key} --stage ${stage}`).join("\n") + "\nA managed service set with its real host is not affected.");
|
|
2773
|
+
this.stage = stage;
|
|
2774
|
+
this.stale = stale;
|
|
2775
|
+
this.name = "StaleStageSecrets";
|
|
2776
|
+
}
|
|
2777
|
+
};
|
|
2778
|
+
/** The host a stored value names, if it is a URL. */
|
|
2779
|
+
function hostOf(value) {
|
|
2780
|
+
try {
|
|
2781
|
+
return new URL(value).hostname;
|
|
2782
|
+
} catch {
|
|
2783
|
+
return;
|
|
2784
|
+
}
|
|
2785
|
+
}
|
|
2786
|
+
/**
|
|
2787
|
+
* Each stored key that a construct provides an address for, holding an
|
|
2788
|
+
* address on this machine.
|
|
2789
|
+
*/
|
|
2790
|
+
function staleStageSecrets(manifest, supplied) {
|
|
2791
|
+
const stale = [];
|
|
2792
|
+
for (const [id, declaration] of Object.entries(manifest)) {
|
|
2793
|
+
if (!ADDRESSED.has(declaration.kind)) continue;
|
|
2794
|
+
const key = providedKeyFor(id, declaration.kind, "url");
|
|
2795
|
+
const value = supplied[key];
|
|
2796
|
+
if (value === void 0) continue;
|
|
2797
|
+
const host = hostOf(value);
|
|
2798
|
+
if (host === void 0 || !LOOPBACK.has(host)) continue;
|
|
2799
|
+
stale.push({
|
|
2800
|
+
key,
|
|
2801
|
+
construct: id,
|
|
2802
|
+
host
|
|
2803
|
+
});
|
|
2804
|
+
}
|
|
2805
|
+
return stale.sort((a, b) => a.key.localeCompare(b.key));
|
|
2806
|
+
}
|
|
2807
|
+
/**
|
|
2808
|
+
* Refuse a stage whose stored secrets would shadow a construct's address with
|
|
2809
|
+
* `localhost`.
|
|
2810
|
+
*
|
|
2811
|
+
* @throws {StaleStageSecrets} naming each key and the command removing it
|
|
2812
|
+
*/
|
|
2813
|
+
function assertNoStaleSecrets(options) {
|
|
2814
|
+
const stale = staleStageSecrets(options.manifest, options.supplied);
|
|
2815
|
+
if (stale.length > 0) throw new StaleStageSecrets(options.stage, stale);
|
|
2816
|
+
}
|
|
2817
|
+
//#endregion
|
|
2818
|
+
//#region src/deploy/readiness.ts
|
|
2819
|
+
/**
|
|
2820
|
+
* Whether a stage is ready to be deployed: the one check, run once by every
|
|
2821
|
+
* deploy through a server target — after the stage's providers have created
|
|
2822
|
+
* what they create and written its keys, and before the target touches
|
|
2823
|
+
* anything.
|
|
2824
|
+
*
|
|
2825
|
+
* A build is not a deploy. Images carry no stage's keys, so a build-only run
|
|
2826
|
+
* (`gkm compose --build --push`) never calls this: a key the deploy itself
|
|
2827
|
+
* writes — a provider's bucket URL — is not there yet when CI builds, and
|
|
2828
|
+
* must not stop the build.
|
|
2829
|
+
*/
|
|
2830
|
+
/**
|
|
2831
|
+
* The stage has every key it needs, read from its secrets as the providers
|
|
2832
|
+
* left them:
|
|
2833
|
+
*
|
|
2834
|
+
* 1. no kind it declares is one the stage set to `false`;
|
|
2835
|
+
* 2. its mail and storage are its own — or a dev service, where allowed,
|
|
2836
|
+
* reported once, loudly;
|
|
2837
|
+
* 3. what its providers created is still there, and its key reaches it;
|
|
2838
|
+
* 4. no key holds an address an older gkm stored for what a construct now
|
|
2839
|
+
* provides;
|
|
2840
|
+
* 5. each third party's credentials match their construct's schema.
|
|
2841
|
+
*
|
|
2842
|
+
* The local stage derives its mail and storage, so only 4 and 5 apply to it.
|
|
2843
|
+
*
|
|
2844
|
+
* @throws {StageProviderDisabled}
|
|
2845
|
+
* @throws {ExternalServicesNotConfigured} naming every missing key at once
|
|
2846
|
+
* @throws {StaleStageSecrets}
|
|
2847
|
+
* @throws {CredentialsInvalid}
|
|
2848
|
+
*/
|
|
2849
|
+
async function assertStageReady(ctx) {
|
|
2850
|
+
const { workspace, manifest, stage } = ctx;
|
|
2851
|
+
const supplied = (await ctx.secrets.read())?.custom ?? {};
|
|
2852
|
+
if (stage !== workspace.stages.local) {
|
|
2853
|
+
assertStageProvidersEnabled(workspace, manifest, stage);
|
|
2854
|
+
const domain = workspace.domains?.[stage];
|
|
2855
|
+
const services = externalServices({
|
|
2856
|
+
stage,
|
|
2857
|
+
declarations: stageServiceDeclarations({
|
|
2858
|
+
manifest,
|
|
2859
|
+
...ctx.runnables ? { runnables: ctx.runnables } : {}
|
|
2860
|
+
}),
|
|
2861
|
+
supplied,
|
|
2862
|
+
allow: ctx.allowDevServices,
|
|
2863
|
+
...domain ? { domain } : {},
|
|
2864
|
+
providers: stageProviderNotes(workspace, stage)
|
|
2865
|
+
});
|
|
2866
|
+
if (services.missing.length > 0) throw new ExternalServicesNotConfigured(stage, services.missing);
|
|
2867
|
+
reportDevServices(ctx, devServicesUsed(services));
|
|
2868
|
+
const verified = await verifyStageProviders({
|
|
2869
|
+
workspace,
|
|
2870
|
+
manifest,
|
|
2871
|
+
stage,
|
|
2872
|
+
secrets: supplied,
|
|
2873
|
+
resources: (await ctx.state.read(stage))?.resources ?? {},
|
|
2874
|
+
log: (line) => ctx.logger.info(line)
|
|
2875
|
+
});
|
|
2876
|
+
for (const line of verified) ctx.logger.info(`✓ ${line} verified`);
|
|
2877
|
+
}
|
|
2878
|
+
assertNoStaleSecrets({
|
|
2879
|
+
manifest,
|
|
2880
|
+
stage,
|
|
2881
|
+
supplied
|
|
2882
|
+
});
|
|
2883
|
+
await assertStageCredentials({
|
|
2884
|
+
root: workspace.root,
|
|
2885
|
+
patterns: constructGlobs(workspace),
|
|
2886
|
+
manifest,
|
|
2887
|
+
stage,
|
|
2888
|
+
supplied
|
|
2889
|
+
});
|
|
2890
|
+
}
|
|
2891
|
+
//#endregion
|
|
2892
|
+
//#region src/deploy/orchestrate.ts
|
|
2893
|
+
/**
|
|
2894
|
+
* The run around a target: what every deploy does whatever it deploys
|
|
2895
|
+
* through.
|
|
2896
|
+
*
|
|
2897
|
+
* It loads the workspace, resolves the target, works out the apps and the
|
|
2898
|
+
* identity, takes the stage's lock, and calls the target's phases in order,
|
|
2899
|
+
* reporting each as it starts, finishes or fails. Everything that is about
|
|
2900
|
+
* *where* the stage goes is the target's.
|
|
2901
|
+
*/
|
|
2902
|
+
/** Apps were asked for by name that the workspace does not have. */
|
|
2903
|
+
var UnknownDeployApps = class extends GkmError {
|
|
2904
|
+
apps;
|
|
2905
|
+
available;
|
|
2906
|
+
constructor(apps, available) {
|
|
2907
|
+
super(`Unknown apps: ${apps.join(", ")}\nAvailable apps: ${available.join(", ")}`);
|
|
2908
|
+
this.apps = apps;
|
|
2909
|
+
this.available = available;
|
|
2910
|
+
this.name = "UnknownDeployApps";
|
|
2911
|
+
}
|
|
2912
|
+
};
|
|
2913
|
+
/** Every app asked for deploys through some other target. */
|
|
2914
|
+
var NoDeployableApps = class extends GkmError {
|
|
2915
|
+
stage;
|
|
2916
|
+
target;
|
|
2917
|
+
constructor(stage, target) {
|
|
2918
|
+
super(`No apps to deploy through "${target}": every selected app deploys somewhere else. Pick the target with --target, or set each app's deploy in gkm.config.ts.`);
|
|
2919
|
+
this.stage = stage;
|
|
2920
|
+
this.target = target;
|
|
2921
|
+
this.name = "NoDeployableApps";
|
|
2922
|
+
}
|
|
2923
|
+
};
|
|
2924
|
+
/** A build-only run through a target with no build phase. */
|
|
2925
|
+
var TargetBuildsNothing = class extends GkmError {
|
|
2926
|
+
target;
|
|
2927
|
+
constructor(target) {
|
|
2928
|
+
super(`"${target}" has no build phase, so a build-only run would do nothing. Deploy it in full, or build through a target that builds images (compose).`);
|
|
2929
|
+
this.target = target;
|
|
2930
|
+
this.name = "TargetBuildsNothing";
|
|
2931
|
+
}
|
|
2932
|
+
};
|
|
2933
|
+
/** A failed rollback, after the failure that called for it. */
|
|
2934
|
+
var RollbackFailed = class extends Error {
|
|
2935
|
+
target;
|
|
2936
|
+
failure;
|
|
2937
|
+
constructor(target, failure, cause) {
|
|
2938
|
+
super(`Deploying through "${target}" failed, and so did rolling it back: ${cause instanceof Error ? cause.message : String(cause)}. The stage may be part-released; deploy again, or roll it back by hand. The first failure: ${failure instanceof Error ? failure.message : String(failure)}`, { cause });
|
|
2939
|
+
this.target = target;
|
|
2940
|
+
this.failure = failure;
|
|
2941
|
+
this.name = "RollbackFailed";
|
|
2942
|
+
}
|
|
2943
|
+
};
|
|
2944
|
+
/**
|
|
2945
|
+
* Generate image tag from stage and timestamp
|
|
2946
|
+
*/
|
|
2947
|
+
function generateTag(stage) {
|
|
2948
|
+
return `${stage}-${(/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-").slice(0, 19)}`;
|
|
2949
|
+
}
|
|
2950
|
+
/** Why an app that deploys through another target is left out. */
|
|
2951
|
+
function elsewhere(target, stage) {
|
|
2952
|
+
const instead = builtinTarget(target)?.instead?.(stage);
|
|
2953
|
+
if (instead) return `it deploys through "${target}" — ${instead}`;
|
|
2954
|
+
return `it deploys through "${target}" — run \`gkm deploy --target ${target} --stage ${stage}\``;
|
|
2955
|
+
}
|
|
2956
|
+
/**
|
|
2957
|
+
* Deploy a workspace's stage through its target.
|
|
2958
|
+
*
|
|
2959
|
+
* The engine under `deploy()` and `gkm deploy`. It never prompts and never
|
|
2960
|
+
* exits: credentials come from `ctx.credentials`, progress goes to
|
|
2961
|
+
* `ctx.emit` and to `output`, and every failure is thrown.
|
|
2962
|
+
*
|
|
2963
|
+
* @internal
|
|
2964
|
+
*/
|
|
2965
|
+
async function runDeploy(source, request, ctx) {
|
|
2966
|
+
const { stage } = request;
|
|
2967
|
+
const redactor = ctx.redactor ?? new Redactor();
|
|
2968
|
+
const emit = (event) => ctx.emit(event.type === "log" ? {
|
|
2969
|
+
...event,
|
|
2970
|
+
message: redactor.redact(event.message)
|
|
2971
|
+
} : event);
|
|
2972
|
+
/** Run one phase between its events. */
|
|
2973
|
+
const phase = async (name, body) => {
|
|
2974
|
+
ctx.signal?.throwIfAborted();
|
|
2975
|
+
emit({
|
|
2976
|
+
type: "phase.started",
|
|
2977
|
+
phase: name
|
|
2978
|
+
});
|
|
2979
|
+
try {
|
|
2980
|
+
const value = await body();
|
|
2981
|
+
emit({
|
|
2982
|
+
type: "phase.finished",
|
|
2983
|
+
phase: name
|
|
2984
|
+
});
|
|
2985
|
+
return value;
|
|
2986
|
+
} catch (error) {
|
|
2987
|
+
emit({
|
|
2988
|
+
type: "phase.failed",
|
|
2989
|
+
phase: name,
|
|
2990
|
+
error: eventError(error)
|
|
2991
|
+
});
|
|
2992
|
+
throw error;
|
|
2993
|
+
}
|
|
2994
|
+
};
|
|
2995
|
+
ctx.signal?.throwIfAborted();
|
|
2996
|
+
emit({
|
|
2997
|
+
type: "phase.started",
|
|
2998
|
+
phase: "validate"
|
|
2999
|
+
});
|
|
3000
|
+
let prepared;
|
|
3001
|
+
try {
|
|
3002
|
+
prepared = await prepare(source, request, ctx, emit, redactor);
|
|
3003
|
+
} catch (error) {
|
|
3004
|
+
emit({
|
|
3005
|
+
type: "phase.failed",
|
|
3006
|
+
phase: "validate",
|
|
3007
|
+
error: eventError(error)
|
|
3008
|
+
});
|
|
3009
|
+
throw error;
|
|
3010
|
+
}
|
|
3011
|
+
const { resolved, phaseCtx, store } = prepared;
|
|
3012
|
+
const { target } = resolved;
|
|
3013
|
+
const resources = !ctx.buildOnly && phaseCtx.stage !== phaseCtx.workspace.stages.local;
|
|
3014
|
+
const provision = async () => {
|
|
3015
|
+
if (!request.skipDns && resolved.target.runtime === "server") try {
|
|
3016
|
+
await dnsCredentials(phaseCtx.stage, stageDnsDomains(phaseCtx.stage, phaseCtx.workspace.domains, phaseCtx.workspace.dns), ctx.credentials);
|
|
3017
|
+
} catch (error) {
|
|
3018
|
+
emit({
|
|
3019
|
+
type: "phase.failed",
|
|
3020
|
+
phase: "validate",
|
|
3021
|
+
error: eventError(error)
|
|
3022
|
+
});
|
|
3023
|
+
throw error;
|
|
3024
|
+
}
|
|
3025
|
+
await provisionResources(phaseCtx, request, ctx, store, emit);
|
|
3026
|
+
};
|
|
3027
|
+
if (ctx.dryRun) {
|
|
3028
|
+
if (resources) await provision();
|
|
3029
|
+
const run = await validate(phaseCtx, emit, () => readyToDeploy(target, phaseCtx));
|
|
3030
|
+
await phase("plan", () => target.plan(phaseCtx, run));
|
|
3031
|
+
return target.result(phaseCtx, run);
|
|
3032
|
+
}
|
|
3033
|
+
if (ctx.buildOnly) {
|
|
3034
|
+
if (!target.build) throw new TargetBuildsNothing(resolved.name);
|
|
3035
|
+
const run = await validate(phaseCtx, emit, () => target.validate(phaseCtx));
|
|
3036
|
+
await phase("build", () => target.build(phaseCtx, run));
|
|
3037
|
+
return target.result(phaseCtx, run);
|
|
3038
|
+
}
|
|
3039
|
+
let lock;
|
|
3040
|
+
try {
|
|
3041
|
+
assertStateOutlivesRun(phaseCtx.workspace, stage, "deploy");
|
|
3042
|
+
lock = await store.lock(stage, { operation: "deploy" });
|
|
3043
|
+
} catch (error) {
|
|
3044
|
+
emit({
|
|
3045
|
+
type: "phase.failed",
|
|
3046
|
+
phase: "validate",
|
|
3047
|
+
error: eventError(error)
|
|
3048
|
+
});
|
|
3049
|
+
throw error;
|
|
3050
|
+
}
|
|
3051
|
+
try {
|
|
3052
|
+
try {
|
|
3053
|
+
await assertStateRoom(store, stage);
|
|
3054
|
+
} catch (error) {
|
|
3055
|
+
emit({
|
|
3056
|
+
type: "phase.failed",
|
|
3057
|
+
phase: "validate",
|
|
3058
|
+
error: eventError(error)
|
|
3059
|
+
});
|
|
3060
|
+
throw error;
|
|
3061
|
+
}
|
|
3062
|
+
if (resources) await provision();
|
|
3063
|
+
const run = await validate(phaseCtx, emit, () => readyToDeploy(target, phaseCtx));
|
|
3064
|
+
if (target.provision) await phase("provision", () => target.provision(phaseCtx, run));
|
|
3065
|
+
if (target.build) await phase("build", () => target.build(phaseCtx, run));
|
|
3066
|
+
let releasing = "release";
|
|
3067
|
+
try {
|
|
3068
|
+
await phase("release", () => target.release(phaseCtx, run));
|
|
3069
|
+
releasing = "verify";
|
|
3070
|
+
if (target.verify) await phase("verify", () => target.verify(phaseCtx, run));
|
|
3071
|
+
} catch (error) {
|
|
3072
|
+
if (ctx.signal?.aborted || !target.capabilities.rollback) throw error;
|
|
3073
|
+
if (!target.rollback) throw error;
|
|
3074
|
+
try {
|
|
3075
|
+
await phase("rollback", () => target.rollback(phaseCtx, run, {
|
|
3076
|
+
phase: releasing,
|
|
3077
|
+
error
|
|
3078
|
+
}));
|
|
3079
|
+
} catch (rollbackError) {
|
|
3080
|
+
throw new RollbackFailed(resolved.name, error, rollbackError);
|
|
3081
|
+
}
|
|
3082
|
+
throw error;
|
|
3083
|
+
}
|
|
3084
|
+
return await target.result(phaseCtx, run);
|
|
3085
|
+
} finally {
|
|
3086
|
+
await lock.release();
|
|
3087
|
+
}
|
|
3088
|
+
}
|
|
3089
|
+
/**
|
|
3090
|
+
* The stage's providers, run inside the validate phase. A dry run plans them
|
|
3091
|
+
* and, so the checks after it see the stage as the run would leave it, reads
|
|
3092
|
+
* the keys they would write as placeholders.
|
|
3093
|
+
*/
|
|
3094
|
+
async function provisionResources(phaseCtx, request, ctx, store, emit) {
|
|
3095
|
+
try {
|
|
3096
|
+
const reports = await provisionStage({
|
|
3097
|
+
workspace: phaseCtx.workspace,
|
|
3098
|
+
stage: phaseCtx.stage,
|
|
3099
|
+
manifest: phaseCtx.manifest,
|
|
3100
|
+
runnables: phaseCtx.runnables ?? {},
|
|
3101
|
+
...phaseCtx.subscribers ? { subscribers: phaseCtx.subscribers } : {},
|
|
3102
|
+
secrets: phaseCtx.secrets,
|
|
3103
|
+
state: store,
|
|
3104
|
+
dryRun: ctx.dryRun,
|
|
3105
|
+
...request.rotateKeys ? { rotateKeys: true } : {},
|
|
3106
|
+
...request.retireOldKeys ? { retireOldKeys: true } : {},
|
|
3107
|
+
...ctx.home ? { home: ctx.home } : {},
|
|
3108
|
+
credentials: ctx.credentials,
|
|
3109
|
+
log: (line) => phaseCtx.logger.info(line)
|
|
3110
|
+
});
|
|
3111
|
+
if (!ctx.dryRun) return;
|
|
3112
|
+
const planned = reports.flatMap((r) => r.planned);
|
|
3113
|
+
if (planned.length === 0) return;
|
|
3114
|
+
const secrets = phaseCtx.secrets;
|
|
3115
|
+
phaseCtx.secrets = {
|
|
3116
|
+
...secrets,
|
|
3117
|
+
async read() {
|
|
3118
|
+
const stored = await secrets.read() ?? initStageSecrets(phaseCtx.stage);
|
|
3119
|
+
const custom = { ...stored.custom };
|
|
3120
|
+
for (const key of planned) custom[key] ??= PLANNED_BY_THE_DEPLOY;
|
|
3121
|
+
return {
|
|
3122
|
+
...stored,
|
|
3123
|
+
custom
|
|
3124
|
+
};
|
|
3125
|
+
}
|
|
3126
|
+
};
|
|
3127
|
+
} catch (error) {
|
|
3128
|
+
emit({
|
|
3129
|
+
type: "phase.failed",
|
|
3130
|
+
phase: "validate",
|
|
3131
|
+
error: eventError(error)
|
|
3132
|
+
});
|
|
3133
|
+
throw error;
|
|
3134
|
+
}
|
|
3135
|
+
}
|
|
3136
|
+
/** What a dry run reads for a key its providers would write. */
|
|
3137
|
+
const PLANNED_BY_THE_DEPLOY = "s3://planned-by-this-deploy";
|
|
3138
|
+
/**
|
|
3139
|
+
* What a deploy validates: the stage ready to be deployed — every key it
|
|
3140
|
+
* needs, read as its providers left them — then what the target would run,
|
|
3141
|
+
* then whatever only the target checks. A build-only run validates the
|
|
3142
|
+
* target's run alone: it calls neither readiness check.
|
|
3143
|
+
*/
|
|
3144
|
+
async function readyToDeploy(target, phaseCtx) {
|
|
3145
|
+
if (target.runtime === "server") await assertStageReady(phaseCtx);
|
|
3146
|
+
const run = await target.validate(phaseCtx);
|
|
3147
|
+
await target.ready?.(phaseCtx, run);
|
|
3148
|
+
return run;
|
|
3149
|
+
}
|
|
3150
|
+
/** `body`, then the run's `deploy.started`, inside the validate phase. */
|
|
3151
|
+
async function validate(phaseCtx, emit, body) {
|
|
3152
|
+
try {
|
|
3153
|
+
const run = await body();
|
|
3154
|
+
const skipped = new Set(phaseCtx.skipped.map((s) => s.app));
|
|
3155
|
+
emit({
|
|
3156
|
+
type: "deploy.started",
|
|
3157
|
+
stage: phaseCtx.stage,
|
|
3158
|
+
target: phaseCtx.target,
|
|
3159
|
+
identity: phaseCtx.identity.key,
|
|
3160
|
+
tag: phaseCtx.tag,
|
|
3161
|
+
apps: phaseCtx.apps.filter((app) => !skipped.has(app)),
|
|
3162
|
+
dryRun: phaseCtx.dryRun
|
|
3163
|
+
});
|
|
3164
|
+
emit({
|
|
3165
|
+
type: "phase.finished",
|
|
3166
|
+
phase: "validate"
|
|
3167
|
+
});
|
|
3168
|
+
return run;
|
|
3169
|
+
} catch (error) {
|
|
3170
|
+
emit({
|
|
3171
|
+
type: "phase.failed",
|
|
3172
|
+
phase: "validate",
|
|
3173
|
+
error: eventError(error)
|
|
3174
|
+
});
|
|
3175
|
+
throw error;
|
|
3176
|
+
}
|
|
3177
|
+
}
|
|
3178
|
+
/** Everything the phases are handed, worked out before the first of them. */
|
|
3179
|
+
async function prepare(source, request, ctx, emit, redactor) {
|
|
3180
|
+
const { stage } = request;
|
|
3181
|
+
const configured = typeof source === "function" ? await source() : source;
|
|
3182
|
+
const local = stage === configured.stages.local;
|
|
3183
|
+
if (!local) assertDeployedStage(configured.stages, stage);
|
|
3184
|
+
const targetName = request.target ?? configured.deploy?.default ?? "dokploy";
|
|
3185
|
+
const resolved = await resolveTarget(targetName, {
|
|
3186
|
+
workspace: configured,
|
|
3187
|
+
stage,
|
|
3188
|
+
...ctx.targets ? { host: ctx.targets } : {}
|
|
3189
|
+
});
|
|
3190
|
+
if (local && !resolved.target.capabilities.localStage) assertDeployedStage(configured.stages, stage);
|
|
3191
|
+
const options = await parseTargetOptions(resolved);
|
|
3192
|
+
const allowDevServices = request.allowDevServices === true;
|
|
3193
|
+
if (allowDevServices && resolved.target.runtime === "aws") throw new DevServicesNeedServerTarget(targetName);
|
|
3194
|
+
const runnables = {};
|
|
3195
|
+
const background = {};
|
|
3196
|
+
const subscribers = {};
|
|
3197
|
+
const manifest = await discover({
|
|
3198
|
+
patterns: constructGlobs(configured),
|
|
3199
|
+
cwd: configured.root,
|
|
3200
|
+
runnables,
|
|
3201
|
+
background,
|
|
3202
|
+
subscribers
|
|
3203
|
+
});
|
|
3204
|
+
resolveStageTelemetry({
|
|
3205
|
+
...configured.deploy?.telemetry ? { telemetry: configured.deploy.telemetry } : {},
|
|
3206
|
+
stage,
|
|
3207
|
+
local,
|
|
3208
|
+
target: targetName,
|
|
3209
|
+
runtime: resolved.target.runtime,
|
|
3210
|
+
selfHosted: resolved.target.capabilities.selfHostedTelemetry === true,
|
|
3211
|
+
used: usesTelemetry(manifest)
|
|
3212
|
+
});
|
|
3213
|
+
const units = derivedApps(manifest, configured);
|
|
3214
|
+
const workspace = Object.keys(units).length > 0 ? {
|
|
3215
|
+
...configured,
|
|
3216
|
+
apps: units
|
|
3217
|
+
} : configured;
|
|
3218
|
+
const identity = deployIdentity(workspace, stage);
|
|
3219
|
+
const tag = request.tag ?? (resolved.target.tag ? await resolved.target.tag({
|
|
3220
|
+
cwd: workspace.root,
|
|
3221
|
+
stage
|
|
3222
|
+
}) : generateTag(stage));
|
|
3223
|
+
const buildOrder = getAppBuildOrder(workspace);
|
|
3224
|
+
let selected = buildOrder;
|
|
3225
|
+
if (request.apps && request.apps.length > 0) {
|
|
3226
|
+
const unknown = request.apps.filter((name) => !workspace.apps[name]);
|
|
3227
|
+
if (unknown.length > 0) throw new UnknownDeployApps(unknown, Object.keys(workspace.apps));
|
|
3228
|
+
selected = buildOrder.filter((name) => request.apps.includes(name));
|
|
3229
|
+
}
|
|
3230
|
+
const skipped = [];
|
|
3231
|
+
const skip = (app, reason) => {
|
|
3232
|
+
skipped.push({
|
|
3233
|
+
app,
|
|
3234
|
+
reason
|
|
3235
|
+
});
|
|
3236
|
+
emit({
|
|
3237
|
+
type: "app.skipped",
|
|
3238
|
+
app,
|
|
3239
|
+
reason
|
|
3240
|
+
});
|
|
3241
|
+
};
|
|
3242
|
+
const byDefault = configured.deploy?.default ?? "dokploy";
|
|
3243
|
+
const apps = selected.filter((name) => {
|
|
3244
|
+
const own = workspace.apps[name].resolvedDeployTarget;
|
|
3245
|
+
const deploysThrough = own === byDefault ? targetName : own;
|
|
3246
|
+
if (deploysThrough === targetName) return true;
|
|
3247
|
+
skip(name, elsewhere(deploysThrough, stage));
|
|
3248
|
+
return false;
|
|
3249
|
+
});
|
|
3250
|
+
if (apps.length === 0) throw new NoDeployableApps(stage, targetName);
|
|
3251
|
+
assertSitePasswords(configured, manifest, stage, apps, resolved.target.name);
|
|
3252
|
+
const store = await createStateStore({
|
|
3253
|
+
config: stage === workspace.stages.local ? void 0 : workspace.state,
|
|
3254
|
+
workspaceRoot: workspace.root,
|
|
3255
|
+
workspaceName: workspace.name,
|
|
3256
|
+
namespace: workspace.deploy?.namespace
|
|
3257
|
+
});
|
|
3258
|
+
const secrets = await deploySecrets(workspace, stage, redactor, { ...ctx.home ? { home: ctx.home } : {} });
|
|
3259
|
+
const line = (level) => (message) => emit({
|
|
3260
|
+
type: "log",
|
|
3261
|
+
level,
|
|
3262
|
+
message
|
|
3263
|
+
});
|
|
3264
|
+
const logger = {
|
|
3265
|
+
info: line("info"),
|
|
3266
|
+
warn: line("warn"),
|
|
3267
|
+
error: line("error")
|
|
3268
|
+
};
|
|
3269
|
+
return {
|
|
3270
|
+
resolved,
|
|
3271
|
+
phaseCtx: {
|
|
3272
|
+
target: targetName,
|
|
3273
|
+
cwd: workspace.root,
|
|
3274
|
+
stage,
|
|
3275
|
+
tag,
|
|
3276
|
+
tagGiven: request.tag !== void 0,
|
|
3277
|
+
apps,
|
|
3278
|
+
skipped,
|
|
3279
|
+
identity,
|
|
3280
|
+
workspace,
|
|
3281
|
+
manifest,
|
|
3282
|
+
runnables,
|
|
3283
|
+
background,
|
|
3284
|
+
subscribers,
|
|
3285
|
+
options,
|
|
3286
|
+
dryRun: ctx.dryRun,
|
|
3287
|
+
atomic: request.atomic ?? false,
|
|
3288
|
+
allowDevServices,
|
|
3289
|
+
...request.skipDns ? { skipDns: true } : {},
|
|
3290
|
+
credentials: ctx.credentials,
|
|
3291
|
+
state: store,
|
|
3292
|
+
secrets,
|
|
3293
|
+
logger,
|
|
3294
|
+
signal: ctx.signal ?? new AbortController().signal,
|
|
3295
|
+
childOutput: ctx.childOutput ?? "inherit",
|
|
3296
|
+
emit,
|
|
3297
|
+
skip,
|
|
3298
|
+
name: (resource) => applicationName(identity, resource)
|
|
3299
|
+
},
|
|
3300
|
+
store
|
|
3301
|
+
};
|
|
3302
|
+
}
|
|
3303
|
+
//#endregion
|
|
3304
|
+
//#region src/deploy/deploy.ts
|
|
3305
|
+
/**
|
|
3306
|
+
* `deploy()`: a deploy as a library call.
|
|
3307
|
+
*
|
|
3308
|
+
* `gkm deploy` could only be driven from an interactive terminal: it prompted
|
|
3309
|
+
* for what it lacked and exited the process on failure, worked out of
|
|
3310
|
+
* `process.cwd()`, and reported progress by printing. Here the caller says
|
|
3311
|
+
* where the project is, hands over the credentials and a signal, and gets back
|
|
3312
|
+
* a run — events to iterate while it goes, and a structured result at the end.
|
|
3313
|
+
* Nothing below this asks a person anything or ends the process; `gkm deploy`
|
|
3314
|
+
* is a thin wrapper that does both.
|
|
3315
|
+
*/
|
|
3316
|
+
/**
|
|
3317
|
+
* Every event a run emits, kept so that each iteration — however late it
|
|
3318
|
+
* starts — sees the run from its first event to its last.
|
|
3319
|
+
*/
|
|
3320
|
+
var EventLog = class {
|
|
3321
|
+
events = [];
|
|
3322
|
+
closed = false;
|
|
3323
|
+
waiting = [];
|
|
3324
|
+
push(event) {
|
|
3325
|
+
this.events.push(event);
|
|
3326
|
+
this.wake();
|
|
3327
|
+
}
|
|
3328
|
+
close() {
|
|
3329
|
+
this.closed = true;
|
|
3330
|
+
this.wake();
|
|
3331
|
+
}
|
|
3332
|
+
wake() {
|
|
3333
|
+
const waiting = this.waiting;
|
|
3334
|
+
this.waiting = [];
|
|
3335
|
+
for (const resume of waiting) resume();
|
|
3336
|
+
}
|
|
3337
|
+
async *iterate() {
|
|
3338
|
+
let next = 0;
|
|
3339
|
+
while (true) if (next < this.events.length) yield this.events[next++];
|
|
3340
|
+
else if (this.closed) return;
|
|
3341
|
+
else await new Promise((resume) => this.waiting.push(resume));
|
|
3342
|
+
}
|
|
3343
|
+
};
|
|
3344
|
+
function forward(logger, level, message) {
|
|
3345
|
+
if (!logger) return;
|
|
3346
|
+
if (level === "info") logger.info(message);
|
|
3347
|
+
else if (level === "error" && logger.error) logger.error(message);
|
|
3348
|
+
else logger.warn(message);
|
|
3349
|
+
}
|
|
3350
|
+
/**
|
|
3351
|
+
* Deploy a project's stage.
|
|
3352
|
+
*
|
|
3353
|
+
* ```ts
|
|
3354
|
+
* import { deploy } from '@geekmidas/cli/deploy';
|
|
3355
|
+
*
|
|
3356
|
+
* const run = deploy({ cwd: '/srv/checkouts/shop', stage: 'production' });
|
|
3357
|
+
* for await (const event of run) {
|
|
3358
|
+
* if (event.type === 'app.deployed') console.log(event.app, event.url);
|
|
3359
|
+
* }
|
|
3360
|
+
* const result = await run.result;
|
|
3361
|
+
* ```
|
|
3362
|
+
*
|
|
3363
|
+
* The run starts at once; iterating it is optional, and an iteration started
|
|
3364
|
+
* late still sees every event from the first.
|
|
3365
|
+
*/
|
|
3366
|
+
function deploy(input) {
|
|
3367
|
+
const log = new EventLog();
|
|
3368
|
+
const redactor = new Redactor();
|
|
3369
|
+
const emit = (event) => {
|
|
3370
|
+
const masked = event.type === "log" ? {
|
|
3371
|
+
...event,
|
|
3372
|
+
message: redactor.redact(event.message)
|
|
3373
|
+
} : event;
|
|
3374
|
+
log.push(masked);
|
|
3375
|
+
if (masked.type === "log") forward(input.logger, masked.level, masked.message);
|
|
3376
|
+
};
|
|
3377
|
+
const ctx = {
|
|
3378
|
+
emit,
|
|
3379
|
+
credentials: input.credentials ?? storedCredentials({ home: input.home ?? gkmHome() }),
|
|
3380
|
+
dryRun: input.dryRun ?? false,
|
|
3381
|
+
...input.buildOnly ? { buildOnly: true } : {},
|
|
3382
|
+
home: input.home ?? gkmHome(),
|
|
3383
|
+
redactor,
|
|
3384
|
+
...input.signal ? { signal: input.signal } : {},
|
|
3385
|
+
...input.childOutput ? { childOutput: input.childOutput } : {},
|
|
3386
|
+
...input.targets ? { targets: input.targets } : {}
|
|
3387
|
+
};
|
|
3388
|
+
const result = withOutput((level, message) => emit({
|
|
3389
|
+
type: "log",
|
|
3390
|
+
level,
|
|
3391
|
+
message
|
|
3392
|
+
}), async () => {
|
|
3393
|
+
try {
|
|
3394
|
+
input.signal?.throwIfAborted();
|
|
3395
|
+
const sandbox = input.sandbox ?? new LocalSandbox({ root: findWorkspaceRoot(resolve(input.cwd)) });
|
|
3396
|
+
const deployed = await withSandbox(sandbox, () => runDeploy(() => load(input, sandbox), {
|
|
3397
|
+
stage: input.stage,
|
|
3398
|
+
...input.target ? { target: input.target } : {},
|
|
3399
|
+
...input.tag ? { tag: input.tag } : {},
|
|
3400
|
+
...input.apps ? { apps: input.apps } : {},
|
|
3401
|
+
...input.atomic ? { atomic: true } : {},
|
|
3402
|
+
...input.allowDevServices ? { allowDevServices: input.allowDevServices } : {},
|
|
3403
|
+
...input.skipDns ? { skipDns: true } : {},
|
|
3404
|
+
...input.rotateKeys ? { rotateKeys: true } : {},
|
|
3405
|
+
...input.retireOldKeys ? { retireOldKeys: true } : {}
|
|
3406
|
+
}, ctx));
|
|
3407
|
+
emit({
|
|
3408
|
+
type: "deploy.finished",
|
|
3409
|
+
result: deployed
|
|
3410
|
+
});
|
|
3411
|
+
return deployed;
|
|
3412
|
+
} catch (error) {
|
|
3413
|
+
emit({
|
|
3414
|
+
type: "deploy.failed",
|
|
3415
|
+
error: eventError(error)
|
|
3416
|
+
});
|
|
3417
|
+
throw error;
|
|
3418
|
+
} finally {
|
|
3419
|
+
log.close();
|
|
3420
|
+
}
|
|
3421
|
+
});
|
|
3422
|
+
result.catch(() => {});
|
|
3423
|
+
return {
|
|
3424
|
+
result,
|
|
3425
|
+
[Symbol.asyncIterator]: () => log.iterate()
|
|
3426
|
+
};
|
|
3427
|
+
}
|
|
3428
|
+
/**
|
|
3429
|
+
* The workspace at `input.cwd`. The stage is checked by the run, once it
|
|
3430
|
+
* knows the target: one that runs on this machine may take the local stage.
|
|
3431
|
+
*/
|
|
3432
|
+
async function load(input, sandbox) {
|
|
3433
|
+
const { workspace } = await loadWorkspaceConfig(resolve(input.cwd), { sandbox });
|
|
3434
|
+
return workspace;
|
|
3435
|
+
}
|
|
3436
|
+
//#endregion
|
|
3437
|
+
export { UnknownDeployApps as a, bucketName as c, TargetBuildsNothing as i, iamUserName as l, NoDeployableApps as n, backupsUserName as o, RollbackFailed as r, IAM_USER_PATH as s, deploy as t };
|
|
3438
|
+
|
|
3439
|
+
//# sourceMappingURL=deploy-B35L6CMr.mjs.map
|