Bookmark this page

Guided Exercise: Securing APIs with API Keys and API Key-pair Authentication

In this exercise, you will secure an API by using an API key and an API key-ID pair.

Outcomes

You should be able to configure a Red Hat 3scale API Management product to require authentication by using an API key-ID pair.

As the student user on the workstation machine, use the lab command to prepare your system for this exercise.

This command:

  • Deploys the application you will use in this exercise.

  • Configures a 3scale API Management product and backend.

[student@workstation ~]$ lab start secure-keys

Procedure 4.2. Instructions

  1. Configure the secure_keys product to require authentication by using the API key.

    1. Verify that the secure_keys_basic application plan in the secure_keys product does not have an associated application.

      [student@workstation ~]$ 3scale application list 3scale-tenant \
        --plan=secure_keys_basic --service=secure_keys
      ID	NAME	STATE	ENABLED	ACCOUNT_ID	SERVICE_ID	PLAN_ID
    2. Create a secure_key_app application that is associated with the secure_keys_basic application plan:

      [student@workstation ~]$ 3scale application create 3scale-tenant \
        john secure_keys secure_keys_basic secure_keys_app
      Created application id: 13
    3. List the API key that is associated with the secure_key_app application. Use the application ID from the preceding step.

      [student@workstation ~]$ API_KEY=$(3scale application show 3scale-tenant 13 \
        -o json | jq -r '.user_key')
      [student@workstation ~]$ echo $API_KEY
      bc18aa400edba94148e37a4f632e34b2
    4. Use the key to call the books-api service:

      [student@workstation ~]$ curl \
        "https://secure-keys-3scale-apicast-staging.apps.ocp4.example.com:443/books?user_key=$API_KEY" | jq
      ...output omitted...
  2. Configure the secure_keys product to require authentication by using the API key-ID pair.

    1. Log in to RHOCP:

      [student@workstation ~]$ oc login \
      -u=admin -p=redhat --server=https://api.ocp4.example.com:6443
      ...output omitted...
    2. In a web browser, log in to the 3scale API Management Administration Portal as the admin user.

      Execute the following command to see the ADMIN_PASSWORD:

      [student@workstation ~]$ oc get secret system-seed -n 3scale \
        -o json | jq -r .data.ADMIN_PASSWORD | base64 -d; echo
      ...output omitted...
    3. In the Products section, click the secure_keys product.

    4. Click IntegrationSettings.

    5. In the Authentication section, select App_ID and App_Key Pair The application is identified via the App_ID and authenticated via the App_Key.

      Note that the user_key value becomes the default app_key value.

    6. Click Update Product. Then, click Configuration and click Promote v. 2 to Staging APIcast.

  3. Use the key-ID pair to authenticate your API call.

    1. Verify that using API key is no longer sufficient to authorize to the API:

      [student@workstation ~]$ curl \
        "https://secure-keys-3scale-apicast-staging.apps.ocp4.example.com:443/books?user_key=$API_KEY"; echo
      Authentication parameters missing
    2. Find the application ID for the application you created in the preceding steps. The following step uses ID 13 as the ID for the application:

      [student@workstation ~]$ API_ID=$(3scale application show 3scale-tenant 13 \
        -o json | jq -r '.application_id')
      [student@workstation ~]$ echo $API_ID
      eb34878e
    3. Authenticate your API call by using the API_KEY and API_ID variables:

      [student@workstation ~]$ curl \
        "https://secure-keys-3scale-apicast-staging.apps.ocp4.example.com:443/books?app_key=$API_KEY&app_id=$API_ID"| jq
      ...output omitted...

Finish

On the workstation machine, use the lab command to complete this exercise. This is important to ensure that resources from previous exercises do not impact upcoming exercises.

[student@workstation ~]$ lab finish secure-keys

This concludes the guided exercise.

Revision: do240-2.11-40390f6