Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Authenticating with Azure AD credentials? #213

Open
KNejad opened this issue Feb 14, 2022 · 1 comment
Open

Authenticating with Azure AD credentials? #213

KNejad opened this issue Feb 14, 2022 · 1 comment

Comments

@KNejad
Copy link

KNejad commented Feb 14, 2022

Is it possible to authenticate with Active Directory credentials?

I've been trying to work out how to do this but haven't had any luck yet.

This PR seems to add support for it, but it's not documented how to use it anywhere: #153

I tried copying the code from the test, which uses ADAL, but the gem doesn't seem to install properly because the ADAL gem seems to be dead and bugged: AzureAD/azure-activedirectory-library-for-ruby#44

Is there any other way to authenticate using AD credentials?

@drewda
Copy link

drewda commented Jan 19, 2023

This was a very frustrating experience, but I believe I have now gotten this to work using the following:

# Gemfile
gem 'azure-storage-common'
gem 'azure-storage-blob'
gem 'adal', github: 'AzureAD/azure-activedirectory-library-for-ruby', ref: '24e1b7f0dc374dcb968171f80e832118691d288c'

def azure_token_signer
  tenant_id = ENV.fetch("AZURE_TENANT_ID", nil)
  client_id = ENV.fetch("AZURE_CLIENT_ID", nil)
  client_secret = ENV.fetch("AZURE_CLIENT_SECRET", nil)

  if tenant_id.nil? || client_id.nil? || client_secret.nil?
    raise "Azure ActiveDirectory credentials not provided"
  end

  auth_ctx = ADAL::AuthenticationContext.new("login.microsoftonline.com", tenant_id)
  client_cred = ADAL::ClientCredential.new(client_id, client_secret)
  token = auth_ctx.acquire_token_for_client("https://storage.azure.com/", client_cred)
  access_token = token.access_token

  token_credential = Azure::Storage::Common::Core::TokenCredential.new access_token
  Azure::Storage::Common::Core::Auth::TokenSigner.new token_credential
end

def storage_common_client
  Azure::Storage::Common::Client::create(storage_account_name: ENV['AZURE_STORAGE_ACCOUNT'], signer: self.azure_token_signer)
end

def blob_service_client
  Azure::Storage::Blob::BlobService.new(api_version: "2018-11-09", client: storage_common_client)
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants