Secure OpenAI organizations with cnspec
Scan OpenAI organizations, projects, and API keys against security best practices with cnspec.
Scan your OpenAI organization to review who has access, how API keys and service accounts are managed, and how uploaded data is governed. cnspec queries the OpenAI API to evaluate organization users and invites, projects and their API keys, service accounts, files, vector stores, and audit log visibility.
OpenAI is one of the AI targets cnspec can assess. For the full list, see the AI infrastructure overview. New to cnspec? Start with the quickstart.
Prerequisites
To scan an OpenAI organization with cnspec, you must have:
- cnspec installed on your workstation
- An OpenAI admin API key (
sk-admin-...), a project API key (sk-proj-...), or both
Organization resources such as users, invites, audit logs, project API keys, and service accounts require an admin API key.
Connect to OpenAI
Pass an API key with --token. cnspec accepts both project keys and admin keys and detects the key type automatically:
cnspec shell openai --token YOUR_ADMIN_API_KEYYou can also set the OPENAI_API_KEY environment variable to omit the --token flag:
export OPENAI_API_KEY='YOUR_ADMIN_API_KEY'
cnspec shell openaiSome queries need one credential for an object and the other for its governance, such as which projects a fine tuning checkpoint is shared into. To reach them, pass a project key with --token and an admin key with --admin-token (or the OPENAI_ADMIN_KEY environment variable):
cnspec shell openai --token YOUR_PROJECT_API_KEY --admin-token YOUR_ADMIN_API_KEYVerify the connection by listing your projects:
cnspec> openai.projects { name status }Connection options
| Option | Description |
|---|---|
--token | OpenAI API key, either a project key (sk-proj-...) or an admin key (sk-admin-...). Defaults to OPENAI_API_KEY |
--admin-token | OpenAI admin API key to use alongside --token. Defaults to OPENAI_ADMIN_KEY |
--organization | OpenAI organization ID. Defaults to OPENAI_ORG_ID |
--project | OpenAI project ID. Defaults to OPENAI_PROJECT_ID |
--base-url | OpenAI API base URL for custom endpoints. Defaults to OPENAI_BASE_URL |
Scan an OpenAI organization
To scan your OpenAI organization:
cnspec scan openai --token YOUR_ADMIN_API_KEYWhen the scan completes, cnspec prints a pass or fail result for each check and an overall risk score. To output results in other formats such as JSON, JUnit, or SARIF, read Report Results.
Scan with the Mondoo OpenAI Security policy
Mondoo maintains an out of the box Mondoo OpenAI Security policy that checks organization owner count, pending owner invites, service account roles, API keys in archived projects, project API key rotation, vector store expiration, and audit log access. Several of its checks require an admin API key.
Mondoo Platform users: Enable the policy in your space. In the Mondoo App, go to Findings > Policies, search for "OpenAI", and add the policy. All future scans of your OpenAI organization automatically evaluate against it. To learn more, read Manage policies in Mondoo Platform.
Open source users: Pass the policy bundle URL directly to cnspec:
cnspec scan openai --token YOUR_ADMIN_API_KEY \
--policy-bundle https://raw.githubusercontent.com/mondoohq/cnspec/refs/heads/main/content/mondoo-openai-security.mql.yamlYou can also create your own policies to meet your specific requirements.
Explore an OpenAI organization
Run cnspec shell openai --token YOUR_ADMIN_API_KEY to open the interactive shell.
List organization users and their roles
cnspec> openai.users { name email role }Review project API keys
cnspec> openai.projects { name apiKeys { name ownerType createdAt lastUsedAt } }Review project service accounts
cnspec> openai.projects { name serviceAccounts { name role } }Example security checks
Ensure no more than three organization owners
cnspec> openai.users.where(role == "owner").length <= 3Ensure no pending invitations grant owner access
cnspec> openai.invites.where(status == "pending" && role == "owner").length == 0Ensure project service accounts do not hold the owner role
cnspec> openai.projects.all(serviceAccounts.all(role != "owner"))Ensure archived projects do not retain active API keys
cnspec> openai.projects.where(status == "archived").all(apiKeys.length == 0)Ensure vector stores have an expiration policy
cnspec> openai.vectorStores.all(expiresAt != empty)Learn more
-
To learn more about how the MQL query language works, read Write Effective MQL.
-
To learn about all the OpenAI resources and properties you can query, read the Mondoo OpenAI Resource Pack Reference.