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
12 changes: 8 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,14 @@ 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] = Array.from(new Set([
...target[key] ?? [],
...value,
]));
} else {
target[key] = value;
}
continue;
}
if (typeof value === 'object' && value) {
Expand Down
38 changes: 38 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,44 @@ 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'],
},
},
},
},
});
});

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