Using APIs in Red Hat Subscription Management

Red Hat Subscription Management 2023

authorizing, managing, and troubleshooting APIs in Red Hat Subscription Management

Customer Content Services

Abstract

Red Hat Subscription Management offers developed and documented APIs to help you better automate, manage, and track your subscriptions to Red Hat products.

1. Using APIs in Red Hat Subscription Management

Using APIs in Red Hat Subscription Management can help you more effectively keep track of and automate how you manage your Red Hat subscriptions and entitlement usage. By using APIs in Red Hat Subscription Management, you can:

  • Control which tooling you use for which products
  • Better manage your system inventory
  • Update and secure your systems more efficiently
  • Continue receiving official support for your Red Hat products

Red Hat Subscription Management APIs use OAuth 2.0 for authorization. To obtain a token and access the APIs, you will need the following pieces of information:

2. Using tokens for authentication

Offline and refresh tokens are used by Red Hat Subscription Management to authenticate your system after you set up your account using your secret to authenticate your Customer Portal account.

Warning

Please use password management that is consistent with networking best practices. It is never safe to store any passwords or credentials in plaintext. Treat your offline token with the same security measures that you would a password to protect it against unauthorized use.

2.1. Generating a new offline token

An offline token never expires as long as it is used at least once every 30 days and is used to create access tokens for the Red Hat Subscription Management APIs. It works as a password and allows you to continue being able to authenticate your account without having to create new refresh tokens.

Procedure

  1. Visit the Red Hat Subscription Management API Tokens page.
  2. Click the Generate Token button.

2.2. Generating a new refresh token

Once you have created the offline token, you can use that token to create a new refresh token, which includes an access token that is valid for five minutes. Access tokens are passed in the header to authenticate your Customer Portal user to the Red Hat Subscription Management APIs.

Procedure

  1. Set the offline token value. In this example, we set it in plaintext and shorten the token value for clarity:

    # offline_token='eyJhbGciOiJSUzI1NiIsInR5cCIgOiA'
  2. Create a function to easily filter out JSON values.

    # function jsonValue() {
    KEY=$1
    num=$2
    awk -F"[,:}]" '{for(i=1;i<=NF;i++){if($i~/'$KEY'\042/){print $(i+1)}}}' | tr -d '"' | sed -n ${num}p
    }
    
    # curl https://sso.redhat.com/auth/realms/redhat-external/protocol/openid-connect/token -d grant_type=refresh_token -d client_id=rhsm-api -d refresh_token=$offline_token

You should see an output similar to the below where access_token is what will be used as authorization token:

{"access_token":"o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nlc3MiLCJjYW5kbGVwaW5fc3lzdGVtX2FjY2Vzc192aWV3X2VkaXRfYWxsIiwiYWRtaW46b3JnOmFsbCIsInBvcnRhbF9tYW5hZ2VfY2FzZXMiLCJwb3J0YWxfc3lzdGVtX21hbmFnZW1lbnQiLCJwb3J0YWxfZG93bmxvYWQiXX0sInJlc291cmNlX2FjY2VzcyI6e319.S_pmAWzQUc04f0uGHN9rRYd4sH1t4IPnEwCcOH1aBL9Qo4_EbXPWCrtnf84f1pfuKJTQwUS-DldY6eloyVEsGgnqkygBKh270bu_bNXCNAuLJigEMsYx_2VzdnwWLptWS2_FUaNwe7Tai8qXwd8F0ge0Zjoi3P15S_8z4Tp79uD-qKcvwz6NlPKCOZwEbwZqOkJDZ8JKTIK8O0jfqdtHMfaWwlXMXdvx3B70tTOtHjQGAsxZA2dPPvqVGuyMOMmC3bMaISReUbtDwsCV-eAZplDfDZthr4k4JbmG9Iwq1aATaF3aCwfpebcmoIZGHE4_RLZrXCZKapXVVvRxcOrJytxIZrbDHq6ozX7j-j1SE3kuexcSLvlodmfTlxwPX9g7aqJu2ZLno54NxQSgYO8lQqSvScFgLtbX5f_FUS0Iw6yRWWJy2o2fnvfGk83rt5UYTtIb8Xd1GXcpHf8Yl10nVy21BetSQY__VpahF_eZghBNxS689GJnwUqAwlu01pOlb26mmHaydHc3hqUsudZydRbaFfI7nR6gQP8lCtp6b0z5hgVHLG4ZJ7i4MmEL6C5G4xHUaUs6RZgJUSsc2DzLW0b7rSQj41JuvTmSgD8bMrnVokmkAbfvxjKGc7E8n2GyImO7JiKb3RA7_o0xOTRYDIa_Ns-lnigJkUlQZUzt7JI","token_type":"bearer","not-before-policy":0,"session_state":"f0dbb8d4-4e4e-4654-844c-6f3704c84422","scope":"offline_access"}%

The access_token is what needs to be set/used as an authorization token to perform the API call.

# token=`curl https://sso.redhat.com/auth/realms/redhat-external/protocol/openid-connect/token -d grant_type=refresh_token -d client_id=rhsm-api -d refresh_token=$offline_token | jsonValue access_token`

3. Accessing available Red Hat Subscription Management APIs

Red Hat provides a Swagger file to describe the specifications of the Red Hat Subscription Management APIs. The Swagger specification includes information about the API endpoints available, input parameters, expected output, and possible error responses. The swagger file can be imported into REST clients like Postman or RESTlet to automatically build a library of API calls.

4. Troubleshooting API errors

Table 1. API errors

CodeExplanationResolution

400

BadRequest error

Validate that you entered the API call correctly and try again.

401

Unauthorized

Generate a new authorization token.

403

Forbidden

Generate a new authorization token.

404

Not found

Resource not found or does not exist.

429

Too many requests

Reduce the frequency of requests

500

Internal server error

The problem is on Red Hat’s end. Wait a minute and try your request again.

4.1. Troubleshooting error 403

Error 403 is a "not authorized" error, meaning that the authentication you are using for Red Hat Subscription Management APIs has failed. There are two possible solutions you can try.

Procedure

  1. To authenticate through the Red Hat Subscription Management gateway, ensure the authorization header includes the text "bearer" before entering your API call:

    curl -H "Authorization: Bearer <token>" <api_url>
  2. If the header is correct, create a new token. Refresh tokens last for five minutes.

4.2. Troubleshooting error 429

Error 429 is a “rate limiting” error, meaning that your account has exceeded the number of allowed requests per second. This limit applies to all users of a single Red Hat account.

Procedure

Extract the header of the response, which includes: * X-RateLimit-Limit: the total requests/sec allowed * X-RateLimit-Remaining: the number of requests/sec remaining (this will be a negative integer) * X-RateLimit-Delay: the number of seconds the requester should wait before trying again

Adjust the rate of requests to the X-RateLimit-Limit value and start again once the X-RateLimit-Delay time has passed.