Skip to content

Expert · Cloud security and compliance · topic 44 of 50 · 30 min

Encryption, keys and secrets

Explanation

Encrypt data at rest (on disks, in buckets and databases) and in transit (with TLS). Cloud key services such as AWS KMS hold the master keys and use envelope encryption, so your data keys are themselves encrypted. Passwords, API tokens and database credentials belong in a secrets manager, never in code. Classify data first, so the most sensitive data gets the strongest controls, and encrypt your backups too.

Why encrypt in the cloud?

Encryption turns readable data (plaintext) into scrambled data (ciphertext) that only someone with the right key can read back. Encryption at rest protects stored data: if a disk is lost or a snapshot is shared by mistake, the data is useless without the key. Encryption in transit protects data moving over a network, so nobody in between can read or change it. Many laws and standards, such as PCI DSS for card data, expect both.

Envelope encryption

Encrypting gigabytes directly with a master key held in a central service would be slow, because every byte would travel to that service. Instead, the cloud uses envelope encryption. A data key encrypts your data locally and quickly. Then the master key, which never leaves the key service, encrypts the data key. You store the encrypted data key next to the data. To read, you ask the key service to decrypt the small data key, then decrypt the data locally.

Think of it like this: Think of a bank locker. Your valuables are in a box locked with your own small key (the data key). You leave that small key with the bank manager, who keeps it inside the strongroom (the master key). Anyone who steals the box gets nothing without first convincing the bank to open the strongroom.
bash
# Ask KMS for a data key: you get it in plaintext (use, then forget) and encrypted (store it)
aws kms generate-data-key --key-id alias/payments-key --key-spec AES_256

# Customer managed key with yearly automatic rotation
aws kms create-key --description "Payments data key"
aws kms create-alias --alias-name alias/payments-key --target-key-id <key-id>
aws kms enable-key-rotation --key-id <key-id>
GenerateDataKey is the envelope encryption building block. Services like S3 and EBS call it for you.

KMS keys: AWS managed or customer managed

AWS managed keyCustomer managed key
Example nameaws/s3, aws/ebsalias/payments-key
Who controls the key policyAWSYou
RotationAutomatic, every yearOptional automatic rotation (yearly by default) or on demand
CostNo monthly key feeA small monthly fee per key plus requests (check pricing)
Use whenSimple default encryptionYou must control who can use or disable the key, or need audit separation

A key policy is the resource policy on a KMS key. It decides who can administer the key and who can use it to encrypt and decrypt. With a customer managed key, a developer who has s3:GetObject but no kms:Decrypt still cannot read the encrypted object. That separation is why regulated companies prefer customer managed keys. Rotation creates new key material; old material is kept so old data can still be decrypted.

Defaults help: since January 2023, Amazon S3 encrypts every new object at rest (SSE-S3). You can turn on EBS encryption by default for each region, and RDS encryption is chosen when you create the database. Azure Key Vault and Google Cloud KMS play the same role on the other clouds.

Encryption in transit: TLS and ACM

TLS (Transport Layer Security) is the protocol behind HTTPS. Use TLS 1.2 or newer everywhere, including between your load balancer and your servers when the data is sensitive. AWS Certificate Manager (ACM) issues free public TLS certificates for services it integrates with, such as load balancers, CloudFront and API Gateway, and renews them automatically. A certificate for CloudFront must be created in us-east-1. You can force HTTPS on a bucket with a policy like this:

json
{
  "Version": "2012-10-17",
  "Statement": [{
    "Sid": "DenyPlainHTTP",
    "Effect": "Deny",
    "Principal": "*",
    "Action": "s3:*",
    "Resource": ["arn:aws:s3:::upi-reports", "arn:aws:s3:::upi-reports/*"],
    "Condition": { "Bool": { "aws:SecureTransport": "false" } }
  }]
}
Any request to the bucket that does not use TLS is denied.

Secrets management

A secret is any value that grants access: a database password, an API token, a private key. Never hard-code secrets in source code, Docker images or environment files committed to Git. Put them in a secrets manager, and let the app fetch them at runtime through its role.

  • AWS Secrets Manager: stores secrets, encrypts them with KMS, and can rotate them automatically (managed rotation for RDS, or a Lambda function you write). Charged per secret per month.
  • AWS Systems Manager Parameter Store: SecureString parameters encrypted with KMS. Standard parameters have no extra charge, but rotation is your job.
  • Azure Key Vault: secrets, keys and certificates in one service.
  • Google Cloud Secret Manager: versioned secrets with IAM per secret.
bash
aws secretsmanager get-secret-value --secret-id prod/payments/db --query SecretString --output text
aws ssm get-parameter --name /payments/api-token --with-decryption
The app's role needs permission to read the secret and to use the KMS key that protects it.

Data classification and backups

Data classification means labelling data by sensitivity, for example Public, Internal, Confidential and Restricted. Payment data, Aadhaar numbers and health records are Restricted. The label decides the controls: Restricted data gets customer managed keys, strict access, extra logging and no copies in test environments. Tag buckets and databases with their class. Backups must be encrypted too, and copies in a separate account protect you if the main account is compromised.

Careful: Common mistakes: (1) An AWS key or database password committed to GitHub. Fix: rotate it immediately (deleting the commit is not enough, it is already copied), then add a secret scanner to your pipeline. (2) Encrypting data but giving everyone kms:Decrypt on "*". Fix: grant decrypt only to the roles that need it. (3) Scheduling a KMS key for deletion while data still uses it: that data becomes unreadable for ever. Disable the key first and watch for errors. (4) Copying production data, unmasked, into a test account.

Need help?

Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.

Try:

Watch the lesson

Encryption, keys and secrets · 1/7

At rest: disks, buckets, databases In transit: TLS

Encryption scrambles data so only someone with the key can read it. We encrypt data at rest and data in transit.

Prefer a video by a teacher? Find free videos on “AWS KMS envelope encryption secrets manager explained” on YouTube ↗

In real life: A hospital chain in Hyderabad protects patient records

A hospital chain stores scanned reports and lab results in S3 and patient details in RDS PostgreSQL in ap-south-2 (Hyderabad). Records are classified as Restricted. Each data store uses a customer managed KMS key whose key policy allows decrypt only for the application role and two named admins. The database password lives in Secrets Manager with automatic 30-day rotation. All traffic uses HTTPS with an ACM certificate on the load balancer. Nightly backups are copied to a separate backup account, encrypted with that account's own key. When a developer accidentally pushed a test API token to a public repository, a secret scanner in the CI pipeline flagged it within minutes and the token was revoked the same day.

Practice (0/3 done)

Task 1 · Question

Where should a Lambda function get its database password from?

Task 2 · Code · Python

Write find_secrets(text) that scans a file's text line by line (line numbers start at 1) and returns a list of (line_number, kind) tuples. Kinds, checked in this order on each line: "aws-access-key" if the line contains AKIA followed by exactly 16 capital letters or digits; "password" if it contains password, then optional spaces, "=", optional spaces and a value (any case: PASSWORD= also counts); "private-key" if it contains a header like -----BEGIN PRIVATE KEY----- or -----BEGIN RSA PRIVATE KEY-----. Use the re module.

Task 3 · Question

A developer has s3:GetObject on a bucket encrypted with a customer managed KMS key, but no kms:Decrypt on that key. What happens when they download an object?

Quick quiz

Question 1

1.In envelope encryption, what does the master key in KMS encrypt?

Question 2

2.Which statement about Amazon S3 encryption is true for new objects today?

Question 3

3.You need a free public TLS certificate for a CloudFront distribution. Where do you request it in ACM?

Question 4

4.A database password was committed to a public GitHub repository an hour ago. What is the first thing to do?

Question 5

5.Which service can rotate an RDS database password automatically?

Read more (free)

Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.

In envelope encryption, what does the master key in KMS encrypt?

Answer: The data key

The data key encrypts the data; the master key encrypts the data key and never leaves KMS.

Which statement about Amazon S3 encryption is true for new objects today?

Answer: They are encrypted at rest by default with SSE-S3

Since January 2023, S3 applies SSE-S3 to all new objects by default.

You need a free public TLS certificate for a CloudFront distribution. Where do you request it in ACM?

Answer: us-east-1

CloudFront uses ACM certificates from the us-east-1 region (N. Virginia).

A database password was committed to a public GitHub repository an hour ago. What is the first thing to do?

Answer: Rotate (change) the password immediately

Assume it is already copied. Rotate it first, then clean the history and add secret scanning.

Which service can rotate an RDS database password automatically?

Answer: AWS Secrets Manager

Secrets Manager supports managed rotation for RDS and Lambda-based rotation for other secrets.

You need an audit trail of every use of the encryption key for a bucket. Which option fits?Related

Answer: SSE-KMS

With SSE-KMS, key use is logged in CloudTrail and controlled by a key policy.

From Securing and sharing S3 data

Which tool lets you keep an encrypted Kubernetes Secret in Git that only the cluster can decrypt?Related

Answer: Sealed Secrets

Sealed Secrets encrypts with the controller's public key; only the controller in the cluster has the private key.

From Secrets management

You committed a key, then deleted it in the next commit. Is it safe now?Related

Answer: No, git history still contains it, so revoke the key

Git keeps every old version. Treat any committed secret as leaked and revoke it.

From Keeping secrets safe

Which TLS versions should a server allow today?Related

Answer: TLS 1.2 and TLS 1.3

SSL and TLS 1.0/1.1 are deprecated. Use 1.2 and 1.3.

From HTTP, HTTPS and TLS

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next steps

  1. 2 more topics in “Cloud security and compliance” before its exam: Logging, threat detection and incident response, Compliance in the cloud, with Indian rules.
  2. Module 11 exam: Cloud security and compliance · 14 questions · 20 min, without help. 4 topics of this module still to finish first.
  3. Then the next module, Certifications and cloud careers. 50 topics left in the course before the final exam.

← Previous: Identity is the new perimeter