Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Securing cloud storage files with KMS
Sadhan Sarker
Sadhan Sarker

Posted on • Edited on

     

Securing cloud storage files with KMS

Cloud Key Management Service (KMS) allows us to create, import & manage cryptographic keys and also perform cryptographic operations in a single centralized cloud service the same way on-premises. By usingCloud KMS,Cloud HSM, orCloud External Key Manager orCustomer-Managed Encryption Keys (CMEK integrations) we can encrypt, decrypt, and verify.

In this post, we are going to deal withCloud KMS,Cloud Storage,Cloud SDK. Also, learn about encryption and manage encryption keys using KMS. So, let’s a drive-in. Sign-in to Google Cloud Platform(GCP) Console and Create a new project and activate our Cloud Shell.

Alt Text

ClickContinue

Alt Text

Create a Cloud Storage Bucket

Create Cloud Storage Bucket, we can do that usinggsutil, remember bucket names are globally unique. Run the following command in Cloud Shell to set a variable to our bucket name:

exportCLOUD_STORAGE_BUCKET_NAME=put_our_unique_bucket_name
Enter fullscreen modeExit fullscreen mode

Now, just hit the following command, to create a new cloud storage bucket,

gsutil mb gs://${CLOUD_STORAGE_BUCKET_NAME}
Enter fullscreen modeExit fullscreen mode

Alt Text

Create a sample data

Create a simple file so that we can encrypt & decrypt that file, Open Cloud Shell and create a new file, Here, I’m usingVim console-based text editor. If we want, we can download the source file from another location. To create a new file, run the following command:

vi hello.txt
Enter fullscreen modeExit fullscreen mode

Input: to insert content pressi,

Hello! From Cloud Storage KMS. We are going to encrypt and decrypt this file using Cloud KMS.
Enter fullscreen modeExit fullscreen mode

Note: To save thathello.txt file pressCtrl+c and type:wq . To read that file content hit below commands,

cathello.txt
Enter fullscreen modeExit fullscreen mode

Output:

Hello! From Cloud Storage KMS. We are going to encrypt and decrypt this file using Cloud KMS.
Enter fullscreen modeExit fullscreen mode

Enable Cloud KMS Service

Before using Cloud KMS, we must need to enable that service. It could be done fromCloud Console UI and another is from gcloud CLI command. To enable the Cloud KMS Service, run the following command in our Cloud Shell:

gcloud servicesenablecloudkms.googleapis.com
Enter fullscreen modeExit fullscreen mode

Optional, this only needs to be done once per project

gcloud servicesenablecloudkms.googleapis.com\--project"${GOOGLE_CLOUD_PROJECT}"
Enter fullscreen modeExit fullscreen mode

Create KeyRing and CryptoKey

In order to encrypt & decrypt data, we need to create a KeyRing and a CryptoKey. KeyRings are useful for Grouping keys. To create KeyRing for a global region:

gcloud kms keyrings create"our-keyring"--location"global"
Enter fullscreen modeExit fullscreen mode

Note: If we want to view that newly created key thenOpen Web UI.

Next, using the new KeyRing, create a CryptoKey

gcloud kms keys create"our-cryptokey"\--location"global"\--keyring"our-keyring"\--purpose"encryption"
Enter fullscreen modeExit fullscreen mode

From Web UI We can view that Keys,

Alt Text

Click onour-keyring then we are able to seeour-cryptokey, which is group together

Alt Text

Encrypt our file

Encrypt thehello.txt file contents using Cloud KMS. Here, I’m using the gcloud command-line tool. But we can also encrypt data using theCloud KMS API.

gcloud kms encrypt--location"global"\--keyring"our-keyring"--key"our-cryptokey"\--plaintext-file ./hello.txt\--ciphertext-file ./hello.enc
Enter fullscreen modeExit fullscreen mode

This will create ahello.enc file which will be encrypted. To open that encrypt file run:

cathello.enc
Enter fullscreen modeExit fullscreen mode

Output: Cloud be like an unreadable hash like “6B!h>X7^RR*IRt;_*b~0IrP1<)]'ǞЉt c”

Now, we can upload that encrypted file to the Cloud Storage, run the following command

gsutilcp ./hello.enc gs://${CLOUD_STORAGE_BUCKET_NAME}
Enter fullscreen modeExit fullscreen mode

We can view our encrypted file which actually uploaded,

Alt Text

Decrypt our file

If we want to decrypt thathello.enc. Or, we have already encrypted data then we can copy that from Cloud Storage bucket by the following command,

gsutilcpgs://${CLOUD_STORAGE_BUCKET_NAME}/hello.enc.
Enter fullscreen modeExit fullscreen mode

Note: In this case, we don’t have to do that because we already have ourhello.enc file.

Now, We can decrypt that file by the following command below,

gcloud kms decrypt--location"global"\--keyring"our-keyring"--key"our-cryptokey"\--ciphertext-file ./hello.enc\--plaintext-file ./hello-decryped.txt
Enter fullscreen modeExit fullscreen mode

To open thathello-decryped.txt file run following command

cathello-decryped.txt
Enter fullscreen modeExit fullscreen mode

Output:

Hello! From Cloud Storage KMS. We are going to encrypt and decrypt this file using Cloud KMS.
Enter fullscreen modeExit fullscreen mode

Cleanup environment

To delete cloud storage bucket, which we created earlier, run the following command

gsutilrm-r gs://${CLOUD_STORAGE_BUCKET_NAME}
Enter fullscreen modeExit fullscreen mode

Note: Cloud KMS resources can’t be deleted. However, we can destroy that by the following command

gcloud kms keys versions destroy"1"\--location"global"\--key"our-cryptokey"\--keyring"our-keyring"
Enter fullscreen modeExit fullscreen mode

Congratulations

We have successfully encrypted and decrypt data using Cloud KMS and stored encrypted data in Cloud Storage. Thanks for time & passion. Feel free to ask me anything.

Say Hi to me onTwitter,Linkedin, andMedium where I keep on sharing interesting updates.

Original Post

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I’m very much eager about new tools and technologies. I love to work with the R&D Team. Till now my findings, I believe nothing is impossible just need focus, dedication, and time.
  • Location
    Dhaka, Bangladesh
  • Work
    Full-Stack Software Engineer
  • Joined

More fromSadhan Sarker

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp