AzureCheckTest 0.1.16

There is a newer version of this package available.
See the version list below for details.
dotnet add package AzureCheckTest --version 0.1.16
                    
NuGet\Install-Package AzureCheckTest -Version 0.1.16
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AzureCheckTest" Version="0.1.16" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AzureCheckTest" Version="0.1.16" />
                    
Directory.Packages.props
<PackageReference Include="AzureCheckTest" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add AzureCheckTest --version 0.1.16
                    
#r "nuget: AzureCheckTest, 0.1.16"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package AzureCheckTest@0.1.16
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=AzureCheckTest&version=0.1.16
                    
Install as a Cake Addin
#tool nuget:?package=AzureCheckTest&version=0.1.16
                    
Install as a Cake Tool

IsAzureVM function (azurecheck.h)

The IsAzureVM function checks if the Windows Virtual Machine is on Azure using the attested metadata service.

This function uses the attested Azure IMDS Service which sends signed responses with trusted certificates. IsAzureVM will do attestation verifying those certificates and get the parameters decrypted from the response. For current Azure Instance Metadata Service (IMDS) attestation, refer here.

Syntax

HRESULT IsAzureVM (
[in] DWORD flags, 
[in, optional] LPCWSTR nonce, 
[in, out] AZURE_HOST_TYPE azureHostType, 
[in, out, optional] AZURE_METADATA* response, 
[out] BOOL* isAzureVM
);

Parameters

[in] flags

Flags which determine additional validations.

Value Meaning
AZURECHECK_USE_DIRECTACCESS 0x000000001 Skip proxy configuration. Do direct HTTP Access on IMDS endpoint.
AZURECHECK_USE_NONCE 0x000000002 Send nonce and validate it with IMDS response. If nonce not provided, a nonce will be generated using calling process name.
AZURECHECK_CERT_PINNING 0x000000100 Do intermediate certificate check.

[in, optional] nonce

A pointer to a string variable that contains the custom nonce which the calling application will validate with.

[in, out] azureHostType

A pointer to the AZURE_HOST_TYPE enumeration.

AZURE_HOST_TYPE

Syntax

typedef enum    
{ 
    AZURE_HOST_INVALID = 0, 
    AZURE_CLOUD, 
    AZURE_STACK, 
    AZURE_STACK_HCI, 
    AZURE_PRODUCT_MAX 
} AZURE_HOST_TYPE;

Members

Value Meaning
AZURE_CLOUD Azure VMs created with ARM (Azure Resource Manager) or created by using the classic deployment model.
AZURE_STACK Azure VMs created with Azure Stack Hub.
AZURE_STACK_HCI Azure VMs created with Azure Stack Hybrid Computing Instance (HCI) v2 product.

If the value is AZURE_HOST_INVALID, then the API will determine the host product type and set the type accordingly.

If any other value is provided, then the API will attest if current VM is hosted on that specific host product.

[in, out, optional] response

A pointer to the AZURE_METADATA structure to be converted. It is a decrypted metadata from IMDS response. For more information, refer to Azure Instance Metadata Service for Windows - Azure Virtual Machines | Microsoft Docs

AZURE_METADATA

Syntax

typedef struct _tagAzureMetadataResponse
{   
    LPCWSTR nonce;
    LPCWSTR plan;
    LPCWSTR timeStampCreatedOn;
    LPCWSTR timeStampExpiresOn;
    LPCWSTR vmId;
    LPCWSTR subscriptionId;
    LPCWSTR sku;
    LPCWSTR licenseType;
} AZURE_METADATA;

Members

Data Description
nonce A string that can be optionally provided with the request. If no nonce was supplied, the current Coordinated Universal Time timestamp is used.
plan The Azure Marketplace Image plan. Contains the plan ID (name), product image or offer (product), and publisher ID (publisher).
timeStampCreatedOn The UTC timestamp for when the signed document was created
timeStampExpiresOn The UTC timestamp for when the signed document expires
vmId Unique identifier for the VM
subscriptionId Azure subscription for the Virtual Machine
sku Specific SKU for the VM image (correlates to compute/sku property from the Instance Metadata endpoint [/metadata/instance])
licenseType Type of license for Azure Hybrid Benefit. This is only present for AHB-enabled VMs.

[out] isAzureVM

A pointer to Boolean value which indicates whether the VM is running in Azure.

Return value

Returns a HRESULT

Remarks

  • Windows.h header file must be included.
  • COM library must be initialized before the operation and closed after the operation.
  • This function/API must always be used from its latest NuGet package Microsoft.Windows.AzureCheck

Example

#include <iostream>
#include <windows.h>
#include <azurecheck.h>

int main()
{
    HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);

    BOOL isAzureVM = FALSE;
    AZURE_HOST_TYPE host_type = AZURE_HOST_TYPE::AZURE_HOST_INVALID;
    AZURE_METADATA metadata = {};
    
    HRESULT hrResult = IsAzureVM(0, NULL, &host_type, &metadata, &isAzureVM);

    if (FAILED(hrResult)) {
        std::cerr << "Call to IsAzureVM failed with this error: 0x" << std::hex << hrResult << std::endl;
    }
    else {
        std::cout << "Is the code running in Azure? " << isAzureVM << std::endl;
        std::cout << "Host type: " << host_type << std::endl;
    }
    CoUninitialize();
}

Requirements

Minimum supported client Windows Vista
Minimum supported server Windows Server 2008
Target Platform Windows
Header azurecheck.h
Library azurecheck.lib
DLL azurecheck.dll
Product Compatible and additional computed target framework versions.
native native is compatible. 
Windows win32 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.