AWS KMS密钥轮换会自动重新加密SSM参数存储中的SecureString值,以确保数据的安全性。以下是关于如何在AWS KMS和SSM中使用SecureString值的示例代码:
首先,我们需要创建一个KMS主密钥,并使用它来进行加密和解密操作。以下是创建主密钥的示例代码:
import boto3
kms = boto3.client('kms')
response = kms.create_key() key_id = response['KeyMetadata']['KeyId']
接下来,我们可以使用KMS主密钥来加密和解密SecureString值。以下是加密和解密SecureString值的示例代码:
import boto3
kms = boto3.client('kms') ssm = boto3.client('ssm')
key_id = 'my-kms-key-id'
plaintext = 'my-plaintext-secret'
response = kms.encrypt(KeyId=key_id, Plaintext=plaintext) ciphertext = response['CiphertextBlob']
response = ssm.put_parameter(Name='my-secret', Value=ciphertext, Type='SecureString', KeyId=key_id)
response = ssm.get_parameter(Name='my-secret', WithDecryption=True) ciphertext = response['Parameter']['Value']
response = kms.decrypt(CiphertextBlob=ciphertext) plaintext = response['Plaintext']
print(plaintext)
需要注意的是,在SSM参数存储中使用SecureString值时,必须指定与用于加密值的KMS主密钥相同的密钥ID。