Friday, March 21, 2025

AWS Secrets Manager: Securely Managing Application Secrets

AWS Secrets Manager provides a robust and secure service for managing secrets like database credentials, API keys, and other sensitive information. This article explores the intricacies of AWS Secrets Manager, covering its architecture, features, lifecycle management, comparisons, and best practices.














1. Secret Stores and Types:

AWS Secrets Manager stores secrets in an encrypted, managed database. The service supports various secret types:

  • Database Credentials: Specifically designed for database credentials, enabling automatic rotation for supported databases.

  • API Keys: Securely stores API keys for various services.

  • Arbitrary Secrets: Allows storing any type of secret as a key-value pair or JSON structure.

  • Other Credentials: OAuth tokens, SSH keys, and other credentials.

Note: The underlying storage is encrypted at rest using AWS Key Management Service (KMS) customer master keys (CMKs) or AWS managed CMKs.


















2. Secret Lifecycle Management:

AWS Secrets Manager offers comprehensive lifecycle management capabilities:

  • Secret Creation: Secrets can be created through the AWS Management Console, AWS CLI, or AWS SDKs.

  • Secret Rotation: Automated rotation of database credentials for supported databases like Amazon RDS, Amazon Redshift, and Amazon DocumentDB. Custom rotation functions can be created for other secret types.

  • Secret Versioning: Each secret change creates a new version, providing an audit trail and enabling rollback.

  • Secret Deletion: Secrets can be scheduled for deletion, with a recovery window for accidental deletions.

  • Access Control: AWS Identity and Access Management (IAM) policies are used to control access to secrets.



















3. Key Features of AWS Secrets Manager:

  • Encryption at Rest and in Transit: Secrets are encrypted using KMS.

  • Automatic Secret Rotation: Automates the process of rotating database credentials.

  • Integration with AWS Services: Seamlessly integrates with other AWS services.

  • API and CLI Access: Enables programmatic access and automation.

  • Audit Logging: AWS CloudTrail logs all API calls to Secrets Manager.

  • Cross-Account Access: Enables sharing secrets across AWS accounts.

  • Resource-Based Policies: Allows fine-grained control over secret access.

  • Versioning: Maintains a history of secret changes.











4. Pros and Cons of AWS Secrets Manager:

Pros:

  • Enhanced security through encryption and access control.

  • Simplified secret lifecycle management with automated rotation.

  • Seamless integration with the AWS ecosystem.

  • Improved compliance with audit logging and versioning.

  • Cross account sharing.

Cons:

  • Cost implications based on API calls and stored secrets.

  • Vendor lock-in to the AWS platform.

  • Learning curve associated with IAM policies.























5. Key Points of Comparison with Other Secret Managers:












6. When to Choose AWS Secrets Manager and Real-Time 

Use Cases:

Choose AWS Secrets Manager when:

  • If You are primarily using AWS services.

  • If You require automated rotation of database credentials.

  • If You need tight integration with AWS IAM and other services.

  • If You require cross region secret replication

  • if You require a secure and centralized solution for managing secrets within the AWS ecosystem.

  • If You need automated secret rotation to comply with security best practices.

  • if You want to reduce the operational overhead of managing secrets manually.



















Real-Time Use Cases:

  • Database Credential Management: Securely store and rotate database credentials for Amazon RDS, Redshift, and DocumentDB.

  • API Key Management: Protect API keys used to access third-party services.

  • Microservices Security: Store and manage secrets used by microservices deployed on Amazon ECS or EKS.

  • CI/CD Pipeline Security: Protect credentials used in CI/CD pipelines, such as deployment keys and API tokens.

  • Serverless Applications: Securely manage secrets used by AWS Lambda functions.
















7. Terraform Code to Provision AWS Secrets Manager:

Terraform

resource "aws_secretsmanager_secret" "example" {
  name = "example-secret"

  recovery_window_in_days = 7
}

resource "aws_secretsmanager_secret_version" "example" {
  secret_id     = aws_secretsmanager_secret.example.id
  secret_string = jsonencode({
    "username" : "exampleuser",
    "password" : "examplepassword"
  })
}

8.Best Practices for AWS Secrets Manager:

  • Principle of Least Privilege: Grant only necessary permissions to access secrets using IAM policies.

  • Automated Secret Rotation: Implement automated rotation for supported databases and create custom rotation functions for other secret types.

  • Use Secret Versioning: Leverage versioning for audit trails and rollback capabilities.

  • Encrypt Secrets at Rest and in Transit: Utilize KMS encryption.

  • Monitor Audit Logs: Regularly review CloudTrail logs for suspicious activity.

  • Use Resource-Based Policies: Implement resource-based policies for fine-grained access control.

  • Avoid Embedding Secrets in Code: Never hardcode secrets in application code or configuration files.

  • Secure Network Access: Ensure secure network access to Secrets Manager.

  • Use Cross-Account Access Sparingly: When using cross account access, make sure that the proper roles are applied.

  • Utilize a recovery window: setting the recovery window for deleted secrets allows for recovery in case of accidental deletion.

9. Centralized Account for Secrets Management with Lambda Rotation Functions




















Steps:

  1. Create a Dedicated Secrets Management Account: Establish a separate AWS account for managing secrets.

  2. Configure IAM Roles: Create IAM roles with appropriate permissions for accessing and managing secrets.

  3. Create Secrets in the Central Account: Store all secrets in the dedicated account.

  4. Develop Lambda Rotation Functions: Write Lambda functions to automate secret rotation for various services.

  5. Configure Secrets Manager Rotation: Configure Secrets Manager to trigger the Lambda rotation functions.

  6. Grant Cross-Account Access: Use resource based policies to grant other accounts access to specific secrets.

Lambda Rotation Function Example (Python):

Python

import boto3
import json

secretsmanager = boto3.client('secretsmanager')

def lambda_handler(event, context):
    arn = event['SecretId']
    token = event['ClientRequestToken']
    step = event['Step']

    metadata = secretsmanager.describe_secret(SecretId=arn)

    if not metadata['RotationEnabled']:
        print("Secret {} is not enabled for rotation.".format(arn))
        return

    if step == "createSecret":
        create_secret(arn, token)
    elif step == "setSecret":
        set_secret(arn, token)
    elif step == "testSecret":
        test_secret(arn, token)
    elif step == "finishSecret":
        finish_secret(arn, token)
    else:
        raise ValueError("Invalid rotation step {}".format(step))

def create_secret(arn, token):
    # Generate new secret credentials
    new_secret = json.dumps({'username': 'newuser', 'password': 'newpassword'})
    secretsmanager.put_secret_value(SecretId=arn, ClientRequestToken=token, SecretString=new_secret)

def set_secret(arn, token):
    # Configure the target service with the new secret
    # ... (Implementation depends on the target service) ...
    print("New secret set in target service.")

def test_secret(arn, token):
    # Test the new secret to ensure it works
    # ... (Implementation depends on the target service) ...
    print("New secret tested successfully.")

def finish_secret(arn, token):
    # Update the secret version to mark it as the current version
    secretsmanager.update_secret_version_stage(SecretId=arn, VersionStage='AWSCURRENT', 
                VersionStageArn=arn + ":version:" + token)

10. Takeaways

AWS Secrets Manager provides a robust and secure solution for managing sensitive data in the cloud. By implementing best practices and leveraging its features, you can significantly reduce the risk of credential exposure and simplify secret lifecycle management. A centralized approach to managing secrets, combined with automated rotation through Lambda functions, is key to achieving optimal security and operational efficiency. Always remember to use the principle of least privilege, and audit secret access regularly.


Sunday, March 9, 2025

Integration of External secret operator with IBM Secret Manager

 

Background

This document provides an overview of integrating IBM Cloud Secret Manager with the External Secret Operator. It outlines the benefits of using these tools together, the setup process, and how to manage secrets effectively in a Kubernetes environment. By leveraging IBM Cloud Secret Manager alongside the External Secret Operator, users can enhance their security posture while simplifying secret management in cloud-native applications.





Introduction

IBM Cloud Secret Manager is a secure service for managing sensitive information such as API keys, passwords, and certificates. The External Secret Operator is a Kubernetes operator that allows you to use external secret management systems to populate Kubernetes secrets. By combining these two powerful tools, organizations can streamline their secret management processes while ensuring that sensitive data is securely stored and accessed.

Benefits of Using IBM Cloud Secret Manager with External Secret Operator

  1. Enhanced Security: Secrets are stored securely in IBM Cloud Secret Manager, reducing the risk of exposure.

  2. Centralized Management: Manage all secrets from a single location, simplifying operations and compliance.

  3. Dynamic Secrets: Automatically update Kubernetes secrets when the IBM Cloud Secret Manager changes occur.

  4. Kubernetes Native: Seamlessly integrates with Kubernetes, allowing developers to work within familiar environments.

Prerequisites

Before proceeding with the setup, ensure you have the following:

  • An IBM Cloud account with access to IBM Cloud Secret Manager.

  • A Kubernetes cluster where you can deploy the External Secret Operator.

  • kubectl command-line tool installed and configured to interact with your Kubernetes cluster.



Login to IBM Cloud Console:

Search for Secret manager

Provision Secret manager
















Type of Secret stored in Secret Manager

Setup Instructions

Step 1: Install the External Secret Operator

To install the External Secret Operator, you can use Helm. First, add the Helm repository:

helm repo add external-secrets https://charts.external-secrets.io
helm install external-secrets external-secrets/external-secrets \
-n external-secrets \
--create-namespace \
--set installCRDs=true


Step 2: Configure IBM Cloud Secret Manager

Create a Secret: Log in to your IBM Cloud account and navigate to the Secret Manager service. Create a new secret and note its name and ID.

IAM API Key: Create an IAM API key with permissions to access the Secret Manager.

Step 3: Create a Kubernetes Secret for the IAM API Key

Store the IAM API key in a Kubernetes secret:

kubectl create secret generic ibm-cloud-secret-manager 
--from-literal=api-key='<YOUR_IAM_API_KEY>'

Step 4: Define an External Secret

Create a YAML file for the External Secret that references the IBM Cloud Secret Manager. Here’s an example:

apiVersion: external-secrets.io/v1beta1
kind: ExternalSecret
metadata:
  name: my-secret
spec:
  backendType: ibmcloud
  data:
    - key: <YOUR_SECRET_NAME>
      name: <K8S_SECRET_KEY_NAME>
  refreshInterval: 1h
  serviceAccount: <YOUR_SERVICE_ACCOUNT>

Replace <YOUR_SECRET_NAME> and <K8S_SECRET_KEY_NAME> with your actual secret name and desired Kubernetes secret key name.

Step 5: Apply the External Secret

Deploy the External Secret to your Kubernetes cluster:

kubectl apply -f external-secret.yaml

Managing Secrets

Once the External Secret is set up, the External Secret Operator will automatically sync the secrets from IBM Cloud Secret Manager to your Kubernetes cluster. You can manage your secrets directly from the IBM Cloud console, and any updates will be reflected in your Kubernetes environment based on the defined refresh interval.

Conclusion

Integrating IBM Cloud Secret Manager with the External Secret Operator provides a robust solution for managing secrets in Kubernetes. This setup not only enhances security but also simplifies the management of sensitive data across cloud-native applications. By following the outlined steps, organizations can ensure that their secrets are securely stored and efficiently accessed, paving the way for more secure application development and deployment.