Query API key information API

edit

Query API key information API

edit

Retrieves information for API keys with Query DSL in a paginated fashion.

Request

edit

GET /_security/_query/api_key

POST /_security/_query/api_key

Prerequisites

edit
  • To use this API, you must have at least the manage_own_api_key cluster privilege.
  • If you have only the manage_own_api_key privilege, this API returns only the API keys that you own. If you have the manage_api_key or greater privileges (including manage_security), this API returns all API keys regardless of ownership.

Description

edit

Use this API to retrieve the API keys created with the create API key API in a paginated manner. You can optionally filter the results with a query.

Request body

edit

You can specify the following parameters in the request body:

query

(Optional, string) A query to filter which API keys to return. The query supports a subset of query types, including match_all, bool, term, terms, ids, prefix, wildcard, exists, and range.

You can query all public information associated with an API key, including the following values.

Valid values for query
id
ID of the API key. Note id must be queried with the ids query.
name
Name of the API key.
creation
Creation time of the API key in milliseconds.
expiration
Expiration time of the API key in milliseconds.
invalidated
Indicates whether the API key is invalidated. If true, the key is invalidated. Defaults to false.
username
Username of the API key owner.
realm
Realm name of the API key owner.
metadata
Metadata field associated with the API key, such as metadata.my_field. Because metadata is stored as a flattened field type, all fields act like keyword fields when querying and sorting.
from

(Optional, integer) Starting document offset. Needs to be non-negative and defaults to 0.

By default, you cannot page through more than 10,000 hits using the from and size parameters. To page through more hits, use the search_after parameter.

size

(Optional, integer) The number of hits to return. Must not be negative and defaults to 10.

By default, you cannot page through more than 10,000 hits using the from and size parameters. To page through more hits, use the search_after parameter.

sort
(Optional, object) Sort definition. Other than id, all public fields of an API key are eligible for sorting. In addition, sort can also be applied to the _doc field to sort by index order.
search_after
(Optional, array) Search after definition.

Response body

edit

This API returns the following top level fields:

total
The total number of API keys found.
count
The number of API keys returned in the response.
api_keys
A list of API key information.

Examples

edit

The following request lists all API keys, assuming you have the manage_api_key privilege:

GET /_security/_query/api_key

A successful call returns a JSON structure that contains the information retrieved from one or more API keys:

{
  "total": 3,
  "count": 3,
  "api_keys": [ 
    {
      "id": "nkvrGXsB8w290t56q3Rg",
      "name": "my-api-key-1",
      "creation": 1628227480421,
      "expiration": 1629091480421,
      "invalidated": false,
      "username": "elastic",
      "realm": "reserved",
      "metadata": {
        "letter": "a"
      }
    },
    {
      "id": "oEvrGXsB8w290t5683TI",
      "name": "my-api-key-2",
      "creation": 1628227498953,
      "expiration": 1628313898953,
      "invalidated": false,
      "username": "elastic",
      "realm": "reserved",
      "metadata": {
        "letter": "b"
      }
    }
  ]
}

The list of API keys that were retrieved for this request

If you create an API key with the following details:

POST /_security/api_key
{
  "name": "application-key-1",
  "metadata": { "application": "my-application"}
}

A successful call returns a JSON structure that provides API key information. For example:

{
  "id": "VuaCfGcBCdbkQm-e5aOx",
  "name": "application-key-1",
  "api_key": "ui2lp2axTNmsyakw9tvNnw",
  "encoded": "VnVhQ2ZHY0JDZGJrUW0tZTVhT3g6dWkybHAyYXhUTm1zeWFrdzl0dk5udw=="
}

Use the information from the response to retrieve the API key by ID:

GET /_security/_query/api_key
{
  "query": {
    "ids": {
      "values": [
        "VuaCfGcBCdbkQm-e5aOx"
      ]
    }
  }
}

You can also retrieve the API key by name:

GET /_security/_query/api_key
{
  "query": {
    "term": {
      "name": {
        "value": "application-key-1"
      }
    }
  }
}

Use a bool query to issue complex logical conditions and use from, size, sort to help paginate the result:

GET /_security/_query/api_key
{
  "query": {
    "bool": {
      "must": [
        {
          "prefix": {
            "name": "app1-key-" 
          }
        },
        {
          "term": {
            "invalidated": "false" 
          }
        }
      ],
      "must_not": [
        {
          "term": {
            "name": "app1-key-01" 
          }
        }
      ],
      "filter": [
        {
          "wildcard": {
            "username": "org-*-user" 
          }
        },
        {
          "term": {
            "metadata.environment": "production" 
          }
        }
      ]
    }
  },
  "from": 20, 
  "size": 10, 
  "sort": [ 
    { "creation": { "order": "desc", "format": "date_time" } },
    "name"
  ]
}

The API key name must begin with app1-key-

The API key must still be valid

The API key name must not be app1-key-01

The API key must be owned by a username of the wildcard pattern org-*-user

The API key must have the metadata field environment that has the value of production

The offset to begin the search result is the 20th (zero-based index) API key

The page size of the response is 10 API keys

The result is first sorted by creation date in descending order, then by name in ascending order

The response contains a list of matched API keys along with their sort values:

{
  "total": 100,
  "count": 10,
  "api_keys": [
    {
      "id": "CLXgVnsBOGkf8IyjcXU7",
      "name": "app1-key-79",
      "creation": 1629250154811,
      "invalidated": false,
      "username": "org-admin-user",
      "realm": "native1",
      "metadata": {
        "environment": "production"
      },
      "_sort": [
        "2021-08-18T01:29:14.811Z",  
        "app1-key-79"  
      ]
    },
    {
      "id": "BrXgVnsBOGkf8IyjbXVB",
      "name": "app1-key-78",
      "creation": 1629250153794,
      "invalidated": false,
      "username": "org-admin-user",
      "realm": "native1",
      "metadata": {
        "environment": "production"
      },
      "_sort": [
        "2021-08-18T01:29:13.794Z",
        "app1-key-78"
      ]
    },
    ...
  ]
}

The first sort value is creation time, which is displayed in date_time format as defined in the request

The second sort value is the API key name

You can use the following request to retrieve all valid API keys, i.e. not invalidated and not expired:

GET /_security/_query/api_key
{
  "query": {
    "bool": {
      "must": {
        "term": {
          "invalidated": false  
        }
      },
      "should": [  
        {
          "range": {
            "expiration": {
              "gte": "now"
            }
          }
        },
        {
          "bool": {
            "must_not": {
              "exists": {
                "field": "expiration"
              }
            }
          }
        }
      ],
      "minimum_should_match": 1
    }
  }
}

Matching API keys must not be invalidated

Matching API keys must be either not expired or does not have an expiration date