API key and OPA
Learn how to combine the API key and OPA extauth modules to perform multi-step authentication for incoming requests.
When a client sends an API key to authenticate with another service in the cluster, the gateway proxy can extract and validate the API key by using the API key extauth module. In addition, if the API key contains additional metadata, such as the user ID or email address, Gloo automatically extracts these metadata fields from the API key, and forwards them with the API key to the OPA module where additional validation checks can be performed.
In this guide, you can try out different API key and OPA configurations, such as:
- Successfully validate requests with additional API key metadata by using the OPA extauth module.
- Deny requests that do not provide additional metadata in the API key.
- Create an OPA rule to allow only certain API keys.
If you import or export resources across workspaces, your policies might not apply. For more information, see Import and export policies.
Before you begin
This guide assumes that you use the same names for components like clusters, workspaces, and namespaces as in the getting started. If you have different names, make sure to update the sample configuration files in this guide.
- Set up Gloo Mesh Gateway in a single cluster.
- Install Bookinfo and other sample apps.
Configure an HTTP listener on your gateway and set up basic routing for the sample apps.
Make sure that the external auth service is installed and running. If not, install the external auth service.
Make sure that the
extAuthService.extAuth.apiKey.metadataToState=true
field is set on your external auth server. In Gloo Mesh Gateway version 2.6.0 and later, this setting ensures that Gloo automatically extracts metadata fields from the API key to forward to OPA for validation checks.Check the Helm releases in your cluster. Depending on your installation method, you either have only a main installation release (such as
gloo-platform
), or a main installation and a separate add-ons release (such asgloo-agent-addons
), in addition to your CRDs release.Get your current installation values.
- If you have only one release for your installation, get those values. Note that your Helm release might have a different name.
- If you have a separate add-ons release, get those values.
- If you have only one release for your installation, get those values. Note that your Helm release might have a different name.
Add the following settings to allow API key metadata to be sent to the OPA state.
Upgrade your Helm release.
- If you have only one release for your installation, upgrade the
gloo-platform
release. Note that your Helm release might have a different name. - If you have a separate add-ons release, upgrade the
gloo-agent-addons
release.
- If you have only one release for your installation, upgrade the
Make sure that you have the following CLI tools, or something comparable:
base64
to encode strings.
Configure API key and OPA external auth
Create an extauth server that you use to enforce the extauth policies in this guide.
Create a Kubernetes secret that stores your API key and additional metadata, such as the user ID and email address.
Label the secret so that you can reference this secret in your extauth policy more easily.
Create a configmap for an OPA rule that validates the user’s email address that you added earlier. The following examples verifies that the email address in the
user-email
field ends withsolo.io
.Create an extauth policy that references both the secret that contains the API key and the configmap with your OPA rule. Note that Gloo Mesh Gateway automatically extracts the
user-email
field from the API key and passes it on to the OPA module for further validation.Send a request to the ratings app and pass the API key that you added to the Kubernetes secret.
- HTTP:
- HTTPS:
Example output:
- HTTP:
Create another configmap and add an OPA rule that references metadata that does not exist in the API key.
Update the extauth policy to reference the new configmap.
Send another request to the ratings service. This time, the request is denied, because the new
user-cost-center
metadata field could not be extracted from the API key to be processed by the OPA module.- HTTP:
- HTTPS:
Example output:
- HTTP:
Add the
user-cost-center
field to the API key.Send another request to the ratings service. This time, the request succeeds as the
user-cost-center
can be extracted and forwarded to the OPA extauth module for further validation.- HTTP:
- HTTPS:
Example output:
- HTTP:
Create another configmap and add another OPA rule to verify the API key itself and deny requests that use a specific API key.
Update the extauth policy to use the new OPA rule.
Send another request to the ratings app. This time, the request is denied, because the API key that is used in the curl request is part of the API keys that are not allowed to be forwarded to the ratings app.
- HTTP:
- HTTPS:
Example output:
- HTTP:
Cleanup
You can optionally remove the resources that you set up as part of this guide.
Known limitations
When you have multiple Kubernetes secrets that share the same label, and you use labels to reference the Kubernetes secret, the extauth policy passes the API key and metadata information from the first Kubernetes secret that is found to the OPA module.