common.prefect-get-api-token.sh
Overview
prefect-get-api-token.sh is a Bash helper script for obtaining an OIDC access token and exporting it as the Prefect API key for the current shell environment.
The script supports both environment-variable based input and interactive prompts. It obtains the token endpoint and user credentials, sends a password-grant request with curl, extracts the returned access token, exports it as PREFECT_API_KEY, removes the original credential variables, and reports whether login succeeded.
Requirements
The script requires:
- Bash
curlgrepwith Perl-compatible regular-expression support (grep -P)
The script starts with:
#!/bin/bash
Environment Variables
TOKEN_ENDPOINT
OIDC token endpoint used for the authentication request.
If the variable is not already set, the script prompts:
OIDC token endpoint:
PREFECT_USER
Username used for authentication.
If the variable is not already set, the script prompts:
Username:
PREFECT_PASSWORD
Password used for authentication.
If the variable is not already set, the script prompts:
Password:
The password is read with read -sp, so interactive input is not echoed to the terminal.
PREFECT_API_KEY
The access token returned by the OIDC endpoint is exported to:
PREFECT_API_KEY
This makes the token available to Prefect processes started from the same shell environment.
Execution Flow
1. Resolve the token endpoint
if [[ -z "${TOKEN_ENDPOINT}" ]]; then
read -p 'OIDC token endpoint: ' TOKEN_ENDPOINT
fi
If TOKEN_ENDPOINT is empty or unset, the user is prompted to enter it.
2. Resolve the username
if [[ -z "${PREFECT_USER}" ]]; then
read -p 'Username: ' PREFECT_USER
fi
If PREFECT_USER is empty or unset, the user is prompted to enter it.
3. Resolve the password
if [[ -z "${PREFECT_PASSWORD}" ]]; then
read -sp 'Password: ' PREFECT_PASSWORD
echo
fi
If PREFECT_PASSWORD is empty or unset, the user is prompted for it without terminal echo.
4. Request an access token
The script sends a form-encoded request with curl:
curl \
-s \
-d "client_id=wdm-orion" \
-d "username=$PREFECT_USER" \
-d "password=$PREFECT_PASSWORD" \
-d "grant_type=password" \
"$TOKEN_ENDPOINT"
The request contains:
| Field | Value |
|---|---|
client_id |
wdm-orion |
username |
value of PREFECT_USER |
password |
value of PREFECT_PASSWORD |
grant_type |
password |
The response is piped to grep to extract the value of access_token.
5. Export the Prefect API key
The extracted token is assigned to:
export PREFECT_API_KEY=...
The token becomes available to child processes launched from the current shell environment.
6. Remove credential variables
After the token request, the script removes the original variables:
unset PREFECT_USER PREFECT_PASSWORD TOKEN_ENDPOINT
PREFECT_API_KEY remains exported.
7. Report login result
The script checks whether PREFECT_API_KEY is empty.
If no token was extracted:
Login failed.
Otherwise:
Login succeeded.
Usage
Interactive usage
To keep the exported API key in the current shell, source the script:
source common/prefect-get-api-token.sh
The script then prompts for:
- OIDC token endpoint
- username
- password
Environment-variable based usage (please insert username and passwords in the respective fields)
The required values may also be defined beforehand:
export TOKEN_ENDPOINT="https://example.org/token"
export PREFECT_USER=
export PREFECT_PASSWORD=
source common/prefect-get-api-token.sh
If these values are already set, the corresponding prompts are skipped.
Interface Reference
TOKEN_ENDPOINT
Purpose
Specifies the OIDC token endpoint.
Required
Yes.
Input Sources
- existing environment variable
- interactive prompt
Lifetime
Unset by the script after the authentication request.
PREFECT_USER
Purpose
Specifies the username used for authentication.
Required
Yes.
Input Sources
- existing environment variable
- interactive prompt
Lifetime
Unset by the script after the authentication request.
PREFECT_PASSWORD
Purpose
Specifies the password used for authentication.
Required
Yes.
Input Sources
- existing environment variable
- hidden interactive prompt
Lifetime
Unset by the script after the authentication request.
PREFECT_API_KEY
Purpose
Stores the access token returned by the OIDC endpoint.
Produced By
The curl authentication request and token-extraction pipeline.
Exported
Yes.
Lifetime
Remains available in the current shell environment after the script finishes, provided the script was sourced.
Token Extraction
The script extracts the token with:
grep -Po 'access_token":"\K(.*?)(?=")'
This assumes that the response contains an access_token field in a format matching that expression.
For example:
{
"access_token": "example-token"
}
The extracted value becomes PREFECT_API_KEY.
Operational Considerations
Source the script when the API key must persist
Running:
./prefect-get-api-token.sh
executes the script in a child shell. Variables exported there disappear when that process exits.
To retain PREFECT_API_KEY in the current terminal session, use:
source prefect-get-api-token.sh
or:
. prefect-get-api-token.sh
Password visibility
Interactive password input is hidden because read -s is used. If PREFECT_PASSWORD is supplied as an environment variable, it exists in plaintext in the environment until the script unsets it.
Silent curl mode
The request uses curl -s, which suppresses progress and error output. Network or TLS failures may therefore provide limited diagnostics.
HTTP status handling
The script does not explicitly inspect the HTTP status code. Login success is determined only by whether a token string was extracted into PREFECT_API_KEY.
JSON parsing
The response is parsed with grep rather than a JSON parser. The script therefore depends on the response formatting matching the regular expression.
Password grant
The request uses:
grant_type=password
The identity provider must support this grant type for the script to work.
Exported API key
PREFECT_API_KEY remains in the shell environment after successful authentication. It can later be removed with:
unset PREFECT_API_KEY