Need to see job creator when investigating a job but can only see service principal

Leverage the API to retrieve job creator details.

Written by raahat.varma

Last published at: December 12th, 2024

Problem

When trying to investigate job failures or resource consumption in an automated process, you need to identify the actual user or system that initiated a job, but are only able to see the service principal. 

 

Cause

Service principals are commonly used for automation and identity management in place of a specific user or group identity.  

 

Solution

Use the Databricks API to retrieve job creator details under the service principal. The following script fetches the job details, including the job creator's information, when provided with a job ID.

For more information, review the Get a single job API documentation.

 

import requests
import json

# Databricks API information
DATABRICKS_HOST = "<your-Databricks-instance>"  # Replace with your Databricks instance URL
DATABRICKS_TOKEN = "<your-Databricks-access-token>"  # Replace with your Databricks access token
JOB_ID = "<your Databricks-job-id>"  # Replace with your Databricks job ID

# API URL for fetching job details
api_url = f"{DATABRICKS_HOST}/api/2.1/jobs/get"

# Headers with authorization
headers = {
    "Authorization": f"Bearer {DATABRICKS_TOKEN}",
    "Content-Type": "application/json"
}

# Request payload
payload = {
    "job_id": JOB_ID
}

# Make the API request
response = requests.get(api_url, headers=headers, params=payload)

# Check if the request was successful
if response.status_code == 200:
    job_details = response.json()
    
    # Fetch the creator details from the response
    creator_user_name = job_details.get("creator_user_name", "Creator not found")
    
    print(f"Job ID: {JOB_ID}")
    print(f"Job Creator: {creator_user_name}")
else:
    print(f"Failed to fetch job details. Status Code: {response.status_code}")
    print(f"Error: {response.text}")