IAM 教學課程:使用 AWS CloudFormation 範本建立 SAML Identity Provider (IdP) - AWS Identity and Access Management

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

IAM 教學課程:使用 AWS CloudFormation 範本建立 SAML Identity Provider (IdP)

若要為 AWS 您的帳戶設定 SAML 聯合,您需要建立 SAML 身分提供者 (IdP)。本教學課程說明如何使用 AWS CloudFormation 範本來建立 SAML IdP,以在 AWS 和外部 IdP 之間建立信任。

範本會建立使用 IdP 中繼資料文件設定的 SAML IdP。然後,聯合 IAM 角色可以參考此 IdP,以允許來自外部 IdP 的已驗證使用者存取 AWS 資源。

部署的資源包含使用 IdP 中繼資料文件設定的 SAML IdP,以及選用的加密設定。

先決條件

此教學課程假設您已備妥下列項目:

  • 在本機電腦上安裝 Python 3.6 或更新版本,以執行本教學課程中用於格式化 IdP SAML 中繼資料 XML 檔案的 Python 命令。

  • 外部 IdP 中儲存為 XML 檔案的 SAML 中繼資料文件。

使用 建立 SAML IdP AWS CloudFormation

若要建立 SAML IdP,您將建立 CloudFormation 範本,並使用它來建立包含 IdP 資源的堆疊。

建立範本

首先,建立 CloudFormation 範本。

  1. 範本區段中,按一下 JSONYAML 標籤上的複製圖示,以複製範本內容。

  2. 將範本內容貼到新檔案中。

  3. 在本機儲存檔案。

建立 堆疊。

接著,使用您儲存的範本來佈建 CloudFormation 堆疊。

  1. 在 https://https://console.aws.amazon.com/cloudformation 開啟 AWS CloudFormation 主控台。

  2. 堆疊頁面上,從建立堆疊功能表中,選擇使用新資源 (標準)

  3. 指定範本:

    1. 先決條件下,選擇選擇現有範本

    2. 指定範本下,選擇上傳範本檔案

    3. 選擇選擇檔案,導覽至範本檔案,然後選擇它。

    4. 選擇下一步

  4. 指定下列堆疊詳細資訊:

    1. 輸入堆疊名稱。

    2. 對於 IdentityProviderName,您可以將此保留空白,以根據堆疊名稱自動產生名稱,或輸入 SAML IdP 的自訂名稱。自訂名稱只能包含英數字元、句點、底線和連字號。

    3. 對於 IdentityProviderSAMLMetadataDocument,您需要將 SAML 中繼資料 XML 檔案格式化為單行,才能將其貼入此欄位。這是必要的,因為 CloudFormation 主控台要求 XML 內容在通過主控台參數時格式化為單行。

      使用下列 Python 命令來重新格式化 XML 檔案:

      python3 -c "import sys, re; content=open(sys.argv[1]).read(); print(re.sub(r'>\s+<', '><', content.replace('\n', '').replace('\r', '').strip()))" saml-metadata.xml
      注意

      IdP 的 SAML 中繼資料文件必須格式化為主控台參數輸入的單行。Python 命令會移除換行符號和額外的空格,以建立所需的格式,同時維護所有原始內容和結構。

      從 Python 命令複製輸出,並將其貼到 IdentityProviderSAMLMetadataDocument 欄位。

      格式化 SAML 中繼資料文件的範例 (縮寫):

      <?xml version="1.0" encoding="UTF-8"?><md:EntityDescriptor xmlns:md="urn:oasis:names:tc:SAML:2.0:metadata" entityID="https://portal.sso.example.com/saml/assertion/CompanyIdP"><md:IDPSSODescriptor WantAuthnRequestsSigned="false" protocolSupportEnumeration="urn:oasis:names:tc:SAML:2.0:protocol"><md:KeyDescriptor use="signing"><ds:KeyInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#"><ds:X509Data><ds:X509Certificate>MIIDXTCCAkWgAwIBAgIJAJC1HiIAZAiIMA0GCSqGSIb3DQEBBQUAMEUxCzAJBgNV...</ds:X509Certificate></ds:X509Data></ds:KeyInfo></md:KeyDescriptor><md:SingleLogoutService Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST" Location="https://portal.sso.example.com/saml/logout/CompanyIdP"/><md:NameIDFormat>urn:oasis:names:tc:SAML:2.0:nameid-format:persistent</md:NameIDFormat><md:SingleSignOnService Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST" Location="https://portal.sso.example.com/saml/assertion/CompanyIdP"/></md:IDPSSODescriptor></md:EntityDescriptor>
    4. 對於其他參數,請接受預設值或根據您的需求輸入您自己的值:

      • IdentityProviderAddPrivateKey - 用於解密 SAML 聲明的選用私有金鑰

      • IdentityProviderAssertionEncryptionMode - 選用,設定 SAML 宣告的加密模式 (允許、必要或空白)

    5. 選擇下一步

  5. 設定堆疊選項:

    1. 堆疊失敗選項下,選擇刪除所有新建立的資源

      注意

      選擇此選項可避免針對刪除政策指定的資源向您收費,即使堆疊建立失敗也一樣。

    2. 接受所有其他預設值。

    3. 功能下,勾選核取方塊以確認 CloudFormation 可能會在您的帳戶中建立 IAM 資源。

    4. 選擇下一步

  6. 檢閱堆疊詳細資訊,然後選擇提交

AWS CloudFormation 會建立堆疊。堆疊建立完成後,堆疊資源即可使用。您可以使用堆疊詳細資訊頁面上的資源索引標籤來檢視帳戶中佈建的資源。

堆疊將輸出下列值,您可以在輸出索引標籤上檢視這些值:

  • ProviderARN:已建立 SAML IdP 的 ARN (例如,arn:aws:iam::123456789012:saml-provider/CompanyIdP)。建立信任此提供者的角色時,您將需要此 ARN。

  • ProviderName:已建立 SAML IdP 的名稱 (例如,CompanyIdP如果您指定自訂名稱,或者my-saml-stack-saml-provider如果您使用預設命名)。

這些輸出也會匯出,讓其他 AWS CloudFormation 堆疊使用 Fn::ImportValue函數匯入。

驗證 SAML IdP

建立 SAML IdP 之後,您可以驗證其組態,並記下其 ARN 以搭配聯合角色使用。

  1. 在以下網址開啟 IAM 主控台:https://console.aws.amazon.com/iam/

  2. 在導覽窗格中,請選擇 Identity providers (身分提供者)。

    您應該會在清單中看到新建立的 SAML IdP。

  3. 選擇 IdP 名稱以檢視其詳細資訊。

    在 IdP 詳細資訊頁面上,您可以看到 SAML 中繼資料文件和其他組態詳細資訊。

  4. 請注意詳細資訊頁面上顯示的提供者 ARN

    建立信任此 IdP 的聯合 IAM 角色時,您將需要此 ARN。

  5. 檢閱中繼資料文件,以確保其符合您從外部 IdP 提供的內容。

您的 SAML IdP 現在已準備好供聯合 IAM 角色使用。您可以建立信任此 IdP 的角色,以允許來自外部 IdP 的已驗證使用者擔任這些角色和存取 AWS 資源。

清除:刪除資源

最後步驟是刪除堆疊及其包含的資源。

  1. 開啟 AWS CloudFormation 主控台。

  2. 堆疊頁面上,選擇從範本建立的堆疊,然後選擇刪除,然後確認刪除

    CloudFormation 會啟動刪除堆疊及其包含的所有資源。

CloudFormation 範本詳細資訊

資源

本教學課程的 AWS CloudFormation 範本會在您的帳戶中建立下列資源:

AWS::IAM::SAMLProvider:在 AWS 和外部 IdP 之間建立信任的 SAML IdP。

組態

範本包含下列可設定的參數:

  • IdentityProviderName - SAML IdP 的名稱 (為自動產生的名稱保留空白)

    範例: CompanyIdPEnterpriseSSO

  • IdentityProviderSAMLMetadataDocument - 來自外部 IdP 的 SAML 中繼資料文件 (格式化為單行)

  • IdentityProviderAddPrivateKey - 用於解密 SAML 聲明的選用私有金鑰

  • IdentityProviderAssertionEncryptionMode - 選用,為 SAML 聲明設定加密模式

CloudFormation 範本

將下列 JSON 或 YAML 程式碼儲存為個別檔案,以做為本教學課程的 CloudFormation 範本。

JSON
{ "AWSTemplateFormatVersion": "2010-09-09", "Description": "[AWSDocs] IAM: tutorial_saml-idp", "Parameters": { "IdentityProviderName": { "Type": "String", "Description": "Name of the SAML Identity Provider (leave empty for auto-generated name like '{StackName}-{UniqueId}')", "Default": "", "AllowedPattern": "^$|^[a-zA-Z0-9._-]+$", "ConstraintDescription": "Must be empty or contain only alphanumeric characters, periods, underscores, and hyphens" }, "IdentityProviderSAMLMetadataDocument": { "Type": "String", "Description": "SAML metadata document from identity provider" }, "IdentityProviderAddPrivateKey": { "Type": "String", "Description": "Optional private key for decrypting SAML assertions. The private key must be a .pem file that uses AES-GCM or AES-CBC encryption algorithm to decrypt SAML assertions.", "Default": "" }, "IdentityProviderAssertionEncryptionMode": { "Type": "String", "Description": "Optional, sets encryption mode for SAML assertions", "Default": "", "AllowedValues": ["", "Allowed", "Required"] } }, "Conditions": { "HasPrivateKey": {"Fn::Not": [{"Fn::Equals": [{"Ref": "IdentityProviderAddPrivateKey"}, ""]}]}, "HasEncryptionMode": {"Fn::Not": [{"Fn::Equals": [{"Ref": "IdentityProviderAssertionEncryptionMode"}, ""]}]}, "HasCustomName": {"Fn::Not": [{"Fn::Equals": [{"Ref": "IdentityProviderName"}, ""]}]} }, "Resources": { "SAMLProvider": { "Type": "AWS::IAM::SAMLProvider", "Properties": { "Name": {"Fn::If": ["HasCustomName", {"Ref": "IdentityProviderName"}, {"Ref": "AWS::NoValue"}]}, "SamlMetadataDocument": {"Ref": "IdentityProviderSAMLMetadataDocument"}, "Tags": [ { "Key": "Name", "Value": {"Fn::If": ["HasCustomName", {"Ref": "IdentityProviderName"}, {"Fn::Sub": "${AWS::StackName}-saml-provider"}]} } ], "AddPrivateKey": {"Fn::If": ["HasPrivateKey", {"Ref": "IdentityProviderAddPrivateKey"}, {"Ref": "AWS::NoValue"}]}, "AssertionEncryptionMode": {"Fn::If": ["HasEncryptionMode", {"Ref": "IdentityProviderAssertionEncryptionMode"}, {"Ref": "AWS::NoValue"}]} } } }, "Outputs": { "ProviderARN": { "Description": "ARN of the created SAML Identity Provider", "Value": {"Ref": "SAMLProvider"}, "Export": { "Name": {"Fn::Sub": "${AWS::StackName}-ProviderARN"} } }, "ProviderName": { "Description": "Name of the SAML Identity Provider", "Value": {"Fn::If": ["HasCustomName", {"Ref": "IdentityProviderName"}, {"Fn::Sub": "${AWS::StackName}-saml-provider"}]}, "Export": { "Name": {"Fn::Sub": "${AWS::StackName}-ProviderName"} } } } }
YAML
AWSTemplateFormatVersion: '2010-09-09' Description: '[AWSDocs] IAM: tutorial_saml-idp' Parameters: IdentityProviderName: Type: String Description: Name of the SAML Identity Provider (leave empty for auto-generated name like '{StackName}-{UniqueId}') Default: "" AllowedPattern: '^$|^[a-zA-Z0-9._-]+$' ConstraintDescription: 'Must be empty or contain only alphanumeric characters, periods, underscores, and hyphens' IdentityProviderSAMLMetadataDocument: Type: String Description: SAML metadata document from identity provider IdentityProviderAddPrivateKey: Type: String Description: Optional private key for decrypting SAML assertions. The private key must be a .pem file that uses AES-GCM or AES-CBC encryption algorithm to decrypt SAML assertions. Default: "" IdentityProviderAssertionEncryptionMode: Type: String Description: Optional, sets encryption mode for SAML assertions Default: "" AllowedValues: - "" - "Allowed" - "Required" Conditions: HasPrivateKey: !Not [!Equals [!Ref IdentityProviderAddPrivateKey, ""]] HasEncryptionMode: !Not [!Equals [!Ref IdentityProviderAssertionEncryptionMode, ""]] HasCustomName: !Not [!Equals [!Ref IdentityProviderName, ""]] Resources: SAMLProvider: Type: 'AWS::IAM::SAMLProvider' Properties: Name: !If - HasCustomName - !Ref IdentityProviderName - !Ref AWS::NoValue SamlMetadataDocument: !Ref IdentityProviderSAMLMetadataDocument Tags: - Key: Name Value: !If - HasCustomName - !Ref IdentityProviderName - !Sub '${AWS::StackName}-saml-provider' AddPrivateKey: !If - HasPrivateKey - !Ref IdentityProviderAddPrivateKey - !Ref AWS::NoValue AssertionEncryptionMode: !If - HasEncryptionMode - !Ref IdentityProviderAssertionEncryptionMode - !Ref AWS::NoValue Outputs: ProviderARN: Description: 'ARN of the created SAML Identity Provider' Value: !Ref SAMLProvider Export: Name: !Sub '${AWS::StackName}-ProviderARN' ProviderName: Description: 'Name of the SAML Identity Provider' Value: !If - HasCustomName - !Ref IdentityProviderName - !Sub '${AWS::StackName}-saml-provider' Export: Name: !Sub '${AWS::StackName}-ProviderName'