Back to Checks
openai

OpenAI API keys have been used recently

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

Each project and admin API key is checked for use within api_key_max_unused_days (default 90). A key never used is measured from its creation date.

Prowler logoProwlerprowler-provider-openai

Risk

An unused key is access nobody needs: if it leaks, it works, and nobody is watching it.

Run this check with Prowler

Run in Prowler Cloud
Run
prowler openai --checks apikey_recently_used

Recommendation

Delete keys that have not been used within the threshold.

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_recently_used(Check):
    def execute(self):
        limit = apikey_client.audit_config.get("api_key_max_unused_days", 90)
        now = time.time()
        findings = []
        for key in apikey_client.all_keys:
            last_used = key["last_used_at"]
            days = int((now - (last_used or key["created_at"] or now)) // 86400)
            if last_used:
                text = f"{label(key)} was last used {days} days ago (limit {limit})."
            else:
                text = f"{label(key)} has never been used and was created {days} days ago (limit {limit})."
            findings.append(finding(self.metadata(), key, days <= limit, text))
        return findings