Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: mount a volume and enforce readonlyRootFilesystem in ecs-task #2544

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fluffy-cameras-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@guardian/cdk": major
---

Enforce readonlyRootFilesystem in ecs-task. Support mounting a volume in ecs-task.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we more descriptive here and offer an example implementation?

28 changes: 26 additions & 2 deletions src/constructs/ecs/__snapshots__/ecs-task.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -479,8 +479,15 @@ exports[`The GuEcsTask pattern should create the correct resources with lots of
},
},
"Memory": 1024,
"MountPoints": [
{
"ContainerPath": "/opt/testing",
"ReadOnly": false,
"SourceVolume": "ecs-test-volume",
},
],
"Name": "test-ecs-task-ecs-test-TaskContainer",
"ReadonlyRootFilesystem": false,
"ReadonlyRootFilesystem": true,
},
],
"Cpu": "1024",
Expand Down Expand Up @@ -531,6 +538,11 @@ exports[`The GuEcsTask pattern should create the correct resources with lots of
"Arn",
],
},
"Volumes": [
{
"Name": "ecs-test-volume",
},
],
},
"Type": "AWS::ECS::TaskDefinition",
},
Expand Down Expand Up @@ -1191,8 +1203,15 @@ exports[`The GuEcsTask pattern should support overriding the subnets used by the
},
},
"Memory": 1024,
"MountPoints": [
{
"ContainerPath": "/opt/testing",
"ReadOnly": false,
"SourceVolume": "ecs-test-volume",
},
],
"Name": "test-ecs-task-ecs-test-TaskContainer",
"ReadonlyRootFilesystem": false,
"ReadonlyRootFilesystem": true,
},
],
"Cpu": "1024",
Expand Down Expand Up @@ -1243,6 +1262,11 @@ exports[`The GuEcsTask pattern should support overriding the subnets used by the
"Arn",
],
},
"Volumes": [
{
"Name": "ecs-test-volume",
},
],
},
"Type": "AWS::ECS::TaskDefinition",
},
Expand Down
1 change: 1 addition & 0 deletions src/constructs/ecs/ecs-task.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ describe("The GuEcsTask pattern", () => {
cpu: 1024,
memory: 1024,
storage: 30,
volumeMountPath: "/opt/testing",
monitoringConfiguration: { snsTopicArn: "arn:something:else:here:we:goalarm-topic", noMonitoring: false },
taskCommand: `echo "yo ho row ho it's a pirates life for me"`,
securityGroups: [securityGroup(stack, app)],
Expand Down
28 changes: 21 additions & 7 deletions src/constructs/ecs/ecs-task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ export interface GuEcsTaskProps extends AppIdentity {
securityGroups?: ISecurityGroup[];
customTaskPolicies?: PolicyStatement[];
environmentOverrides?: TaskEnvironmentVariable[];
/**
* If your container needs to write to disk whilst running, you will need to mount a non-root volume to use. Setting
* volumeMountPath will ensure a volume is mounted at that location, making use of Faragate ephemeral storage (set by
* 'storage' param)
*
*/
volumeMountPath?: string;
Comment on lines +123 to +129
Copy link
Member

@akash1810 akash1810 Dec 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative might be to expose the created taskDefinition and containerDefinition as properties from GuEcsTask:

export class GuEcsTask extends Construct {
  public readonly task: TaskDefinition;
  public readonly container: ContainerDefinition;

  constructor() {
    super();
    const taskDefinition = new TaskDefinition(...);
    const containerDefinition = new ContainerDefinition(...);

    this.task = taskDefinition;
    this.container = containerDefinition;
  }
}

This approach avoids adding a new optional prop1 and increases flexibility as we can now freely call any function available on TaskDefinition and ContainerDefinition from our own project's infrastructure definition:

const { task, container } = new GuEcsTask(...);

task.addVolume({
  name: 'asd'
});

task.someOtherUsefulFunction(...);

Footnotes

  1. It also means this repository has a smaller surface area (less maintenance).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh that's a much better approach!

storage?: number;
/**
* Whether to give the task IAM role access to the account's dist bucket.
Expand All @@ -128,11 +135,6 @@ export interface GuEcsTaskProps extends AppIdentity {
* shoud set this value to `false`.
*/
enableDistributablePolicy?: boolean;
/**
* When this parameter is true, the container is given read-only access to its root file system.
* @default false
*/
readonlyRootFilesystem?: boolean;
/**
* If `true`, CloudWatch Container Insights will be enabled for the cluster
* @default false
Expand Down Expand Up @@ -185,8 +187,8 @@ export class GuEcsTask extends Construct {
monitoringConfiguration,
securityGroups = [],
environmentOverrides,
volumeMountPath,
enableDistributablePolicy = true,
readonlyRootFilesystem = false,
containerInsights = false,
} = props;

Expand Down Expand Up @@ -227,9 +229,21 @@ export class GuEcsTask extends Construct {
streamPrefix: app,
logRetention: 14,
}),
readonlyRootFilesystem,
readonlyRootFilesystem: true,
});

if (volumeMountPath) {
taskDefinition.addVolume({
name: `${app}-volume`,
});

containerDefinition.addMountPoints({
sourceVolume: `${app}-volume`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work only because the string matches name in taskDefinition.addVolume above? If so, I think we could make this more explicit via:

import type { Volume } from 'aws-cdk-lib/aws-ecs';

const volume: Volume = {
  name: `${app}-volume`,
};

taskDefinition.addVolume(volume);

containerDefinition.addMountPoints({
  sourceVolume: volume.name,
  containerPath: volumeMountPath,
  readOnly: false
});

containerPath: volumeMountPath,
readOnly: false,
});
}

if (enableDistributablePolicy) {
const distPolicy = new GuGetDistributablePolicyStatement(scope, { app });
taskDefinition.addToTaskRolePolicy(distPolicy);
Expand Down
Loading