<@ULVA73B9P> Show me the python script to print th...
# ask-marvin
c
@Marvin Show me the python script to print the name of the service account that is being used
m
The Prefect documentation does not provide a direct Python script to print the name of the service account used. However, in Prefect, service accounts are typically associated with an API key that does not correspond to a user account and is used for programmatic access to Prefect Cloud or for running workers/executing flow runs on remote infrastructure. If you want to obtain the name of a service account programmatically, you would typically need to interact with the Prefect Cloud API, which would involve authenticating with the API using your service account's API key. From there, you could use the API to query the service account details, including its name. The documentation suggests that after creating the service account, you view and manage service accounts through the Prefect Cloud UI. There's no excerpt that includes a specific API call or Python script for obtaining the name of a service account directly. If you already have a service account's API key, you can set it as the
PREFECT_API_KEY
environment variable or configure it in your flow script to use Prefect Cloud. The service account's name isn't typically necessary for interacting with the API, but if you want to display it, you might need to use Prefect's GraphQL API to query the details of your service account, which goes beyond the excerpts provided. For more specific details, you would need to consult the Prefect GraphQL API documentation or possibly use the Prefect Python client inside your scripts to interact with the API. If you can provide more context, or if you need to see how to authenticate and use the Prefect Cloud API, please let me know, and I'll do my best to assist you further.
n
hi @Chris Bui - which service account do you mean? do you mean the actor associated with a given API key?
c
yes exactly
n
Copy code
» curl -s -H "Authorization: Bearer $PREFECT_API_KEY" "<https://api.prefect.cloud/api/me/>" | jq
{
  "id": "<uuid>",
  "handle": "stoat",
  "first_name": "Nathan",
  "last_name": "Nowack",
  "email": "<mailto:nate@prefect.io|nate@prefect.io>",
  "personal_account_id": "<uuid>",
  "settings": {
    "default_timezone": "America/Chicago",
    "default_color_palette": "protanomaly",
    "tutorial_completed": true,
    "feature_previews": []
  }
}
thank you 1
or the python
Copy code
» ipython
In [1]: from prefect.client.cloud import get_cloud_client

In [2]: from prefect.settings import PREFECT_CLOUD_API_URL

In [3]: async with get_cloud_client() as client:
   ...:     print((await client._client.get(f"{PREFECT_CLOUD_API_URL.value()}/me")).json())
   ...:
{'id': '<uuid>', 'handle': 'stoat', 'first_name': 'Nathan', 'last_name': 'Nowack', 'email': 'nate@prefect.io', 'personal_account_id': '<uuid>', 'settings': {'default_timezone': 'America/Chicago', 'default_color_palette': 'protanomaly', 'tutorial_completed': True, 'feature_previews': []}}
c
thanks @Nate that one works for me even better!
n
catjam
c
@Nate this works just fine when i’m running it locally, but looks a service account can’t access this endpoint
Copy code
File "/usr/local/lib/python3.9/site-packages/httpx/_client.py", line 1786, in get
    return await self.request(
  File "/usr/local/lib/python3.9/site-packages/httpx/_client.py", line 1559, in request
    return await self.send(request, auth=auth, follow_redirects=follow_redirects)
  File "/usr/local/lib/python3.9/site-packages/prefect/client/base.py", line 312, in send
    response.raise_for_status()
  File "/usr/local/lib/python3.9/site-packages/prefect/client/base.py", line 164, in raise_for_status
    raise PrefectHTTPStatusError.from_httpx_error(exc) from exc.__cause__
prefect.exceptions.PrefectHTTPStatusError: Client error '403 Forbidden' for url '<https://api.prefect.cloud/api/me/>'
Response: {'detail': 'Only users (not service accounts) can access this endpoint.'}
Can you help us here?
n
can you explain high level what you’re trying to do?
c
I’m trying to log some information in a database. One piece of that information is “who is the current actor/user in prefect”. We are running our workflows using kubernetes and the pods are set up to connect to the workspace using a service account API key. When i test the python function to hit the endpoint https://api.prefect.cloud/api/me/ locally, i get my own handle “cbui”. However, when the function is running on a k8s pod, it fails with the error message above. In some of our other code, we were able to get the service account name by creating an object (e.g. a deployment) using the api. The results of the api include the fields
created_by.display_value
and
updated_by.display_value
which happen to be the service account name. Is there a way to get this value without having to first create an object in Prefect cloud?
hey @Nate! Any update on this? Please let me know if that explanation doesn’t make sense
n
hey chris - i can get to this later. i dont have time today sorry
feel free to ask in #CL09KU1K7
c
not a problem! I’ll dot hat