注意:
- 本教程需要访问 Oracle Cloud。要注册免费账户,请参阅开始使用 Oracle Cloud Infrastructure 免费套餐。
- 它对 Oracle Cloud Infrastructure 身份证明、租户和区间使用示例值。完成实验室后,请使用特定于云环境的那些值替换这些值。
将密钥从 AWS Secrets Manager 迁移到 VaultOracle Cloud Infrastructure Secrets in Vault 中的 Oracle Cloud Infrastructure Secrets
简介
将密钥从 Amazon Web Services (AWS) 迁移到 Oracle Cloud Infrastructure (OCI) 时的优势:
- 集中式密钥管理:如果您已将 OCI 用于基础设施,则将所有密钥保存在 OCI Vault 中可以确保安全策略一致,从而简化管理。
- 合规性和安全性: OCI Vault 通过提供定制密钥、访问控制和审计日志加密功能,与需要客户管理的许多 OCI 服务相集成。
- 成本考虑: OCI Vault 的定价和存储选项可能比 AWS Secrets Manager 更经济高效,具体取决于您的使用情况。
OCI Vault 中支持的密钥类型
-
OCI 密钥服务支持任何类型的密钥,包括 PASSPHRASE、SSH_KEY 和 BYTES。虽然这些类型可以由 OCI 自动生成,但手动创建密钥时,您并不局限于这些类型。确保 AWS 中的密钥类型与 OCI 接受的格式相对应。有关详细信息,请参阅管理 Vault 密钥。
-
OCI Vault 要求密钥内容采用 base64 格式,从而确保二进制数据的安全存储。
目标
- 本教程演示如何使用 Python 将 AWS Secrets Manager 中存储的密钥迁移到 OCI Vault。您将学习如何从 AWS 检索密钥,并使用 base64 编码将密钥安全地存储在 Vault 中的 OCI 密钥中。
先决条件
-
AWS 和 OCI SDK:确保您的环境中安装了
boto3
和oci
库。这些分别是 AWS 和 OCI 的官方 SDK。请运行如下命令进行安装。pip install boto3 oci
-
OCI Vault 设置:
-
Vault ID:将存储密钥的 OCI Vault 的唯一标识符。
-
密钥 ID:用于加密 OCI Vault 中的密钥的加密密钥。
-
区间 ID: Vault 所在的 OCI 区间。
-
-
AWS 身份证明:确保您在 OCI Cloud Shell 或 OCI CLI 中使用
aws configure
或通过环境变量正确配置了 AWS 身份证明。有关更多信息,请参见使用 AWS CLI 的 IAM 用户凭据进行验证。 -
OCI 配置:确保使用 OCI Cloud Shell 或 OCI CLI 与 OCI 身份证明正确设置了
~/.oci/config
。有关更多信息,请参见 Installing the CLI 。
将密钥从 AWS 迁移到 OCI
-
检索和查看 AWS 密钥。要从 AWS 迁移密钥,请首先确保您具有要传输的密钥列表。在本示例中,我们将迁移两个密钥:
awssecret4
和awssecret5
。以下屏幕截图显示了迁移前 AWS Secrets Manager 中的机密。
-
在 OCI 中创建 Vault 和加密密钥。要设置 OCI Vault 和密钥以加密密钥,请参见 Creating a Master Encryption Key 。
-
使用 Python 脚本迁移密钥。下载以下 Python 脚本并将其另存为
aws_to_oci_secret_migration.py
。该脚本自动执行检索 AWS 密钥并将其存储在 OCI Vault 中的过程。注意:将
your_aws_secret1
、your_aws_region
、your_oci_vault_id
等占位符替换为实际值。指定的前缀将用于在 Vault 中创建密钥,有助于使用脚本隔离密钥创建。脚本:
import boto3 # AWS SDK to interact with AWS services import oci # OCI SDK to interact with Oracle Cloud import base64 # To handle base64 encoding from botocore.exceptions import ClientError # For handling errors with AWS # AWS configuration AWS_SECRET_NAMES = ["your_aws_secret1", "your_aws_secret2"] # List of AWS secrets to migrate AWS_REGION = "your_aws_region" # AWS region where the secrets are stored # OCI configuration VAULT_ID = "your_oci_vault_id" # OCI Vault ID COMPARTMENT_ID = "your_oci_compartment_id" # OCI Compartment ID KEY_ID = "your_oci_key_id" # OCI Key ID OCI_SECRET_NAME_PREFIX = "your_prefix" # Prefix for the secret names in OCI Vault def get_aws_secret(secret_name): """Retrieve the secret value from AWS Secrets Manager.""" session = boto3.session.Session() # Create a session with AWS client = session.client(service_name='secretsmanager', region_name=AWS_REGION) # Create a Secrets Manager client try: # Get the secret value get_secret_value_response = client.get_secret_value(SecretId=secret_name) secret = get_secret_value_response['SecretString'] # This is the actual secret data return secret except ClientError as e: print(f"Error retrieving secret from AWS: {e}") # In case something goes wrong return None def create_oci_secret(secret_content, secret_name): """Create a new secret in OCI Vault.""" config = oci.config.from_file() # This loads your OCI configuration from ~/.oci/config vaults_client = oci.vault.VaultsClient(config) # Use the VaultsClient to create a secret in OCI Vault # Encode secret content to Base64 format secret_content_base64 = base64.b64encode(secret_content.encode('utf-8')).decode('utf-8') # Proper base64 encoding try: # Creating a new secret in OCI Vault create_secret_response = vaults_client.create_secret( create_secret_details=oci.vault.models.CreateSecretDetails( vault_id=VAULT_ID, compartment_id=COMPARTMENT_ID, secret_name=secret_name, key_id=KEY_ID, secret_content=oci.vault.models.Base64SecretContentDetails( content=secret_content_base64, # Base64 encoded content content_type="BASE64" # Specifying the content type as BASE64 ), description="Migrated from AWS Secrets Manager" # A brief description ) ) print(f"Secret '{secret_name}' successfully created in OCI Vault.") except oci.exceptions.ServiceError as e: print(f"Error creating secret in OCI Vault: {e}") # If something goes wrong def main(): # Step 1: Retrieve and migrate secrets from AWS Secrets Manager for aws_secret_name in AWS_SECRET_NAMES: aws_secret_content = get_aws_secret(aws_secret_name) if aws_secret_content: # Generate OCI secret name based on AWS secret name oci_secret_name = f"{OCI_SECRET_NAME_PREFIX}_{aws_secret_name}" # Step 2: Create the secret in OCI Vault create_oci_secret(aws_secret_content, oci_secret_name) if __name__ == "__main__": main()
-
保存脚本后,使用以下命令运行该脚本。
python3 aws_to_oci_secret_migration.py
-
成功运行脚本后,您可以在 OCI Vault 中验证新创建的密钥。
后续步骤
将密钥从 AWS 迁移到 OCI 可帮助您简化运营,尤其是当您将 OCI 用于其他云基础设施时。通过使用 Python 自动化,您可以高效传输密钥,确保密钥安全存储并符合 OCI 的加密标准。
确认
- 作者 — Vishak Chittuvalapil(高级云工程师)
更多学习资源
浏览 docs.oracle.com/learn 上的其他实验室,或者访问 Oracle Learning YouTube 渠道上的更多免费学习内容。此外,请访问 education.oracle.com/learning-explorer 成为 Oracle Learning Explorer。
有关产品文档,请访问 Oracle 帮助中心。
Migrate Secrets from AWS Secrets Manager to Oracle Cloud Infrastructure Secrets in Vault
G17355-02
October 2024