本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用 Amazon MQ for RabbitMQ 的 IAM 身分驗證和授權
下列程序示範如何啟用 AWS Amazon MQ for RabbitMQ 代理程式的 IAM 身分驗證和授權。啟用 IAM 後,使用者可以使用 AWS IAM 憑證進行身分驗證,以存取 RabbitMQ Management API 並透過 AMQP 連線。如需 IAM 身分驗證如何與 Amazon MQ for RabbitMQ 搭配使用的詳細資訊,請參閱 Amazon MQ for RabbitMQ 的 IAM 身分驗證和授權。
先決條件
-
AWS 擁有 Amazon MQ for RabbitMQ 代理程式 AWS 之帳戶的管理員登入資料
-
使用這些管理員登入資料設定的 shell 環境 (使用 CLI AWS 設定檔或環境變數)
-
AWS 已安裝和設定的 CLI
-
jq已安裝命令列 JSON 處理器 -
curl已安裝命令列工具
使用 設定 IAM 身分驗證和授權 AWS CLI
-
設定環境變數
為您的代理程式設定所需的環境變數:
export AWS_DEFAULT_REGION=<region>export BROKER_ID=<broker-id> -
啟用傳出 JWT 字符
為 AWS 您的帳戶啟用傳出 Web 聯合身分:
ISSUER_IDENTIFIER=$(aws iam enable-outbound-web-identity-federation --query 'IssuerIdentifier' --output text) echo $ISSUER_IDENTIFIER輸出會以 格式顯示您帳戶的唯一發行者識別符 URL
https://<id>.tokens.sts.global.api.aws。 -
建立 IAM 政策文件
建立政策文件,授予取得 Web 身分字符的許可:
cat > policy.json << 'EOF' { "Version": "2012-10-17", "Statement": [ { "Sid": "VisualEditor0", "Effect": "Allow", "Action": [ "sts:GetWebIdentityToken", "sts:TagGetWebIdentityToken" ], "Resource": "*" } ] } EOF -
建立信任政策
擷取您的發起人身分並建立信任政策文件:
CALLER_ARN=$(aws sts get-caller-identity --query Arn --output text) cat > trust-policy.json << EOF { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "AWS": "$CALLER_ARN" }, "Action": "sts:AssumeRole" } ] } EOF -
建立 IAM 角色
建立 IAM 角色並連接政策:
aws iam create-role --role-name RabbitMqAdminRole --assume-role-policy-document file://trust-policy.json aws iam put-role-policy --role-name RabbitMqAdminRole --policy-name RabbitMqAdminRolePolicy --policy-document file://policy.json -
設定 RabbitMQ OAuth2 設定
使用 OAuth2 身分驗證和授權設定建立 RabbitMQ 組態檔案:
cat > rabbitmq.conf << EOF auth_backends.1 = oauth2 auth_backends.2 = internal auth_oauth2.jwks_url = ${ISSUER_IDENTIFIER}/.well-known/jwks.json auth_oauth2.resource_server_id = rabbitmq auth_oauth2.scope_prefix = rabbitmq/ auth_oauth2.additional_scopes_key = sub auth_oauth2.scope_aliases.1.alias = arn:aws:iam::$(aws sts get-caller-identity --query Account --output text):role/RabbitMqAdminRole auth_oauth2.scope_aliases.1.scope = rabbitmq/tag:administrator rabbitmq/read:*/* rabbitmq/write:*/* rabbitmq/configure:*/* auth_oauth2.https.hostname_verification = wildcard management.oauth_enabled = true EOF -
更新代理程式組態
將新組態套用至您的代理程式:
# Retrieve the configuration ID CONFIG_ID=$(aws mq describe-broker --broker-id $BROKER_ID --query 'Configurations[0].Id' --output text) # Create a new configuration revision REVISION=$(aws mq update-configuration --configuration-id $CONFIG_ID --data "$(cat rabbitmq.conf | base64 --wrap=0)" --query 'LatestRevision.Revision' --output text) # Apply the configuration to the broker aws mq update-broker --broker-id $BROKER_ID --configuration Id=$CONFIG_ID,Revision=$REVISION # Reboot the broker to apply changes aws mq reboot-broker --broker-id $BROKER_ID等待代理程式狀態回到 ,
RUNNING再繼續下一個步驟。 -
取得 JWT 字符
擔任 IAM 角色並取得 Web 身分字符:
# Assume the RabbitMqAdminRole ROLE_CREDS=$(aws sts assume-role --role-arn arn:aws:iam::$(aws sts get-caller-identity --query Account --output text):role/RabbitMqAdminRole --role-session-name rabbitmq-session) # Configure the session with temporary credentials export AWS_ACCESS_KEY_ID=$(echo "$ROLE_CREDS" | jq -r '.Credentials.AccessKeyId') export AWS_SECRET_ACCESS_KEY=$(echo "$ROLE_CREDS" | jq -r '.Credentials.SecretAccessKey') export AWS_SESSION_TOKEN=$(echo "$ROLE_CREDS" | jq -r '.Credentials.SessionToken') # Obtain the web identity token TOKEN_RESPONSE=$(aws sts get-web-identity-token \ --audience "rabbitmq" \ --signing-algorithm ES384 \ --duration-seconds 300 \ --tags Key=scope,Value="rabbitmq/tag:administrator") # Extract the token TOKEN=$(echo "$TOKEN_RESPONSE" | jq -r '.WebIdentityToken') -
存取 RabbitMQ Management API
使用 JWT 字符存取 RabbitMQ 管理 API:
BROKER_URL=<broker-id>.mq.<region>.on.aws curl -u ":$TOKEN" \ -X GET https://${BROKER_URL}/api/overview \ -H "Content-Type: application/json"成功的回應會確認 IAM 身分驗證是否正常運作。回應包含 JSON 格式的代理程式概觀資訊。
-
使用 JWT 字符透過 AMQP 連接
使用 JWT 字符搭配 perf-test 工具測試 AMQP 連線:
BROKER_DNS=<broker-endpoint>CONNECTION_STRING=amqps://:${TOKEN}@${BROKER_DNS}:5671 docker run -it --rm --ulimit nofile=40960:40960 pivotalrabbitmq/perf-test:latest \ --queue-pattern 'test-queue-%d' --queue-pattern-from 1 --queue-pattern-to 1 \ --producers 1 --consumers 1 \ --uri ${CONNECTION_STRING} \ --flag persistent --rate 1如果您收到
ACCESS_REFUSED錯誤,您可以使用代理程式的 CloudWatch 日誌對組態設定進行疑難排解。您可以在 Amazon MQ 主控台中找到代理程式的 CloudWatch Logs 日誌群組連結。