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
    • Helm Chart
    • Isito-EnvoyFilter
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Vault
    • Longhorn – Storage
    • VictoriaMetrics
    • MetalLB
    • Kong Gateway
  • CI/CD
    • ArgoCD
    • ArgoWorkflows
    • Spinnaker
    • Jenkins
    • Harbor
    • TeamCity
    • Git
      • Bitbucket
  • Coding
    • Terraform
      • GCP – Google Cloud
      • AWS – Amazon Web Service
    • Golang
    • Laravel
    • Python
    • Jquery & JavaScript
    • Selenium
  • Log & Monitor
    • DataDog
    • Prometheus
    • Grafana
    • ELK
      • Kibana
      • Logstash
  • BareMetal
    • NextCloud
  • Toggle search form

[AWS] Encrypting your data easily via KMS on AWS

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

Contents

  • 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 fedramp-dev-mdcl-mdaas-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

[MongoDB] Creating MongoDB Atlas to integrate with your workload on any Cloud AWS - Amazon Web Service
[AWS] Login Argocd via Cognito in AWS ArgoCD
Experiences for IP Addresses Shortage on EKS Clusters AWS - Amazon Web Service
[AWS] Pull docker images from the other ECR AWS - Amazon Web Service
[Terraform] – Terraform Beginner – Lesson 8: Terraform Functions and Conditional Expressions AWS - Amazon Web Service
[Terraform] – Terraform Beginner – Lesson 4: Remote State 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

  • Experiences for IP Addresses Shortage on EKS Clusters March 29, 2023
  • [Talisman] Discover the sensitive information in your code. March 28, 2023
  • [Prometheus/Grafana] Install Prometheus and Grafana on ubuntu. March 27, 2023
  • [Kong Gateway] WebSocket connection failed March 26, 2023
  • [Nextcloud] Can’t download files to have a size bigger than 2Gi on NextCloud – RaspBerry March 24, 2023

Archives

  • 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
    • ArgoCD
    • ArgoWorkflows
    • Git
      • Bitbucket
    • Harbor
    • Jenkins
    • Spinnaker
    • TeamCity
  • Coding
    • Golang
    • Jquery & JavaScript
    • Laravel
    • Python
    • Selenium
    • Terraform
      • AWS – Amazon Web Service
      • GCP – Google Cloud
  • Kubernetes & Container
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Docker
    • Helm Chart
    • Isito-EnvoyFilter
    • Kong Gateway
    • Kubernetes
      • Ingress
    • Longhorn – Storage
    • MetalLB
    • Vault
    • VictoriaMetrics
  • Log & Monitor
    • DataDog
    • ELK
      • Kibana
      • Logstash
    • Grafana
    • Prometheus
  • Uncategorized
  • Admin

Copyright © 2023 NimTechnology.