authz-keycloak
tip
Although this Plugin was developed to work with Keycloak, it should work with any OAuth/OIDC and UMA compliant identity providers as well.
Refer to Authorization Services Guide for more information on Keycloak.
Attributes
NOTE: encrypt_fields = {"client_secret"}
is also defined in the schema, which means that the field will be stored encrypted in etcd. See encrypted storage fields.
It is recommended to use the discovery
attribute as the authz-keycloak
Plugin can discover the Keycloak API endpoints from it.
If set, the token_endpoint
and resource_registration_endpoint
will override the values obtained from the discovery document.
Client ID and secret
The Plugin needs the client_id
attribute for identification and to specify the context in which to evaluate permissions when interacting with Keycloak.
If the lazy_load_paths
attribute is set to true, then the Plugin additionally needs to obtain an access token for itself from Keycloak. In such cases, if the client access to Keycloak is confidential, you need to configure the client_secret
attribute.
The policy_enforcement_mode
attribute specifies how policies are enforced when processing authorization requests sent to the server.
ENFORCING
mode
Requests are denied by default even when there is no policy associated with a resource.
The policy_enforcement_mode
is set to ENFORCING
by default.
PERMISSIVE
mode
Requests are allowed when there is no policy associated with a given resource.
Permissions
If the lazy_load_paths
attribute is set to false
, the permissions are taken from the permissions
attribute. Each entry in permissions
needs to be formatted as expected by the token endpoint’s permission
parameter. See .
note
A valid permission can be a single resource or a resource paired with on or more scopes.
If the lazy_load_paths
attribute is set to true
, the request URI is resolved to one or more resources configured in Keycloak using the resource registration endpoint. The resolved resources are used as the permissions to check.
note
This requires the Plugin to obtain a separate access token for itself from the token endpoint. So, make sure to set the option in the client settings in Keycloak.
Also make sure that the issued access token contains the resource_access
claim with the uma_protection
role to ensure that the Plugin is able to query resources through the Protection API.
The http_method_as_scope
is often used together with lazy_load_paths
but can also be used with a static permission list.
If the http_method_as_scope
attribute is set to true
, the Plugin maps the request’s HTTP method to the scope with the same name. The scope is then added to every permission to check.
If the lazy_load_paths
attribute is set to false, the Plugin adds the mapped scope to any of the static permissions configured in the permissions
attribute—even if they contain on or more scopes already.
Generating a token using password
grant
To generate a token using password
grant, you can set the value of the password_grant_token_generation_incoming_uri
attribute.
If the incoming URI matches the configured attribute and the request method is POST, a token is generated using the token_endpoint
.
You also need to add application/x-www-form-urlencoded
as Content-Type
header and username
and password
as parameters.
The example below shows how you can enable the authz-keycloak
Plugin on a specific Route. ${realm}
represents the realm name in Keycloak.
curl http://127.0.0.1:9180/apisix/admin/routes/5 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"uri": "/get",
"plugins": {
"authz-keycloak": {
"token_endpoint": "http://127.0.0.1:8090/auth/realms/${realm}/protocol/openid-connect/token",
"client_id": "Client ID"
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"127.0.0.1:8080": 1
}
}'
Example usage
Once you have enabled the Plugin on a Route you can use it.
First, you have to get the JWT token from Keycloak:
Now you can make requests with the obtained JWT token:
curl http://127.0.0.1:9080/get -H 'Authorization: Bearer {JWT Token}'
To learn more about how you can integrate authorization policies into your API workflows you can checkout the unit test authz-keycloak.t.
Run the following Docker image and go to http://localhost:8090
to view the associated policies for the unit tests.
The image below shows how the policies are configured in the Keycloak server:
To disable the authz-keycloak
Plugin, you can delete the corresponding JSON configuration from the Plugin configuration. APISIX will automatically reload and you do not have to restart for this to take effect.
curl http://127.0.0.1:9180/apisix/admin/routes/5 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"uri": "/get",
"plugins": {
},
"upstream": {
"type": "roundrobin",
"nodes": {
"127.0.0.1:8080": 1
}
}
}'
Plugin roadmap
To support reading scope and configurations from the Keycloak JSON file.