Class: Kitchen::Driver::Azure::ManagedIdentityToken

Inherits:
TokenProvider
  • Object
show all
Defined in:
lib/kitchen/driver/azure/token_provider.rb

Overview

Authenticates as a managed identity, via the Instance Metadata Service.

This replaces the legacy MSI extension endpoint on port 50342 that the old SDK used; IMDS is the supported endpoint on modern Azure VMs.

Constant Summary collapse

IMDS_URL =

Returns the IMDS token endpoint.

Returns:

  • (String)

    the IMDS token endpoint.

"http://169.254.169.254/metadata/identity/oauth2/token".freeze
API_VERSION =

Returns IMDS API version.

Returns:

  • (String)

    IMDS API version.

"2018-02-01".freeze

Constants inherited from TokenProvider

TokenProvider::EXPIRY_MARGIN

Instance Attribute Summary

Attributes inherited from TokenProvider

#environment

Instance Method Summary collapse

Methods inherited from TokenProvider

#access_token, #authorization_header

Constructor Details

#initialize(environment:, client_id: nil) ⇒ ManagedIdentityToken

Returns a new instance of ManagedIdentityToken.

Parameters:

  • environment (Environments::Environment)
  • client_id (String, nil) (defaults to: nil)

    the user-assigned identity to use, or nil for the system-assigned identity.



198
199
200
201
# File 'lib/kitchen/driver/azure/token_provider.rb', line 198

def initialize(environment:, client_id: nil)
  super(environment:)
  @client_id = client_id
end

Instance Method Details

#fetch_tokenArray(String, Integer)

Returns:

  • (Array(String, Integer))


204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/kitchen/driver/azure/token_provider.rb', line 204

def fetch_token
  query = { "api-version" => API_VERSION, "resource" => environment.token_audience }
  query["client_id"] = @client_id if @client_id

  response = Http.request(
    method: :get,
    url: "#{IMDS_URL}?#{URI.encode_www_form(query)}",
    headers: { "Metadata" => "true" }
  )

  token_from(response, "the instance metadata service")
end