|
| 1 | +import { |
| 2 | + BeforeDeploy, |
| 3 | + GetProductionEnvironments, |
| 4 | + GetProjectConfig, |
| 5 | + ProjectSdkParamsService |
| 6 | +} from "~/abstractions/index.js"; |
| 7 | +import { GracefulError } from "@webiny/project"; |
| 8 | + |
| 9 | +class ValidateEncryptionBeforeDeployImpl implements BeforeDeploy.Interface { |
| 10 | + constructor( |
| 11 | + private getProductionEnvironments: GetProductionEnvironments.Interface, |
| 12 | + private projectSdkParamsService: ProjectSdkParamsService.Interface, |
| 13 | + private getProjectConfig: GetProjectConfig.Interface |
| 14 | + ) {} |
| 15 | + |
| 16 | + async execute() { |
| 17 | + const { env } = this.projectSdkParamsService.get(); |
| 18 | + const prodEnvs = await this.getProductionEnvironments.execute(); |
| 19 | + |
| 20 | + if (!prodEnvs.includes(env)) { |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + const projectConfig = await this.getProjectConfig.execute(); |
| 25 | + const [encryption] = projectConfig.extensionsByType("Infra/Encryption"); |
| 26 | + |
| 27 | + if (encryption) { |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + const error = new Error("Encryption is not configured for production environment."); |
| 32 | + |
| 33 | + const message = [ |
| 34 | + "Deploying to a production environment requires encryption to be configured.", |
| 35 | + "Set the %s environment variable and add %s to your project config.", |
| 36 | + "Learn more: https://webiny.link/encryption" |
| 37 | + ].join(" "); |
| 38 | + |
| 39 | + throw GracefulError.from( |
| 40 | + error, |
| 41 | + message, |
| 42 | + "WEBINY_ENCRYPTION_PASSPHRASE", |
| 43 | + "<Infra.Encryption>" |
| 44 | + ); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +export const ValidateEncryptionBeforeDeploy = BeforeDeploy.createImplementation({ |
| 49 | + implementation: ValidateEncryptionBeforeDeployImpl, |
| 50 | + dependencies: [GetProductionEnvironments, ProjectSdkParamsService, GetProjectConfig] |
| 51 | +}); |
0 commit comments