Control service requests with application-aware intentions
In the previous tutorial you learned how to define intentions for your services in Consul service mesh.
Intentions presented in the previous tutorial allow broad all-or-nothing access control between pairs of services and provide a secure approach for TCP-based services. For this reason they are also referred to as L4 intentions, because they operate at the transport layer of the ISO/OSI model, and are only capable of allowing or denying access to a specific service.
Most modern scenarios, however, contain HTTP-based services, such as APIs or web services, that expose multiple endpoints and that might have different access profiles depending on the source of the request or on some metadata of the request itself. This includes HTTP headers, cookies, and URL paths.
To secure such services, a simple all-or-nothing access control is often insufficient.
Consul allows to specify a different, more powerful, kind of intentions that takes into account request metadata to define request permissions. They are usually referred as L7 intentions, or application-aware intentions, because they operate at the application layer of he ISO/OSI model, and permit a fine grained filtering of the different requests.
In this tutorial, you will learn how to apply application-aware intentions to your Consul datacenter to fine tune allowed requests for an existing service.
Tutorial scenario
This tutorial uses HashiCups, a demo coffee shop application made up of several microservices running on VMs.
At the beginning of the tutorial, you have a fully deployed Consul service mesh, with Envoy sidecar proxies running alongside each service, and an API Gateway configured to permit access to the NGINX service.
The Consul service mesh is initially configured with a deny-all
default policy for ACL and intentions, that disables undesired service-to-service communications, and with a set of minimal intentions that permit the HashiCups services to communicate properly.
During the tutorial, you will verify that the API service exposes multiple endpoints, /
, /api
, and /health
, and that the L4 intentions are not suited to selectively block requests on the different endpoints.
By the end of the tutorial, you will have learned how to apply L7 intentions to your Consul service mesh to remove access to the /
and /health
endpoints while allowing traffic over the /api
endpoint needed for the HashiCups application.
Prerequisites
This tutorial assumes you are already familiar with Consul service mesh and its core functionalities. If you are new to Consul refer to refer to the Consul Getting Started tutorials collection.
This tutorial uses an interactive lab to guide you through the process of setting up a service mesh on your VM workloads. The lab environment includes all required binaries and sample configurations.
Launch Terminal
This tutorial includes a free interactive command-line lab that lets you follow along on actual cloud infrastructure.
Configure CLI to interact with Consul
Configure your bastion host to communicate with your Consul environment using the two dynamically generated environment variable files.
After loading the needed variables, verify you can connect to your Consul datacenter.
Setup production intentions
Use the provided script to generate service intentions.
First, setup the output folder where to store the configuration files.
Then, use the script to generate intentions.
Apply the intentions to the Consul datacenter.
This will return the following output.
Check intentions for a Consul datacenter
After setting up the prerequisites, check the intentions configuration for your Consul datacenter.
Use the consul intention
command to check existing intentions.
Create ACL token for intentions management
To configure intentions for a service, Consul includes the intentions
scope for the ACL rules of a service.
In this tutorial you will configure intentions for the hashicups-api
service, therefore the policy will need the intentions = "write"
for this service.
First define the output folder where to store the files generated.
Then, create a policy file for the ACL rules you want to grant.
Tip
Consul grants permissions for creating and managing intentions based on the destination, not the source. When ACLs are enabled, services and operators must present a token linked to a policy that grants the necessary permissions to the destination service.
Create the Consul policy.
The output should look like the following.
Then, create a token associated with the policy.
To continue with the tutorial, using minimal permissions, export the first token as an environment variable.
Explore HashiCups API service endpoints
The HashiCups API service is configured to be reachable only from the NGINX node in this scenario. This implementation allows all-or-nothing access control between pairs of services and is a secure approach for TCP-based services.
HTTP-based services, like the HashiCups API service, often offer a more complex range of requests and can expose multiple endpoints to implement different request types or to expose different content.
You will now explore the different endpoints provided by the HashiCups API service.
Explore the HashiCups API /api path
Click on the NGINX tab.
Verify that the API service is exposed locally on the NGINX node on the port specified in the service upstream definition, 8081
.
If the service mesh is configured correctly you should get an output similar to the following, with one entry, on port 8081
corresponding to the API service.
Run a query to the HashiCups API service from the NGINX node.
A successful request will return an output similar to the following:
In the previous command, you used the /api
path for the request, but the API service also exposes other paths.
Explore the HashiCups API root path
The default path, /
, provides a number of information about the API service configuration.
Example output.
Explore the HashiCups API /health path
The API service also exposes a simple /health
endpoint that provides a summary of the state of the service.
If the service is up and running the call will respond with a simple message.
In some cases it can respond with a message informing about the temporary state of one of its sub-components.
Security considerations
When allowing connections towards an HTTP-based service, you also allow access to all endpoints exposed by that service. While some of these endpoints might be necessary for the correct behavior of the service, or might be needed by the service's downstreams, some of them might be unnecessary or might also expose sensitive information to external users.
In this case, allowing general connections towards the HashiCups API service allows access to all paths, including the /
and /health
paths, which are not required for the correct behavior of HashiCups. Extraneous application paths such as these could represent a security risk.
For cases like this, where a simple yes/no configuration is not enough to guarantee security, Consul allows to specify a different, more powerful, kind of intentions.
They are usually referred as L7 intentions, or application-aware intentions, because they operate at the application layer of he ISO/OSI model and permit a fine grained filtering of the different requests.
In the rest of the tutorial, you will learn how to apply application-aware intentions to your Consul datacenter to fine tune allowed requests for the API service.
Define and apply L7 intentions
The use of L7 intentions is only available for destination services using an HTTP-based protocol.
Click on the Bastion Host tab.
In this case the destination service is hashicups-api
and you can verify the configuration for it using the consul config
command.
The 404
message indicates that there is no configuration present for the hashicups-api
service. The service can be regarded as a TCP-based service.
Tip
Service protocol can also be specified using proxy-defaults
and, in that case, they will apply to all services in the service mesh. The protocol specified for individual service instances in the service-defaults
configuration entry takes precedence over the globally-configured value set in the proxy-defaults
.
Define service protocol for hashicups-api
To apply L7 intentions to hashicups-api
you will have to define it as an HTTP-based service first.
Define the output folder for the configuration file to be created.
Then, create the configuration file that defines the Protocol
value for the service.
Finally, apply the configuration to your Consul datacenter.
Once the configuration is applied, verify the hashicups-api
service now gets a configuration applied that defines it as an HTTP-based service.
The command will return the service-defaults
configuration applied to the hashicups-api
service. Notice that the output is not empty this time and that the Protocol
value is set to http
.
Define L7 intentions for hashicups-api
Once the API service is configured to be HTTP-based, it is possible to apply the L7 intention to it.
First, create the configuration file to describe the intention.
Then, apply the configuration using the consul config
command.
Note
Notice that the intention definition file also defines the intention between hashicups-nginx
and hashicups-api
even if an intention already exists for that connection in Consul. This is because when writing a configuration into Consul, the Name
parameter is used to describe the configuration and, in case an existing configuration with that name is already in place, it will get overwritten by the new one.
Check intentions in your Consul datacenter
After applying the desired intention, check the new intentions configuration for your Consul datacenter.
Use the consul intention
command to check existing intentions.
Note
The intentions shown by the command depend on the ACL token present in the CONSUL_HTTP_TOKEN
variable. If using a limited token, that only gives permissions over the hashicups-api
service, only the intentions relative to that service will be returned by the command.
To get more details over the specific intentions in place for hashicups-api
, use the consul config
command.
This will provide you with the full details of all service intentions regarding the hashicups-api
service.
Verify connections are now secured
Now that all intentions are in place, it is time to verify that the service works as expected and that the undesired endpoints are now unavailable from other nodes.
Check HashiCups UI
The first check to perform is to make sure that the HashiCups application is still functional.
Click on the HashiCups tab.
Confirm that HashiCups still works with the new intentions in place.
Test service to service connection
Now, after confirming that the application is working as expected, you can verify that only the /api
endpoint is reachable from the NGINX node.
Click on the NGINX tab.
Verify the connection to the API service from the NGINX node.
If the intentions are being enforced properly, the command will return an output similar to the following:
Next, you can test if the /
endpoint is reachable.
If the intentions are being enforced properly, the command will return an output similar to the following:
This means that the request is being blocked by the Role Based Access Control (RBAC) enforced by Consul.
Finally, you can test if the /health
endpoint is reachable.
If the intentions are being enforced properly, the command will return an output similar to the following:
This means that the request is being blocked by the Role Based Access Control (RBAC) enforced by Consul.
Next steps
In this tutorial you learned how to use L7 intentions in Consul service mesh to control service requests at the application level.
More specifically, you learned to:
- Verify the existing intentions in a Consul datacenter
- Modify the service default protocol from TCP to HTTP
- Create an L7 intention definition
- Apply the intention to Consul datacenter using API endpoints or Consul CLI commands
For more information about the topics covered in this tutorial, refer to the following resources: