Google密钥管理(GCP KMS):入门指南

确保数据安全是任何组织的首要任务,而加密是保护数据的最有效方法之一。但具体是如何实现的呢?Google Cloud Platform(GCP)提供了一种称为Google Key Management Service(KMS)的关键管理服务,可以让您快速创建和管理加密密钥。

在本教程中,您将学习如何在GCP KMS中创建和使用密钥。您还将深入了解与密钥管理相关的一些重要概念,如密钥、密钥环和密钥版本。

准备好了吗?继续阅读,将您的数据安全提升到一个新的水平!

先决条件

本教程将进行实际演示。要跟着做,请确保您拥有一个具有活动计费的GCP帐户,不过一个免费试用也足够了。

A Google Cloud Project is a collection of resources you create and manage in GCP. Note that you should create separate projects for each of your applications. Doing so lets you efficiently manage the resources for each application. In the end, you can delete all the resources associated with a project, which is helpful for clean-up.

要创建一个新的Google Cloud项目:

1. 打开您喜欢的网络浏览器,登录到GCP控制台。就像AWS控制台一样,GCP控制台是用于管理Google Cloud中资源的基于Web的界面。

2. 在GCP控制台上,导航到管理资源页面,您可以在该页面上看到并管理所有现有项目。

3. 接下来,点击“创建项目”按钮以创建新项目。

Creating a new Google Cloud project

最后,为新项目配置以下设置,并点击创建来创建您的项目。

  • 项目名称 – 为您的项目输入一个名称,该名称无需唯一,并且稍后可以更改。本教程选择的项目名称是gcp-kms-demo

  • 位置 – 选择要创建此项目的父组织或文件夹。您新创建的项目将是顶级父项目。但如果您没有组织,可以跳过此字段。

如果使用试用帐户,可能不会显示“位置”字段。

Configuring a new Google Cloud project

如果成功,您将看到下面显示的新创建的Google Cloud项目。

Viewing the newly-created Google Cloud project

为Google Cloud项目启用Cloud KMS API

您刚刚创建了您的第一个如果成功,您将看到下面显示的新创建的Google Cloud项目。Cloud项目,通常您不能将其保持无保护状态。不用担心!Cloud KMS API是一组由Google提供的加密API,您可以使用它来保护您的数据。

要使用Cloud KMS API,您需要为您的项目启用它:

1. 转到Google项目选择器页面,选择您的Google Cloud项目(gcp-kms-demo)。

Selecting a project

2. 接下来,转到启用对API的访问页面,点击“下一步”以确认为所选项目启用API。

Confirming the selected Google Cloud project

3. 现在,点击“启用”以启用Cloud KMS API。

Enabling the Cloud KMS API

这时,Cloud KMS API已为您的项目启用。您现在可以开始使用Cloud KMS API来创建和管理加密密钥。

Verifying the Cloud KMS API is enabled

启动Cloud Shell以创建密钥和密钥环

启用Cloud KMS API将使您能够创建用于加密和解密数据的密钥和密钥环。密钥和密钥环是Cloud KMS中的基本概念。

Key A key is an encryption key stored in a keyring that you can use to encrypt and decrypt your data. Each key has a name, and the name must be unique within the keyring. You can use IAM ( Identity and Access Management) policies to control who has access to keys.
Keyring A keyring is a container in Cloud KMS that holds a set of keys. You can think of the keyring as a folder on your computer that contains many files. But in essence, a keyring holds a set of keys in a specific Cloud KMS location.

但在创建密钥和密钥环之前,您首先需要启动Cloud Shell。Cloud Shell是一个免费的交互式Shell,您可以使用它在Google Cloud Platform上运行命令,而无需在您的计算机上安装任何工具。

1. 在GCP控制台中,点击激活Cloud Shell按钮(右上角),如下所示,以启动Cloud Shell。

Starting the Cloud Shell

A Cloud Shell session initializes, as shown below (at the bottom of the page), where you can run commands.

Viewing the Cloud Shell prompt

2. 接下来,在Cloud Shell中运行以下gcloud命令以创建一个名为kms-demo-keyring的新密钥环(keyrings),其全局位置为“global”

gcloud kms keyrings create "kms-demo-keyring" \
--location "global"

–location “global”

在提示时,点击“授权”以继续运行命令。

Authorizing access to the KSM API

第一次在Cloud Shell中运行gcloud命令时会出现此提示,询问您是否授权gcloud工具访问KSM API。

Creating a new keyring

如下所示,当密钥环成功创建时不会输出任何消息。

相关:使用AWS密钥管理专业知识使用加密密钥

gcloud kms keys create "kms-demo-key01" \
  --location "global" \
  --keyring "kms-demo-keyring" \
  --purpose "encryption"

请注意,位置(global)必须与您为密钥环设置的位置相同。

–location “global” \

–keyring “kms-demo-keyring” \

Creating a key for encryption

–purpose “encryption”

gcloud kms keys list --location "global" --keyring "kms-demo-keyring"

每次创建或轮换密钥时,Cloud KMS都会创建密钥的新版本。这个功能让您可以在必要时轻松回滚到以前的版本。

PURPOSE ENCRYPT_DECRYPT Indicates the purpose of the key (encryption and decryption).
ALGORITHM GOOGLE_SYMMETRIC_ENCRYPTION Indicates the key uses Google Symmetric Encryption, the default algorithm for Cloud KMS keys.
PROTECTION_LEVEL SOFTWARE Indicates the key is software-protected.
LABELS You can add labels to keys to categorize them or to add additional information.
PRIMARY ID 1 Indicates the key is the primary key for the keyring, which is what Cloud KMS uses by default when you do not specify a key.

Note that you can have only one primary key per keyring.

PRIMARY STATE ENABLED Shows the current status of the key. The ENABLED state indicates the key can be used for its specified purpose.
Listing the keys in a keyring

就像创建密钥环一样,当密钥成功创建时不会输出任何消息。

但是在这一点上,您有一个可以用来加密和解密数据的密钥环和密钥。

4. 现在,运行以下命令来列出–keyring(kms-demo-keyring)中的密钥

您将会看到类似以下截图的输出,信息如下:

echo "This is some sensitive data that I want to encrypt." > gcp-kms-demo.txt

使用 GCP KMS 加密和解密数据

加密是防止恶意方访问敏感数据的重要防线。现在您有了加密密钥,可以使用它来加密您的数据。
ls cat gcp-kms-demo.txt
使用 GCP KMS 加密数据的步骤如下:
cat gcp-kms-demo.txt
Listing the text file and verifying the data

1. 运行下面的命令,它不会提供输出,但会创建一个文本文件(gcp-kms-demo.txt),其中包含一些要加密的文本。

2. 接下来,运行以下每个命令来列出(ls)文本文件(gcp-kms-demo.txt)并查看(cat)其内容以验证数据。

gcloud kms encrypt \
    --location "global" \
    --keyring "kms-demo-keyring" \
    --key "kms-demo-key01" \
    --plaintext-file ./gcp-kms-demo.txt \
    --ciphertext-file ./gcp-kms-demo.txt.encrypted
Encrypting data

# 列出文本文件

cat gcp-kms-demo.txt.encrypted

# 查看文本文件的内容

3. 验证完毕后,运行以下命令来使用您的密钥(kms-demo-key01)加密文件(gcp-kms-demo.txt)中的数据。加密后的数据将存储在一个名为 gcp-kms-demo.txt.encrypted 的新文件中。

Verifying the file data is encrypted

–location “global” \

–keyring “kms-demo-keyring” \

gcloud kms decrypt \
  --location "global" \
  --keyring "kms-demo-keyring" \
  --key "kms-demo-key01" \
  --ciphertext-file ./gcp-kms-demo.txt.encrypted \
  --plaintext-file ./gcp-kms-demo.txt.decrypted
Decrypting encrypted data

–key “kms-demo-key01” \

cat gcp-kms-demo.txt.decrypted

–plaintext-file ./gcp-kms-demo.txt \

Verifying the encrypted file has been decrypted

–ciphertext-file ./gcp-kms-demo.txt.encrypted

4. 现在,运行以下命令来尝试查看加密文件的数据(gcp-kms-demo.txt.encrypted)。

您将看到破碎的、奇怪的、难以理解的符号,如下所示。此输出表明文件已成功加密。

gcloud kms keys versions list \
  --location "global" \
  --keyring "kms-demo-keyring" \
  --key "kms-demo-key01"

您需要将此加密文件存储在安全位置。例如,您可以将多个副本存储在AWS S3、GCP Cloud Storage或Azure Blob Storage中。Google Cloud不会保存您数据的明文版本。因此,如果您丢失了加密文件,将其解密以恢复原始数据是不可能的。

Listing all the key versions

相关:如何使用AWS CLI上传文件到S3

5. 加密后,运行以下命令使用相同的密钥(kms-demo-key01)解密您的数据(gcp-kms-demo.txt.encrypted)。但这次,解密数据存储在一个名为gcp-kms-demo.txt.decrypted的新文件中。

gcloud kms keys versions destroy 1\
  --location "global" \
  --keyring "kms-demo-keyring" \
  --key "kms-demo-key01"

与加密一样,此命令不提供输出,但您将在下一步验证解密。

gcloud kms keys versions list

–location “global” \

–keyring “kms-demo-keyring” \

Verifying the key has been scheduled for destruction

–key “kms-demo-key01” \

Deleting the Google Cloud project

–ciphertext-file ./gcp-kms-demo.txt.encrypted \

–plaintext-file ./gcp-kms-demo.txt.decrypted

Confirming Google Cloud project deletion

最后,运行以下命令查看(cat)解密后的文件(gcp-kms-demo.txt.decrypted),并验证数据是否成功解密。

在下面,您可以看到您保存在文本文件中的原始数据。恭喜!您已成功使用Google Cloud KMS加密和解密数据。

清理Google Cloud资源

Source:
https://adamtheautomator.com/gcp-kms/