Create a Key Set with a PEM Key

Uses: Kong Gateway
Related Documentation
Tags
Minimum Version
Kong Gateway - 3.4
TL;DR

Create a Key Set with the /key-sets endpoint, then create a Key and configure the set.id or set.name parameter to point to the Key Set.

Prerequisites

This is a Konnect tutorial and requires a Konnect personal access token.

  1. Create a new personal access token by opening the Konnect PAT page and selecting Generate Token.

  2. Export your token to an environment variable:

     export KONNECT_TOKEN='YOUR_KONNECT_PAT'
    
  3. Run the quickstart script to automatically provision a Control Plane and Data Plane, and configure your environment:

     curl -Ls https://get.konghq.com/quickstart | bash -s -- -k $KONNECT_TOKEN --deck-output
    

    This sets up a Konnect Control Plane named quickstart, provisions a local Data Plane, and prints out the following environment variable exports:

     export DECK_KONNECT_TOKEN=$KONNECT_TOKEN
     export DECK_KONNECT_CONTROL_PLANE_NAME=quickstart
     export KONNECT_CONTROL_PLANE_URL=https://us.api.konghq.com
     export KONNECT_PROXY_URL='http://localhost:8000'
    

    Copy and paste these into your terminal to configure your session.

This tutorial requires Kong Gateway Enterprise. If you don’t have Kong Gateway set up yet, you can use the quickstart script with an enterprise license to get an instance of Kong Gateway running almost instantly.

  1. Export your license to an environment variable:

     export KONG_LICENSE_DATA='LICENSE-CONTENTS-GO-HERE'
    
  2. Run the quickstart script:

     curl -Ls https://get.konghq.com/quickstart | bash -s -- -e KONG_LICENSE_DATA 
    

    Once Kong Gateway is ready, you will see the following message:

     Kong Gateway Ready
    

This tutorial requires a public key and private key. You can generate them using OpenSSL:

openssl genrsa -out private.pem 2048
openssl rsa -in private.pem -outform PEM -pubout -out public.pem

Create a Key Set

Using the Admin API, create a Key Set to hold PEM keys:

curl -X POST "$KONNECT_CONTROL_PLANE_URL/key-sets" \
     -H "Accept: application/json"\
     -H "Content-Type: application/json" \
     --json '{
       "name": "my-pem-key-set"
     }'
curl -X POST "http://localhost:8001/key-sets" \
     -H "Accept: application/json"\
     -H "Content-Type: application/json" \
     --json '{
       "name": "my-pem-key-set"
     }'

You will get a 201 Created response with details about the new Key Set.

Create a Key

Create a Key and use either the set.id from the response in the previous step, or the set.name parameter to add it to the Key Set. To avoid errors, the private and public keys should be strings, and newlines should replaced with \n:

curl -X POST "$KONNECT_CONTROL_PLANE_URL/keys" \
     -H "Accept: application/json"\
     -H "Content-Type: application/json"\
     -H "Kong-Admin-Token: $KONG_ADMIN_TOKEN" \
     --json '{
       "name": "my-pem-key",
       "kid": "my-pem-key",
       "set": {
         "name": "my-pem-key-set"
       },
       "pem": {
         "private_key": "-----BEGIN PRIVATE KEY-----\nprivate-key-content\n-----END PRIVATE KEY-----",
         "public_key": "-----BEGIN PUBLIC KEY-----\npublic-key-content\n-----END PUBLIC KEY-----"
       }
     }'
curl -X POST "http://localhost:8001/keys" \
     -H "Accept: application/json"\
     -H "Content-Type: application/json"\
     -H "Kong-Admin-Token: $KONG_ADMIN_TOKEN" \
     --json '{
       "name": "my-pem-key",
       "kid": "my-pem-key",
       "set": {
         "name": "my-pem-key-set"
       },
       "pem": {
         "private_key": "-----BEGIN PRIVATE KEY-----\nprivate-key-content\n-----END PRIVATE KEY-----",
         "public_key": "-----BEGIN PUBLIC KEY-----\npublic-key-content\n-----END PUBLIC KEY-----"
       }
     }'

You will get a 201 Created response with details about the new Key, including the Key Set ID.

Cleanup

If you created a new control plane and want to conserve your free trial credits or avoid unnecessary charges, delete the new control plane used in this tutorial.

curl -Ls https://get.konghq.com/quickstart | bash -s -- -d
Something wrong?

Help us make these docs great!

Kong Developer docs are open source. If you find these useful and want to make them better, contribute today!