Skip to content

Commit

Permalink
feat: Check BIOS for GCE residency on Windows.
Browse files Browse the repository at this point in the history
  • Loading branch information
amanda-tarafa committed Oct 27, 2023
1 parent c3ada0f commit 9d59f00
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Src/Support/Google.Apis.Auth/Google.Apis.Auth.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Supported Platforms:
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)'=='netstandard2.0'">
<PackageReference Include="System.Management" Version="7.0.2" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)'=='net45'">
Expand All @@ -48,6 +49,7 @@ Supported Platforms:

<ItemGroup Condition="'$(TargetFramework)'=='net461'">
<Reference Include="System.Net.Http" />
<PackageReference Include="System.Management" Version="7.0.2" />
</ItemGroup>

<!-- build and include the (empty) platformservices dll in package; required for binary backward compatibility -->
Expand Down
25 changes: 23 additions & 2 deletions Src/Support/Google.Apis.Auth/OAuth2/ComputeCredential.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<bool> IsLinuxGoogleBiosAsync()
{
Logger.Info("Checking BIOS values on Linux.");

string fileName = "/sys/class/dmi/id/product_name";
if (!File.Exists(fileName))
{
Expand Down

0 comments on commit 9d59f00

Please sign in to comment.