Class: Kitchen::Driver::Credentials

Inherits:
Object
  • Object
show all
Defined in:
lib/kitchen/driver/credentials.rb

Overview

Credentials

Constant Summary collapse

CONFIG_PATH =
"#{ENV['HOME']}/.azure/credentials".freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCredentials

Creates and initializes a new instance of the Credentials class.



14
15
16
17
18
19
20
21
# File 'lib/kitchen/driver/credentials.rb', line 14

def initialize
  config_file = ENV["AZURE_CONFIG_FILE"] || File.expand_path(CONFIG_PATH)
  if File.file?(config_file)
    @credentials = IniFile.load(File.expand_path(config_file))
  else
    warn "#{CONFIG_PATH} was not found or not accessible."
  end
end

Class Method Details

.singletonObject



85
86
87
# File 'lib/kitchen/driver/credentials.rb', line 85

def self.singleton
  @credentials ||= Credentials.new
end

Instance Method Details

#ad_settings_for_azure_environment(azure_environment) ⇒ MsRestAzure::ActiveDirectoryServiceSettings

Retrieves a [MsRestAzure::ActiveDirectoryServiceSettings] object representing the AD settings for the given cloud.

Parameters:

  • azure_environment (String)

    The Azure environment to retrieve settings for.

Returns:

  • (MsRestAzure::ActiveDirectoryServiceSettings)

    Settings to be used for subsequent requests



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/kitchen/driver/credentials.rb', line 53

def ad_settings_for_azure_environment(azure_environment)
  case azure_environment.downcase
  when "azureusgovernment"
    ::MsRestAzure::ActiveDirectoryServiceSettings.get_azure_us_government_settings
  when "azurechina"
    ::MsRestAzure::ActiveDirectoryServiceSettings.get_azure_china_settings
  when "azuregermancloud"
    ::MsRestAzure::ActiveDirectoryServiceSettings.get_azure_german_settings
  when "azure"
    ::MsRestAzure::ActiveDirectoryServiceSettings.get_azure_settings
  end
end

#azure_options_for_subscription(subscription_id, azure_environment = "Azure") ⇒ Object

Retrieves an object containing options and credentials for the given subscription_id and azure_environment.

Parameters:

  • subscription_id (String)

    The subscription_id to retrieve a token for

  • azure_environment (String) (defaults to: "Azure")

    The azure_environment to use

Returns:

  • (Object)

    Object that can be supplied along with all Azure client requests.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/kitchen/driver/credentials.rb', line 32

def azure_options_for_subscription(subscription_id, azure_environment = "Azure")
  tenant_id = ENV["AZURE_TENANT_ID"] || @credentials[subscription_id]["tenant_id"]
  client_id = ENV["AZURE_CLIENT_ID"] || @credentials[subscription_id]["client_id"]
  client_secret = ENV["AZURE_CLIENT_SECRET"] || @credentials[subscription_id]["client_secret"]
  token_provider = ::MsRestAzure::ApplicationTokenProvider.new(tenant_id, client_id, client_secret, ad_settings_for_azure_environment(azure_environment))
  options = { tenant_id: tenant_id,
              client_id: client_id,
              client_secret: client_secret,
              subscription_id: subscription_id,
              credentials: ::MsRest::TokenCredentials.new(token_provider),
              active_directory_settings: ad_settings_for_azure_environment(azure_environment),
              base_url: endpoint_settings_for_azure_environment(azure_environment).resource_manager_endpoint_url }
  options
end

#endpoint_settings_for_azure_environment(azure_environment) ⇒ MsRestAzure::AzureEnvironment

Retrieves a [MsRestAzure::AzureEnvironment] object representing endpoint settings for the given cloud.

Parameters:

  • azure_environment (String)

    The Azure environment to retrieve settings for.

Returns:

  • (MsRestAzure::AzureEnvironment)

    Settings to be used for subsequent requests



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/kitchen/driver/credentials.rb', line 72

def endpoint_settings_for_azure_environment(azure_environment)
  case azure_environment.downcase
  when "azureusgovernment"
    ::MsRestAzure::AzureEnvironments::AzureUSGovernment
  when "azurechina"
    ::MsRestAzure::AzureEnvironments::AzureChinaCloud
  when "azuregermancloud"
    ::MsRestAzure::AzureEnvironments::AzureGermanCloud
  when "azure"
    ::MsRestAzure::AzureEnvironments::AzureCloud
  end
end