附註:
- 此教學課程需要存取 Oracle Cloud。若要註冊免費帳戶,請參閱 Oracle Cloud Infrastructure Free Tier 入門。
- 它使用 Oracle Cloud Infrastructure 證明資料、租用戶及區間的範例值。完成實驗室時,請將這些值取代為您雲端環境特定的值。
Migrate Secrets from AWS Secrets Manager to Oracle Cloud Infrastructure Secrets in Vault
簡介
將加密密碼從 Amazon Web Services (AWS) 移轉至 Oracle Cloud Infrastructure (OCI) 的優點:
- 集中式加密密碼管理:如果您已經使用 OCI 作為基礎架構,則在 OCI Vault 中保留所有加密密碼可確保安全原則一致,以簡化管理。
- 規範和安全: OCI Vault 可透過提供自訂金鑰、存取控制和稽核日誌的加密,與許多需要客戶管理控制的 OCI 服務整合。
- 成本考量: OCI Vault 的定價和儲存選項可能比 AWS Secrets Manager 更具成本效益,視您的使用情況而定。
OCI 保存庫中支援的加密密碼類型
-
OCI 加密密碼服務支援任何類型的加密密碼,包括 PASSPHRASE、SSH_KEY 和 BYTES。雖然這些類型可以由 OCI 自動產生,但手動建立加密密碼時,並不限於這些類型。確定 AWS 中的加密密碼類型與 OCI 接受的格式相對應。如需詳細資訊,請參閱管理 Vault 加密密碼。
-
OCI 保存庫的加密密碼內容必須是 base64 格式,可確保二進位資料的安全儲存。
目標
- 本教學課程示範如何使用 Python 將儲存在 AWS Secrets Manager 中的加密密碼移轉至 OCI 保存庫。您將瞭解如何從 AWS 擷取加密密碼,並以 base64 編碼將加密密碼安全地儲存在金鑰保存庫中的 OCI 加密密碼中。
必要條件
-
AWS 和 OCI SDK:請確定您的環境中已安裝
boto3
和oci
程式庫。這些分別是 AWS 和 OCI 的官方 SDK。執行下列命令以進行安裝。pip install boto3 oci
-
OCI 保存庫設定:
-
保存庫 ID:將儲存加密密碼之 OCI 保存庫的唯一 ID。
-
金鑰 ID:加密 OCI 保存庫中加密密碼的加密金鑰。
-
區間 ID:保存庫所在的 OCI 區間。
-
-
AWS 證明資料:使用
aws configure
或環境變數,確保已在 OCI Cloud Shell 或 OCI CLI 中正確設定 AWS 證明資料。如需詳細資訊,請參閱使用 AWS CLI 的 IAM 使用者證明資料進行認證。 -
OCI 組態:使用 OCI Cloud Shell 或 OCI CLI 使用您的 OCI 證明資料正確設定
~/.oci/config
。如需詳細資訊,請參閱 Installing the CLI 。
將加密密碼從 AWS 移轉至 OCI
-
擷取及檢視 AWS 加密密碼。若要從 AWS 移轉加密密碼,請先確定您有要傳輸的加密密碼清單。在此範例中,我們移轉了兩個加密密碼:
awssecret4
和awssecret5
。下列螢幕擷取畫面顯示移轉前 AWS 加密密碼管理員中的加密密碼。
-
在 OCI 中建立保存庫和加密金鑰。若要設定加密密碼的 OCI 保存庫和金鑰,請參閱建立主要加密金鑰。
-
使用 Python 命令檔移轉加密密碼。將下列 Python 命令檔下載並儲存為
aws_to_oci_secret_migration.py
。此命令檔會自動化擷取 AWS 加密密碼並將其儲存在 OCI 保存庫中的處理作業。注意:將預留位置 (例如
your_aws_secret1
、your_aws_region
、your_oci_vault_id
等) 取代為實際值。指定的前置碼將用於在保存庫中建立加密密碼,以協助隔離使用命令檔建立加密密碼。Script:
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 保存庫中驗證新建立的加密密碼。
接下來的步驟
將加密密碼從 AWS 移轉至 OCI 可協助您簡化作業,特別是將 OCI 運用在其餘的雲端基礎架構上。透過使用 Python 自動化,您可以有效率地傳輸加密密碼,確保加密密碼安全地儲存並符合 OCI 的加密標準。
認可
- 作者 - Vishak Chittuvalapil (雲端資深工程師)
其他學習資源
探索 docs.oracle.com/learn 上的其他實驗室,或存取 Oracle Learning YouTube 頻道上的更多免費學習內容。此外,請造訪 education.oracle.com/learning-explorer 以成為 Oracle Learning Explorer。
如需產品文件,請造訪 Oracle Help Center 。
Migrate Secrets from AWS Secrets Manager to Oracle Cloud Infrastructure Secrets in Vault
G17356-02
October 2024