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

fix(ec2): prevent deduplication of init command args #30821

Merged
merged 13 commits into from
Aug 8, 2024

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,7 @@
"Fn::Join": [
"",
[
"#!/bin/bash\n# fingerprint: 8787022e9944cbeb\n(\n set +e\n /opt/aws/bin/cfn-init -v --region ",
"#!/bin/bash\n# fingerprint: 370d9b2dcf8bf44b\n(\n set +e\n /opt/aws/bin/cfn-init -v --region ",
{
"Ref": "AWS::Region"
},
Expand Down Expand Up @@ -955,6 +955,29 @@
"owner": "root",
"group": "root"
}
},
"commands": {
"000": {
"command": [
"useradd",
"-u",
"1001",
"-g",
"1001",
"eguser"
]
},
"001": {
"command": [
"useradd",
"-a",
"-u",
"1001",
"-g",
"1001",
"eguser"
]
}
}
}
},
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ class TestStack extends cdk.Stack {
'/target/path/config.json',
path.join(tmpDir, 'testConfigFile2'),
),
ec2.InitCommand.argvCommand([
'useradd', '-u', '1001', '-g', '1001', 'eguser',
]),
ec2.InitCommand.argvCommand([
'useradd', '-a', '-u', '1001', '-g', '1001', 'eguser',
]),
]),
},
}),
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 11 additions & 4 deletions packages/aws-cdk-lib/aws-ec2/lib/cfn-init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,17 @@ function deepMerge(target?: Record<string, any>, src?: Record<string, any>) {
if (target[key] && !Array.isArray(target[key])) {
throw new Error(`Trying to merge array [${value}] into a non-array '${target[key]}'`);
}
target[key] = Array.from(new Set([
...target[key] ?? [],
...value,
]));
if (key === 'command') { // don't deduplicate command arguments
target[key] = new Array(
...target[key] ?? [],
...value,
);
} else {
target[key] = Array.from(new Set([
...target[key] ?? [],
...value,
]));
}
continue;
}
if (typeof value === 'object' && value) {
Expand Down
62 changes: 62 additions & 0 deletions packages/aws-cdk-lib/aws-ec2/test/cfn-init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,68 @@ test('empty configs are not rendered', () => {
});
});

test('duplicate config arguments not deduplicated', () => {
//GIVEN
const config = new ec2.InitConfig([
ec2.InitCommand.argvCommand([
'useradd', '-u', '1001', '-g', '1001', 'eguser',
]),
ec2.InitCommand.argvCommand([
'useradd', '-a', '-u', '1001', '-g', '1001', 'eguser',
]),
]);

// WHEN
const init = ec2.CloudFormationInit.fromConfigSets({
configSets: { default: ['config'] },
configs: { config },
});
init.attach(resource, linuxOptions());

// THEN
expectMetadataLike({
'AWS::CloudFormation::Init': {
configSets: {
default: ['config'],
},
config: {
commands: {
'000': {
command: ['useradd', '-u', '1001', '-g', '1001', 'eguser'],
},
'001': {
command: ['useradd', '-a', '-u', '1001', '-g', '1001', 'eguser'],
},
},
},
},
});
});

test('deepMerge properly deduplicates non-command arguments', () => {
// WHEN
const config = new ec2.InitConfig([
ec2.InitSource.fromUrl('/tmp/blinky', 'https://amazon.com/blinky.zip'),
ec2.InitSource.fromUrl('/tmp/blinky', 'https://amazon.com/blinky.zip'),
ec2.InitSource.fromUrl('/tmp/pinky', 'https://amazon.com/pinky.zip'),
ec2.InitSource.fromUrl('/tmp/pinky', 'https://amazon.com/pinky.zip'),
ec2.InitSource.fromUrl('/tmp/inky', 'https://amazon.com/inky.zip'),
ec2.InitSource.fromUrl('/tmp/clyde', 'https://amazon.com/blinky.zip'),
ec2.InitSource.fromUrl('/tmp/clyde', 'https://amazon.com/blinky.zip'),
ec2.InitSource.fromUrl('/tmp/clyde', 'https://amazon.com/blinky.zip'),
]);

// THEN
expect(config._bind(stack, linuxOptions()).config).toEqual(expect.objectContaining({
sources: {
'/tmp/blinky': 'https://amazon.com/blinky.zip',
'/tmp/pinky': 'https://amazon.com/pinky.zip',
'/tmp/inky': 'https://amazon.com/inky.zip',
'/tmp/clyde': 'https://amazon.com/blinky.zip',
},
}));
});

describe('userdata', () => {
let simpleInit: ec2.CloudFormationInit;
beforeEach(() => {
Expand Down