如何使用 NGINX Proxy 從 VPC 外部存取具有 Amazon Cognito 驗證的 OpenSearch Dashboards?

4 分的閱讀內容
0

我的 Amazon OpenSearch Service 網域位於虛擬私有雲端 (VPC) 中。我想使​​用 NGINX Proxy 從 VPC 外部透過 Amazon Cognito 驗證存取 OpenSearch Dashboards。

簡短描述

使用 NGINX 將 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體設定為 Proxy 伺服器。然後,Proxy 伺服器會將瀏覽器請求轉送到 Amazon Cognito 和 OpenSearch Dashboards。

**注意:**以下解決方法僅適用於原生 Amazon Cognito 使用者。

您也可以使用 SSH 通道或 Client VPN,透過 Amazon Cognito 驗證從 VPC 外部存取 OpenSearch Dashboards。如需詳細資訊,請參閱如何使用 Amazon Cognito 驗證從 VPC 外部存取 OpenSearch Dashboards?

解決方法

**重要:**限制 VPC 中使用者的存取權後,您的 OpenSearch Service 網域會更加安全。在繼續之前,請確保此解決方案符合您組織的安全要求。

建立 Amazon Cognito 使用者集區和身分池

請完成下列步驟:

  1. 建立 Amazon Cognito 使用者集區。設定下列設定:
    Application type (應用程式類型),請選擇 Traditional web application (傳統 Web 應用程式)。
    Name your application (命名您的應用程式),請輸入自訂應用程式名稱或保留預設名稱。
    Options for sign-in identifiers (登入識別碼選項),請選擇 Username (使用者名稱)。
    Required attributes for sign-up (註冊所需的屬性),請選擇 Email (電子郵件)。

  2. 開啟 Amazon Cognito console (Amazon Cognito 主控台)。

  3. 在導覽窗格中,選擇 User pools (使用者集區)。

  4. 選取您的使用者集區,然後設定以下設定:
    在導覽窗格中的 Branding (品牌) 下,請選擇 Managed Login (受管登入)。
    Domains with managed login branding (具有受管登入品牌的網域),請選擇 Update version (更新版本)。
    Branding version (品牌版本),請選擇 Hosted UI (classic) (託管 UI (傳統)。

  5. 設定您的使用者和群組

  6. 建立 Amazon Cognito 身分池。設定下列設定:
    User Access (使用者存取),請選擇 Authenticated access (經過驗證的存取)。
    Authenticated identity sources (經過驗證的身份來源),請輸入 Amazon Cognito user pool (Amazon Cognito 使用者集區)。
    IAM role (IAM 角色),請選擇 Create a new IAM role (建立新的 IAM 角色),然後輸入角色名稱。
    User pool details (使用者集區詳細資訊),請選取您的 user pool ID (使用者集區 ID),然後選擇 App client ID (應用程式用戶端 ID)。
    Role settings (角色設定),請選擇 Use default authenticated role (使用預設經過驗證的角色)。
    Claim mapping (宣告對應),請選擇 Inactive (非作用中)。

  7. 設定您的 OpenSearch Service 網域,以使用 Amazon Cognito 驗證。設定下列設定:
    Cognito User Pool (Cognito 使用者集區),請選擇您的使用者集區。
    Cognito Identity Pool (Cognito 身分池),請選取您的身分池。

  8. Domain (網域) 存取政策,請輸入以下存取政策:

    
    {  "Version": "2012-10-17",  
      "Statement": \[  
        {  
          "Effect": "Allow",  
          "Principal": {  
            "AWS": "arn:aws:iam::account-id:role/service-role/identitypool-role"  
          },  
          "Action": "es:\*",  
          "Resource": "arn:aws:es:region:account-id:domain/domain-name/\*"  
        }  
      \]  
    }

    **注意:**將 account-id 替換為您的 AWS 帳戶 ID,並將 identitypool-role 替換為您身分池角色的名稱。將 domain-name 替換為您的 OpenSearch Service 網域,並將 region 替換為您網域的 AWS 區域。

設定 NGINX Proxy

**注意:**以下設定適用於 Amazon Linux 2023 上的 Amazon Machine Image (AMI)。如果您使用不同的 AMI,則可能需要調整設定。

請完成下列步驟:

  1. 在您 OpenSearch Service 網域所在的 VPC 的公有子網路中,啟動 Amazon EC2 執行個體。該執行個體使用的安全群組必須與您網域使用的相同。

  2. (選用) 如果您未使用測試環境,請配置彈性 IP 位址,以與您的 EC2 執行個體相關聯

  3. (選用) 如果您未使用測試環境,請設定您的 DNS,以解析對彈性 IP 位址的請求。有關如何使用 Amazon Route 53 解析請求的更多資訊,請參閱設定 Amazon Route 53,以將流量路由到 Amazon EC2 執行個體

  4. 若要連線到您的執行個體並安裝 NGINX,請執行以下命令:

    sudo yum update
    sudo yum install nginx -y
  5. 若要為 NGINX 設定 SSL,請從憑證授權單位 (CA) 取得 SSL 憑證。
    **注意:**如果您使用測試環境,請改為產生自我簽署憑證。最佳做法是只在生產環境中,使用由第三方憑證授權單位簽署的 SSL 憑證。

  6. (選用) 如果您使用具有自我簽署憑證的測試環境,請執行 OpenSSL x509 命令來產生私有金鑰:

    sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/nginx/cert.key -out /etc/nginx/cert.crt

    如需詳細資訊,請參閱 OpenSSL 網站上的 x509
    上述命令會產生 cert.key,這是自我簽署 SSL 憑證的私有金鑰。

  7. 導覽至 /etc/nginx/conf.d 目錄,然後建立一個名為 default.conf 的檔案。

  8. 請使用下列值修改 default.conf 檔案:
    /etc/nginx/cert.crt,請輸入您 SSL 憑證的路徑。
    /etc/nginx/cert.key,請輸入為 SSL 憑證產生的私有金鑰路徑。
    my_domain_host,請輸入您的 OpenSearch Service 端點。
    my_cognito_host,請輸入您的 Amazon Cognito 使用者集區網域。
    **重要:**您必須使用 HTTPS。
    如果您的 Amazon OpenSearch Service 網域執行 OpenSearch Service 1.0 或更新版本,請使用 _dashboards 端點。
    如果您的 Amazon OpenSearch Service 網域執行 Elasticsearch 版本 5.x-7.x,請使用 _plugin/kibana 端點。

    注意:****解析程式值會根據您的 VPC 設定而變化。使用主 CIDR 區塊的基本 IP 位址加二的 DNS 解析程式。例如,如果您建立一個 CIDR 區塊為 10.0.0.0/24 的 VPC,那麼您的 DNS 解析程式會位於 10.0.0.2。

    default.conf 檔案範例:

    server {  listen 443 ssl;
      server_name $host;
      rewrite ^/$ https://$host/_dashboards redirect;
      resolver 10.0.0.2 ipv6=off valid=5s;
      set $domain_endpoint my_domain_host;
      set $cognito_host my_cognito_host;
    
      ssl_certificate           /etc/nginx/cert.crt;
      ssl_certificate_key       /etc/nginx/cert.key;
    
      # ssl on;
      ssl_session_cache  builtin:1000  shared:SSL:10m;
      ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
      ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
      ssl_prefer_server_ciphers on;
    
      location ^~ /_dashboards {
    
        # Forward requests to Dashboards
        proxy_pass https://$domain_endpoint;
    
        # Handle redirects to Cognito
        proxy_redirect https://$cognito_host https://$host;
    
        # Handle redirects to Dashboards
        proxy_redirect https://$domain_endpoint https://$host;
    
        # Update cookie domain and path
        proxy_cookie_domain $domain_endpoint $host;
        proxy_cookie_path ~*^/$ /_dashboards/;
    
        # Response buffer settings
        proxy_buffer_size 128k;
        proxy_buffers 4 256k;
        proxy_busy_buffers_size 256k;
      }
    
      location ~ \/(log|sign|fav|forgot|change|saml|oauth2|confirm) {
    
        # Forward requests to Cognito
        proxy_pass https://$cognito_host;
    
        # Handle redirects to Dashboards
        proxy_redirect https://$domain_endpoint https://$host;
    
        # Handle redirects to Cognito
        proxy_redirect https://$cognito_host https://$host;
    
        proxy_cookie_domain $cognito_host $host;
      }
    }
  9. (選用) 若要將 my_domain_hostmy_cognito_host 指派為變數,請執行 sed 命令:

    sudo sed -i 's/my_domain_host/vpc-cognito-private-xxxxxxxxxx.us-east-1.es.amazonaws.com/' /etc/nginx/conf.d/default.conf
    sudo sed -i 's/my_cognito_host/domain-xxxxxxx.auth.us-east-1.amazoncognito.com/' /etc/nginx/conf.d/default.conf

    **注意:**如果您執行上述命令,則不必在 /etc/nginx/conf.d/default.conf 檔案中替換 my_domain_hostmy_cognito_host

  10. 若要啟用並啟動 NGINX,請執行以下命令:

sudo systemctl enable nginx && sudo systemctl start nginx

存取 OpenSearch Dashboards

請完成下列步驟:

  1. 使用您的瀏覽器開啟 NGINX IP 位址或 DNS 名稱,將您重新導向至 Amazon Cognito 登入頁面。
  2. 輸入您的使用者名稱和臨時密碼登入 OpenSearch Dashboards。
  3. 出現提示時,請變更密碼並重新登入。
AWS 官方
AWS 官方已更新 2 個月前