auth 命令组

注意

此信息适用于 Databricks CLI 0.205 及更高版本,这些版本为公共预览版。 若要查找你的 Databricks CLI 的版本,请运行 databricks -v

使用 Databricks CLI 中的 auth 命令组可以执行以下操作:

  • 列出任何可用的身份验证配置文件。
  • 获取有关个人身份验证配置文件的信息。
  • 使用 OAuth 用户到计算机 (U2M) 身份验证通过 Databricks CLI 对 Azure Databricks 帐户和工作区进行身份验证。
  • 获取有关任何 OAuth 访问令牌(可能已由 Databricks CLI 缓存)的信息。
  • 获取 Databricks CLI 用于进行身份验证的配置的详细信息。

重要

在使用 Databricks CLI 之前,请务必设置 Databricks CLI为 Databricks CLI 设置身份验证

可以通过将 auth 命令追加到 databricks auth 中来运行这些命令。 若要显示 auth 命令的帮助,请运行 databricks auth -h

列出配置文件

若要获取所有可用配置文件的列表并检查它们是否有效,请运行 auth profiles 命令,如下所示:

databricks auth profiles

输出(为简洁起见,使用省略号来代表省略的内容):

Name            Host                Valid
DEFAULT         https://<host-url>  YES
<profile-name>  https://<host-url>  NO

为了确定每个配置文件是否有效,Databricks CLI 对每个帐户级配置文件运行 list workspaces 命令,并对每个工作区级配置文件运行 get current user 命令。 如果命令成功,将显示 YES;否则,将显示 NO

auth profiles 命令的输出不显示任何访问令牌。 若要显示访问令牌,请参阅获取有关配置文件的信息

默认情况下,配置文件存储在 Linux 或 macOS 上的文件 ~/.databrickscfg 中,或 Windows 上的 %USERPROFILE%\.databrickscfg 中。 可以通过设置环境变量 DATABRICKS_CONFIG_FILE 来更改此文件的默认路径。 若要了解如何设置环境变量,请参阅操作系统的文档。

若要创建配置文件,请参阅 configure 命令组

获取有关配置文件的信息

若要获取有关现有配置文件的信息,请运行 auth env 命令,其中 <profile-name> 代表配置文件的名称,<account-or-workspace-url> 代表 Azure Databricks 帐户控制台 URL 或 Azure Databricks 工作区 URL,如下所示:

databricks auth env --profile <profile-name>

# Or:
databricks auth env --host <account-or-workspace-url>

提示

可以在 --profile-p 之后按 Tab 以显示可供选择的现有可用配置文件的列表,而不是手动输入配置文件名称。

例如,下面是使用 Azure Databricks 访问令牌身份验证配置的配置文件的输出:

{
  "env": {
    "DATABRICKS_AUTH_TYPE": "pat",
    "DATABRICKS_CONFIG_PROFILE": "<profile-name>",
    "DATABRICKS_HOST": "<workspace-url>",
    "DATABRICKS_TOKEN": "<token-value>"
  }
}

注意

如果多个配置文件与 --host 值匹配,则将显示一个错误,指出找不到单个匹配的配置文件。 例如,你可能有一个仅具有主机值的配置文件,而另一个配置文件具有相同的主机值和令牌值。 在这种情况下,Databricks CLI 不会选择配置文件并停止。 为了帮助 Databricks CLI 选择所需的配置文件,请尝试指定不同的 --host 值。 对于作为帐户控制台 URL 的 --host 值,请尝试指定 --account-id 值而不是 --host 值。

若要创建配置文件,请参阅 configure 命令组

使用 OAuth 进行身份验证

可以使用 OAuth 用户到计算机 (U2M) 身份验证,而不使用访问令牌和配置文件通过 Azure Databricks 进行身份验证。 OAuth 提供的令牌比 Azure Databricks 个人访问令牌的过期时间更快,并提供了更好的服务器端会话失效和范围界定。 由于 OAuth 访问令牌会在一小时内过期,因此可降低与意外将令牌签入源代码管理相关的风险。 请参阅 OAuth 用户到计算机 (U2M) 身份验证

若要配置和设置 OAuth U2M 身份验证,请参阅 OAuth 用户到计算机 (U2M) 身份验证

获取 OAuth 访问令牌详细信息

如果要查看有关 Databricks CLI 之前为 Azure Databricks 工作区生成的缓存 OAuth 访问令牌的信息,请运行 auth token 命令,其中 <workspace-url> 代表 Azure Databricks 工作区的 URL,如下所示:

databricks auth token <workspace-url>

输出:

{
  "access_token": "<token-value>",
  "token_type": "Bearer",
  "expiry": "<token-expiration-date-time>"
}

获取身份验证详细信息

若要获取 Databricks CLI 用于进行身份验证的配置的详细信息,请运行 auth describe 命令。

如果未指定任何选项,auth describe 命令将遵循客户端统一身份验证方法和凭据的默认评估顺序

databricks auth describe

输出:

Host: https://<workspace-instance-name>
User: <user-name>@<domain>
Authenticated with: <authentication-type>
-----
Current configuration:
  ✓ host: https://<workspace-instance-name> (from <path>/<to>/.databrickscfg config file)
  ✓ profile: default
  ✓ auth_type: <authentication-type> (from <path>/<to>/.databrickscfg config file)

若要指定使用有关特定 Azure Databricks 工作区的信息,请指定 --host 选项以及工作区的 URL。

databricks auth describe --host https://<workspace-instance-name>

输出:

Host: https://<workspace-instance-name>
User: <user-name>@<domain>
Authenticated with: <authentication-type>
-----
Current configuration:
  ✓ host: https://<workspace-instance-name> (from --host flag)
  ✓ profile: default
  ✓ auth_type: <authentication-type>

若要指定使用有关特定 Azure Databricks 帐户的信息,请指定 --host 选项以及 Azure Databricks 帐户控制台 URL https://accounts.azuredatabricks.net

databricks auth describe --host <account-console-url>

输出:

Host: <account-console-url>
User: <user-name>@<domain>
AccountId: <account-id>
Authenticated with: <authentication-type>
-----
Current configuration:
  ✓ host: <account-console-url> (from --host flag)
  ✓ account_id: <account-id>
  ✓ profile: default
  ✓ auth_type: <authentication-type>

若要指定使用有关特定 Azure Databricks 配置文件的信息,请指定 -p--profile 选项以及配置文件的名称。

databricks auth describe -p <profile-name>

输出:

Host: https://<workspace-instance-name>
User: <user-name>@<domain>
Authenticated with: <authentication-type>
-----
Current configuration:
  ✓ host: https://<workspace-instance-name> (from <path>/<to>/.databrickscfg config file)
  ✓ token: ******** (from <path>/<to>/.databrickscfg config file)
  ✓ profile: <profile-name> (from --profile flag)
  ✓ auth_type: <authentication-type>

若要在输出中包含敏感信息(例如 Azure Databricks 个人访问令牌和客户端密码),请指定 --sensitive 选项。

databricks auth describe --sensitive

输出:

Host: https://<workspace-instance-name>
User: <user-name>@<domain>
Authenticated with: pat
-----
Current configuration:
  ✓ host: https://<workspace-instance-name> (from <path>/<to>/.databrickscfg config file)
  ✓ token: <token-value> (from <path>/<to>/.databrickscfg config file)
  ✓ profile: <profile-name>
  ✓ auth_type: pat