<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Generate longer token for Databricks with Azure. in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/generate-longer-token-for-databricks-with-azure/m-p/12619#M7404</link>
    <description>&lt;P&gt;I made up an alternative solution. I made up my own python class to handle my PAT from Databricks : &lt;A href="https://stackoverflow.com/questions/75071869/python-defaultazurecredential-get-token-set-expiration-or-renew-token/" target="test_blank"&gt;https://stackoverflow.com/questions/75071869/python-defaultazurecredential-get-token-set-expiration-or-renew-token/&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You can be fancier or even register an atexit inside the class to destroy the PAT. But this will have a side effect. The python process will exit with no error code, but if you have a logger, it will warn you that connection with databricks are closed because of invalid token. Which is "normal", but ugly.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 16 Jan 2023 16:15:21 GMT</pubDate>
    <dc:creator>Etyr</dc:creator>
    <dc:date>2023-01-16T16:15:21Z</dc:date>
    <item>
      <title>Generate longer token for Databricks with Azure.</title>
      <link>https://community.databricks.com/t5/data-engineering/generate-longer-token-for-databricks-with-azure/m-p/12614#M7399</link>
      <description>&lt;P&gt;I'm using&amp;nbsp;&lt;A href="https://learn.microsoft.com/en-us/python/api/azure-identity/azure.identity.defaultazurecredential?view=azure-python" alt="https://learn.microsoft.com/en-us/python/api/azure-identity/azure.identity.defaultazurecredential?view=azure-python" target="_blank"&gt;DefaultAzureCredential&lt;/A&gt; from&amp;nbsp;azure-identity&amp;nbsp;to connect to Azure with service principal environment variables (AZURE_CLIENT_SECRET, AZURE_TENANT_ID, AZURE_CLIENT_ID).&lt;/P&gt;&lt;P&gt;I can&amp;nbsp;get_token&amp;nbsp;from a specific scope for databricks like this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;from azure.identity import DefaultAzureCredential
&amp;nbsp;
dbx_scope = "2ff814a6-3304-4ab8-85cb-cd0e6f879c1d/.default"
token = DefaultAzureCredential().get_token(dbx_scope).token&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;So this is working great, I get the token, and then I can use `databricks-connect` to configure my connection to the cluster. This generates me a configuration ($HOME/.databricks-connect) file for Spark to know where to connect and use the given token.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;{
  "host": "https://adb-1234.azuredatabricks.net",
  "token": "eyJ0eXAiXXXXXXXXXXXXXXXXXXXXXx",
  "cluster_id": "1234",
  "org_id": "1234",
  "port": "15001"
}&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The issue is that this token does not last very long. When I use spark for more than an hour, I get disconnected because the token is expired.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Is there a way to get a longer token for databricks with a Service Principal ? Since this aim to be for production, I wish my code could generate a PAT for any run, I don't want to create a PAT manually and store it to an Azure Key Vault.&lt;/P&gt;</description>
      <pubDate>Wed, 11 Jan 2023 14:15:32 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/generate-longer-token-for-databricks-with-azure/m-p/12614#M7399</guid>
      <dc:creator>Etyr</dc:creator>
      <dc:date>2023-01-11T14:15:32Z</dc:date>
    </item>
    <item>
      <title>Re: Generate longer token for Databricks with Azure.</title>
      <link>https://community.databricks.com/t5/data-engineering/generate-longer-token-for-databricks-with-azure/m-p/12615#M7400</link>
      <description>&lt;P&gt;There is REST API endpoint to manage tokens:&lt;/P&gt;&lt;P&gt;&lt;A href="https://docs.databricks.com/dev-tools/api/latest/token-management.html" target="test_blank"&gt;https://docs.databricks.com/dev-tools/api/latest/token-management.html&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So using your code, you get the host and a short token. So all you need to do is construct Rest API, which will generate long-term connections.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Create a token on behalf of a service principal. &amp;gt;&amp;gt; &lt;A href="https://docs.databricks.com/dev-tools/api/latest/token-management.html#operation/create-obo-token" target="test_blank"&gt;https://docs.databricks.com/dev-tools/api/latest/token-management.html#operation/create-obo-token&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 11 Jan 2023 15:44:07 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/generate-longer-token-for-databricks-with-azure/m-p/12615#M7400</guid>
      <dc:creator>Hubert-Dudek</dc:creator>
      <dc:date>2023-01-11T15:44:07Z</dc:date>
    </item>
    <item>
      <title>Re: Generate longer token for Databricks with Azure.</title>
      <link>https://community.databricks.com/t5/data-engineering/generate-longer-token-for-databricks-with-azure/m-p/12616#M7401</link>
      <description>&lt;P&gt;This issue with this (I think) is that it will create a new token for each run of my code in Azure ML. So if I get over 600 runs, I generate 600 PAT and that's the Databricks limit of PATs. The next ones wont be able to create new tokens and runs would be stucks.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Is there a way to remove "old" PAT for exemple PAT that are older than 24 hours?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I was thinking of a solution that kept the host short token. Every X minutes I ask for a new token, but I have to re init my sparksession and loose all the work. Isn"t a way to inject the token in spark.config ?&lt;/P&gt;&lt;P&gt;Something like this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;spark_session.conf.set("spark.some.option.otken", new_token)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 11 Jan 2023 15:53:54 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/generate-longer-token-for-databricks-with-azure/m-p/12616#M7401</guid>
      <dc:creator>Etyr</dc:creator>
      <dc:date>2023-01-11T15:53:54Z</dc:date>
    </item>
    <item>
      <title>Re: Generate longer token for Databricks with Azure.</title>
      <link>https://community.databricks.com/t5/data-engineering/generate-longer-token-for-databricks-with-azure/m-p/12617#M7402</link>
      <description>&lt;P&gt;there is API calls to delete or manage so you can implement own logic&lt;/P&gt;</description>
      <pubDate>Wed, 11 Jan 2023 15:55:03 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/generate-longer-token-for-databricks-with-azure/m-p/12617#M7402</guid>
      <dc:creator>Hubert-Dudek</dc:creator>
      <dc:date>2023-01-11T15:55:03Z</dc:date>
    </item>
    <item>
      <title>Re: Generate longer token for Databricks with Azure.</title>
      <link>https://community.databricks.com/t5/data-engineering/generate-longer-token-for-databricks-with-azure/m-p/12619#M7404</link>
      <description>&lt;P&gt;I made up an alternative solution. I made up my own python class to handle my PAT from Databricks : &lt;A href="https://stackoverflow.com/questions/75071869/python-defaultazurecredential-get-token-set-expiration-or-renew-token/" target="test_blank"&gt;https://stackoverflow.com/questions/75071869/python-defaultazurecredential-get-token-set-expiration-or-renew-token/&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You can be fancier or even register an atexit inside the class to destroy the PAT. But this will have a side effect. The python process will exit with no error code, but if you have a logger, it will warn you that connection with databricks are closed because of invalid token. Which is "normal", but ugly.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 16 Jan 2023 16:15:21 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/generate-longer-token-for-databricks-with-azure/m-p/12619#M7404</guid>
      <dc:creator>Etyr</dc:creator>
      <dc:date>2023-01-16T16:15:21Z</dc:date>
    </item>
  </channel>
</rss>

