aws / aws/aws-cdk

ec2: aws_ec2.InitFile.fromFileInline doesn't standardise line endings on Windows

Open
#24,212 0 comments 0 reactions 0 assignees View on GitHub
@aws-cdk/aws-ec2 bug p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
1d 19h
Merged PRs (30d)
74

Description

### Describe the bug

Using `aws_ec2.InitFile.fromFileInline` on Windows adds the file content to the cfn template as-is, without replacing `\r\n` line endings with `\n`. Since cfn-init assumes that files have universal line endings (i.e. `\n`), this causes Windows instances using InitFile.fromFileInline to have files deployed with `\r\r\n` line endings. This can break e.g. PowerShell scripts, since the line ending is parsed (it would appear) as two "Mac-style" line endings (`\r`).

The root cause is that cfn-init uses Python's `open` builtin to write the files in `'w'` mode, without the `newline` argument set. This means `file.write()` assumes the use of universal line endings in its input, and then adapts to the platform (Windows), so replaces `\n` with `\r\n`.

#### Workaround
This issue is avoided by setting `base64Encoded: true`, since this causes cfn-init to use `'wb'` mode, which doesn't modify line endings.

### Expected Behavior

`InitFile.fromFileInline` either transparently fixes line endings to whatever the target platform expects or passes them all the way through to the target instance unmodified.

### Current Behavior

Using `InitFile.fromFileInline` leads mangled line endings when you are both developing on and deploying to Windows

### Reproduction Steps

Minimal working example: https://github.com/crh23/CDKLineEndingsMWE

Deploy the project, connect to the deployed instance. Observe that the deployed files have incorrect line endings.

### Possible Solution

Adding a find/replace to standardise line-endings (i.e. replace all `\r\n` with `\n`) https://github.com/aws/aws-cdk/blob/c4e714e0c933eeb8d6235a54e5b9d5e9f602d1ab/packages/%40aws-cdk/aws-ec2/lib/cfn-init-elements.ts#L373-L385

### Additional Information/Context

_No response_

### CDK CLI Version

2.59.0

### Framework Version

2.59.0

### Node.js Version

16.19.0

### OS

Windows 10

### Language

Typescript

### Language Version

TypeScript (4.9.4)

### Other information

_No response_

Contributor guide

Open the contributing guide

Research direction

Start in packages/@aws-cdk/aws-ec2/lib/cfn-init-elements.ts around lines 373-385 and use the linked minimal reproduction on Windows to observe the generated line endings. Done means InitFile.fromFileInline no longer produces doubled carriage returns when cfn-init deploys the file, while the intended content remains intact.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
cloud, infrastructure
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.