API keys are secrets used to authenticate to an API endpoint. They are similar to passwords, but have several key differences:
Generated by the server and not the client
Generally consists of a long string of random characters
Intended to be used in scripts and automation
API keys can easily be exposed accidentally and subsequently abused. Here are some common ways that they can be exposed:
Insecure sharing - sharing keys in chat, documents, email, or public repositories
Front-end - using keys in client-side code
HTTP - exposing keys in URLs or HTTP headers
Code - exposing keys in website source code or debug endpoints
Here are some general guidelines for protecting API keys:
Safe use - authenticate with API keys via HTTPS request headers, for example X-Authorization
Sharing - treat keys the same way you would passwords and only share them using the same methods
Least Privilege - assign the minimum role or permissions required to the key itself, or the account the key belongs to. Avoid granting API keys blanket administrator roles with no restrictions.
Remaining vigilant - In the event a key is compromised, reach out to [email protected] as soon as possible.
The best defense is a layered one (defense in depth). Here are some additional methods to keep API keys safe:
There are multiple methods to store keys that each have their own advantages and disadvantages.
Environment variables are dynamic values stored locally on a computer. They are commonly used by developers to handle API keys securely.
Advantages
Disadvantages
Configuration management tools such as Ansible can store, deploy, and manage API keys securely. These tools encrypt keys, only allow authorized users access, and track changes across time.
Advantages
Disadvantages
Secrets Mangers or vaults such as AWS Secrets Manager are specialized tools designed for secure storage, management, and retrieval of secrets like API keys.
Advantages
Disadvantages
API keys are a useful and essential tool for modern applications and services, but protecting API keys is important as a single leaked key can lead to data loss, system compromises, high cloud costs, and/or reputation damage.
If you have additional questions please reach out to [email protected]