

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

# RabbitMQ용 Amazon MQ에 OAuth 2.0 인증 및 권한 부여 사용
<a name="oauth-tutorial"></a>

이 자습서에서는 Amazon Cognito를 OAuth 2.0 공급자로 사용하여 RabbitMQ용 Amazon MQ 브로커에 대한 [OAuth 2.0 인증](oauth-for-amq-for-rabbitmq.md)을 구성하는 방법을 설명합니다.

**참고**  
중국(베이징) 및 중국(닝샤) 리전에서 Amazon Cognito를 사용할 수 없습니다.

**중요**  
이 자습서는 Amazon Cognito에만 적용되지만 다른 ID 제공업체(IdP)를 사용할 수 있습니다. 자세한 내용은 [OAuth 2.0 인증 예제](https://www.rabbitmq.com/docs/oauth2-examples)를 참조하세요.

**Topics**
+ [

## OAuth 2.0 인증을 구성하기 위한 사전 조건
](#oauth-tutorial-prerequisites)
+ [

## AWS CLI를 사용하여 Amazon Cognito로 OAuth 2.0 인증 구성
](#oauth-tutorial-config-cognito-using-cli)
+ [

## Amazon Cognito를 사용한 OAuth 2.0 및 단순 인증 구성
](#oauth-tutorial-config-both-auth-methods-using-cli)

## OAuth 2.0 인증을 구성하기 위한 사전 조건
<a name="oauth-tutorial-prerequisites"></a>

AWS CDK 스택, [RabbitMQ용 Amazon Cognito OAuth 2 플러그인](https://github.com/aws-samples/amazon-mq-samples/tree/main/rabbitmq-samples/rabbitmq-oauth2-cognito-sample)을 배포하여 이 자습서에 필요한 Amazon Cognito 리소스를 설정할 수 있습니다. Amazon Cognito를 수동으로 설정하는 경우 RabbitMQ용 Amazon MQ 브로커에서 OAuth 2.0을 구성하기 전에 다음 사전 조건을 충족해야 합니다.

**Amazon Cognito를 설정하기 위한 사전 조건**
+ 사용자 풀을 생성하여 Amazon Cognito 엔드포인트를 설정합니다. 이렇게 하려면 [Amazon Cognito에서 OAuth 2.0을 사용하는 방법: 다양한 OAuth 2.0 권한 부여에 대해 알아보기](https://aws.amazon.com/blogs/security/how-to-use-oauth-2-0-in-amazon-cognito-learn-about-the-different-oauth-2-0-grants/)라는 제목의 블로그를 참조하세요.
+ 범위가 정의된 사용자 풀 `read:all`, `write:all`, `configure:all` 및 `tag:administrator`에서 `rabbitmq`라는 리소스 서버를 생성합니다. 이러한 범위는 RabbitMQ 권한과 연결됩니다.

  리소스 서버 생성에 대한 자세한 내용은 *Amazon Cognito 개발자 안내서*의 [사용자 풀(AWS Management Console)에 대한 리소스 서버 정의](https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-pools-define-resource-servers.html#cognito-user-pools-define-resource-servers-console)를 참조하세요.
+ 다음 애플리케이션 클라이언트를 생성합니다.
  + `Machine-to-Machine application` 유형의 사용자 풀에 대한 애플리케이션 클라이언트. RabbitMQ AMQP 클라이언트에 사용할 클라이언트 보안 암호가 있는 기밀 클라이언트입니다. 애플리케이션 클라이언트 및 생성에 대한 자세한 내용은 [앱 클라이언트 유형](https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-settings-client-apps.html#user-pool-settings-client-app-client-types) 및 [앱 클라이언트 생성](https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-settings-client-apps.html#cognito-user-pools-app-idp-settings-console-create)을 참조하세요.
  + `Single-page application` 유형의 사용자 풀에 대한 애플리케이션 클라이언트. RabbitMQ 관리 콘솔에 사용자를 로그인하는 데 사용되는 퍼블릭 클라이언트입니다. 다음 절차에서 생성할 RabbitMQ용 Amazon MQ 브로커의 엔드포인트를 허용된 콜백 URL로 포함하도록 이 애플리케이션 클라이언트를 업데이트해야 합니다. 자세한 내용은 [Amazon Cognito 콘솔을 사용하여 관리형 로그인 설정](https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-pools-managed-login.html#set-up-managed-login)을 참조하세요.

**Amazon MQ를 설정하기 위한 사전 조건**
+ OAuth 2.0 설정의 성공 여부를 확인하는 bash 스크립트를 실행하는 작업 [Docker](https://docs.docker.com/engine/install/) 설치.
+ AWS CLI 버전 >= `2.28.23` - 브로커 생성 중에 사용자 이름과 암호를 선택적으로 추가할 수 있습니다.

## AWS CLI를 사용하여 Amazon Cognito로 OAuth 2.0 인증 구성
<a name="oauth-tutorial-config-cognito-using-cli"></a>

다음 절차에서는 Amazon Cognito를 IdP로 사용하여 RabbitMQ용 Amazon MQ 브로커에 대한 OAuth 2.0 인증을 설정하는 방법을 보여줍니다. 이 절차에서는 AWS CLI를 사용하여 필요한 리소스를 생성하고 구성합니다.

다음 절차에서는 configurationID 및 Revision, *<c-fa3390a5-7e01-4559-ae0c-eb15b38b22ca>* 및 *<2>*와 같은 자리 표시자 값을 실제 값으로 바꿔야 합니다.

1. 다음 예제와 같이 [create-configuration](https://docs.aws.amazon.com/cli/latest/reference/mq/create-configuration.html) AWS CLI 명령을 사용하여 새 구성을 생성합니다.

   ```
   aws mq create-configuration \
     --name "rabbitmq-oauth2-config" \
     --engine-type "RABBITMQ" \
     --engine-version "3.13"
   ```

   이 명령은 다음 예제와 유사한 응답을 반환합니다.

   ```
   {
       "Arn": "arn:aws:mq:us-west-2:123456789012:configuration:c-fa3390a5-7e01-4559-ae0c-eb15b38b22ca",
       "AuthenticationStrategy": "simple",
       "Created": "2025-07-17T16:03:01.759943+00:00",
       "Id": "c-fa3390a5-7e01-4559-ae0c-eb15b38b22ca",
       "LatestRevision": {
       "Created": "2025-07-17T16:03:01.759000+00:00",
       "Description": "Auto-generated default for rabbitmq-oauth2-config on RabbitMQ 3.13",
       "Revision": 1
       },
       "Name": "rabbitmq-oauth2-config"
   }
   ```

1. 다음 예제와 같이 OAuth 2.0을 인증 및 권한 부여 방법으로 사용하도록 **rabbitmq.conf**라는 구성 파일을 생성합니다.

   ```
   auth_backends.1 = oauth2
   
   # FIXME: Update this value with the token signing key URL of your Amazon Cognito user pool.
   # If you used the AWS CDK stack to deploy Amazon Cognito, this is one of the stack outputs.
   auth_oauth2.jwks_url = ${RabbitMqOAuth2TestStack.JwksUri}
   auth_oauth2.resource_server_id = rabbitmq
   # Amazon Cognito does not include an audience field in access tokens
   auth_oauth2.verify_aud = false 
   
   # Amazon Cognito does not allow * in its custom scopes. Use aliases to translate between Amazon Cognito and RabbitMQ.
   auth_oauth2.scope_prefix = rabbitmq/
   auth_oauth2.scope_aliases.1.alias = rabbitmq/read:all
   auth_oauth2.scope_aliases.1.scope = rabbitmq/read:*/*
   auth_oauth2.scope_aliases.2.alias = rabbitmq/write:all
   auth_oauth2.scope_aliases.2.scope = rabbitmq/write:*/*
   auth_oauth2.scope_aliases.3.alias = rabbitmq/configure:all
   auth_oauth2.scope_aliases.3.scope = rabbitmq/configure:*/*
   
   # Allow OAuth 2.0 login for RabbitMQ management console
   management.oauth_enabled = true
   # FIXME: Update this value with the client ID of your public application client
   management.oauth_client_id = ${RabbitMqOAuth2TestStack.ManagementConsoleAppClientId}
   # FIXME: Update this value with the base JWKS URI (without /.well-known/jwks.json)
   auth_oauth2.issuer = ${RabbitMqOAuth2TestStack.Issuer}
   management.oauth_scopes = rabbitmq/tag:administrator
   ```

   이 구성은 [범위 별칭](https://www.rabbitmq.com/docs/oauth2#scope-translation)을 사용하여 Amazon Cognito에 정의된 범위를 RabbitMQ 호환 범위에 매핑합니다.

1. 다음 예제와 같이 [update-configuration](https://docs.aws.amazon.com/cli/latest/reference/mq/update-configuration.html) AWS CLI 명령을 사용하여 구성을 업데이트합니다. 이 명령에서 이 절차의 1단계에 대한 응답으로 받은 구성 ID를 추가합니다. 예를 들어 **c-fa3390a5-7e01-4559-ae0c-eb15b38b22ca**입니다.

   ```
   aws mq update-configuration \
     --configuration-id "<c-fa3390a5-7e01-4559-ae0c-eb15b38b22ca>" \
     --data "$(cat rabbitmq.conf | base64 --wrap=0)"
   ```

   이 명령은 다음 예제와 유사한 응답을 반환합니다.

   ```
   {
       "Arn": "arn:aws:mq:us-west-2:123456789012:configuration:c-b600ac8e-8183-4f74-a713-983e59f30e3d",
       "Created": "2025-07-17T16:57:04.520931+00:00",
       "Id": "c-b600ac8e-8183-4f74-a713-983e59f30e3d",
       "LatestRevision": {
           "Created": "2025-07-17T16:57:39.172000+00:00",
           "Revision": 2
       },
       "Name": "rabbitmq-oauth2-config",
       "Warnings": []
   }
   ```

1. 이 절차의 2단계에서 생성한 OAuth 2.0 구성으로 브로커를 생성합니다. 이렇게 하려면 다음 예제에서와 같이 [create-broker](https://docs.aws.amazon.com/cli/latest/reference/mq/create-broker.html) AWS CLI 명령을 사용하면 됩니다. 이 명령에서 1단계와 2단계의 응답에서 얻은 구성 ID와 개정 번호를 각각 입력합니다. 예: **c-fa3390a5-7e01-4559-ae0c-eb15b38b22ca** 및 **2**.

   ```
   aws mq create-broker \
    --broker-name "rabbitmq-oauth2-broker" \ 
    --engine-type "RABBITMQ" \
    --engine-version "3.13" \
    --host-instance-type "mq.m7g.large" \
    --deployment-mode "CLUSTER_MULTI_AZ" \
    --logs '{"General": true}' \
    --publicly-accessible \
    --configuration '{"Id": "<c-fa3390a5-7e01-4559-ae0c-eb15b38b22ca>","Revision": <2>}' \
   ```

   이 명령은 다음 예제와 유사한 응답을 반환합니다.

   ```
   {
       "BrokerArn": "arn:aws:mq:us-west-2:123456789012:broker:rabbitmq-oauth2-broker:b-2a1b5133-a10c-49d2-879b-8c176c34cf73",
       "BrokerId": "b-2a1b5133-a10c-49d2-879b-8c176c34cf73"
   }
   ```

1. 다음 예제와 같이 [describe-broker](https://docs.aws.amazon.com/cli/latest/reference/mq/describe-broker.html) AWS CLI 명령을 사용하여 브로커의 상태가 `CREATION_IN_PROGRESS`에서 `RUNNING`로 전환되는지 확인합니다. 이 명령에서 이전 단계의 결과에서 얻은 브로커 ID를 입력합니다. 예: **b-2a1b5133-a10c-49d2-879b-8c176c34cf73**.

   ```
   aws mq describe-broker \
    --broker-id "<b-2a1b5133-a10c-49d2-879b-8c176c34cf73>"
   ```

   이 명령은 다음 예제와 유사한 응답을 반환합니다. 다음 응답은 `describe-broker` 명령이 반환하는 전체 출력의 약식 버전입니다. 이 응답은 브로커 상태와 브로커를 보호하는 데 사용되는 인증 전략을 보여줍니다. 이 경우 `config_managed` 인증 전략은 브로커가 OAuth 2 인증 방법을 사용함을 나타냅니다.

   ```
   {
       "AuthenticationStrategy": "config_managed",
       ...,
       "BrokerState": "RUNNING",
       ...
   }
   ```

    OAuth2를 사용하여 RabbitMQ 관리 콘솔에 로그인하려면 브로커 엔드포인트를 해당 Amazon Cognito 앱 클라이언트에 유효한 콜백 URL로 추가해야 합니다. 자세한 내용은 샘플 [ Amazon Cognito CDK 스택](https://github.com/aws-samples/amazon-mq-samples/tree/main/rabbitmq-samples/rabbitmq-oauth2-cognito-sample#step-5-update-callback-urls-if-using-placeholder-urls) 설정의 5단계를 참조하세요.

1. 다음 `perf-test.sh` 스크립트를 사용하여 OAuth 2.0 인증 및 권한 부여를 확인합니다.

   이 bash 스크립트를 사용하여 RabbitMQ용 Amazon MQ 브로커에 대한 연결을 테스트합니다. 이 스크립트는 Amazon Cognito에서 토큰을 가져와 연결이 제대로 구성되었는지 확인합니다. 성공적으로 구성되면 브로커가 메시지를 게시하고 소비하는 것을 볼 수 있습니다.

   `ACCESS_REFUSED` 오류가 발생하면 브로커에 대한 CloudWatch 로그를 사용하여 구성 설정 문제를 해결할 수 있습니다. Amazon MQ 콘솔에서 브로커의 CloudWatch 로그 그룹에 대한 링크를 찾을 수 있습니다.

   이 스크립트에서는 다음 값을 제공해야 합니다.
   + `CLIENT_ID` 및 `CLIENT_SECRET`: Amazon Cognito 콘솔의 **앱 클라이언트** 페이지에서 이러한 값을 찾을 수 있습니다.
   + Cognito 도메인: Amazon Cognito 콘솔에서 찾을 수 있습니다. **브랜딩**에서 **도메인**을 선택합니다. **도메인** 페이지의 **리소스 서버** 섹션에서 이 값을 찾을 수 있습니다.
   + Amazon MQ 브로커 엔드포인트: Amazon MQ 콘솔의 브로커 세부 정보 페이지에 있는 **연결**에서 이 값을 찾을 수 있습니다.

   ```
   #! /bin/bash
   set -e
   
   # Client information
   ## FIXME: Update this value with the client ID and secret of your confidential application client
   CLIENT_ID=${RabbitMqOAuth2TestStack.AmqpAppClientId}
   CLIENT_SECRET=${RabbitMqOAuth2TestStack.AmqpAppClientSecret}
   
   # FIXME: Update this value with the domain of your Amazon Cognito user pool
   RESPONSE=$(curl -X POST ${RabbitMqOAuth2TestStack.TokenEndpoint} \
                   -H "Content-Type: application/x-www-form-urlencoded" \
                   -d "grant_type=client_credentials&client_id=${CLIENT_ID}&client_secret=${CLIENT_SECRET}&scope=rabbitmq/configure:all rabbitmq/read:all rabbitmq/tag:administrator rabbitmq/write:all")
   
   
   # Extract the access_token from the response.
   # This token will be passed in the password field when connecting to the broker.
   # Note that the username is left blank, the field is ignored by the plugin.
   BROKER_PASSWORD=$(echo ${RESPONSE} | jq -r '.access_token')
   
   # FIXME: Update this value with the endpoint of your broker. For example, b-89424106-7e0e-4abe-8e98-8de0dada7630.mq.us-east-1.on.aws.
   BROKER_DNS=<broker_dns>
   CONNECTION_STRING=amqps://:${BROKER_PASSWORD}@${BROKER_DNS}:5671 
   
   # Produce/consume messages using the above connection string
   QUEUES_COUNT=1
   PRODUCERS_COUNT=1
   CONSUMERS_COUNT=1
   PRODUCER_RATE=1
   
   docker run -it --rm --ulimit nofile=40960:40960 pivotalrabbitmq/perf-test:latest \
       --queue-pattern 'test-queue-%d' --queue-pattern-from 1 --queue-pattern-to $QUEUES_COUNT \
       --producers $PRODUCERS_COUNT --consumers $CONSUMERS_COUNT \
       --id "test${QUEUES_COUNT}q${PRODUCERS_COUNT}p${CONSUMERS_COUNT}c${PRODUCER_RATE}r" \
       --uri ${CONNECTION_STRING} \
       --flag persistent --rate $PRODUCER_RATE
   ```

## Amazon Cognito를 사용한 OAuth 2.0 및 단순 인증 구성
<a name="oauth-tutorial-config-both-auth-methods-using-cli"></a>

OAuth 2.0 인증을 사용하여 브로커를 생성할 때 다음 인증 방법 중 하나를 지정할 수 있습니다.
+ **OAuth 2.0 전용**: 이 방법을 사용하려면 브로커를 생성하는 동안 사용자 이름과 암호를 제공하지 마십시오. [이전 절차](#oauth-tutorial-config-cognito-using-cli)에서는 OAuth 2.0 인증 방법만 사용하는 방법을 보여줍니다.
+ **OAuth 2.0 및 단순 인증**: 이 방법을 사용하려면 브로커를 생성하는 동안 사용자 이름과 암호를 제공합니다. 또한 다음 절차와 같이 `auth_backends.2 = internal`을 브로커 구성에 추가합니다.

다음 절차에서는 *<ConfigurationId>* 및 *<Revision>*과 같은 자리 표시자 값을 실제 값으로 바꿔야 합니다.

1. 두 인증 방법을 모두 사용하려면 다음 예제와 같이 브로커 구성을 생성합니다.

   ```
   auth_backends.1 = oauth2
   auth_backends.2 = internal
   
   # FIXME: Update this value with the token signing key URL of your Amazon Cognito user pool
   auth_oauth2.jwks_url = ${RabbitMqOAuth2TestStack.JwksUri}
   auth_oauth2.resource_server_id = rabbitmq
   auth_oauth2.verify_aud = false
   
   auth_oauth2.scope_prefix = rabbitmq/
   auth_oauth2.scope_aliases.1.alias = rabbitmq/read:all
   auth_oauth2.scope_aliases.1.scope = rabbitmq/read:*/*
   auth_oauth2.scope_aliases.2.alias = rabbitmq/write:all
   auth_oauth2.scope_aliases.2.scope = rabbitmq/write:*/*
   auth_oauth2.scope_aliases.3.alias = rabbitmq/configure:all
   auth_oauth2.scope_aliases.3.scope = rabbitmq/configure:*/*
   ```

   이 구성은 [범위 별칭](https://www.rabbitmq.com/docs/oauth2#scope-translation)을 사용하여 Amazon Cognito에 정의된 범위를 RabbitMQ 호환 범위에 매핑합니다.

1. 다음 예제와 같이 두 인증 방법을 모두 사용하는 브로커를 생성합니다.

   ```
   aws mq create-broker \
    --broker-name "rabbitmq-oauth2-broker-with-internal-user" \
    --engine-type "RABBITMQ" \
    --engine-version "3.13" \
    --host-instance-type "mq.m7g.large" \
    --deployment-mode "CLUSTER_MULTI_AZ" \
    --logs '{"General": true}' \
    --publicly-accessible \
    --configuration '{"Id": "<ConfigurationId>","Revision": <Revision>}' \
    --users '[{"Username":"<myUser>","Password":"<myPassword11>"}]'
   ```

1. [Amazon Cognito를 사용하여 OAuth 2.0 인증 구성](#oauth-tutorial-config-cognito-using-cli) 절차의 5단계와 6단계에 설명된 대로 브로커 상태와 인증 방법 설정 구성이 성공했는지 확인합니다.