liveBook · Manning

Secrets manager

As explained in Grokking Software Architecture

A secrets manager securely stores and controls access to credentials, keys, and other sensitive values.

A secrets manager lets an application fetch sensitive values, such as database credentials, at runtime instead of keeping them in source code. Pair it with a short-lived IAM role that can read only the required secret. section 11.5.1

Replace fragile hardcoded credentials with a runtime retrieval pattern:

  • Hardcoded credentials can leak through source control, require redeployment when changed, and give attackers excessive access if they belong to an administrator account. section 11.5.1
  • Fetch credentials at runtime using a server or container's short-lived IAM role, limited to reading the specific secret it needs. section 11.5.1
  • Plan for rotation: handle an unauthorized response by refreshing credentials and retrying to avoid manual intervention and downtime. section 11.10
Listing 11.2 Secure Runtime Retrieval Refactor
import boto3
import json
from botocore.exceptions import ClientError
 
def get_database_credentials():
    secret_name = "prod/BillingDatabase/Credentials"
    region_name = "us-east-1"
 
    # 1. Connect to the highly secure Secrets Manager
    session = boto3.session.Session()
    client = session.client(
        service_name='secretsmanager',
        region_name=region_name
    )
 
    try:
        # 2. Request the credentials at runtime
        get_secret_value_response = client.get_secret_value(
            SecretId=secret_name
        )
    except ClientError as e:
        raise e #A
 
    # 3. Parse the secure response
    secret_string = get_secret_value_response['SecretString']
    secret_dict = json.loads(secret_string)
    
    # 4. Return the credentials to be used by the ORM/Database connection
    return secret_dict['username'], secret_dict['password']
 
db_user, db_pass = get_database_credentials() #B
connect_to_database(db_user, db_pass)
 

See a Python example using boto3 to request a named secret from AWS Secrets Manager and parse the returned credentials. section 11.5.1

For local development, environment variables can mimic runtime secret delivery; a source-control-excluded .env file is one suggested workflow. section 11.8

How Grokking Software Architecture covers it

How deep it goes
Explains runtime retrieval through short-lived IAM roles and names rotation-and-retry behavior without detailing its implementation.
Examples
Uses a Python boto3 retrieval example and suggests environment variables or a .env file for local development.
What it assumes
Assumes a compute resource can receive an IAM role and teaches local secret handling through environment variables.
What you can do after
Apply it via AWS Secrets Manager and boto3 to retrieve credentials at runtime, or use environment variables to mimic the pattern locally.
How it connects
Connects secrets management to least privilege, IAM roles, credential rotation, and hardcoded-secret risks.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Software Architecture. The section links point to the parts of the book it draws on.