Skip to content

bug: ISSUE-7452: Fix EC2 Workspace Connect for Any OS #7455

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions packages/core/src/awsService/ec2/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ export interface Ec2RemoteEnv extends VscodeRemoteConnection {
ssmSession: StartSessionResponse
}

export type Ec2OS = 'Amazon Linux' | 'Ubuntu' | 'macOS'
interface RemoteUser {
os: Ec2OS
os: string
name: string
}

Expand Down Expand Up @@ -301,7 +300,7 @@ export class Ec2Connecter implements vscode.Disposable {
public async tryCleanKeys(
instanceId: string,
hintComment: string,
hostOS: Ec2OS,
hostOS: string,
remoteAuthorizedKeysPath: string
) {
try {
Expand Down Expand Up @@ -345,6 +344,13 @@ export class Ec2Connecter implements vscode.Disposable {
return { name: 'ubuntu', os }
}

const input = await vscode.window.showInputBox({
prompt: `Unrecognized OS "${os}". Please enter the username to use for instance ${instanceId}:`,
})
if (input) {
return { name: input, os }
}

throw new ToolkitError(`Unrecognized OS name ${os} on instance ${instanceId}`, { code: 'UnknownEc2OS' })
}
}
Expand All @@ -355,14 +361,9 @@ export class Ec2Connecter implements vscode.Disposable {
* @param filepath filepath (as string) to target with the command.
* @returns bash command to remove lines from file.
*/
export function getRemoveLinesCommand(pattern: string, hostOS: Ec2OS, filepath: string): string {
export function getRemoveLinesCommand(pattern: string, hostOS: string, filepath: string): string {
if (pattern.includes('/')) {
throw new ToolkitError(`ec2: cannot match pattern containing '/', given: ${pattern}`)
}
// Linux allows not passing extension to -i, whereas macOS requires zero length extension.
return `sed -i${isLinux(hostOS) ? '' : " ''"} /${pattern}/d ${filepath}`
}

function isLinux(os: Ec2OS): boolean {
return os === 'Amazon Linux' || os === 'Ubuntu'
return `sed -i.bak '/${pattern}/d' ${filepath} && rm ${filepath}.bak`
}