Add Basic Authentication to a Service or a Route with username and password protection. The plugin
checks for valid credentials in the Proxy-Authorization
and Authorization
headers (in that order).
Configuration Reference
This plugin is partially compatible with DB-less mode.
In DB-less mode, you configure Kong Gateway
declaratively.
Therefore, the Admin API is mostly read-only. The only tasks it can perform are all
related to handling the declarative config, including:
- Setting a target's health status in the load balancer
- Validating configurations against schemas
- Uploading the declarative configuration using the
/config
endpoint
Consumers and Credentials can be created with declarative configuration.
Admin API endpoints which do POST, PUT, PATCH or DELETE on Credentials are not available on DB-less mode.
Example plugin configuration
Enable on a service
Enable on a route
Enable globally
The following examples provide some typical configurations for enabling
the basic-auth
plugin on a
service.
Admin API
Kubernetes
Declarative (YAML)
Konnect Cloud
Kong Manager
Make the following request:
curl -X POST http://localhost:8001/services/SERVICE_NAME|SERVICE_ID/plugins \
--data "name=basic-auth" \
--data "config.hide_credentials=true"
Replace SERVICE_NAME|SERVICE_ID
with the id
or name
of the service that this plugin configuration will target.
First, create a KongPlugin
resource:
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
name: basic-auth-example
config:
hide_credentials: true
plugin: basic-auth
Next, apply the KongPlugin resource to a
service by annotating the
service as follows:
apiVersion: v1
kind: Service
metadata:
name: SERVICE_NAME|SERVICE_ID
labels:
app: SERVICE_NAME|SERVICE_ID
annotations:
konghq.com/plugins: basic-auth-example
spec:
ports:
- port: 80
targetPort: 80
protocol: TCP
name: SERVICE_NAME|SERVICE_ID
selector:
app: SERVICE_NAME|SERVICE_ID
Replace SERVICE_NAME|SERVICE_ID
with the id
or name
of the service that this plugin configuration will target.
Note: The KongPlugin resource only needs to be defined once
and can be applied to any service, consumer, or route in the namespace. If you
want the plugin to be available cluster-wide, create the resource as a
KongClusterPlugin
instead of KongPlugin
.
Add this section to your declarative configuration file:
plugins:
- name: basic-auth
service: SERVICE_NAME|SERVICE_ID
config:
hide_credentials: true
Replace SERVICE_NAME|SERVICE_ID
with the id
or name
of the service that this plugin configuration will target.
You can configure this plugin through the Konnect UI.
From the
Service Hub, select a service version, then set up the plugin:
- In the Plugins section, click Add Plugin.
- Find and select the Basic Authentication plugin.
-
Configure the plugin’s parameters.
You can test out the plugin with the following sample configuration:
- Config.Hide Credentials: select checkbox
- Click Create.
You can configure this plugin through the Kong Manager UI.
- In Kong Manager, select the workspace.
- From the Services section, click View for the
service row.
- From the plugin section, click Add Plugin.
- Find and select the Basic Authentication plugin.
- If the option is available, select Scoped.
- Add the service name and ID to the Service field if it
is not already pre-filled.
-
Configure the plugin’s parameters.
You can test out the plugin with the following sample configuration:
- Config.Hide Credentials: select checkbox
- Click Create.
The following examples provide some typical configurations for enabling
the basic-auth
plugin on a
route.
Admin API
Kubernetes
Declarative (YAML)
Konnect Cloud
Kong Manager
Make the following request:
curl -X POST http://localhost:8001/routes/ROUTE_NAME|ROUTE_ID/plugins \
--data "name=basic-auth" \
--data "config.hide_credentials=true"
Replace ROUTE_NAME|ROUTE_ID
with the id
or name
of the route that this plugin configuration will target.
First, create a KongPlugin
resource:
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
name: basic-auth-example
config:
hide_credentials: true
plugin: basic-auth
Then, apply it to an ingress (route or routes)
by annotating the ingress as follows:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ROUTE_NAME|ROUTE_ID
annotations:
kubernetes.io/ingress.class: kong
konghq.com/plugins: basic-auth-example
spec:
rules:
- host: examplehostname.com
http:
paths:
- path: /bar
backend:
service:
name: echo
port:
number: 80
Replace ROUTE_NAME|ROUTE_ID
with the id
or name
of the route that this plugin configuration will target.
Note: The KongPlugin resource only needs to be defined once
and can be applied to any service, consumer, or route in the namespace. If you
want the plugin to be available cluster-wide, create the resource as a
KongClusterPlugin
instead of KongPlugin
.
Add this section to your declarative configuration file:
plugins:
- name: basic-auth
route: ROUTE_NAME
config:
hide_credentials: true
Replace ROUTE_NAME|ROUTE_ID
with the id
or name
of the route that this plugin configuration
will target.
You can configure this plugin through the Konnect UI.
From the
Service Hub, select a service version, then set up the plugin:
- Select a route.
- In the Plugins section, click Add Plugin.
- Find and select the Basic Authentication plugin.
-
Configure the plugin’s parameters.
You can test out the plugin with the following sample configuration:
- Config.Hide Credentials: select checkbox
- Click Create.
You can configure this plugin through the Kong Manager UI.
- In Kong Manager, select the workspace.
- Open Routes from the menu, then click View for the
route row.
- From the plugin section, click Add Plugin.
- Find and select the Basic Authentication plugin.
- If the option is available, select Scoped.
- Add the route ID if it is not already prefilled.
-
Configure the plugin’s parameters.
You can test out the plugin with the following sample configuration:
- Config.Hide Credentials: select checkbox
- Click Create.
A plugin which is not associated to any service, route, or consumer is
considered global, and will be run on every request. Read the
Plugin Reference and the Plugin Precedence
sections for more information.
The following examples provide some typical configurations for enabling
the basic-auth
plugin globally.
Admin API
Kubernetes
Declarative (YAML)
Kong Manager
Make the following request:
curl -X POST http://localhost:8001/plugins/ \
--data "name=basic-auth" \
--data "config.hide_credentials=true"
Create a KongClusterPlugin
resource and label it as global:
apiVersion: configuration.konghq.com/v1
kind: KongClusterPlugin
metadata:
name: <global-basic-auth>
annotations:
kubernetes.io/ingress.class: kong
labels:
global: \"true\"
config:
hide_credentials: true
plugin: basic-auth
Add a plugins
entry in the declarative
configuration file:
plugins:
- name: basic-auth
config:
hide_credentials: true
You can configure this plugin through the Kong Manager UI.
- In Kong Manager, select the workspace.
- Open Plugins from the menu, then click New Plugin.
- Find and select the Basic Authentication plugin.
- If the option is available, set the plugin scope to Global.
-
Configure the plugin’s parameters.
You can test out the plugin with the following sample configuration:
- Config.Hide Credentials: select checkbox
- Click Create.
Parameters
Here's a list of all the parameters which can be used in this plugin's configuration:
Form Parameter |
Description |
name
required
Type: string |
The name of the plugin, in this case basic-auth . |
service.name or service.id
Type: string |
The name or ID of the service the plugin targets.
Set one of these parameters if adding the plugin to a service through the top-level /plugins endpoint.
Not required if using /services/SERVICE_NAME|SERVICE_ID/plugins . |
route.name or route.id
Type: string |
The name or ID of the route the plugin targets.
Set one of these parameters if adding the plugin to a route through the top-level /plugins endpoint.
Not required if using /routes/ROUTE_NAME|ROUTE_ID/plugins . |
enabled
Type: boolean
Default value: true |
Whether this plugin will be applied. |
config.hide_credentials
required
Type: boolean
Default value: false
|
An optional boolean value telling the plugin to show or hide the credential from the upstream service. If true , the plugin will strip the credential from the request (i.e. the Authorization header) before proxying it.
|
config.anonymous
optional
Type: string
|
An optional string (Consumer UUID or username) value to use as an “anonymous” consumer if authentication fails. If empty (default null), the request will fail with an authentication failure 4xx . Please note that this value must refer to the Consumer id or username attribute, and not its custom_id .
|
Once applied, any user with a valid credential can access the Service.
To restrict usage to only some of the authenticated users, also add the
ACL plugin (not covered here) and create allowed or
denied groups of users.
Usage
To use the plugin, you first need to create a Consumer to associate one or more credentials to. The Consumer represents a developer or an application consuming the upstream service.
Create a Consumer
You need to associate a credential to an existing Consumer object.
A Consumer can have many credentials.
With a Database
Without a Database
To create a Consumer, you can execute the following request:
curl -d "username={USER123}&custom_id={SOME_CUSTOM_ID}" http://kong:8001/consumers/
Your declarative configuration file needs to have one or more Consumers.
You can create them using a consumers:
YAML section:
consumers:
- username: {USER123}
custom_id: {SOME_CUSTOM_ID}
Consumer parameters:
Parameter |
Description |
username semi-optional |
The username of the consumer. Either this field or custom_id must be specified. |
custom_id semi-optional |
A custom identifier used to map the consumer to another database. Either this field or username must be specified. |
If you are also using the ACL plugin and allow lists with this
service, you must add the new consumer to the allowed group. See
ACL: Associating Consumers for details.
Create a Credential
With a Database
Without a Database
You can provision new username/password credentials by making the following HTTP request:
curl -X POST http://kong:8001/consumers/{CONSUMER}/basic-auth \
--data "username=Aladdin" \
--data "password=OpenSesame"
You can add credentials on your declarative config file with the basicauth_credentials
YAML entry:
basicauth_credentials:
- consumer: {CONSUMER}
username: Aladdin
password: OpenSesame
Consumer credential parameters:
field/parameter |
description |
consumer |
The id or username property of the Consumer entity to associate the credentials to. |
username |
The username to use in the basic authentication credential. |
password |
The password to use in the basic authentication credential. |
Using the Credential
The authorization header must be base64 encoded. For example, if the credential
uses Aladdin
as the username and OpenSesame
as the password, then the field’s
value is the base64-encoding of Aladdin:OpenSesame
, or QWxhZGRpbjpPcGVuU2VzYW1l
.
The Authorization
(or Proxy-Authorization
) header must appear as:
Authorization: Basic QWxhZGRpbjpPcGVuU2VzYW1l
Make a request with the header:
curl http://kong:8000/{PATH_MATCHING_CONFIGURED_ROUTE} \
-H 'Authorization: Basic QWxhZGRpbjpPcGVuU2VzYW1l'
gRPC clients are supported too:
grpcurl -H 'Authorization: Basic QWxhZGRpbjpPcGVuU2VzYW1l' ...
When a client has been authenticated, the plugin appends some headers to
the request before proxying it to the upstream service, so that you
can identify the consumer in your code:
X-Consumer-ID
: The ID of the consumer in Kong.
X-Consumer-Custom-ID
: The custom_id
of the consumer (if set).
X-Consumer-Username
: The username
of the consumer (if set).
X-Credential-Identifier
: The identifier of the credential (only if the consumer is not the anonymous
consumer).
X-Anonymous-Consumer
: Is set to true
if authentication fails, and the anonymous
consumer is set instead.
You can use this information on your side to implement additional logic.
You can use the X-Consumer-ID
value to query the Kong Admin API and retrieve
more information about the consumer.
Paginate through the basic-auth Credentials
You can paginate through the basic-auth Credentials for all Consumers using the
following request:
curl -X GET http://kong:8001/basic-auths
{
"total": 3,
"data": [
{
"created_at": 1511379926000,
"id": "805520f6-842b-419f-8a12-d1de8a30b29f",
"password": "37b1af03d3860acf40bd9c681aa3ef3f543e49fe",
"username": "baz",
"consumer": { "id": "5e52251c-54b9-4c10-9605-b9b499aedb47" }
},
{
"created_at": 1511379863000,
"id": "8edfe5c7-3151-4d92-971f-3faa5e6c5d7e",
"password": "451b06c564a06ce60874d0ea2f542fa8ed26317e",
"username": "foo",
"consumer": { "id": "89a41fef-3b40-4bb0-b5af-33da57a7ffcf" }
},
{
"created_at": 1511379877000,
"id": "f11cb0ea-eacf-4a6b-baea-a0e0b519a990",
"password": "451b06c564a06ce60874d0ea2f542fa8ed26317e",
"username": "foobar",
"consumer": { "id": "89a41fef-3b40-4bb0-b5af-33da57a7ffcf" }
}
]
}
You can filter the list by Consumer with the following endpoint:
curl -X GET http://kong:8001/consumers/{USERNAME_OR_ID}/basic-auth
{
"total": 1,
"data": [
{
"created_at": 1511379863000,
"id": "8edfe5c7-3151-4d92-971f-3faa5e6c5d7e",
"password": "451b06c564a06ce60874d0ea2f542fa8ed26317e",
"username": "foo",
"consumer": { "id": "89a41fef-3b40-4bb0-b5af-33da57a7ffcf" }
}
]
}
username
or id
: The username or id of the consumer whose credentials need
to be listed.
Retrieve the Consumer associated with a Credential
It is possible to retrieve a Consumer associated with a
basic-auth Credential using the following request:
curl -X GET http://kong:8001/basic-auths/{USERNAME_OR_ID}/consumer
{
"created_at":1507936639000,
"username":"foo",
"id":"c0d92ba9-8306-482a-b60d-0cfdd2f0e880"
}
username or id
: The id
or username
property of the basic-auth
Credential for which to get the associated Consumer.
Note that the username
accepted here is not the username
property of a
Consumer.
Changelog
Kong Gateway 3.0.x
- The deprecated
X-Credential-Username
header has been removed.
Kong Gateway 2.7.x
- Starting with Kong Gateway 2.7.0.0, if keyring encryption is enabled
and you are using basic authentication, the
basicauth_credentials.password
field will be encrypted.