【问题标题】:Databricks API 2.0- Create Secret Scope - TEMPORARILY_UNAVAILABLEDatabricks API 2.0 - 创建秘密范围 - TEMPORARILY_UNAVAILABLE
【发布时间】:2020-11-24 12:05:03
【问题描述】:

我正在自动部署包含 Azure Databricks 实例的基础结构。为了能够在 Databricks 中使用 Azure Blob 存储,我想在运行 Python 作业的 DevOps Pipeline 中通过 Databricks REST API 2.0 创建一个 Secret Scope。

当我尝试创建秘密范围时,我得到了响应

{"message":"身份验证暂时不可用。请稍后再试。", "error_code": "TEMPORARILY_UNAVAILABLE"}

我已经能够使用 API 创建一个 databricks 访问令牌,即端点 /token/create 运行良好。

我正在使用此问题答案中的代码对数据块进行身份验证:https://stackoverflow.com/a/61826488/2196531
这就是我能够创建令牌以及尝试生成范围的方式:

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_url = "<Databricks Azure URL>"

# 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://{}/api/2.0".format(dbricks_url)

# 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) )
    }

# Creating a databricks token
payload = {
    "comment": "This token is created by API call"
}
requests.post(f"{dbricks_api}/token/create", headers=dbricks_auth, json=payload)
# works

# Creating a databricks secret scope
payload = {
    "scope": "my-databricks-secret-scope",
    "initial_manage_principal": "users"
}
requests.post(f"{dbricks_api}/secrets/scopes/create", headers=dbricks_auth, json=payload)
# returns {"message":"Authentication is temporarily unavailable. Please try again later.", "error_code": "TEMPORARILY_UNAVAILABLE"}

Databricks 正在西欧运行。
Python 3.8.5 x64
代码中用到的包sn -p

  • adal-1.2.4
  • requests-2.24.0

databricks API 有问题还是我做错了什么?

【问题讨论】:

  • 请尝试使用个人访问令牌来创建秘密范围。
  • 是的!这样可行。非常感谢!我只需将 dbricks_auth["Authorization"] = f"Bearer {token}" 设置为令牌作为创建的个人令牌。当您从您的评论中做出回答时,我会接受它,否则我将自己回答。
  • 我已经总结了我的建议作为答案。另外,既然对你有用,你能接受它作为答案吗?

标签: python azure databricks azure-databricks


【解决方案1】:

根据我的测试,当我们使用 Databricks Rest API 创建 Secret Scope 时,我们应该使用 person access token。

例如

  1. 创建服务主体
az login
az ad sp create-for-rbac -n "MyApp"
  1. 代码
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_url = "<Databricks Azure URL>"

# 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://{}/api/2.0".format(dbricks_url)

# 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) )
    }

# Creating a databricks token
payload = {
    "lifetime_seconds": 3600, # the token lifetime
    "comment": "This token is created by API call"
}

data =requests.post(f"{dbricks_api}/token/create", headers=dbricks_auth, json=payload)
dict_content = json.loads(data.content.decode('utf-8'))
token = dict_content.get('token_value')


payload = {
    "scope": "my-databricks-secret-scope",
    "initial_manage_principal": "users"
}
res=requests.post(f"{dbricks_api}/secrets/scopes/create", headers={
    "Authorization": "Bearer {}".format(token),
    }, json=payload)

print(res.status_code)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-25
    • 2022-08-11
    • 1970-01-01
    • 2022-01-18
    • 2021-10-26
    • 2021-12-23
    相关资源
    最近更新 更多