Databricks 個人用アクセス トークン認証
Databricks個人用アクセス トークン (PAT) は、APIs Databricksワークスペース レベルでリソースと へのアクセスを認証するために使用されます。資格情報と関連情報 (環境変数や Databricks 構成プロファイルなど) の多くのストレージ メカニズムは、Databricks の個人用アクセス トークンのサポートを提供します。 ユーザーは 1 つの Databricks ワークスペースに複数の個人用アクセス トークンを持つことができますが、各個人用アクセス トークンは 1 つの Databricks ワークスペースに対してのみ機能します。 ユーザーあたりの個人用アクセス トークンの数は、ワークスペースあたり 600 に制限されています。
Databricks は、90 日以上使用されていない個人用アクセス トークンを自動的に取り消します。
Databricks では、OAuth のセキュリティが向上しているため、ユーザー アカウント クライアントの認証と承認に PAT の代わりに OAuth を使用することをお勧めします。 OAuth を使用して Databricks ユーザーアカウントでクライアント認証を実行する方法については、「 OAuth を使用してユーザーアカウントで Databricks リソースへの対話型アクセスを承認 する (ユーザーアカウント認証用)」を参照してください。
Databricks のユーザー名とパスワードを使用した基本 (トークンベースではない) 認証は、2024 年 7 月 10 日にサポートが終了しました。
Databricksアカウント レベルの機能を自動化するには、 Databricks個人用アクセストークンは使用できません。代わりに、Databricksアカウント管理者ユーザーまたはサービスプリンシパルのOAuthトークンを使用する必要があります。詳細については、以下を参照してください。
ワークスペース ユーザー用の Databricks 個人用アクセス トークン
DatabricksワークスペースユーザーのDatabricks個人用アクセストークンを作成するには、次の手順を実行します。
-
Databricksワークスペースで、上部のバーに表示されているDatabricksユーザー名をクリックし、ドロップダウンから [ 設定 ] を選択します。
-
[開発者] をクリックします。
-
[アクセストークン] の横にある [管理] をクリックします。
-
[新規トークンを生成] をクリックします。
-
今後、このトークンを識別するのに役立つコメントを入力します。
-
トークンの有効期間を日数で設定します。
[有効期間 (日)] ボックスを空のままにすると、トークンの有効期間はワークスペースの最大有効期間に設定されます。デフォルトでは、ワークスペースの最大トークン有効期間は 730 日です。 「新しい個人用アクセス トークンの最大有効期間を設定する」を参照してください。
-
[生成] をクリックします。
-
表示されたトークンを安全な場所にコピーし、 [完了] をクリックします。
コピーしたトークンは必ず安全な場所に保存してください。コピーしたトークンを他のユーザーと共有しないでください。コピーしたトークンを紛失した場合、まったく同じトークンを再生成することはできません。代わりに、この手順を繰り返して新しいトークンを作成する必要があります。コピーしたトークンを紛失した場合、またはトークンが漏洩したと思われる場合は、 [アクセストークン] ページでトークンの横にあるゴミ箱( [取り消し] )アイコンをクリックして、直ちにそのトークンをワークスペースから削除することを強くお勧めします。
ワークスペースでトークンを作成または使用できない場合は、ワークスペース管理者がトークンを無効にしているか、トークンを作成または使用する権限を付与していない可能性があります。 ワークスペース管理者または次のトピックを参照してください。
Databricks personal access token for サービスプリンシパル
ステップ 1:Databricks Databricks管理者として、 から サービスプリンシパルの PAT を作成しますCLI
ワークスペース管理者は、次のようにDatabricks を使用して、サービスプリンシパルの代わりに 個人用アクセストークンを作成できます。CLI
-
Databricks CLIの認証をまだ設定していない場合は、設定します。
-
DatabricksサービスプリンシパルのアプリケーションIDを取得します(まだ取得していない場合)。
- ワークスペースの管理コンソールがまだ開いていない場合は、ワークスペースの上部のバーにあるユーザー名をクリックし、 [設定] をクリックします。
- [ ワークスペース管理 ]で、[ IDとアクセス ]をクリックします。
- サービスプリンシパル の横にある [ 管理 ] をクリックします。
- Databricksサービスプリンシパルの名前をクリックして、その設定ページを開きます。名前が表示されない場合は、 サービスプリンシパルのフィルター を使用して検索してください。
- [ 構成 ]タブで、 アプリケーションID の値を書き留めます。
-
Databricks CLIを使用して次のコマンドを実行します。これにより、Databricksサービスプリンシパルのアクセストークンが生成されます。
次のコマンドを実行します。
Bashdatabricks token-management create-obo-token <application-id> --lifetime-seconds <lifetime-seconds> --comment <comment> -p <profile-name>
<application-id>
をDatabricksサービスプリンシパルのアプリケーションIDに置き換えます。--lifetime-seconds
:<lifetime-seconds>
を、アクセス トークンの有効期間を秒数に置き換えます。 たとえば、1 日は 86400 秒です。--lifetime-seconds
オプションが指定されていない場合、アクセス トークンはワークスペースの最大有効期間に設定されます。デフォルトでは、ワークスペースの最大トークン有効期間は 730 日です。--comment
:<comment>
をアクセス トークンの目的に関する意味のあるコメントに置き換えます。--comment
オプションが指定されていない場合、コメントは生成されません。--profile-name
:<profile-name>
を、Databricks サービスプリンシパルとターゲット ワークスペースの認証情報を含む Databricks 構成プロファイルの名前に置き換えます。-p
オプションが指定されていない場合、Databricks CLI はDEFAULT
という名前の構成プロファイルを検索して使用しようとします。
-
レスポンス内の、Databricksサービスプリンシパルのアクセストークンである
token_value
の値をコピーします。コピーしたトークンは必ず安全な場所に保存してください。コピーしたトークンを他のユーザーと共有しないでください。コピーしたトークンを紛失した場合、まったく同じトークンを再生成することはできません。代わりに、この手順を繰り返して新しいトークンを作成する必要があります。
ワークスペースでトークンを作成または使用できない場合は、ワークスペース管理者がトークンを無効にしたか、トークンを作成または使用する権限を与えていないことが原因である可能性があります。ワークスペース管理者に問い合わせるか、以下をご覧ください。
ステップ 2: サービスプリンシパルの追加の PAT を作成する
Databricks サービスプリンシパルの PAT を作成したら、その PAT を使用して、次のようにサービスプリンシパルの追加の Databricks personal access token を作成できます。
この手順では、Databricks サービスプリンシパルの最初の Databricks personal access トークンがすでに生成されていることを前提としています。このアクセス トークンを使用して、DatabricksCLI Databricksサービス プリンシパルを認証するように を設定し、その後、サービスプリンシパル自体の追加のアクセス トークンを生成できるようにします。「Databricks 個人用アクセス トークン認証」を参照してください。
-
Databricks CLIを使用して次のコマンドを実行すると、Databricksサービスプリンシパルに対する別のアクセストークンが生成されます。
次のコマンドを実行します。
Bashdatabricks tokens create --comment <comment> --lifetime-seconds <lifetime-seconds> -p <profile-name>
--comment
:<comment>
をアクセス トークンの目的に関する意味のあるコメントに置き換えます。--comment
オプションが指定されていない場合、コメントは生成されません。--lifetime-seconds
:<lifetime-seconds>
を、アクセス トークンの有効期間を秒数に置き換えます。 たとえば、1 日は 86400 秒です。--lifetime-seconds
オプションが指定されていない場合、アクセス トークンはワークスペースの最大有効期間に設定されます。デフォルトでは、ワークスペースの最大トークン有効期間は 730 日です。--profile-name
:<profile-name>
を、Databricks サービスプリンシパルとターゲット ワークスペースの認証情報を含む Databricks 構成プロファイルの名前に置き換えます。-p
オプションが指定されていない場合、Databricks CLI はDEFAULT
という名前の構成プロファイルを検索して使用しようとします。
-
レスポンス内の、Databricks サービスプリンシパルのアクセストークンである
token_value
の値をコピーします。コピーしたトークンは必ず安全な場所に保存してください。コピーしたトークンを他のユーザーと共有しないでください。コピーしたトークンを紛失した場合、まったく同じトークンを再生成することはできません。代わりに、この手順を繰り返して新しいトークンを作成する必要があります。
ワークスペースでトークンを作成または使用できない場合は、ワークスペース管理者がトークンを無効にしたか、トークンを作成または使用する権限を与えていないことが原因である可能性があります。ワークスペース管理者に問い合わせるか、以下をご覧ください。
Databricks の個人用アクセス トークン認証を実行する
Databricks個人用アクセストークン認証を構成するには、以下に挙げられた関連する環境変数、.databrickscfg
フィールド、Terraformフィールド、またはConfig
フィールドを設定する必要があります。
- ターゲット Databricks ワークスペース URL として指定される Databricks ホスト (例:
https://dbc-a1b2345c-d6e7.cloud.databricks.com
. - DatabricksユーザーアカウントまたはDatabricksサービスプリンシパルのDatabricks個人用アクセストークン。
Databricks個人用アクセストークン認証を実行するには、参加しているツールまたはSDKに基づいてコード内に以下を統合します。
- Environment
- Profile
- CLI
- Connect
To use environment variables for a specific Databricks authentication type with a tool or SDK, see Authorizing access to Databricks resources or the tool’s or SDK’s documentation. See also Environment variables and fields for client unified authentication and the Default methods for client unified authentication.
Set the following environment variables:
DATABRICKS_HOST
, set to the Databricks workspace URL, for examplehttps://dbc-a1b2345c-d6e7.cloud.databricks.com
.DATABRICKS_TOKEN
, set to the token string.
Create or identify a Databricks configuration profile with the following fields in your .databrickscfg
file. If you create the profile, replace the placeholders with the appropriate values. To use the profile with a tool or SDK, see Authorizing access to Databricks resources or the tool’s or SDK’s documentation. See also Environment variables and fields for client unified authentication and the Default methods for client unified authentication.
Set the following values in your .databrickscfg
file. In this case, the host is the Databricks workspace URL, for example https://dbc-a1b2345c-d6e7.cloud.databricks.com
:
[<some-unique-configuration-profile-name>]
host = <workspace-url>
token = <token>
Instead of manually setting the preceding values in your .databrickscfg
file, you can use the Databricks CLI to set these values instead, as follows:
The following procedure uses the Databricks CLI to create a Databricks configuration profile with the name DEFAULT
. If you already have a DEFAULT
configuration profile, this procedure overwrites your existing DEFAULT
configuration profile.
To check whether you already have a DEFAULT
configuration profile, and to view this profile’s settings if it exists, use the Databricks CLI to run the command databricks auth env --profile DEFAULT
.
To create a configuration profile with a name other than DEFAULT
, replace the DEFAULT
part of --profile DEFAULT
in the following databricks configure
command with a different name for the configuration profile.
-
Use the Databricks CLI to create a Databricks configuration profile named
DEFAULT
that uses Databricks personal access token authentication. To do this, run the following command:Bashdatabricks configure --profile DEFAULT
-
For the prompt Databricks Host, enter your Databricks workspace instance URL, for example
https://dbc-a1b2345c-d6e7.cloud.databricks.com
. -
For the prompt Personal Access Token, enter the Databricks personal access token for your workspace.
For the Databricks CLI, run the databricks configure
command. At the prompts, enter the following settings:
- The Databricks host, specified as the target Databricks workspace URL, for example
https://dbc-a1b2345c-d6e7.cloud.databricks.com
. - The Databricks personal access token, for the Databricks user account or Databricks service principal.
For more details, see Databricks personal access token authentication.
Databricks personal access token authentication is supported on the following Databricks Connect versions:
-
For Python, Databricks Connect for Databricks Runtime 13.3 LTS and above.
-
For Scala, Databricks Connect for Databricks Runtime 13.3 LTS and above.
For Databricks Connect, you can use the Databricks CLI to set the values in your .databrickscfg
file, for Databricks workspace-level operations as specified in this article’s “Profile” section, as follows:
The following procedure uses the Databricks CLI to create a Databricks configuration profile with the name DEFAULT
. If you already have a DEFAULT
configuration profile, this procedure overwrites your existing DEFAULT
configuration profile.
To check whether you already have a DEFAULT
configuration profile, and to view this profile’s settings if it exists, use the Databricks CLI to run the command databricks auth env --profile DEFAULT
.
To create a configuration profile with a name other than DEFAULT
, replace the DEFAULT
part of --profile DEFAULT
in the databricks configure
command as shown in the following step with a different name for the configuration profile.
-
Use the Databricks CLI to create a Databricks configuration profile named
DEFAULT
that uses Databricks personal access token authentication. To do this, run the following command:Bashdatabricks configure --configure-cluster --profile DEFAULT
-
For the prompt Databricks Host, enter your Databricks workspace instance URL, for example
https://dbc-a1b2345c-d6e7.cloud.databricks.com
. -
For the prompt Personal Access Token, enter the Databricks personal access token for your workspace.
-
In the list of available clusters that appears, use your up arrow and down arrow keys to select the target Databricks cluster in your workspace, and then press
Enter
. You can also type any part of the cluster’s display name to filter the list of available clusters.
Databricks REST API を使用して個人用アクセス トークンを発行する
Databricks には、PAT を発行するための REST エンドポイント /api/2.0/token/create
が用意されています。 API の詳細については 、「ユーザー トークンの作成 」を参照してください。
REST API には特定の値を指定する必要があります。 次の例では、次の値を設定します。
<databricks-instance>
を Databricks ワークスペースの URL に置き換えます。たとえば、dbc-abcd1234-5678.cloud.databricks.com
.<your-existing-access-token>
を、新しいトークンを作成する権限を持つ既存の有効な PAT (文字列) に置き換えます。
次のパラメーターの値を指定します。
comment
: 新しいトークンの説明。lifetime_seconds
: トークンの有効期間 (秒単位)。
curl -X POST https://<databricks-instance>/api/2.0/token/create \
-H "Authorization: Bearer <your-existing-access-token>" \
-H "Content-Type: application/json" \
-d '{
"comment": "New PAT using DB API",
"lifetime_seconds": <lifetime-of-pat-in-seconds>
}'
-d
フラグは、要求の JSON ペイロードを提供します。
成功すると、次のような応答ペイロードが生成されます。
{
"access_token": "<your-newly-issued-pat>",
"token_type": "Bearer",
"expires_in": <the-duration-of-the-new-pat>
}
応答からの新しいトークンを、後続の Databricks REST APIsへの呼び出しの Authorization ヘッダーに指定します。 例えば:
# This example uses a simple GET. For POST or other REST verbs, you may need to provide additional parameters.
curl -X GET "https://<databricks-instance>/api/2.0/<path-to-endpoint>" \
-H "Authorization: Bearer <your-new-pat>"
import requests
headers = {
'Authorization': 'Bearer <your-new-pat>'
}
# This example is for an HTTP GET operation.
response = requests.get('https://<databricks-instance>/api/2.0/<path-to-endpoint>', headers=headers)