Skip to content

Commit

Permalink
remove base class and rename variable to requireImdsv2
Browse files Browse the repository at this point in the history
  • Loading branch information
jericht committed Oct 13, 2021
1 parent 8b3906a commit 8e54242
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 167 deletions.
14 changes: 6 additions & 8 deletions packages/@aws-cdk/aws-autoscaling/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -385,23 +385,21 @@ new autoscaling.AutoScalingGroup(stack, 'ASG', {
You can configure [EC2 Instance Metadata Service](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html) options to either
allow both IMDSv1 and IMDSv2 or enforce IMDSv2 when interacting with the IMDS.

To do this for a single `AutoScalingGroup`, you can use set the `disableImdsv1` property.
The example below demonstrates IMDSv1 being disabled on a single `AutoScalingGroup`:
To do this for a single `AutoScalingGroup`, you can use set the `requireImdsv2` property.
The example below demonstrates IMDSv2 being required on a single `AutoScalingGroup`:

```ts
new autoscaling.AutoScalingGroup(stack, 'ASG', {
disableImdsv1: true,
requireImdsv2: true,
// ...
});
```

You can also use `AutoScalingGroupImdsAspect` to apply the operation to multiple AutoScalingGroups.
The example below demonstrates the `AutoScalingGroupImdsAspect` being used to disable IMDSv1 for all AutoScalingGroups in a stack:
You can also use `AutoScalingGroupRequireImdsv2Aspect` to apply the operation to multiple AutoScalingGroups.
The example below demonstrates the `AutoScalingGroupRequireImdsv2Aspect` being used to require IMDSv2 for all AutoScalingGroups in a stack:

```ts
const aspect = new autoscaling.AutoScalingGroupImdsAspect({
enableImdsV1: false,
});
const aspect = new autoscaling.AutoScalingGroupRequireImdsv2Aspect();

Aspects.of(stack).add(aspect);
```
Expand Down
67 changes: 0 additions & 67 deletions packages/@aws-cdk/aws-autoscaling/lib/aspects/imds-aspect.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-autoscaling/lib/aspects/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './imds-aspect';
export * from './require-imdsv2-aspect';
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import * as cdk from '@aws-cdk/core';
import { AutoScalingGroup } from '../auto-scaling-group';
import { CfnLaunchConfiguration } from '../autoscaling.generated';

/**
* Aspect that makes IMDSv2 required on instances deployed by AutoScalingGroups.
*/
export class AutoScalingGroupRequireImdsv2Aspect implements cdk.IAspect {
constructor() {
}

public visit(node: cdk.IConstruct): void {
if (!(node instanceof AutoScalingGroup)) {
return;
}

const launchConfig = node.node.tryFindChild('LaunchConfig') as CfnLaunchConfiguration;
if (cdk.isResolvableObject(launchConfig.metadataOptions)) {
this.warn(node, 'CfnLaunchConfiguration.MetadataOptions field is a CDK token.');
return;
}

launchConfig.metadataOptions = {
...launchConfig.metadataOptions,
httpTokens: 'required',
};
}

/**
* Adds a warning annotation to a node.
*
* @param node The scope to add the warning to.
* @param message The warning message.
*/
protected warn(node: cdk.IConstruct, message: string) {
cdk.Annotations.of(node).addWarning(`${AutoScalingGroupRequireImdsv2Aspect.name} failed on node ${node.node.id}: ${message}`);
}
}
10 changes: 5 additions & 5 deletions packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
Tokenization, withResolved,
} from '@aws-cdk/core';
import { Construct } from 'constructs';
import { AutoScalingGroupImdsAspect } from './aspects';
import { AutoScalingGroupRequireImdsv2Aspect } from './aspects';
import { CfnAutoScalingGroup, CfnAutoScalingGroupProps, CfnLaunchConfiguration } from './autoscaling.generated';
import { BasicLifecycleHookProps, LifecycleHook } from './lifecycle-hook';
import { BasicScheduledActionProps, ScheduledAction } from './scheduled-action';
Expand Down Expand Up @@ -388,11 +388,11 @@ export interface AutoScalingGroupProps extends CommonAutoScalingGroupProps {
readonly initOptions?: ApplyCloudFormationInitOptions;

/**
* Whether IMDSv1 should be disabled on launched instances.
* Whether IMDSv2 should be required on launched instances.
*
* @default - false
*/
readonly disableImdsv1?: boolean;
readonly requireImdsv2?: boolean;
}

/**
Expand Down Expand Up @@ -1075,8 +1075,8 @@ export class AutoScalingGroup extends AutoScalingGroupBase implements

this.spotPrice = props.spotPrice;

if (props.disableImdsv1 === true) {
Aspects.of(this).add(new AutoScalingGroupImdsAspect({ enableImdsV1: false }));
if (props.requireImdsv2) {
Aspects.of(this).add(new AutoScalingGroupRequireImdsv2Aspect());
}
}

Expand Down
84 changes: 0 additions & 84 deletions packages/@aws-cdk/aws-autoscaling/test/aspects/imds-aspect.test.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import {
expect as expectCDK,
haveResourceLike,
} from '@aws-cdk/assert-internal';
import '@aws-cdk/assert-internal/jest';
import * as ec2 from '@aws-cdk/aws-ec2';
import * as cdk from '@aws-cdk/core';
import {
AutoScalingGroup,
AutoScalingGroupRequireImdsv2Aspect,
CfnLaunchConfiguration,
} from '../../lib';

describe('AutoScalingGroupRequireImdsv2Aspect', () => {
let app: cdk.App;
let stack: cdk.Stack;
let vpc: ec2.Vpc;

beforeEach(() => {
app = new cdk.App();
stack = new cdk.Stack(app, 'Stack');
vpc = new ec2.Vpc(stack, 'Vpc');
});

test('warns when metadataOptions is a token', () => {
// GIVEN
const asg = new AutoScalingGroup(stack, 'AutoScalingGroup', {
vpc,
instanceType: new ec2.InstanceType('t2.micro'),
machineImage: ec2.MachineImage.latestAmazonLinux(),
});
const launchConfig = asg.node.tryFindChild('LaunchConfig') as CfnLaunchConfiguration;
launchConfig.metadataOptions = fakeToken();
const aspect = new AutoScalingGroupRequireImdsv2Aspect();

// WHEN
cdk.Aspects.of(stack).add(aspect);

// THEN
expectCDK(stack).notTo(haveResourceLike('AWS::AutoScaling::LaunchConfiguration', {
MetadataOptions: {
HttpTokens: 'required',
},
}));
expect(asg.node.metadataEntry).toContainEqual({
data: expect.stringContaining('CfnLaunchConfiguration.MetadataOptions field is a CDK token.'),
type: 'aws:cdk:warning',
trace: undefined,
});
});

test('requires IMDSv2', () => {
// GIVEN
new AutoScalingGroup(stack, 'AutoScalingGroup', {
vpc,
instanceType: new ec2.InstanceType('t2.micro'),
machineImage: ec2.MachineImage.latestAmazonLinux(),
});
const aspect = new AutoScalingGroupRequireImdsv2Aspect();

// WHEN
cdk.Aspects.of(stack).add(aspect);

// THEN
expectCDK(stack).to(haveResourceLike('AWS::AutoScaling::LaunchConfiguration', {
MetadataOptions: {
HttpTokens: 'required',
},
}));
});
});

function fakeToken(): cdk.IResolvable {
return {
creationStack: [],
resolve: (_c) => {},
toString: () => '',
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -1365,7 +1365,7 @@ describe('auto scaling group', () => {

});

test('disables imdsv1', () => {
test('requires imdsv2', () => {
// GIVEN
const stack = new cdk.Stack();
const vpc = mockVpc(stack);
Expand All @@ -1375,7 +1375,7 @@ describe('auto scaling group', () => {
vpc,
instanceType: new ec2.InstanceType('t2.micro'),
machineImage: ec2.MachineImage.latestAmazonLinux(),
disableImdsv1: true,
requireImdsv2: true,
});

// THEN
Expand Down

0 comments on commit 8e54242

Please sign in to comment.