4.2. REST API를 사용한 인증

API 호출에는 API 토큰을 사용한 인증이 필요합니다. API_TOKEN 을 변수 이름으로 사용하는 경우 REST API로 인증하기 위해 API 호출에 -H "Authorization: Bearer ${API_TOKEN}" 를 추가합니다.

참고

API 토큰은 15분 후에 만료됩니다.

사전 요구 사항

  • (선택 사항) OpenShift Cluster Manager (ocm) CLI 도구를 설치했습니다.

절차

  1. OFFLINE_TOKEN 을 사용하여 API_TOKEN 변수를 설정하여 사용자를 검증합니다.

    1. (선택 사항) 명령줄 터미널에서 다음 명령을 실행합니다.

      $ export API_TOKEN=$( \
        curl \
        --silent \
        --header "Accept: application/json" \
        --header "Content-Type: application/x-www-form-urlencoded" \
        --data-urlencode "grant_type=refresh_token" \
        --data-urlencode "client_id=cloud-services" \
        --data-urlencode "refresh_token=${OFFLINE_TOKEN}" \
        "https://sso.redhat.com/auth/realms/redhat-external/protocol/openid-connect/token" \
        | jq --raw-output ".access_token" \
      )
    2. (선택 사항) 명령줄 터미널에서 ocm 클라이언트에 로그인합니다.

      $ ocm login --token="${OFFLINE_TOKEN}"

      그런 다음 API 토큰을 생성합니다.

      $ export API_TOKEN=$(ocm token)
  1. 토큰 생성 방법 중 하나의 경로에 스크립트를 생성합니다. 예를 들면 다음과 같습니다.

    $ vim ~/.local/bin/refresh-token
    export API_TOKEN=$( \
      curl \
      --silent \
      --header "Accept: application/json" \
      --header "Content-Type: application/x-www-form-urlencoded" \
      --data-urlencode "grant_type=refresh_token" \
      --data-urlencode "client_id=cloud-services" \
      --data-urlencode "refresh_token=${OFFLINE_TOKEN}" \
      "https://sso.redhat.com/auth/realms/redhat-external/protocol/openid-connect/token" \
      | jq --raw-output ".access_token" \
    )

    그런 다음 파일을 저장합니다.

  2. 파일 모드를 변경하여 실행 가능하게 합니다.

    $ chmod +x ~/.local/bin/refresh-token
  3. API 토큰을 새로 고칩니다.

    $ source refresh-token
  4. 다음 명령을 실행하여 API에 액세스할 수 있는지 확인합니다.

    $ curl -s https://api.openshift.com/api/assisted-install/v2/component-versions -H "Authorization: Bearer ${API_TOKEN}" | jq

    출력 예

    {
      "release_tag": "v2.11.3",
      "versions": {
        "assisted-installer": "registry.redhat.io/rhai-tech-preview/assisted-installer-rhel8:v1.0.0-211",
        "assisted-installer-controller": "registry.redhat.io/rhai-tech-preview/assisted-installer-reporter-rhel8:v1.0.0-266",
        "assisted-installer-service": "quay.io/app-sre/assisted-service:78d113a",
        "discovery-agent": "registry.redhat.io/rhai-tech-preview/assisted-installer-agent-rhel8:v1.0.0-195"
      }
    }