Skip to content

Commit

Permalink
remove suppressWarnings option
Browse files Browse the repository at this point in the history
  • Loading branch information
jericht committed Oct 5, 2021
1 parent 17f123b commit 8cb15e3
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 47 deletions.
15 changes: 13 additions & 2 deletions packages/@aws-cdk/aws-autoscaling/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -383,9 +383,20 @@ new autoscaling.AutoScalingGroup(stack, 'ASG', {
### Toggling IMDSv1

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, you can use the `AutoScalingGroupImdsAspect`.
allow both IMDSv1 and IMDSv2 or enforce IMDSv2 when interacting with the IMDS.

The following example demonstrates how to use the `AutoScalingGroupImdsAspect` to disable IMDSv1 (thus enforcing IMDSv2) for all AutoScalingGroups in a stack:
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`:

```ts
new autoscaling.AutoScalingGroup(stack, 'ASG', {
disableImdsv1: 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:

```ts
const aspect = new autoscaling.AutoScalingGroupImdsAspect({
Expand Down
15 changes: 2 additions & 13 deletions packages/@aws-cdk/aws-autoscaling/lib/aspects/imds-aspect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,28 @@ interface ImdsAspectProps {
* Whether IMDSv1 should be enabled or not.
*/
readonly enableImdsV1: boolean;

/**
* Whether warning annotations from this Aspect should be suppressed or not.
*
* @default - false
*/
readonly suppressWarnings?: boolean;
}

/**
* Base class for IMDS configuration Aspect.
*/
abstract class ImdsAspect implements cdk.IAspect {
protected readonly enableImdsV1: boolean;
protected readonly suppressWarnings: boolean;

constructor(props: ImdsAspectProps) {
this.enableImdsV1 = props.enableImdsV1;
this.suppressWarnings = props.suppressWarnings ?? false;
}

abstract visit(node: cdk.IConstruct): void;

/**
* Adds a warning annotation to a node, unless `suppressWarnings` is true.
* 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) {
if (this.suppressWarnings !== true) {
cdk.Annotations.of(node).addWarning(`${ImdsAspect.name} failed on node ${node.node.id}: ${message}`);
}
cdk.Annotations.of(node).addWarning(`${ImdsAspect.name} failed on node ${node.node.id}: ${message}`);
}
}

Expand Down
10 changes: 4 additions & 6 deletions packages/@aws-cdk/aws-autoscaling/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,15 @@
},
"license": "Apache-2.0",
"devDependencies": {
"@types/jest": "^26.0.24",
"@types/sinon": "^9.0.11",
"@aws-cdk/assert-internal": "0.0.0",
"@aws-cdk/cloud-assembly-schema": "0.0.0",
"@aws-cdk/cx-api": "0.0.0",
"@types/jest": "^26.0.24",
"cdk-build-tools": "0.0.0",
"cdk-integ-tools": "0.0.0",
"cfn2ts": "0.0.0",
"jest": "^26.6.3",
"pkglint": "0.0.0",
"sinon": "^9.2.4",
"@aws-cdk/cloud-assembly-schema": "0.0.0",
"@aws-cdk/assert-internal": "0.0.0"
"pkglint": "0.0.0"
},
"dependencies": {
"@aws-cdk/aws-autoscaling-common": "0.0.0",
Expand Down
26 changes: 0 additions & 26 deletions packages/@aws-cdk/aws-autoscaling/test/aspects/imds-aspect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
import '@aws-cdk/assert-internal/jest';
import * as ec2 from '@aws-cdk/aws-ec2';
import * as cdk from '@aws-cdk/core';
import * as sinon from 'sinon';
import {
AutoScalingGroup,
AutoScalingGroupImdsAspect,
Expand All @@ -23,31 +22,6 @@ describe('ImdsAspect', () => {
vpc = new ec2.Vpc(stack, 'Vpc');
});

test('suppresses warnings', () => {
// GIVEN
const aspect = new AutoScalingGroupImdsAspect({
enableImdsV1: true,
suppressWarnings: true,
});
const errmsg = 'ERROR';
const stub = sinon.stub(aspect, 'visit').callsFake((node) => {
// @ts-ignore
aspect.warn(node, errmsg);
});
const construct = new cdk.Construct(stack, 'Construct');

// WHEN
aspect.visit(construct);

// THEN
expect(stub.calledOnce).toBeTruthy();
expect(construct.node.metadataEntry).not.toContainEqual({
data: expect.stringContaining(errmsg),
type: 'aws:cdk:warning',
trace: undefined,
});
});

describe('AutoScalingGroupImdsAspect', () => {
test('warns when metadataOptions is a token', () => {
// GIVEN
Expand Down

0 comments on commit 8cb15e3

Please sign in to comment.