Skip to content

Commit

Permalink
fix(ec2): InitCommand.shellCommand() renders an argv command instead (
Browse files Browse the repository at this point in the history
#10691)

`InitCommand.shellCommand('abc xyz')` renders to `['abc xyz']` which actually
represents an `argv` command of one element, which is treated
differently. For example, spaces in it are not parsed, which would
be expected for a shell command.

The correct rendering for a shell command is a plain string in the
`command` property.

Fixes #10684.


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
rix0rrr authored Oct 5, 2020
1 parent 63088e1 commit de9d2f7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-ec2/lib/cfn-init-elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ export class InitCommand extends InitElement {
* need to be preceded by a `\` if you want to treat them as part of a filename.
*/
public static shellCommand(shellCommand: string, options: InitCommandOptions = {}): InitCommand {
return new InitCommand([shellCommand], options);
return new InitCommand(shellCommand, options);
}

/**
Expand All @@ -228,7 +228,7 @@ export class InitCommand extends InitElement {

public readonly elementType = InitElementType.COMMAND.toString();

private constructor(private readonly command: string[], private readonly options: InitCommandOptions) {
private constructor(private readonly command: string[] | string, private readonly options: InitCommandOptions) {
super();
}

Expand Down
17 changes: 15 additions & 2 deletions packages/@aws-cdk/aws-ec2/test/cfn-init-element.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,20 @@ describe('InitCommand', () => {

// THEN
expect(rendered).toEqual({
'000': { command: ['/bin/sh'] },
'000': { command: '/bin/sh' },
});
});

test('shell command is rendered as string', () => {
// GIVEN
const command = ec2.InitCommand.shellCommand('/bin/sh -c "echo hello"');

// WHEN
const rendered = getElementConfig(command, InitPlatform.LINUX);

// THEN
expect(rendered).toEqual({
'000': { command: '/bin/sh -c "echo hello"' },
});
});

Expand All @@ -65,7 +78,7 @@ describe('InitCommand', () => {
// THEN
expect(rendered).toEqual({
command_0: {
command: ['/bin/sh'],
command: '/bin/sh',
env: { SECRETS_FILE: '/tmp/secrets' },
cwd: '/home/myUser',
test: 'test -d /home/myUser',
Expand Down

0 comments on commit de9d2f7

Please sign in to comment.