Skip to content

Commit

Permalink
fix(cli): wmic not found on modern Windows systems (#17070)
Browse files Browse the repository at this point in the history
Apparently Microsoft stopped shipping `wmic.exe` in certain situations
(modern systems?). We rely on this to detect whether we are on an EC2
instance, so that we do or do not configure the IMDS credential
provider (we try to avoid the IMDS credential provider if unnecessary,
because in certain network setups it may hang for a long time failing to
connect to `169.254.169.254`).

If calling `wmic` fails, just assume we're not on an EC2 instance
and proceed.

Fixes #16419.


----

*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 committed Nov 5, 2021
1 parent 7886607 commit 332ce4d
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions packages/aws-cdk/lib/api/aws-auth/awscli-compatible.ts
Expand Up @@ -177,12 +177,18 @@ async function isEc2Instance() {
let instance = false;
if (process.platform === 'win32') {
// https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/identify_ec2_instances.html
const result = await util.promisify(child_process.exec)('wmic path win32_computersystemproduct get uuid', { encoding: 'utf-8' });
// output looks like
// UUID
// EC2AE145-D1DC-13B2-94ED-01234ABCDEF
const lines = result.stdout.toString().split('\n');
instance = lines.some(x => matchesRegex(/^ec2/i, x));
try {
const result = await util.promisify(child_process.exec)('wmic path win32_computersystemproduct get uuid', { encoding: 'utf-8' });
// output looks like
// UUID
// EC2AE145-D1DC-13B2-94ED-01234ABCDEF
const lines = result.stdout.toString().split('\n');
instance = lines.some(x => matchesRegex(/^ec2/i, x));
} catch (e) {
// Modern machines may not have wmic.exe installed. No reason to fail, just assume it's not an EC2 instance.
debug(`Checking using WMIC failed, assuming NOT an EC2 instance: ${e.message} (pass --ec2creds to force)`);
instance = false;
}
} else {
// https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/identify_ec2_instances.html
const files: Array<[string, RegExp]> = [
Expand Down

0 comments on commit 332ce4d

Please sign in to comment.