Figure 2: AuthZ policy logic implemented in backend services
In this case, policies become more complex and must be implemented in customer management and order processing services, as shown in Figure 2. Different teams are responsible for these services, so the implementation must be coordinated and tested.
Listing 1: AuthZ policies pseudocode
The outlined approach can lead to duplication and inconsistency. Furthermore, services risk not being compliant with company policies or regulations. Likewise, they’re harder to maintain and update.
Integrating a separate policy engine, like Open Policy Agent (OPA), with Kong Gateway lets you offload policy decisions from your backend services, ensuring consistent and fine-grained access control.
Policy management with OPA
OPA is an open source, general-purpose policy engine. It can decouple policy decisions from the service logic, making managing and maintaining complex decisions like access controls easier. OPA evaluates policies written in Rego, a declarative language, and provides a REST API to manage policies and query policy decisions.
OPA provides a flexible and powerful way to define policies that can be reused across services. It allows you to define policies based on various attributes, such as user roles, request methods, headers, and more. This makes it easy to create fine-grained access control policies that can be easily updated and maintained.
OPA operates alongside Kong Gateway, microservices, or any other service that needs policy decisions. It receives requests, evaluates them against the policies you’ve defined in Rego, and returns an allow/deny decision. Doing so ensures that the policies are consistently enforced across all your services. In addition, you can easily update and test policies without redeploying your services. Moreover, you can version policies independently of the services, allowing for a more agile policy management process.
OPA and Kong API Gateway
As mentioned, OPA can be integrated with Kong Gateway to provide fine-grained access control for your APIs. But what does this mean, how does it work, and what are the benefits?
When enterprises implement a combined approach of OPA and Kong Gateway, the benefits for API management include:
- Fine-grained access control
- Scalability for distributed systems and microservices
- Declarative policy language (Rego)
- Consistency across various layers of an application (API, Kubernetes, databases, etc.)
Integrating OPA in existing architectures is straightforward. OPA provides a REST API for managing policies and requesting policy decisions from the policy engine. Integrating OPA with Kong Gateway is even easier, as Kong provides an OPA plugin allowing you to delegate authorization decisions to OPA. The OPA plugin for Kong acts as a bridge between Kong and OPA. To developers familiar with Kong’s plugin mechanism, the configuration is intuitive.
Concerning our example, establishing this approach helps to separate complex policy logic from the Gateway configuration. As Figure 3 shows, a policy developer can define the new policies, depicted as pseudocode in Listing 1, in Rego. One crucial fact the figure shows is that a policy developer is not necessarily a service developer but might be part of the security department. This separation of concerns allows for a more agile development process, as policies can be updated and tested independently of the services.