Skip to content

NimTechnology

Trình bày các công nghệ CLOUD một cách dễ hiểu.

  • Kubernetes & Container
    • Docker
    • Kubernetes
      • Ingress
      • Pod
    • Helm Chart
    • OAuth2 Proxy
    • Isito-EnvoyFilter
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Vault
    • Longhorn – Storage
    • VictoriaMetrics
    • MetalLB
    • Kong Gateway
  • CI/CD
    • ArgoCD
    • ArgoWorkflows
    • Argo Events
    • Spinnaker
    • Jenkins
    • Harbor
    • TeamCity
    • Git
      • Bitbucket
  • Coding
    • DevSecOps
    • Terraform
      • GCP – Google Cloud
      • AWS – Amazon Web Service
      • Azure Cloud
    • Golang
    • Laravel
    • Python
    • Jquery & JavaScript
    • Selenium
  • Log, Monitor & Tracing
    • DataDog
    • Prometheus
    • Grafana
    • ELK
      • Kibana
      • Logstash
  • BareMetal
    • NextCloud
  • Toggle search form

[AWS] Encrypting your data easily via KMS on AWS

Posted on September 4, 2022May 24, 2023 By nim No Comments on [AWS] Encrypting your data easily via KMS on AWS

Contents

Toggle
  • 1) Symmetric Encryption
  • 2) Asymmetric Encryption
  • 3) AWS KMS
  • 4) Demo AWS KMS
    • 4.1) Create the customer managed key
    • 4.2) Encrypt data through KMS – AWS
    • 4.3) Decrypt data through KMS – AWS
    • 4.4) Delete Customer managed key
    • 4.5) KMS Other account.
    • 4.6) Get key policy to follow KMS
    • 4.7) Edit key policy to follow KMS

1) Symmetric Encryption

How it works:

  • Stream cipher – encrypt one byte at the time
  • Block cipher – encrypt entire units or blocks using predetermined key length such as 128, 192, or 256 bits.
    Sender and receiver use same key to encrypt and decrypt

Example:
AES(Advance Encrypt Standard) – block cipher of 128, 192, 256 bits, most well-known and effective use. Take billions of years to crack, more secure than DES, Triple DES, IDEA.
DES(Data Encrypt Standard)
Triple DES
IDEA (International Data Encryption Algorithm)
TLS/SSL Protocol: session key

Pros:
– Encrypt & Decrypt large amount data quickly
– Easy to implement
– Speed: because of shorter key length.
– Industry adoption & Acceptance: AES is gold standard
Cons:
– Use single key to encrypt and decrypt

2) Asymmetric Encryption

• Use public key and secret key to encrypt and decrypt
sensitive information

• RSA (Rivest Shamir Adlerman)
• DSA (Digital Signature Algorithm)
• ECC (Elliptical Curve Cryptography)
• Diffie-Hellman exchange method
• TLS/SSL protocol: TLS handshake

Pros:
• Key distribution not necessary
• Exchange of private key not necessary
• Digital signature / message authentication
Cons:
• Speed: longer key length

3) AWS KMS

AWS KMS Supports:
Symmetric KMS key: 256-bit secret encryption, AES algorithm in Galois Counter Mode (AES-GCM) is used
Asymmetric KMS key

4) Demo AWS KMS

4.1) Create the customer managed key

Chỗ define key administrative permission:
nghĩa là ta sẽ chọn ai sẽ được quản trị key này.
=> Nếu bạn ko chọn thì là tất cả các user này!

Chỗ này là chỉ định ai được truy xuất vào key này.
Ai được quyền mã hóa
ai được quền giải mã
Ở đây AWS cho phép bạn add thêm permission cho các tài khoản khác AWS.

Ở đây mình không chọn nên là phải mở tất cả!

Tiếp đến review lại và ấn Finish.

{
  "Id": "key-consolepolicy-3",
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "Enable IAM User Permissions",
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::250887682577:root"
      },
      "Action": "kms:*",
      "Resource": "*"
    }
  ]
}
Đã tạo xong key

Giờ vào custom key này chút
==> thực hiện rotation key mỗi năm

4.2) Encrypt data through KMS – AWS

https://docs.aws.amazon.com/cli/latest/reference/kms/encrypt.html
Giờ thực hiện run command

Mình có file text
Bạn cần ghi nhớ cái ID này
aws kms encrypt \
    --key-id e2a615c5-3689-420d-9445-ffd5feb78adb \
    --plaintext fileb://data.txt \
    --output text \
    --query CiphertextBlob | base64 \
    --decode > dataEncryptedFile

4.3) Decrypt data through KMS – AWS

https://docs.aws.amazon.com/cli/latest/reference/kms/decrypt.html

aws kms decrypt \
    --ciphertext-blob fileb://dataEncryptedFile \
    --key-id e2a615c5-3689-420d-9445-ffd5feb78adb \
    --output text \
    --query Plaintext | base64 \
    --decode > dataDecryptedFile

4.4) Delete Customer managed key

Key này sẽ ko delete được ngay
Mình sẽ cần đặt là 7 ngày sau nó sẽ delete

4.5) KMS Other account.

Giờ mình có 1 account Với ID như ảnh

Giờ mình muốn user thuộc Account ID: 2508-8768-2577 có thể sửa dụng KMS trên ảnh

Đầu tiên mĩnh sẽ sửa key policy ở ảnh trên:

{
    "Version": "2012-10-17",
    "Id": "key-default-1",
    "Statement": [
        {
            "Sid": "Enable IAM User Permissions",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::794155915849:root"
            },
            "Action": "kms:*",
            "Resource": "*"
        },
       // Below content of other AWS account
        {
            "Sid": "Enable IAM User Permissions of other AWS account",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::250887682577:root"
            },
            "Action": "kms:*",
            "Resource": "*"
        }
    ]
}
Bên account 250887682577 thì có 2 user
"Principal": {
                "AWS": "arn:aws:iam::250887682577:root"
            },

khi config là root nó sẽ open cho tất cả các user trong account0 250887682577 đều có thể encrypt và decrypt.

aws kms encrypt \
    --profile dev-nimtechnology-engines \
    --key-id b6713452-966f-4348-90bf-d5083f8f1fdc \
    --plaintext fileb://data.txt \
    --output text \
    --query CiphertextBlob | base64 \
    --decode > dataEncryptedFile

# be different region
# specify profile
aws kms decrypt \
    --region us-west-2 \
    --profile eksadmin1 \
    --ciphertext-blob fileb://dataEncryptedFile \
    --key-id b6713452-966f-4348-90bf-d5083f8f1fdc \
    --output text \
    --query Plaintext | base64 \
    --decode > dataDecryptedFile

Giờ mình limit user trong key policy

{
    "Version": "2012-10-17",
    "Id": "key-default-1",
    "Statement": [
        {
            "Sid": "Enable IAM User Permissions",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::794155915849:root"
            },
            "Action": "kms:*",
            "Resource": "*"
        },
        // Below content of other AWS account
        // Particular User
        {
            "Sid": "Enable IAM User Permissions of other AWS account",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::250887682577:user/eksadmin1"
            },
            "Action": "kms:*",
            "Resource": "*"
        }
    ]
}

Giờ minh thực hiện decrypt

aws kms decrypt \
    --region us-west-2 \
    --ciphertext-blob fileb://dataEncryptedFile \
    --key-id b6713452-966f-4348-90bf-d5083f8f1fdc \
    --output text \
    --query Plaintext | base64 \
    --decode > dataDecryptedFile
==> error
====> 
An error occurred (AccessDeniedException) when calling the Decrypt operation: The ciphertext refers to a customer master key that does not exist, does not exist in this region, or you are not allowed to access.

aws kms decrypt \
    --region us-west-2 \
    --profile eksadmin1 \
    --ciphertext-blob fileb://dataEncryptedFile \
    --key-id b6713452-966f-4348-90bf-d5083f8f1fdc \
    --output text \
    --query Plaintext | base64 \
    --decode > dataDecryptedFile
===> this ok

4.6) Get key policy to follow KMS

Chúng ta có thể kiểm tra key policy bằng command

aws kms get-key-policy \
  --region <region_name> \
  --profile <profile_name> \
  --key-id arn:aws:kms:<region_name>:<ACCOUNT_ID>:key/<KMS_ID> \
  --policy-name default \
  --output text

>>>>output
>>>>>>
{
  "Version" : "2012-10-17",
  "Id" : "key-default-1",
  "Statement" : [ {
    "Sid" : "Enable IAM User Permissions",
    "Effect" : "Allow",
    "Principal" : {
      "AWS" : "arn:aws:iam::2923917XXXXX:root"
    },
    "Action" : "kms:*",
    "Resource" : "*"
  } ]
}

4.7) Edit key policy to follow KMS

giờ bạn chỉnh sửa key policy của KMS bằng aws cli

vi key_policy_flatform.json

{
  "Version" : "2012-10-17",
  "Id" : "key-default-1",
  "Statement" : [ {
    "Sid" : "Enable IAM User Permissions",
    "Effect" : "Allow",
    "Principal" : {
      "AWS" : "arn:aws:iam::2923917XXXXX:root"
    },
    "Action" : "kms:*",
    "Resource" : "*"
  } ]
}

aws kms put-key-policy \
  --region <region_name> \
  --profile <profile_name> \
  --key-id arn:aws:kms:us-west-2:<ACCOUNT_ID>:key/<KMS_ID> \
  --policy-name default \
  --policy file://key_policy_flatform.json
AWS - Amazon Web Service

Post navigation

Previous Post: [AWS] Demo “code build” with experiment easily on AWS
Next Post: [Verdaccio] Creating a Local Private NPM registry

More Related Articles

[AWS] VPC PEERING – Connecting between other VPCs. AWS - Amazon Web Service
[aws] AWS refund AWS - Amazon Web Service
[AWS] Creat Persistent Volume on EKS via EBS. AWS - Amazon Web Service
[AWS Fault Injection Service] Crash Simulation on AWS AWS - Amazon Web Service
[Kubernetes] the exciting things about K8S AWS - Amazon Web Service
[Terraform] – Terraform Beginner – Lesson 5: Terraform Provisioners and creating EC2 AWS - Amazon Web Service

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Tham Gia Group DevOps nhé!
Để Nim có nhiều động lực ra nhiều bài viết.
Để nhận được những thông báo mới nhất.

Recent Posts

  • [Azure] The subscription is not registered to use namespace ‘Microsoft.ContainerService’ May 8, 2025
  • [Azure] Insufficient regional vcpu quota left May 8, 2025
  • [WordPress] How to add a Dynamic watermark on WordPress. May 6, 2025
  • [vnet/Azure] VNet provisioning via Terraform. April 28, 2025
  • [tracetcp] How to perform a tracert command using a specific port. April 3, 2025

Archives

  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • July 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Categories

  • BareMetal
    • NextCloud
  • CI/CD
    • Argo Events
    • ArgoCD
    • ArgoWorkflows
    • Git
      • Bitbucket
    • Harbor
    • Jenkins
    • Spinnaker
    • TeamCity
  • Coding
    • DevSecOps
    • Golang
    • Jquery & JavaScript
    • Laravel
    • NextJS 14 & ReactJS & Type Script
    • Python
    • Selenium
    • Terraform
      • AWS – Amazon Web Service
      • Azure Cloud
      • GCP – Google Cloud
  • Kubernetes & Container
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Docker
    • Helm Chart
    • Isito-EnvoyFilter
    • Kong Gateway
    • Kubernetes
      • Ingress
      • Pod
    • Longhorn – Storage
    • MetalLB
    • OAuth2 Proxy
    • Vault
    • VictoriaMetrics
  • Log, Monitor & Tracing
    • DataDog
    • ELK
      • Kibana
      • Logstash
    • Fluent
    • Grafana
    • Prometheus
  • Uncategorized
  • Admin

Copyright © 2025 NimTechnology.