Skip to content

Commit 330cfef

Browse files
authored
feat: add IsProd components and encryption guardrail (#5131)
1 parent c7b06d0 commit 330cfef

4 files changed

Lines changed: 60 additions & 0 deletions

File tree

packages/create-webiny-project/_templates/aws/ddb-os/webiny.config.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ export const Extensions = () => {
77
<>
88
<Infra.Aws.DefaultRegion name={"{REGION}"} />
99
<Infra.OpenSearch enabled={true} />
10+
{/* Encryption MUST always be configured for production environments. */}
11+
<Infra.Env.IsProd>
12+
<Infra.Encryption passphrase={process.env.WEBINY_ENCRYPTION_PASSPHRASE} />
13+
</Infra.Env.IsProd>
1014
<Cognito />
1115
</>
1216
);

packages/create-webiny-project/_templates/aws/ddb/webiny.config.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ export const Extensions = () => {
66
return (
77
<>
88
<Infra.Aws.DefaultRegion name={"{REGION}"} />
9+
{/* Encryption MUST always be configured for production environments. */}
10+
<Infra.Env.IsProd>
11+
<Infra.Encryption passphrase={process.env.WEBINY_ENCRYPTION_PASSPHRASE} />
12+
</Infra.Env.IsProd>
913
<Cognito />
1014
</>
1115
);

packages/project/src/extensions/Project.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export const Project = () => {
2727
<AdminBeforeWatch src={p("EnsureApiDeployedBeforeAdminWatch.js")} />
2828
<AdminAfterDeploy src={p("TelemetryNoLongerNewUser.js")} />
2929
<BeforeDeploy src={p("EnsureTelemetryEnabledForOss.js")} />
30+
<BeforeDeploy src={p("ValidateEncryptionBeforeDeploy.js")} />
3031
<CoreBeforeDeploy src={p("ValidateProductionPulumiState.js")} />
3132
</>
3233
);
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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

Comments
 (0)