-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve OS username resolution for EC2 instances (#1808)
- Expanded supported patterns to include rhel and suse-sles images. - Introduced a defaultUserName (ec2-user) for unsupported or unmatched image patterns. - Updated resolveOsUserName function to handle null or undefined values for imageName. - Fixed case-sensitivity issues by converting imageName to lowercase before matching. - Simplified logic to ensure robust and predictable username resolution. - Included the --region flag
- Loading branch information
Showing
4 changed files
with
55 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,20 @@ | ||
export function resolveOsUserName(imageName: string) { | ||
export function resolveOsUserName(imageName: string | null | undefined) { | ||
if (!imageName) { | ||
return '' | ||
} | ||
|
||
const defaultUserName = 'ec2-user' | ||
const amiPatterns: { [key: string]: string } = { | ||
'^(amazon|amzn|al2023)': 'ec2-user', | ||
'^(amazon|amzn|al2023|rhel|suse-sles)': defaultUserName, | ||
'^(ubuntu|centos|redhat|debian)': '\\1', // Use backreference for matched pattern (group 1) | ||
'.+': '\\w+', | ||
} | ||
|
||
for (const pattern in amiPatterns) { | ||
const match = imageName.match(new RegExp(pattern)) | ||
if (match) { | ||
return match[1] ? amiPatterns[pattern].replace('\\1', match[1]) : amiPatterns[pattern] | ||
const match = imageName.toLocaleLowerCase().match(new RegExp(pattern, 'i')) | ||
if (match && match[1]) { | ||
return amiPatterns[pattern].replace('\\1', match[1]) | ||
} | ||
} | ||
|
||
return '' | ||
return defaultUserName | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { describe, it, expect } from 'vitest' | ||
|
||
import { resolveOsUserName } from '../../../../src/client/components/aws/ec2Helper' | ||
|
||
describe('resolveOsUserName', () => { | ||
it('should return empty string when imageName is empty', () => { | ||
expect(resolveOsUserName('')).toBe('') | ||
}) | ||
|
||
it('should return "ec2-user" for Amazon Linux image names', () => { | ||
expect(resolveOsUserName('amazon-linux')).toBe('ec2-user') | ||
expect(resolveOsUserName('amzn-ami')).toBe('ec2-user') | ||
expect(resolveOsUserName('al2023')).toBe('ec2-user') | ||
}) | ||
|
||
it('should return "ec2-user" for RHEL or SUSE Linux image names', () => { | ||
expect(resolveOsUserName('rhel-8')).toBe('ec2-user') | ||
expect(resolveOsUserName('suse-sles')).toBe('ec2-user') | ||
}) | ||
|
||
it('should return the corresponding user for Ubuntu, CentOS, RedHat, or Debian image names', () => { | ||
expect(resolveOsUserName('ubuntu-20.04')).toBe('ubuntu') | ||
expect(resolveOsUserName('centos-7')).toBe('centos') | ||
expect(resolveOsUserName('redhat')).toBe('redhat') | ||
expect(resolveOsUserName('debian')).toBe('debian') | ||
}) | ||
|
||
it('should return "ec2-user" for unknown patterns', () => { | ||
expect(resolveOsUserName('unknown-image')).toBe('ec2-user') | ||
expect(resolveOsUserName('custom-linux-image')).toBe('ec2-user') | ||
}) | ||
|
||
it('should be case insensitive', () => { | ||
expect(resolveOsUserName('UbUnTu-18.04')).toBe('ubuntu') | ||
expect(resolveOsUserName('AMAZON-LINUX')).toBe('ec2-user') | ||
expect(resolveOsUserName('RHEL-7')).toBe('ec2-user') | ||
}) | ||
|
||
it('should handle edge cases gracefully', () => { | ||
expect(resolveOsUserName(null)).toBe('') | ||
expect(resolveOsUserName(undefined)).toBe('') | ||
expect(resolveOsUserName(' ')).toBe('ec2-user') // Assuming whitespace counts as unknown pattern | ||
}) | ||
}) |