Back to Checks
openai

OpenAI API keys are rotated regularly

apikey_rotated_regularly
Service: apikeySeverity: medium
OfficialVerifiedPrivate
Categories:gen-aisecrets
Resource Type:OpenAIApiKey

Each project and admin API key is checked for an age within api_key_max_age_days (default 180). OpenAI keys cannot be rotated in place, so an old key is one never replaced.

Risk

The longer a key lives, the more places it has been copied to and the longer a leaked copy keeps working.

Run this check with Prowler

Run in Prowler Cloud
Install
pip install --extra-index-url https://__token__:${PROWLER_REGISTRY_DOWNLOAD_API_KEY}@registry.prowler.com/simple/ "prowler-provider-openai[runtime]"
Run
prowler openai --checks apikey_rotated_regularly

Recommendation

Create a replacement key, move workloads to it and delete the old one.

Check Code

import time

from prowler.lib.check.models import Check

from prowler_provider_openai.check_report import finding
from prowler_provider_openai.services.apikey.apikey_client import apikey_client
from prowler_provider_openai.services.apikey.apikey_service import label


class apikey_rotated_regularly(Check):
    def execute(self):
        limit = apikey_client.audit_config.get("api_key_max_age_days", 180)
        now = time.time()
        findings = []
        for key in apikey_client.all_keys:
            age = int((now - (key["created_at"] or now)) // 86400)
            text = f"{label(key)} is {age} days old (limit {limit})."
            findings.append(finding(self.metadata(), key, age <= limit, text))
        return findings