Encryption, HTTPS/TLS & Secrets Management
Encrypt data on the way (in transit) and on disk (at rest), and keep passwords/API keys outside the code in a safe vault.
42.1 Why is it used?
- Without HTTPS, anyone at the Wi-Fi or ISP level can read a password or card number (man-in-the-middle).
- If a disk or backup is stolen, at-rest encryption keeps the data useless.
- If DB passwords and API keys are hardcoded in code or Git, a leak is almost certain.
- Compliance such as PCI-DSS, RBI guidelines and the DPDP Act requires encryption and key management.
- A central secrets store makes rotation, audit (who read which key when) and access control easy.
42.2 How it works
- TLS handshake: client and server verify the certificate and agree on a session key, then data is encrypted with symmetric (AES). TLS 1.3 is fast and secure.
- Certificate: a CA (like Let's Encrypt, DigiCert) signs the domain's certificate and the browser trusts it.
- TLS termination: HTTPS is decrypted at the load balancer/CDN, and traffic goes to the backend over HTTP or over TLS again (re-encryption).
- mTLS: certificates on both sides, for service-to-service trust (service mesh like Istio).
- At-rest encryption: disks, DBs, S3 objects encrypted with AES-256; keys live in KMS.
- Envelope encryption: encrypt data with a Data Key, encrypt the Data Key with the KMS Master Key. The master key never leaves KMS.
- Secrets management: the app fetches secrets from the vault at runtime; dynamic secrets (temporary DB user) and automatic rotation.
- Hashing passwords: passwords are not encrypted, they are stored hashed with bcrypt/Argon2.
- 1.1. Browser -> Server: ClientHello
- 2.2. Server -> Browser: certificate (CA signed)
- 3.3. Browser verifies the certificate with the CA
- 4.4. Key share, a session key is made on both sides
- 5.5. Data is encrypted with fast symmetric AES
The handshake builds the session key with asymmetric crypto, then all data travels with symmetric AES.
- 1.Card data -> encrypt with Data Key, AES-256 -> Encrypted data
- 2.Encrypt the Data Key with the KMS Master Key -> Encrypted Data Key (stored with the encrypted data)
- 3.The Master Key never leaves KMS
- 4.Decrypt: open the Data Key with KMS, then decrypt the data
Passwords: do not encrypt, HASH with bcrypt / Argon2.
- 1.User App: card details over TLS 1.3 up to the CDN / ALB
- 2.CDN / ALB terminates TLS (cert: Let's Encrypt, expiry monitored)
- 3.mTLS to the Payment Service; card is tokenised
- 4.Payment Service fetches the DB password and API key from Vault / Secrets Manager at startup
- 5.Payments DB: AES-256 at rest + backups, master key in AWS KMS
- 6.Secrets auto-rotate every 30 days, every secret access goes to the audit log
Wrong: secrets in .env / Git = a leak is almost certain, rotate immediately. If cert expiry is not monitored, the site goes down.
42.3 Tools you can use
| Tool | Type | How it is used |
|---|---|---|
| HashiCorp Vault | Secrets manager (open-source/enterprise) | Multi-cloud secrets, dynamic DB credentials, PKI certificates, encryption-as-a-service. |
| AWS KMS | Managed key management | Manages encryption keys for S3, EBS, RDS; envelope encryption, CloudTrail audit. |
| AWS Secrets Manager | Managed secrets store | Stores DB passwords, API keys; automatic rotation with RDS. |
| Azure Key Vault | Azure keys + secrets + certs | Secrets, keys and TLS certificates of Azure apps in one place, accessed with Managed Identity. |
| GCP Secret Manager / Cloud KMS | GCP managed services | To manage secrets and encryption keys of GCP workloads. |
| Let's Encrypt + cert-manager | Free CA + Kubernetes cert automation | Free TLS certificates; cert-manager issues and renews them automatically in Kubernetes. |
42.4 Comparison: HashiCorp Vault vs AWS Secrets Manager vs Azure Key Vault
| Point | HashiCorp Vault | AWS Secrets Manager | Azure Key Vault |
|---|---|---|---|
| Type | Self-hosted or HCP managed | Fully managed AWS | Fully managed Azure |
| Cloud support | Multi-cloud + on-prem | Mainly AWS | Mainly Azure |
| Dynamic secrets | Yes, temporary DB/cloud creds | No, rotation via Lambda | No, mostly static secrets |
| Key management | Encryption via the Transit engine | Together with KMS | Keys, secrets, certs all built in |
| Setup effort | High - HA, unseal, upgrades | Low | Low |
| Cost | OSS free, ops cost; enterprise is expensive | Per secret per month + API calls | Per operation, cheap |
| Best for | Large multi-cloud / hybrid setups | AWS native apps | Azure native apps |
42.5 When to choose which
- HashiCorp Vault: a hybrid or multi-cloud setup, need dynamic credentials and PKI.
- AWS Secrets Manager + KMS: the whole app is on AWS and you need RDS password rotation.
- Azure Key Vault: Azure / AKS workloads, passwordless access with Managed Identity.
- Do TLS termination at the LB/CDN (CloudFront, ALB, Nginx); use mTLS for sensitive internal traffic.
42.6 Real-world example
Such a scenario: the journey of card data in a payment app
- The user enters card details in the app, and the data goes TLS 1.3 encrypted up to the CDN/LB.
- The load balancer terminates TLS and forwards to the payment service over mTLS inside the internal network.
- At startup the payment service gets the DB password and gateway API key from Vault/Secrets Manager, with nothing hardcoded in code.
- The card number is tokenised; any sensitive data that is stored is AES-256 encrypted, with the key in KMS.
- The DB disk and backups are encrypted with a KMS key, so even a stolen backup cannot be read.
- The DB password rotates automatically every 30 days and every secret access is recorded in the audit log.
42.7 Interview tips
- Committing secrets to a .env file or Git repo; if leaked, rotate immediately.
- Not monitoring certificate expiry: the whole site goes down the moment it expires.
- Encrypting passwords instead of hashing them; use bcrypt/Argon2 for passwords.
- Interview tip: you should be able to explain the flow of envelope encryption and TLS termination.