Skip to content

Commit

Permalink
Merge pull request #6 from losisin/policy/cloudrun-disallow-secret-envs
Browse files Browse the repository at this point in the history
cloudrun disallow envs from secrets
  • Loading branch information
losisin authored Dec 26, 2023
2 parents cdd9c13 + 5041ff3 commit c2f2f9b
Show file tree
Hide file tree
Showing 15 changed files with 190 additions and 104 deletions.
1 change: 1 addition & 0 deletions .github/workflows/codeql-analysis.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: CodeQL

on:
push:
pull_request:
branches:
- main
Expand Down
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- CodeQL analysis (#4).
- dependabot npm scan (#4).
- `cloudrunv2-disallow-public-ingress` (#5).
- disallow environment variables from Secret Manager - `cloudrun.Service`, `cloudrunv2.Service` and `cloudrunv2.Job` (#6).

### Changed

- Update test and code file structure (#6).

### Fixed

- codeql event on push in main branch (#6).

## [1.1.0] - 2023-12-25

Expand Down
44 changes: 0 additions & 44 deletions __tests__/cloudrun/cloudrunService.ts

This file was deleted.

33 changes: 32 additions & 1 deletion __tests__/cloudrun/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,32 @@
export * as cloudrunService from "./cloudrunService"
import * as gcp from "@pulumi/gcp";

export const cloudrunPService = new gcp.cloudrun.Service("fail#1", {
location: "europe-west1",
metadata: {
annotations: {
"run.googleapis.com/ingress": "all",
},
},
template: {
spec: {
containers: [{
image: "us-docker.pkg.dev/cloudrun/container/hello",
envs: [
{
name: "FOO",
value: "bar",
},
{
name: "SECRET_ENV_VAR",
valueFrom: {
secretKeyRef: {
name: "my-secret",
key: "1",
},
},
},
],
}],
},
}
});
30 changes: 0 additions & 30 deletions __tests__/cloudrunv2/cloudrunv2Service.ts

This file was deleted.

54 changes: 53 additions & 1 deletion __tests__/cloudrunv2/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,53 @@
export * as cloudrunV2Service from "../cloudrunv2/cloudrunv2Service"
import * as gcp from "@pulumi/gcp";

export const cloudrunv2Service = new gcp.cloudrunv2.Service("fail#1", {
location: "europe-west1",
ingress: "INGRESS_TRAFFIC_ALL",
template: {
containers: [{
image: "us-docker.pkg.dev/cloudrun/container/hello",
envs: [
{
name: "FOO",
value: "bar",
},
{
name: "SECRET_ENV_VAR",
valueSource: {
secretKeyRef: {
secret: "my-secret",
version: "1",
},
},
},
],
}],
},
});

export const cloudrunv2SJob = new gcp.cloudrunv2.Job("fail#1", {
location: "europe-west1",
template: {
template: {
containers: [{
image: "us-docker.pkg.dev/cloudrun/container/hello",
envs: [
{
name: "FOO",
value: "bar",
},
{
name: "SECRET_ENV_VAR",
valueSource: {
secretKeyRef: {
secret: "my-secret",
version: "1",
},
},
},
],
}],
},
},
});

17 changes: 0 additions & 17 deletions __tests__/compute/backendService.ts

This file was deleted.

7 changes: 6 additions & 1 deletion __tests__/compute/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
export * as backendService from "./backendService"
import * as gcp from "@pulumi/gcp";

export const backendService = new gcp.compute.BackendService("fail#1", {
name: "fail#1",
loadBalancingScheme: "EXTERNAL"
});
6 changes: 3 additions & 3 deletions __tests__/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * as cloudrun from './cloudrun/index'
export * as cloudrunv2 from './cloudrunv2/index'
export * as compute from './compute/index'
export * as cloudrun from './cloudrun'
export * as cloudrunv2 from './cloudrunv2'
export * as compute from './compute'
25 changes: 25 additions & 0 deletions src/cloudrun/disallowEnvsSecrets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { ResourceValidationArgs, ReportViolation, EnforcementLevel } from "@pulumi/policy";

export const disallowEnvsSecrets = {
name: "cloudrun-service-disallow-envs-secrets",
description: "Check that CloudRun services do not use environment variables from secrets.",
enforcementLevel: "advisory" as EnforcementLevel,
validateResource: (args: ResourceValidationArgs, reportViolation: ReportViolation) => {
if (args.type === "gcp:cloudrun/service:Service") {
const containers = args.props.template.spec.containers;
if (containers) {
containers.forEach((container: any) => {
if (container.envs) {
container.envs.forEach((env: any) => {
if (env?.valueFrom?.secretKeyRef) {
reportViolation(
"CloudRun services should use secrets as mounted volumes."
);
}
});
}
});
}
}
},
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ResourceValidationArgs, ReportViolation, EnforcementLevel } from "@pulumi/policy";

export const cloudrunDisallowPublicIngress = {
export const disallowPublicIngress = {
name: "cloudrun-disallow-public-ingress",
description: "Check that CloudRun services do not have public ingress set to 'all'.",
enforcementLevel: "advisory" as EnforcementLevel,
Expand Down
6 changes: 4 additions & 2 deletions src/cloudrun/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { cloudrunDisallowPublicIngress } from "./cloudrunDisallowPublicIngress";
import { disallowPublicIngress } from "./disallowPublicIngress";
import { disallowEnvsSecrets } from "./disallowEnvsSecrets";

export const cloudrunPolicies = [
cloudrunDisallowPublicIngress,
disallowEnvsSecrets,
disallowPublicIngress,
];
49 changes: 49 additions & 0 deletions src/cloudrunv2/disallowEnvsSecrets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { ResourceValidationArgs, ReportViolation, EnforcementLevel } from "@pulumi/policy";

export const serviceDisallowEnvsSecrets = {
name: "cloudrunv2-service-disallow-envs-secrets",
description: "Check that CloudRun2 services do not use environment variables from secrets.",
enforcementLevel: "advisory" as EnforcementLevel,
validateResource: (args: ResourceValidationArgs, reportViolation: ReportViolation) => {
if (args.type === "gcp:cloudrunv2/service:Service") {
const containers = args.props.template.containers;
if (containers) {
containers.forEach((container: any) => {
if (container.envs) {
container.envs.forEach((env: any) => {
if (env?.valueSource?.secretKeyRef) {
reportViolation(
"CloudRun2 services should use secrets as mounted volumes."
);
}
});
}
});
}
}
},
}

export const jobDisallowEnvsSecrets = {
name: "cloudrunv2-job-disallow-envs-secrets",
description: "Check that CloudRun2 jobs do not use environment variables from secrets.",
enforcementLevel: "advisory" as EnforcementLevel,
validateResource: (args: ResourceValidationArgs, reportViolation: ReportViolation) => {
if (args.type === "gcp:cloudrunv2/job:Job") {
const containers = args.props.template.template.containers;
if (containers) {
containers.forEach((container: any) => {
if (container.envs) {
container.envs.forEach((env: any) => {
if (env?.valueSource?.secretKeyRef) {
reportViolation(
"CloudRun2 jobs should use secrets as mounted volumes."
);
}
});
}
});
}
}
},
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ResourceValidationArgs, ReportViolation, EnforcementLevel } from "@pulumi/policy";

export const cloudrunv2DisallowPublicIngress = {
name: "cloudrunv2-disallow-public-ingress",
export const disallowPublicIngress = {
name: "cloudrunv2-service-disallow-public-ingress",
description: "Check that CloudRun2 services do not have public ingress set to 'all'.",
enforcementLevel: "advisory" as EnforcementLevel,
validateResource: (args: ResourceValidationArgs, reportViolation: ReportViolation) => {
Expand Down
7 changes: 5 additions & 2 deletions src/cloudrunv2/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { cloudrunv2DisallowPublicIngress } from "./cloudrunv2DisallowPublicIngress";
import { disallowPublicIngress } from "./disallowPublicIngress";
import { serviceDisallowEnvsSecrets, jobDisallowEnvsSecrets } from "./disallowEnvsSecrets";

export const cloudrunv2Policies = [
cloudrunv2DisallowPublicIngress,
serviceDisallowEnvsSecrets,
jobDisallowEnvsSecrets,
disallowPublicIngress,
];

0 comments on commit c2f2f9b

Please sign in to comment.