From 9d59f0062afb2293664c0553b1555fef8d662f29 Mon Sep 17 00:00:00 2001 From: Amanda Tarafa Mas Date: Thu, 26 Oct 2023 23:51:27 -0700 Subject: [PATCH] feat: Check BIOS for GCE residency on Windows. --- .../Google.Apis.Auth/Google.Apis.Auth.csproj | 2 ++ .../OAuth2/ComputeCredential.cs | 25 +++++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/Src/Support/Google.Apis.Auth/Google.Apis.Auth.csproj b/Src/Support/Google.Apis.Auth/Google.Apis.Auth.csproj index 75b2cc61ff0..7da2bfdd325 100644 --- a/Src/Support/Google.Apis.Auth/Google.Apis.Auth.csproj +++ b/Src/Support/Google.Apis.Auth/Google.Apis.Auth.csproj @@ -40,6 +40,7 @@ Supported Platforms: + @@ -48,6 +49,7 @@ Supported Platforms: + diff --git a/Src/Support/Google.Apis.Auth/OAuth2/ComputeCredential.cs b/Src/Support/Google.Apis.Auth/OAuth2/ComputeCredential.cs index 257868e3968..09a39fb0e2f 100644 --- a/Src/Support/Google.Apis.Auth/OAuth2/ComputeCredential.cs +++ b/Src/Support/Google.Apis.Auth/OAuth2/ComputeCredential.cs @@ -21,6 +21,7 @@ limitations under the License. using Newtonsoft.Json; using System; using System.Collections.Generic; +using System.ComponentModel; using System.IO; using System.Linq; using System.Net; @@ -398,12 +399,32 @@ bool IsLinux() #endif } - bool IsWindowsGoogleBios() => throw new NotImplementedException(); + bool IsWindowsGoogleBios() + { + Logger.Info("Checking BIOS values on Windows."); +#if NET45 || NETSTANDARD1_3 + Logger.Debug("System.Management is not supported in net45 and netstandard1.3"); + return false; +#else + System.Management.ManagementClass biosClass = new ("Win32_BIOS"); + using var instances = biosClass.GetInstances(); + + foreach(var instance in instances) using (instance) + { + // We should only find one instance for Win32_BIOS class. + // This will throw a ManagementException "NotFound" if the "Manufacturer" property + // is not present. That's fine, we are catching the exception elsewhere and logging it. + return instance["Manufacturer"]?.ToString() == "Google"; + } + Logger.Debug("No Win32_BIOS management object found."); + return false; +#endif + } async Task IsLinuxGoogleBiosAsync() { Logger.Info("Checking BIOS values on Linux."); - + string fileName = "/sys/class/dmi/id/product_name"; if (!File.Exists(fileName)) {