Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Azure Databricks Token using ARM template

I need to create a token in Azure Databricks using ARM template. I am able to create Azure Databricks using ARM template but unable to create token in Azure Databricks using ARM template

Following is the template which i have used to create Azure Databricks

{
"$schema": "https://schema.management.azure.com/schemas/2015-01- 
01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"workspaceName": {
"type": "string",
  "metadata": {
    "description": "The name of the Azure Databricks workspace to create."
  }
},
"pricingTier": {
  "type": "string",
  "defaultValue": "premium",
  "allowedValues": [
    "standard",
    "premium"
  ],
  "metadata": {
    "description": "The pricing tier of workspace."
  }
},
"location": {
  "type": "string",
  "defaultValue": "[resourceGroup().location]",
  "metadata": {
    "description": "Location for all resources."
  }
}
},
"variables": {
"managedResourceGroupName": "[concat('databricks-rg-', 
parameters('workspaceName'), '-', uniqueString(parameters('workspaceName'), 
resourceGroup().id))]"
},
"resources": [
{
  "type": "Microsoft.Databricks/workspaces",
  "name": "[parameters('workspaceName')]",
  "location": "[parameters('location')]",
  "apiVersion": "2018-04-01",
  "sku": {
    "name": "[parameters('pricingTier')]"
  },
  "properties": {
    "ManagedResourceGroupId": "[concat(subscription().id, '/resourceGroups/', variables('managedResourceGroupName'))]"
  }
}
],
"outputs": {
"workspace": {
  "type": "object",
  "value": "[reference(resourceId('Microsoft.Databricks/workspaces', parameters('workspaceName')))]"
}
}
}

Kindly let me know how to create tokens in Azure Databricks using ARM template

like image 367
kartik iyer Avatar asked Jan 08 '19 10:01

kartik iyer


People also ask

How do I deploy an azure Databricks workspace using arm templates?

Using ARM templates for deployment is a well known method to deploy resource in Azure. By the end of this recipe, you will have learned how to deploy an Azure Databricks workspace using ARM templates. ARM templates can be deployed from an Azure DevOps pipeline, as well as by using PowerShell or CLI commands.

How do I create an azure Databricks personal access token?

You need to create Azure Databricks personal access token manually by going to the Azure Databricks portal. Even for creating using APIs, initial authentication to this API is the same as for all of the Azure Databricks API endpoints: you must first authenticate as described in Authentication.

What is the Azure Databricks virtual network template?

This template allows you to create a network security group, a virtual network and an Azure Databricks workspace with the virtual network, and Private Endpoint. This template allows you to create a a load balancer, network security group, a virtual network and an Azure Databricks workspace with the virtual network.

What diagnostic settings can be configured for Azure Databricks workspace?

Show activity on this post. Diagnostic settings for Azure Databricks Workspace are configured separately from its creation - you can use standard ARM templates for Azure Monitor that you can find in the documentation.


3 Answers

I see in a comment that you ask if it is possible to create a token using a script. It is now possible!

Databricks has a token API: https://docs.databricks.com/dev-tools/api/latest/tokens.html

Check out this blog: https://cloudarchitected.com/2020/01/using-azure-ad-with-the-azure-databricks-api/

It shows how easy it is to create a databricks token using AAD, and a few other methods.

I have some Python Code that I use automate this task. I would extend it to automatically add the token to a key vault of some sort. Here is a sample:

import requests
import adal
import json

# set variables 
clientId = "<Service Principal Id>"
tenantId = "<Tenant Id>"
clientSecret = "<Service Principal Secret>"
subscription_id = "<Subscription Id>"
resource_group = "<Resource Group Name>"
databricks_workspace = "<Databricks Workspace Name>"
dbricks_location = "<Databricks Azure Region i.e. westus>"



# Acquire a token to authenticate against Azure management API
authority_url = 'https://login.microsoftonline.com/'+tenantId
context = adal.AuthenticationContext(authority_url)
token = context.acquire_token_with_client_credentials(
    resource='https://management.core.windows.net/',
    client_id=clientId,
    client_secret=clientSecret
)
azToken = token.get('accessToken')



# Acquire a token to authenticate against the Azure Databricks Resource
token = context.acquire_token_with_client_credentials(
    resource="2ff814a6-3304-4ab8-85cb-cd0e6f879c1d",
    client_id=clientId,
    client_secret=clientSecret
)
adbToken = token.get('accessToken')


# Format Request API Url
dbricks_api = "https://{}.azuredatabricks.net/api/2.0".format(dbricks_location)


# Request Authentication
dbricks_auth = {
    "Authorization": "Bearer {}".format(adbToken),
    "X-Databricks-Azure-SP-Management-Token": azToken,
    "X-Databricks-Azure-Workspace-Resource-Id": ("/subscriptions/{}/resourceGroups/{}/providers/Microsoft.Databricks/workspaces/{}".format(subscription_id, resource_group, databricks_workspace) )
    }


# Optional Paramters 
payload = {
    "comment": "This token is generated through AAD and Databricks APIs", # optional parameter
    # "lifetime_seconds": 3600 # optional parameter. If not passed then it is indefinte
}


# Request and Send Data to Create a Databricks Token
data = requests.post("{}/token/create".format(dbricks_api), headers= dbricks_auth, json=payload)

# display the response data
data.status_code
data.content

# Decode response, get token, and print token
dict_content = json.loads(data.content.decode('utf-8'))
token = dict_content.get('token_value')
print("This is the databricks token: {}".format(token))
like image 132
Ryan Avatar answered Oct 22 '22 19:10

Ryan


This isn't possible today. It is a requested feature here on uservoice https://feedback.azure.com/forums/909463-azure-databricks/suggestions/35257819-expose-api-key-during-arm-deployment

(Please upvote)

Currently you have to log into the web UI manually and generate a token. Even the REST API doesn't support this.

like image 37
simon_dmorias Avatar answered Oct 22 '22 21:10

simon_dmorias


You can actually use azure.databricks.cicd.tools in your CD pipeline to create a new bearer token.

You need to use Connect-Databricks to connect to your workspace first. I usually use the AADwithOrgId method to authenticate to the Databricks workspace:

Connect-Databricks -Region <String> -ApplicationId <String> -Secret <String> -DatabricksOrgId <String> -TenantId <String>

The service principal should have the Contributor role on your resource group and be added a an admin in your Databricks workspace.

like image 1
Nadine Raiss Avatar answered Oct 22 '22 21:10

Nadine Raiss