ノート:

AWSシークレット・マネージャからOracle Cloud Infrastructure Secrets in Vaultへのシークレットの移行

イントロダクション

Amazon Web Services (AWS)からOracle Cloud Infrastructure (OCI)にシークレットを移行する場合のメリット:

OCI Vaultでサポートされているシークレット・タイプ

目的

前提条件

AWSからOCIへのシークレットの移行

  1. AWSシークレットを取得して表示します。AWSからシークレットを移行するには、まず転送するシークレットのリストがあることを確認します。この例では、awssecret4awssecret5の2つのシークレットを移行しています。

    「編集」ボタン

    次のスクリーンショットは、移行前のAWSシークレット・マネージャのシークレットを示しています。

    「編集」ボタン

    「編集」ボタン

  2. OCIにボールトと暗号化キーを作成します。シークレットを暗号化するためのOCI Vaultおよびキーを設定するには、マスター暗号化キーの作成を参照してください。

    「編集」ボタン

    「編集」ボタン

  3. Pythonスクリプトを使用してシークレットを移行します。次のPythonスクリプトをダウンロードし、aws_to_oci_secret_migration.pyとして保存します。このスクリプトは、AWSシークレットを取得し、OCI Vaultに格納するプロセスを自動化します。

    ノート: your_aws_secret1your_aws_regionyour_oci_vault_idなどのプレースホルダを実際の値に置き換えます。指定された接頭辞は、スクリプトを使用してシークレットの作成を分離するのに役立つ、ボールト内のシークレットの作成に使用されます。

    スクリプト:

    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()
    
  4. スクリプトを保存したら、次のコマンドで実行します。

    python3 aws_to_oci_secret_migration.py
    

    「編集」ボタン

  5. スクリプトを正常に実行した後、OCI Vaultで新しく作成されたシークレットを確認できます。

    「編集」ボタン

    「編集」ボタン

次のステップ

AWSからOCIへのシークレットの移行は、特に他のクラウド・インフラストラクチャにOCIを活用している場合、業務の合理化に役立ちます。Pythonの自動化を使用することで、シークレットを効率的に転送し、セキュアに保存してOCIの暗号化標準に準拠していることを確認できます。

承認

その他の学習リソース

docs.oracle.com/learnの他のラボを確認するか、Oracle Learning YouTubeチャネルで無料のラーニング・コンテンツにアクセスしてください。また、education.oracle.com/learning-explorerにアクセスしてOracle Learning Explorerになります。

製品ドキュメントは、Oracle Help Centerを参照してください。