Overview
The API Key Handler module provides functions for loading and managing API keys in the AegisShield Streamlit application. It implements NIST SP 800-53 Rev. 5 controls for authenticator management and secure key storage.NIST SP 800-53 Rev. 5 Controls Implemented
- IA-5 (Authenticator Management): API key management and secure storage
- SC-12 (Cryptographic Key Establishment and Management): Key lifecycle management
- AC-3 (Access Enforcement): Access control through API key validation
- IA-2 (Identification and Authentication): Authentication to external services
- IA-5(1): Password-Based Authentication
- SC-12(2): Symmetric Keys
Functions
load_api_keys()
This function does not return a value. It sets session state variables:
st.session_state['nvd_api_key']: NVD API key or empty stringst.session_state['alienvault_api_key']: AlienVault API key or empty string
Behavior
- Checks
st.secretsfor pre-configured API keys - If keys exist in secrets, loads them into session state
- If keys don’t exist, initializes session state with empty strings
- Handles
KeyErrorexceptions gracefully
Example Usage
Streamlit Secrets Configuration
To pre-configure API keys, add them to.streamlit/secrets.toml:
render_api_key_inputs()
This function does not return a value. It renders UI components and updates session state:
st.session_state['model_provider']: Selected model provider (default: “OpenAI API”)st.session_state['selected_model']: Selected OpenAI model (default: “gpt-4o”)st.session_state['openai_api_key']: OpenAI API key from user inputst.session_state['nvd_api_key']: NVD API key from user input (if not in secrets)st.session_state['alienvault_api_key']: AlienVault API key from user input (if not in secrets)
UI Components Rendered
-
OpenAI Configuration (always shown):
- Markdown instructions with API key link
- Model selector dropdown (gpt-4o, gpt-5)
- Password-masked text input for API key
- Error message if key is missing
- Usage instructions
-
NVD API Key (conditional):
- Only shown if not loaded from secrets
- Password-masked text input
- Help text with link to NVD API key request page
-
AlienVault API Key (conditional):
- Only shown if not loaded from secrets
- Password-masked text input
- Help text with link to AlienVault OTX console
Security Features
- All API key inputs use
type="password"for masked display (NIST IA-5(1)) - Keys are only stored in browser session state
- Keys are not saved to disk or shared
- Input validation requires OpenAI API key to proceed
Example Usage
Session State Variables
The module manages the following Streamlit session state variables:API Keys
OpenAI API key for LLM access (required)
National Vulnerability Database API key (optional, loaded from secrets or user input)
AlienVault OTX API key (optional, loaded from secrets or user input)
Model Configuration
Selected model provider (currently only OpenAI supported)
Selected OpenAI model (“gpt-4o” or “gpt-5”)
Implementation Notes
API Key Sources Priority
-
Streamlit Secrets (highest priority):
- Pre-configured in
.streamlit/secrets.toml - Loaded automatically by
load_api_keys() - Not shown in UI if already loaded
- Pre-configured in
-
User Input (fallback):
- Rendered by
render_api_key_inputs() - Stored in session state
- Persists only for current browser session
- Rendered by
Security Best Practices
- Masked Display: All API key inputs use password type
- Session-Only Storage: Keys never written to disk
- No Logging: API keys are never logged or exposed
- Validation: OpenAI key required before application proceeds
- Error Handling: Graceful fallback if secrets don’t exist
Model Selection
Supported OpenAI models:- gpt-4o: Standard model, balanced performance
- gpt-5: More verbose output, enhanced capabilities
Conditional Rendering
The UI intelligently shows/hides inputs:- OpenAI fields: Always shown (required)
- NVD field: Hidden if key exists in secrets
- AlienVault field: Hidden if key exists in secrets