<@ULVA73B9P> in my self-hosted Prefect setup, the ...
# ask-marvin
v
@Marvin in my self-hosted Prefect setup, the token I put into PREFECT_API_KEY expires within an hour. It means if I have a long running flow, I can, theoretically, get troubles. Question: Prefect reads PREFECT_API_KEY and PREFECT_CLIENT_CUSTOM_HEADERS from system variables each time it uses them, or it reads once and uses the same values during all the lifetime?
m
thought for 231.4 seconds
Based on my research and examination of the Prefect client implementation, I can provide a clear answer about how Prefect handles these environment variables: In Prefect 3.x, the API key and custom headers are read dynamically each time a new client connection is established. This is handled through the client implementation, which creates new instances when needed rather than maintaining a single long-lived connection. Here's what this means for your scenario: 1. For long-running flows, you don't need to worry about token expiration during a single API call/request. 2. However, each new API call to the Prefect server will: - Create a new client instance - Read the current environment variables - Use the current values of
PREFECT_API_KEY
and
PREFECT_CLIENT_CUSTOM_HEADERS
This design is actually beneficial for your use case because: - If you update the
PREFECT_API_KEY
environment variable during flow execution, subsequent API calls will use the new token - You can implement token rotation without needing to restart your flows - There's no risk of getting "stuck" with an expired token for the entire flow duration Best Practices: 1. If you're implementing token rotation, make sure to update the environment variable with the new token 2. Consider implementing a token refresh mechanism that updates the environment variable before the current token expires 3. For very long-running flows, you might want to implement a mechanism to refresh the token periodically during execution This behavior is intentional and provides flexibility for authentication management in long-running processes.
v
@Marvin PREFECT_SERVER_API_AUTH_STRING works only for API? Or for also UI? If only for API, then how deletion/create operations are secured? If works also for UI, then how should I save it in browser? Could you explain a bit?
m
thought for 71.1 seconds
Based on the research and recent GitHub issues, I can provide a comprehensive explanation of how
PREFECT_SERVER_API_AUTH_STRING
works: 1. Scope of Authentication: - The auth string secures BOTH the API and UI access - It implements basic HTTP authentication across all server endpoints - When set, it requires authentication for all operations (create, delete, update, etc.) 2. How it works: - Format:
username:password
(e.g., "admin:secretpassword") - For API access: Used in HTTP Basic Authentication headers - For UI access: Triggers a browser authentication prompt when you first access the UI 3. UI Authentication Flow: - When you first access the UI, your browser will show a native authentication prompt - Once authenticated, the browser will automatically send these credentials with subsequent requests - The credentials are automatically saved by your browser (standard browser behavior for Basic Auth) - You don't need to manually save anything; the browser handles credential persistence 4. Important Implementation Notes: - Don't use quotes when setting the environment variable:
Copy code
bash
     # Correct
     export PREFECT_SERVER_API_AUTH_STRING=admin:secretpassword
     
     # Incorrect (with quotes)
     export PREFECT_SERVER_API_AUTH_STRING="admin:secretpassword"
- Make sure not to have both
PREFECT_API_KEY
and
PREFECT_SERVER_API_AUTH_STRING
set simultaneously - The auth string should be set on both the server and any clients connecting to it 5. Known Limitations/Issues: - As of Prefect 3.2.14, there's an open issue where WebSocket endpoints (used by
prefect events stream
) might not require authentication even when the auth string is set - Some users have reported UI authentication issues when the auth string contains special characters 6. Security Best Practices: - Use a strong password in the auth string - Consider using environment variables or secrets management for storing the auth string - If behind a reverse proxy (like nginx), you can add an additional layer of authentication at the proxy level Remember that this is a basic authentication mechanism. For more robust security requirements, you might want to consider: - Putting Prefect behind a reverse proxy with additional security measures - Using VPN or similar network-level security - Implementing additional authentication at the infrastructure level