ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
AWS KMS ကို ဘဏ်ရဲ့ မီးခံတိုက်ထဲက master key တစ်ချောင်းလို့ စဉ်းစားကြည့်ပါ။ အဲဒီ key က မီးခံတိုက်ကနေ ဘယ်တော့မှ ထွက်မလာပါဘူး။ သင့် data ကို lock လုပ်ချင်ရင် ဘဏ်ဆီက 'data key' သေးသေးလေး တစ်ချောင်း တောင်းပြီး ကိုယ့်သေတ္တာကို ပိတ်ပါတယ်၊ ပြီးရင် အဲဒီ data key ကို master key နဲ့ ပိတ်ထားတဲ့ စာအိတ်ထဲ ထည့်ပြီး သေတ္တာဘေးမှာ ချိတ်ထားပါတယ်။
ဘယ်သူက ဘဏ်ထဲဝင်ပြီး စာအိတ်ကို ဖွင့်ခွင့်ရှိလဲ ဆိုတာကို key policy က ဆုံးဖြတ်ပါတယ်။ Secrets Manager ကတော့ database password လို လျှို့ဝှက်ချက်တွေကို KMS နဲ့ encrypt လုပ်ပြီး သိမ်းပေးတဲ့ သော့တွဲစင်ပါ — ပြီးတော့ password တွေကို အချိန်တန်ရင် အလိုလို ပြောင်းပေးပါတယ်။
Problem → Why → Solution
ပြဿနာ: Secret တွေ ဖြန့်ကျက်နေပြီး key ကို ဘယ်သူ သုံးနေလဲ မသိခြင်း
Startup တစ်ခုမှာ database password က Lambda environment variable ထဲ၊ CI/CD variable ထဲ၊ developer laptop တွေရဲ့ .env file ထဲမှာ ရှိနေပါတယ်။ Password ကို နှစ်နှစ်လုံး မပြောင်းရသေးပါဘူး — ပြောင်းရင် ဘယ် service တွေ ကျိုးမလဲ ဘယ်သူမှ မသိလို့ပါ။ Developer တစ်ယောက် ထွက်သွားတော့ password ကို ဘယ်နေရာတွေမှာ ပြောင်းရမလဲ စာရင်းတောင် မရှိပါဘူး။
Encryption ဘက်မှာလည်း bucket တွေ, EBS volume တွေ အားလုံးက AWS managed key ကို သုံးထားလို့ customer ကြီးတစ်ယောက်က 'ကျွန်တော်တို့ data ကို ဘယ်သူ decrypt လုပ်နိုင်လဲ၊ ကျွန်တော်တို့ data အတွက်ပဲ key သီးသန့် ရနိုင်မလား' လို့ မေးတဲ့အခါ ဖြေစရာ မရှိပါဘူး။
# BAD: the password is visible to anyone who can read the function configuration,
# it is copied into every deploy, and rotating it means redeploying everything.
aws lambda update-function-configuration \
--function-name orders-api \
--environment "Variables={DB_USER=app_user,DB_PASSWORD=Summer2024!}"Secret တွေကို ဗဟိုချုပ်ကိုင်ပြီး key တွေကို policy နဲ့ ထိန်းရပါမယ်
Secret တစ်ခုကို နေရာများများမှာ copy ကူးလေ ပေါက်ကြားနိုင်ခြေ များလေ၊ rotate လုပ်ဖို့ ခက်လေ ဖြစ်ပါတယ်။ Secret ကို နေရာတစ်ခုတည်းမှာ သိမ်းပြီး runtime မှာ IAM permission နဲ့ ဖတ်ခိုင်းရင် rotation က application ကို မထိဘဲ ဖြစ်နိုင်ပါတယ်။ Customer managed key တွေကတော့ key policy, CloudTrail log နဲ့ ဘယ်သူ ဘာကို decrypt လုပ်လဲ ဆိုတာကို ကိုယ်တိုင် ထိန်းချုပ်ပြီး သက်သေပြနိုင်စေပါတယ်။
အဓိက သဘောတရား: Key အမျိုးအစား, key policy, envelope encryption
| Key အမျိုးအစား | ဘယ်သူ ထိန်းလဲ | သတိထားစရာ |
|---|---|---|
| AWS owned key | AWS (သင့် account ထဲမှာ မမြင်ရ) | Service အချို့ရဲ့ default၊ policy/log မရှိ |
| AWS managed key (aws/s3, aws/ebs...) | AWS က သင့် account ထဲမှာ ဖန်တီး | Policy ပြင်မရ၊ နှစ်စဉ် auto-rotate၊ cross-account မရ |
| Customer managed key | သင် | Key policy, grant, rotation, deletion အားလုံး သင်ထိန်း၊ လစဉ်ကြေး ရှိ |
KMS key တိုင်းမှာ key policy တစ်ခုတိတိ ရှိပြီး အဲဒါက access ရဲ့ အခြေခံ ဖြစ်ပါတယ်။ AWS service အများစုမှာ IAM policy တစ်ခုတည်းနဲ့ access ပေးလို့ရပေမဲ့ KMS မှာတော့ key policy က ခွင့်မပြုရင် IAM policy က ဘာမှ မလုပ်နိုင်ပါဘူး။ Default key policy မှာ account principal (arn:aws:iam::111122223333:root) ကို kms:* ပေးထားတဲ့ statement ပါပြီး အဲဒါက 'ဒီ account ထဲက IAM policy တွေကို ယုံပါ' လို့ ဆိုလိုတာပါ — root user တစ်ယောက်တည်းကို ခွင့်ပြုတာ မဟုတ်ပါဘူး။
Grant ကတော့ တတိယ နည်းလမ်းပါ — key policy ကို မပြင်ဘဲ principal တစ်ခုကို operation အချို့ ယာယီ ခွင့်ပြုတာပါ။ EBS လို AWS service တွေက သင့်ကိုယ်စား key သုံးဖို့ grant တွေကို အလိုလို ဖန်တီးပြီး အလုပ်ပြီးရင် retire လုပ်ပါတယ်။
- Envelope encryption
- Data ကို data key တစ်ခုနဲ့ local မှာ encrypt လုပ်ပြီး data key ကိုတော့ KMS key နဲ့ encrypt လုပ်ထားတာပါ။ Encrypted data key ကို encrypted data နဲ့အတူ သိမ်းပြီး plaintext data key ကို ဘယ်တော့မှ မသိမ်းပါဘူး။
KMS ရဲ့ Encrypt API က 4 KB အထိ plaintext ကိုပဲ လက်ခံပါတယ်၊ ပြီးတော့ GB ချီတဲ့ file ကို network ကနေ KMS ဆီ ပို့တာက နှေးပြီး ဈေးကြီးပါတယ်။ ဒါကြောင့် GenerateDataKey ကို ခေါ်ပြီး data key နှစ်မျိုး ယူပါတယ် — plaintext copy နဲ့ KMS key နဲ့ encrypt လုပ်ထားတဲ့ copy ပါ။ Plaintext copy နဲ့ data ကို memory ထဲမှာ encrypt လုပ်ပြီး ချက်ချင်း ဖျက်ပစ်ကာ encrypted copy ကိုပဲ data နဲ့အတူ သိမ်းပါတယ်။ ပြန်ဖတ်တဲ့အခါ encrypted data key ကို KMS Decrypt ဆီ ပို့ရုံပါပဲ။
Encryption context ကတော့ encrypt လုပ်တဲ့အခါ ထည့်ပေးတဲ့ လျှို့ဝှက်ချက်မဟုတ်တဲ့ key-value pair တွေပါ။ Decrypt လုပ်ရင် အတိအကျ တူရပြီး CloudTrail မှာ မှတ်တမ်းဝင်ကာ key policy condition မှာလည်း သုံးနိုင်ပါတယ် — ဥပမာ tenant=acme context ပါမှ decrypt ခွင့်ပေးတာမျိုးပါ။
ENVELOPE ENCRYPTION WITH GENERATEDATAKEY
----------------------------------------
WRITE READ
app --GenerateDataKey(ctx)--> KMS app --Decrypt(blob, ctx)--> KMS
<-- plaintext key + blob -- <-- plaintext key ------
| |
| AES-GCM encrypt data locally | AES-GCM decrypt locally
| then DISCARD plaintext key | then DISCARD plaintext key
v v
+--------------------------------+ KMS key itself never leaves
| stored: ciphertext + blob + ctx| KMS; every call is checked
+--------------------------------+ against the key policy- GenerateDataKey: plaintext + blob
- Encrypt data locally, drop plaintext
- Store blob next to the ciphertext
- Decrypt blob via KMS to read again
- Key policy gates every KMS call
KMS key က KMS ထဲက ဘယ်တော့မှ မထွက်ပါဘူး။ သင် ထိတွေ့ရတာက data key တွေပါ၊ အဲဒါတွေရဲ့ plaintext ကိုလည်း disk ပေါ် ဘယ်တော့မှ မရေးပါနဲ့။
Pattern: least-privilege key policy
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "EnableIamPoliciesInThisAccount",
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::111122223333:root" },
"Action": "kms:*",
"Resource": "*"
},
{
"Sid": "KeyAdministratorsCannotUseTheKey",
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::111122223333:role/KeyAdmins" },
"Action": [
"kms:DescribeKey",
"kms:EnableKeyRotation",
"kms:PutKeyPolicy",
"kms:ScheduleKeyDeletion",
"kms:CancelKeyDeletion",
"kms:TagResource"
],
"Resource": "*"
},
{
"Sid": "InvoiceServiceOnlyForTenantAcme",
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::111122223333:role/invoice-service" },
"Action": ["kms:GenerateDataKey", "kms:Decrypt"],
"Resource": "*",
"Condition": {
"StringEquals": { "kms:EncryptionContext:tenant": "acme" }
}
}
]
}Key policy ထဲမှာ "Resource": "*" က 'ဒီ key ကိုယ်တိုင်' လို့ ဆိုလိုပါတယ်။ ပထမ statement က IAM policy တွေကို ဆက်အလုပ်လုပ်စေပြီး ဖယ်လိုက်ရင် key ကို စီမံလို့ မရတော့တဲ့ အန္တရာယ် ရှိပါတယ်။ ဒုတိယ statement က key admin တွေကို စီမံခွင့်ပဲ ပေးပြီး data decrypt ခွင့် မပေးပါဘူး (separation of duties)။ တတိယ statement က invoice-service ကို encryption context tenant=acme ပါမှပဲ data key ထုတ်ခွင့်၊ decrypt ခွင့် ပေးပါတယ်။ aws kms put-key-policy --key-id $KEY_ID --policy file://key-policy.json နဲ့ apply လုပ်ပါ။
boto3 နဲ့ envelope encryption flow
ဒီ script က alias/app-data ဆိုတဲ့ customer managed key ကို သုံးပြီး envelope encryption ရဲ့ အဆင့်တိုင်းကို ပြပါတယ် — data key ထုတ်တာ၊ encrypted copy ကိုပဲ သိမ်းတာ၊ plaintext ကို ဖျက်တာ၊ KMS နဲ့ ပြန် decrypt လုပ်တာ၊ encryption context မှားရင် ငြင်းခံရတာ စတာတွေပါ။ Key ကို aws kms create-key နဲ့ create-alias နဲ့ ကြိုဖန်တီးထားရပါမယ်။
Payload ကို AES-GCM နဲ့ encrypt လုပ်တဲ့ အပိုင်းကို ဒီမှာ ချန်ထားပါတယ်၊ boto3 တစ်ခုတည်းနဲ့ run နိုင်အောင်ပါ။ Production မှာ AWS Encryption SDK ကို သုံးပါ — data key ထုတ်တာ၊ encrypt လုပ်တာ၊ encrypted key ကို message format ထဲ ထည့်တာ အားလုံးကို မှန်ကန်စွာ လုပ်ပေးပါတယ်။
import base64
import boto3
from botocore.exceptions import ClientError
kms = boto3.client("kms", region_name="us-east-1")
CONTEXT = {"tenant": "acme", "purpose": "invoice-pdf"}
# 1. Ask KMS for a fresh data key: a plaintext copy + an encrypted copy
resp = kms.generate_data_key(
KeyId="alias/app-data", KeySpec="AES_256", EncryptionContext=CONTEXT
)
plaintext_key = resp["Plaintext"]
print("plaintext data key length:", len(plaintext_key), "bytes")
# 2. Real code encrypts the payload locally here with AES-GCM using
# plaintext_key (for example through the AWS Encryption SDK).
# 3. Store ONLY the encrypted data key next to the encrypted payload
record = {
"encrypted_key": base64.b64encode(resp["CiphertextBlob"]).decode(),
"context": CONTEXT,
}
print("stored fields:", sorted(record))
print("plaintext key in record:",
base64.b64encode(plaintext_key).decode() in str(record))
# Demo only: keep a copy to prove the round trip, then drop the key
original_key = bytes(plaintext_key)
del plaintext_key, resp["Plaintext"]
# 4. Later: KMS decrypts the data key (needs kms:Decrypt + the same context)
blob = base64.b64decode(record["encrypted_key"])
restored = kms.decrypt(CiphertextBlob=blob, EncryptionContext=record["context"])
print("data key recovered:", restored["Plaintext"] == original_key)
print("same KMS key used:", restored["KeyId"] == resp["KeyId"])
# 5. A different encryption context -> KMS refuses to decrypt
try:
kms.decrypt(CiphertextBlob=blob,
EncryptionContext={"tenant": "other", "purpose": "invoice-pdf"})
except ClientError as err:
print("wrong context:", err.response["Error"]["Code"])Browser ထဲမှာ run လို့ မရပါ
ဒီ code ဟာ AWS account အစစ်ပေါ်မှာ run ရမယ့် code ဖြစ်လို့ browser playground ထဲမှာ run လို့ မရပါ။ ကိုယ့် terminal (သို့) AWS CloudShell ထဲမှာ စမ်းပါ။
Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- GenerateDataKey
kms.generate_data_key(KeyId="alias/app-data", KeySpec="AES_256", ...)AES-256 data key (32 bytes) တစ်ခုကို plaintext နဲ့ CiphertextBlob နှစ်မျိုးနဲ့ ပြန်ပေးပါတယ်။ Caller မှာ ဒီ key ပေါ် kms:GenerateDataKey permission လိုပါတယ်။
- Store only the blob
record = {"encrypted_key": ..., "context": CONTEXT}သိမ်းတဲ့ record ထဲမှာ encrypted data key နဲ့ context ပဲ ပါပါတယ်။ 'plaintext key in record: False' က plaintext key ကို မသိမ်းထားတာ သက်သေပြပါတယ်။
- Drop the plaintext
del plaintext_key, resp["Plaintext"]သုံးပြီးတာနဲ့ plaintext key ကို reference တွေ ဖယ်ပါ။ Python မှာ memory ကို တိတိကျကျ ရှင်းလို့ မရပေမဲ့ log, disk, cache ထဲ မရောက်အောင် ကာကွယ်တာက အဓိကပါ။
- Decrypt with the same context
kms.decrypt(CiphertextBlob=blob, EncryptionContext=record["context"])Blob ထဲမှာ ဘယ် key နဲ့ encrypt လုပ်ထားလဲ ပါပြီးသားမို့ symmetric key အတွက် KeyId မလိုပါဘူး (ထည့်ပေးတာက ပိုလုံခြုံပါတယ်)။ Context မတူရင် InvalidCiphertextException ရပါတယ်။
plaintext data key length: 32 bytes
stored fields: ['context', 'encrypted_key']
plaintext key in record: False
data key recovered: True
same KMS key used: True
wrong context: InvalidCiphertextExceptionRotation, multi-Region key နဲ့ key deletion
Customer managed symmetric key တွေမှာ automatic rotation ကို ဖွင့်လို့ရပါတယ် — default က ၃၆၅ ရက်တစ်ကြိမ်ဖြစ်ပြီး ရက် ၉၀ ကနေ ၂၅၆၀ ကြား ချိန်လို့ရပါတယ်၊ လိုအပ်ရင် on-demand rotation လည်း လုပ်လို့ရပါတယ်။ Rotation က key material အသစ် ထုတ်ပေမဲ့ key ID, ARN, alias, policy တွေ မပြောင်းပါဘူး၊ key material အဟောင်းကိုလည်း သိမ်းထားလို့ data အဟောင်းတွေကို ဆက် decrypt လုပ်နိုင်ပါတယ်။ ရှိပြီးသား ciphertext တွေကို re-encrypt မလုပ်ပါဘူး — application code ပြောင်းစရာ မလိုတာ ဒါကြောင့်ပါ။
Multi-Region key တွေ (key ID က mrk- နဲ့ စ) က region အများကြီးမှာ key material နဲ့ key ID တူတဲ့ key တွေ ဖြစ်ပါတယ်။ Region တစ်ခုမှာ encrypt လုပ်ထားတဲ့ data ကို အခြား region မှာ cross-region call မလုပ်ဘဲ decrypt လုပ်နိုင်လို့ DR နဲ့ global table တွေအတွက် အသုံးဝင်ပါတယ်။ ဒါပေမဲ့ replica တစ်ခုစီက ကိုယ်ပိုင် key policy, grant, alias ရှိတဲ့ သီးခြား resource ပါ၊ ရှိပြီးသား single-Region key ကို multi-Region အဖြစ် ပြောင်းလို့ မရပါဘူး။
Key ကို ဖျက်တာက ရက် ၇ ကနေ ၃၀ ကြား waiting period ပြီးမှ ဖြစ်ပြီး ဖျက်ပြီးရင် အဲဒီ key နဲ့ encrypt လုပ်ထားတဲ့ data အားလုံး ပြန်ဖတ်လို့ မရတော့ပါဘူး။ မသေချာရင် key ကို disable လုပ်ပြီး CloudTrail မှာ ဘယ်သူ သုံးနေသေးလဲ စောင့်ကြည့်ပါ။
KEY_ID=$(aws kms create-key \
--description "Invoice data key" \
--key-usage ENCRYPT_DECRYPT \
--key-spec SYMMETRIC_DEFAULT \
--query KeyMetadata.KeyId --output text)
aws kms create-alias --alias-name alias/app-data --target-key-id "$KEY_ID"
aws kms put-key-policy --key-id "$KEY_ID" --policy file://key-policy.json
aws kms enable-key-rotation --key-id "$KEY_ID" --rotation-period-in-days 180
aws kms get-key-rotation-status --key-id "$KEY_ID"
# Multi-Region: create the primary, then a replica in another Region
MRK_ID=$(aws kms create-key --multi-region \
--description "Global orders key" \
--query KeyMetadata.KeyId --output text)
aws kms replicate-key --key-id "$MRK_ID" --replica-region eu-west-1Secrets Manager နဲ့ Parameter Store SecureString
Secrets Manager က secret တွေကို KMS နဲ့ encrypt လုပ်ပြီး သိမ်းကာ version တွေကို staging label (AWSCURRENT, AWSPENDING, AWSPREVIOUS) နဲ့ စီမံပါတယ်။ အဓိက အားသာချက်က rotation ပါ — Lambda function တစ်ခုက createSecret, setSecret, testSecret, finishSecret ဆိုတဲ့ အဆင့်လေးဆင့်နဲ့ password အသစ် ထုတ်တယ်၊ database မှာ သတ်မှတ်တယ်၊ စမ်းတယ်၊ ပြီးမှ AWSCURRENT label ကို ရွှေ့ပါတယ်။ RDS နဲ့ Aurora ရဲ့ master password အတွက်တော့ RDS ကိုယ်တိုင် စီမံတဲ့ managed rotation ရှိလို့ Lambda ရေးစရာ မလိုပါဘူး။
Account ခြားကနေ secret ကို ဖတ်ခွင့်ပေးဖို့ အရာသုံးခု လိုပါတယ် — secret ပေါ်က resource policy၊ customer managed key ရဲ့ key policy မှာ အဲဒီ account ကို kms:Decrypt ခွင့်ပြုတာ၊ ဖတ်မယ့် account ထဲက IAM policy ပါ။ Default aws/secretsmanager key ကို cross-account မသုံးနိုင်လို့ customer managed key မဖြစ်မနေ လိုပါတယ်။
| လိုအပ်ချက် | Secrets Manager | Parameter Store SecureString |
|---|---|---|
| Built-in rotation | ရ (Lambda ဒါမှမဟုတ် managed) | မရ — ကိုယ်တိုင် ရေးရ |
| Cross-account access | Resource policy နဲ့ ရ | Advanced tier + sharing လို၊ ပိုရှုပ် |
| ကုန်ကျစရိတ် | Secret တစ်ခုချင်း လစဉ် + API call | Standard parameter အခမဲ့၊ advanced က ပေးရ |
| သင့်တော်တာ | DB credential, API key ကို rotate လုပ်ရမယ့်ဟာ | Config နဲ့ rotate မလုပ်တဲ့ secret |
Lambda ကနေ secret ကို ဖတ်တဲ့အခါ invocation တိုင်း API ခေါ်ရင် latency နဲ့ cost တက်ပါတယ်။ အောက်က code က secret ကို execution environment တစ်ခုစီမှာ ၅ မိနစ်တစ်ကြိမ်ပဲ ဖတ်ပါတယ် — rotation ပြီးရင် cache သက်တမ်းကုန်တာနဲ့ password အသစ်ကို အလိုလို ယူပါတယ်။ (AWS Parameters and Secrets Lambda Extension ကလည်း ဒီ caching ကို လုပ်ပေးနိုင်ပါတယ်။)
import json
import time
import boto3
secrets = boto3.client("secretsmanager", region_name="us-east-1")
SECRET_ID = "prod/orders/db"
CACHE_SECONDS = 300
_cache = {"value": None, "fetched_at": 0.0}
api_calls = 0
def get_db_credentials():
"""Fetch the secret at most once per 5 minutes per execution environment."""
global api_calls
if _cache["value"] is None or time.time() - _cache["fetched_at"] > CACHE_SECONDS:
resp = secrets.get_secret_value(SecretId=SECRET_ID) # AWSCURRENT version
_cache.update(value=json.loads(resp["SecretString"]), fetched_at=time.time())
api_calls += 1
return _cache["value"]
def lambda_handler(event, context):
creds = get_db_credentials()
return {"connect_as": creds["username"]}
if __name__ == "__main__":
for _ in range(3):
print(lambda_handler({}, None))
print("Secrets Manager API calls:", api_calls)Production မှာ
Production မှာ ဘယ်လိုသုံးကြလဲ
Multi-account organization တွေမှာ data sensitivity အလိုက် customer managed key တွေ ခွဲကြပါတယ် — ဥပမာ application တစ်ခုချင်း၊ environment တစ်ခုချင်း (prod/dev) နဲ့ အရေးကြီး customer တွေအတွက် key သီးသန့်ပါ။ Key policy တွေကို IaC ထဲမှာ ရေးပြီး key admin နဲ့ key user ကို ခွဲထားကာ SCP နဲ့ kms:ScheduleKeyDeletion ကို security team ကလွဲပြီး ပိတ်ထားကြပါတယ်။ CloudTrail မှာ Decrypt call တွေကို encryption context နဲ့အတူ ကြည့်လို့ 'ဘယ် service က ဘယ် tenant ရဲ့ data ကို decrypt လုပ်လဲ' ဆိုတာ auditor ကို ပြနိုင်ပါတယ်။
Secret တွေအတွက် database credential အားလုံးကို Secrets Manager မှာ ထားပြီး ရက် ၃၀ ကနေ ၉၀ ကြား rotate လုပ်ကြပါတယ်။ Application တွေက secret ARN ကိုပဲ environment variable ထဲ ထည့်ပြီး runtime မှာ ဖတ်ပါတယ်။ Feature flag, endpoint URL လို config တွေကိုတော့ Parameter Store standard tier မှာ ထားကြပါတယ်။
- Alarm: CloudTrail ကနေ ScheduleKeyDeletion, DisableKey, PutKeyPolicy event တွေ ဖြစ်တာနဲ့ EventBridge rule က security team ကို ချက်ချင်း အကြောင်းကြားပါတယ်။
- Rotation စောင့်ကြည့်ခြင်း: Secrets Manager rotation fail ဖြစ်ရင် (ဥပမာ testSecret အဆင့်မှာ) EventBridge ကနေ alert ပို့ပြီး AWSCURRENT က password အဟောင်းအတိုင်း ကျန်နေလား စစ်ပါတယ်။
- Access review: IAM Access Analyzer နဲ့ key policy နဲ့ secret resource policy တွေမှာ မရည်ရွယ်ဘဲ အပြင် account ကို ခွင့်ပြုထားတာ ရှိမရှိ ပုံမှန် စစ်ပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
Key policy ထဲက account principal statement ကို ဖယ်ခြင်း
'Least privilege' လုပ်ချင်လို့ arn:aws:iam::...:root statement ကို ဖယ်ပြီး role တစ်ခုတည်းကိုပဲ ထားခဲ့ရင် IAM policy တွေ အလုပ်မလုပ်တော့ပါဘူး။ အဲဒီ role ပျက်သွားရင် key ကို ဘယ်သူမှ စီမံလို့ မရတော့ဘဲ AWS Support ကို ဆက်သွယ်ရပါမယ်။ PutKeyPolicy က ဒီလို lockout ဖြစ်စေမယ့် policy ကို default အားဖြင့် ငြင်းပါတယ်၊ ဒါပေမဲ့ safety check ကို bypass လုပ်လို့ရလို့ အဲဒီ flag ကို ဘယ်တော့မှ မသုံးပါနဲ့။
Plaintext data key ကို log ထုတ်ခြင်း ဒါမှမဟုတ် သိမ်းခြင်း
Debug လုပ်ဖို့ GenerateDataKey ရဲ့ response တစ်ခုလုံးကို log ထုတ်မိရင် plaintext key က CloudWatch Logs ထဲ ရောက်သွားပြီး envelope encryption ရဲ့ အဓိပ္ပာယ် ပျက်သွားပါတယ်။ Response ကို log မထုတ်ပါနဲ့၊ CiphertextBlob ကိုပဲ သိမ်းပါ။
Cross-account secret ကို aws/secretsmanager key နဲ့ encrypt လုပ်ခြင်း
Resource policy ကို မှန်မှန်ကန်ကန် ရေးထားပေမဲ့ secret က AWS managed key နဲ့ encrypt လုပ်ထားရင် အခြား account က decrypt လုပ်လို့ မရလို့ AccessDenied ရပါတယ်။ Cross-account secret တွေကို customer managed key နဲ့ encrypt လုပ်ပြီး key policy မှာ အဲဒီ account ကို ခွင့်ပြုပါ။
Rotation ဖွင့်ပြီး application က password ကို cache ထာဝရ လုပ်ခြင်း
Container စတင်ချိန်မှာ password ကို တစ်ကြိမ်ပဲ ဖတ်ပြီး ထာဝရ ကိုင်ထားရင် rotation ပြီးတဲ့အခါ connection အသစ်တွေ fail ဖြစ်ပါတယ်။ Cache ကို TTL ထားပါ၊ authentication error ရရင် secret ကို ပြန်ဖတ်ပြီး ထပ်ကြိုးစားပါ။
💰 Cost & Cleanup
Customer managed KMS key တစ်ခုက လစဉ်ကြေး (နာရီအလိုက် ခွဲတွက်) ရှိပြီး API request တွေအတွက်လည်း ပေးရပါတယ် (free tier မှာ လစဉ် request အရေအတွက် အကန့်အသတ်နဲ့ ပါပါတယ်)။ AWS managed key တွေမှာ လစဉ်ကြေး မရှိပေမဲ့ request ကြေး ရှိပါတယ်။ Secrets Manager က secret တစ်ခုချင်း လစဉ်ကြေးနဲ့ API call ၁၀,၀၀၀ စီ ပေးရပြီး Parameter Store standard parameter တွေ အခမဲ့ပါ။ ဈေးနှုန်းအတိအကျကို pricing page မှာ စစ်ပါ။ Lab ပြီးရင် key ကို deletion schedule လုပ်ပါ (အနည်းဆုံး ၇ ရက် စောင့်ရ)၊ secret နဲ့ parameter တွေကို ဖျက်ပါ။
စမ်းပြီးရင် ဖျက်ဖို့ command
aws secretsmanager delete-secret \
--secret-id prod/orders/db \
--force-delete-without-recovery
aws ssm delete-parameter --name /orders/api-key
KEY_ID=$(aws kms describe-key --key-id alias/app-data \
--query KeyMetadata.KeyId --output text)
aws kms delete-alias --alias-name alias/app-data
aws kms schedule-key-deletion --key-id "$KEY_ID" --pending-window-in-days 7Mini challenge
Key အသစ်နဲ့ SecureString parameter တစ်ခု ကာကွယ်ပါ
Customer managed key တစ်ခု ဖန်တီးပြီး rotation ဖွင့်ကာ အဲဒီ key နဲ့ encrypt လုပ်ထားတဲ့ SecureString parameter တစ်ခု သိမ်းပါ။ ပြီးရင် decrypt လုပ်ပြီး ပြန်ဖတ်ပါ။
- alias/app-data alias ပါတဲ့ symmetric key တစ်ခု ဖန်တီးပါ
- Automatic rotation ကို ရက် ၁၈၀ နဲ့ ဖွင့်ပြီး status ကို စစ်ပါ
- /orders/api-key ကို SecureString အဖြစ် alias/app-data နဲ့ သိမ်းပါ
- --with-decryption နဲ့ ပြန်ဖတ်ပြီး value ကို မြင်ရပါမယ်
ရလဒ်: get-key-rotation-status က KeyRotationEnabled: true နဲ့ RotationPeriodInDays: 180 ပြပြီး get-parameter က plaintext value ကို ပြန်ပေးပါတယ်။ --with-decryption မပါရင် encrypted value ပဲ ရပါတယ်။
အရိပ်အမြွက် ကြည့်ရန်
put-parameter မှာ --type SecureString နဲ့ --key-id alias/app-data ကို သုံးပါ။ Rotation period အတွက် enable-key-rotation ရဲ့ --rotation-period-in-days ကို သုံးပါ။
အဖြေ ကြည့်ရန်
KEY_ID=$(aws kms create-key \
--description "Orders config key" \
--query KeyMetadata.KeyId --output text)
aws kms create-alias --alias-name alias/app-data --target-key-id "$KEY_ID"
aws kms enable-key-rotation --key-id "$KEY_ID" --rotation-period-in-days 180
aws kms get-key-rotation-status --key-id "$KEY_ID"
aws ssm put-parameter \
--name /orders/api-key \
--type SecureString \
--key-id alias/app-data \
--value "sk-test-0123456789"
aws ssm get-parameter --name /orders/api-key --with-decryption \
--query Parameter.Value --output textQuick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- AWS managed key တွေက ရိုးရှင်းပေမဲ့ policy ပြင်မရ၊ cross-account မရပါဘူး၊ ထိန်းချုပ်မှု လိုရင် customer managed key သုံးပါ။
- Key policy က access ရဲ့ အခြေခံပါ — IAM policy တွေက key policy က account ကို ယုံမှသာ အလုပ်လုပ်ပြီး grant တွေက ယာယီ delegation အတွက်ပါ။
- Envelope encryption: GenerateDataKey နဲ့ local မှာ encrypt လုပ်၊ encrypted data key ကိုပဲ သိမ်းပြီး encryption context ကို အမြဲ ထည့်ပါ။
- Rotation က key ID ကို မပြောင်းဘဲ key material ကို ပြောင်းပြီး multi-Region key တွေက region ကြား decrypt လုပ်ခွင့်ပေးပါတယ်။
- Rotate လုပ်ရမယ့်၊ cross-account မျှဝေရမယ့် secret တွေကို Secrets Manager မှာ၊ ရိုးရိုး config ကို Parameter Store မှာ ထားပါ။
AWS KMS concepts — AWS Documentation
Key policies in AWS KMS — AWS Documentation
What is AWS Secrets Manager? — AWS Documentation