Blog | G5 Cyber Security

Secure Key File Storage

TL;DR

Use a dedicated secrets manager (like HashiCorp Vault or AWS Secrets Manager) for sensitive key files. If that’s not possible, encrypt the files with strong encryption and store them securely, controlling access tightly.

1. Understand the Risks

Storing key files directly in code repositories or shared drives is a major security risk. Anyone with access can potentially compromise your system. We need to protect these keys from:

2. Best Option: Use a Secrets Manager

A secrets manager is designed specifically for storing and controlling access to sensitive information like API keys, passwords, and certificates.

How it works (example with AWS Secrets Manager):

  1. Create a secret in AWS Secrets Manager and upload your key file.
  2. Grant access to specific IAM roles or users that need the key.
  3. In your application code, use the AWS SDK to retrieve the secret at runtime.
# Python example using boto3 (AWS SDK)
import boto3

client = boto3.client('secretsmanager')
secret_name = 'my-key-file'
response = client.get_secret_value(SecretId=secret_name)
key_file_content = response['SecretString']
# Now you can use key_file_content in your application

3. If a Secrets Manager Isn’t Possible: Encryption

If you can’t use a secrets manager, encrypt the key files before storing them.

  1. Choose a Strong Encryption Algorithm: AES-256 is widely considered secure.
  2. Use a Key Management System (KMS): Don’t store the encryption key alongside the encrypted file! Use a separate KMS or hardware security module (HSM).
  3. Encrypt the File:
  4. # Example using OpenSSL (command line)
    openssl enc -aes-256-cbc -salt -in my_key_file.txt -out my_key_file.enc
  5. Secure Storage: Store the encrypted file in a secure location with restricted access.
  6. Access Control: Limit who can access the encrypted file and, crucially, the decryption key.

4. Controlling Access

Regardless of whether you use a secrets manager or encryption:

5. Avoid Common Mistakes

Exit mobile version