-
Notifications
You must be signed in to change notification settings - Fork 4
Sample for managing resources via MSI Identity #1
Changes from 4 commits
bf8ffa9
84ddf4a
9e0d949
320dcbf
53fa300
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| # This is a sample of what your .env file to run integration tests. | ||
| # If you move this file to the .env and replace the environment variables with your own values, then the environment | ||
| # will be included when the rake test tasks are executed. Otherwise, you could just export the environment vars | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit:Otherwise, you could just export the environment variables manually |
||
| # manually. | ||
|
|
||
| AZURE_TENANT_ID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
| AZURE_SUBSCRIPTION_ID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
| RESOURCE_GROUP_NAME="xxxxxxxxxxxx" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| ## 2017.09.12 | ||
|
|
||
| *Features* | ||
| * Adding initial sample to show how to manage Azure resources using Managed Service Identity from MSI enabled Azure virtual machine. | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| # encoding: utf-8 | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
|
||
| source 'https://rubygems.org' | ||
|
|
||
| group :development, :test do | ||
| gem 'rake', '~>11.1' | ||
| gem 'dotenv', '~>2.1' | ||
| end | ||
|
|
||
| gem 'azure_mgmt_resources', '~>0.12.0' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| The MIT License (MIT) | ||
|
|
||
| Copyright (c) 2015 Microsoft Corporation | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| SOFTWARE. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| --- | ||
| services: resources | ||
| platforms: ruby | ||
| author: vishrutshah | ||
| --- | ||
|
|
||
| # Manage resources using Managed Service Identity using Ruby | ||
|
|
||
| This sample demonstrates how to manage Azure resources via Managed Service Identity using the Ruby SDK. | ||
|
|
||
| **On this page** | ||
|
|
||
| - [Create an Azure VM with MSI extension](#pre-requisite) | ||
| - [Run this sample](#run) | ||
| - [What is example.rb doing?](#example) | ||
| - [Create an MSI Token Provider](#msi) | ||
| - [Create a resource client](#resource-client) | ||
| - [Create an Azure Vault](#create-vault) | ||
| - [Delete an Azure vault](#delete-vault) | ||
|
|
||
| <a id="pre-requisite"></a> | ||
| ## Create an Azure VM with MSI extension | ||
|
|
||
| [Azure Compute VM with MSI](https://github.com/Azure-Samples/compute-ruby-msi-vm) | ||
|
|
||
| <a id="run"></a> | ||
| ## Run this sample | ||
|
|
||
| 1. log in to the above Azure virtual machine which has MSI service running and then follow the steps on that VM. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All steps are marked with 1.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hahah thanks for catching..updated |
||
|
|
||
| 2. If you don't already have it, [install Ruby and the Ruby DevKit](https://www.ruby-lang.org/en/documentation/installation/). | ||
|
|
||
| 3. If you don't have bundler, install it. | ||
|
|
||
| ``` | ||
| gem install bundler | ||
| ``` | ||
|
|
||
| 4. Clone the repository. | ||
|
|
||
| ``` | ||
| git clone https://github.com/Azure-Samples/resources-ruby-manage-resources-with-msi.git | ||
| ``` | ||
|
|
||
| 5. Install the dependencies using bundler. | ||
|
|
||
| ``` | ||
| cd resources-ruby-manage-resources-with-msi | ||
| bundle install | ||
| ``` | ||
|
|
||
| 6. Set the following environment variables using the information from the service principle that you created. | ||
|
|
||
| ``` | ||
| export AZURE_TENANT_ID={your tenant id} | ||
| export AZURE_SUBSCRIPTION_ID={your subscription id} | ||
| export RESOURCE_GROUP_NAME={name of the resource group} | ||
| ``` | ||
|
|
||
| > [AZURE.NOTE] On Windows, use `set` instead of `export`. | ||
|
|
||
| 7. Run the sample. | ||
|
|
||
| ``` | ||
| bundle exec ruby example.rb | ||
| ``` | ||
|
|
||
| <a id="example"></a> | ||
| ## What does example.rb doing? | ||
| <a id="msi"></a> | ||
| ### Create an MSI Token Provider | ||
| Initialize `subscription_id`, `tenant_id`, `resource_group_name` and `port` from environment variables. | ||
| ```ruby | ||
| subscription_id = ENV['AZURE_SUBSCRIPTION_ID'] || '11111111-1111-1111-1111-111111111111' | ||
| tenant_id = ENV['AZURE_TENANT_ID'] | ||
| resource_group_name = ENV['RESOURCE_GROUP_NAME'] | ||
| port = ENV['MSI_PORT'] || 50342 # If not provided then we assume the default port | ||
| ``` | ||
|
|
||
| Now, we will create token credential using `MSITokenProvider`. | ||
| ```ruby | ||
| # Create Managed Service Identity as the token provider | ||
| provider = MsRestAzure::MSITokenProvider.new(port) | ||
| credentials = MsRest::TokenCredentials.new(provider) | ||
| ``` | ||
|
|
||
| <a id="resource-client"></a> | ||
| ### Create a resource client | ||
| Now, we will create a resource management client using Managed Service Identity token provider. | ||
|
|
||
| ```ruby | ||
| client = Azure::ARM::Resources::ResourceManagementClient.new(credentials) | ||
| client.subscription_id = subscription_id | ||
| ``` | ||
| <a id="create-vault"></a> | ||
| ### Create an Azure Vault | ||
| Now, we will create an Azure key vault account using MSI authenticated resource client. This Azure Key Vault | ||
| account resource is identical to normal account but it is just created under the resource group where MSI enabled | ||
| Azure VM has the permission to create resources. | ||
|
|
||
| ```ruby | ||
| puts 'Creating key vault account with MSI Identity...' | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there anything special on the key vault account creation related to MSI Identity, or is it just the fact that's created under the ResourceGroup that was created with MSITokenProvider? If so, it may be worth commenting on it. |
||
| key_vault_params = Azure::ARM::Resources::Models::GenericResource.new.tap do |rg| | ||
| rg.location = WEST_US | ||
| rg.properties = { | ||
| sku: { family: 'A', name: 'standard' }, | ||
| tenantId: tenant_id, | ||
| accessPolicies: [], | ||
| enabledForDeployment: true, | ||
| enabledForTemplateDeployment: true, | ||
| enabledForDiskEncryption: true | ||
| } | ||
| end | ||
| ``` | ||
|
|
||
| <a id="delete-vault"></a> | ||
| ### Delete an Azure vault | ||
| Now, we will delete key vault account created using this example. Please comment this out to keep the resources alive in you Azure subscription. | ||
|
|
||
| ```ruby | ||
| client.resources.delete(resource_group_name, | ||
| 'Microsoft.KeyVault', | ||
| '', | ||
| 'vaults', | ||
| KEY_VAULT_NAME, | ||
| '2015-06-01') | ||
| ``` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| require 'rspec/core/rake_task' | ||
|
|
||
| RSpec::Core::RakeTask.new(:spec) | ||
| task :default => :spec |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| #!/usr/bin/env ruby | ||
|
|
||
| require 'azure_mgmt_resources' | ||
| require 'dotenv' | ||
|
|
||
| Dotenv.load!(File.join(__dir__, './.env')) | ||
|
|
||
| WEST_US = 'westus' | ||
| KEY_VAULT_NAME = 'sampleVault8976' | ||
|
|
||
| # This script expects that the following environment vars are set: | ||
| # | ||
| # AZURE_TENANT_ID: with your Azure Active Directory tenant id or domain | ||
| # AZURE_SUBSCRIPTION_ID: with your Azure Subscription Id | ||
| # RESOURCE_GROUP_NAME: Name of the Azure resource group to create resource in where Managed Service Identity has enough permissions | ||
| # | ||
| def run_example | ||
| # | ||
| # Create the Resource Manager Client with an Managed Service Identity token provider | ||
| # | ||
| MsRest.use_ssl_cert | ||
| subscription_id = ENV['AZURE_SUBSCRIPTION_ID'] || '11111111-1111-1111-1111-111111111111' # your Azure Subscription Id | ||
| tenant_id = ENV['AZURE_TENANT_ID'] | ||
| resource_group_name = ENV['RESOURCE_GROUP_NAME'] | ||
| port = ENV['MSI_PORT'] || 50342 # If not provided then we assume the default port | ||
|
|
||
| # Create Managed Service Identity as the token provider | ||
| provider = MsRestAzure::MSITokenProvider.new(port) | ||
| credentials = MsRest::TokenCredentials.new(provider) | ||
|
|
||
| # Create a resource client | ||
| client = Azure::ARM::Resources::ResourceManagementClient.new(credentials) | ||
| client.subscription_id = subscription_id | ||
|
|
||
| # Create a Key Vault in the Resource Group | ||
| puts 'Creating key vault account with MSI Identity...' | ||
| key_vault_params = Azure::ARM::Resources::Models::GenericResource.new.tap do |rg| | ||
| rg.location = WEST_US | ||
| rg.properties = { | ||
| sku: { family: 'A', name: 'standard' }, | ||
| tenantId: tenant_id, | ||
| accessPolicies: [], | ||
| enabledForDeployment: true, | ||
| enabledForTemplateDeployment: true, | ||
| enabledForDiskEncryption: true | ||
| } | ||
| end | ||
|
|
||
| puts JSON.pretty_generate(client.resources.create_or_update(resource_group_name, | ||
| 'Microsoft.KeyVault', | ||
| '', | ||
| 'vaults', KEY_VAULT_NAME, | ||
| '2015-06-01', | ||
| key_vault_params).properties) + "\n\n" | ||
|
|
||
| puts 'Now that we have created a Key Vault, lets delete it.' | ||
| puts 'Press any key to continue' | ||
| gets | ||
| puts 'Deleting key vault account with MSI Identity...' | ||
| client.resources.delete(resource_group_name, | ||
| 'Microsoft.KeyVault', | ||
| '', | ||
| 'vaults', | ||
| KEY_VAULT_NAME, | ||
| '2015-06-01') | ||
|
|
||
| puts 'Thanks for learning about managing resources via Managed Service Identity.' | ||
| end | ||
|
|
||
| if $0 == __FILE__ | ||
| run_example | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
something seems odd with this sentence, not sure what you meant to change it to.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:( now I read it and wonder as well..I'll keep it as-is as that seems structured