Safer Methods for API Key Storage and Retrieval #4998
-
There are a couple of problems with keeping API keys in environment variables. At least on macOS, GUI software cannot access them unless they are set in the software's plist configurations on disk. This is sub optimal. The OS has a keystore that can be used, but I don't see any mention of such in the API documentation about settings. I read through the code for the Python implementation of the client library for Hashicorp vault and boiled it down to only the exact steps needed for local token authentication. That resulted in this function: import pathlib
import requests
def getkey(service, name, vault_addr):
"""Get API key for specified service."""
token_path = pathlib.Path().home().joinpath('.vault-token')
if not token_path.exists():
raise FileNotFoundError('Vault token file not found in home directory.')
token = token_path.read_text()
headers = {
'X-Vault-Token': token,
'Content-Type': 'application/json'
}
response = requests.get(vault_addr + f'/v1/apikey/data/{service}', headers=headers)
if not (apikey := response.json().get('data', dict()).get('data', dict()).get(name)):
raise KeyError('API key not found.')
return apikey However, in an ideal situation, there would be a way via the Python and other APIs to interact with the OS's keystore. The settings GUI would have a configuration box that would serve as a way to save a credential to the store as well as a way to retrieve the credentials for use in plugins etc. Thoughts? s = Settings()
s.register_group('vault', 'Hashicorp Vault')
setting = {
'description': 'Enter vault URL.',
'title': 'Use Hashicorp Vault',
'optional': True,
'type': 'string'
}
s.register_setting('vault.url', json.dumps(setting)) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
There are a few options for storing secrets, each with advantages and disadvantages:
So my recommendation would be to use
|
Beta Was this translation helpful? Give feedback.
There are a few options for storing secrets, each with advantages and disadvantages:
SecretsProvider
api which is designed for storage of actual secrets, with no UI but with a couple of standard implementations:SecretsProvider["SystemSecretsProvider"]
uses the rust keyring crate to store secrets in a system-defined manner. This is probably what you want to use.SecretsProvider["AESFileSecretsProvider"]
stores secrets in a file encrypted with AES and some magic key nonsense. The key is based on a per-machine hwid, so it's not settable by the user or enforced by any os protections.Settings
(as of dev) has an option for hidden password-like entries with the key"hidden": true
…