All Projects → build-security → opa-java-spring-client

build-security / opa-java-spring-client

Licence: Apache-2.0 license
Simple Spring client for working with the Open Policy Agent

Programming Languages

java
68154 projects - #9 most used programming language
Dockerfile
14818 projects

Projects that are alternatives of or similar to opa-java-spring-client

authcheck
Analysis for access-control vulnerabilities in Java Spring Security applications.
Stars: ✭ 14 (-26.32%)
Mutual labels:  authorization, springframework
server
AuthzForce Server (Community Edition)
Stars: ✭ 48 (+152.63%)
Mutual labels:  authorization, pdp
opa-kafka-plugin
Open Policy Agent (OPA) plug-in for Kafka authorization
Stars: ✭ 46 (+142.11%)
Mutual labels:  authorization, opa
opa-spring-security
Open Policy Agent for Spring Security
Stars: ✭ 19 (+0%)
Mutual labels:  authorization, opa
opal
Policy and data administration, distribution, and real-time updates on top of Open Policy Agent
Stars: ✭ 459 (+2315.79%)
Mutual labels:  authorization, opa
Opa
An open source, general-purpose policy engine.
Stars: ✭ 5,939 (+31157.89%)
Mutual labels:  authorization, opa
opa-docker-authz
A policy-enabled authorization plugin for Docker.
Stars: ✭ 67 (+252.63%)
Mutual labels:  authorization, opa
logto
🧑‍🚀 Logto helps you build the sign-in, auth, and user identity within minutes. We provide an OIDC-based identity service and the end-user experience with username, phone number, email, and social sign-in, with extendable multi-language support.
Stars: ✭ 3,421 (+17905.26%)
Mutual labels:  authorization
rabbitmq-auth-backend-oauth2-spike
See rabbitmq/rabbitmq-auth-backend-oauth2 instead.
Stars: ✭ 17 (-10.53%)
Mutual labels:  authorization
docker-api-graphql
GraphQL API wrapper around the Docker Remote API. SpringBoot-based app, written in Kotlin
Stars: ✭ 13 (-31.58%)
Mutual labels:  springframework
bouncer
Authorization Package for AdonisJS
Stars: ✭ 39 (+105.26%)
Mutual labels:  authorization
riam
AWS IAM inspired policy engine in Rust
Stars: ✭ 19 (+0%)
Mutual labels:  authorization
ertis-auth
Generic token generator and validator service like auth
Stars: ✭ 28 (+47.37%)
Mutual labels:  authorization
s3-proxy
S3 Reverse Proxy with GET, PUT and DELETE methods and authentication (OpenID Connect and Basic Auth)
Stars: ✭ 106 (+457.89%)
Mutual labels:  opa
fastapi-auth0
FastAPI authentication and authorization using auth0.com
Stars: ✭ 104 (+447.37%)
Mutual labels:  authorization
shield
Shield is a role-based cloud-native user management system, identity & access proxy, and authorization server for your applications and API endpoints.
Stars: ✭ 158 (+731.58%)
Mutual labels:  authorization
HeimGuard
🛡 A simple library that allows you to easily manage permissions in your .NET projects.
Stars: ✭ 77 (+305.26%)
Mutual labels:  authorization
browser-acl
Simple acceess control (ACL) library for the browser inspired by Laravel's guards and policies.
Stars: ✭ 36 (+89.47%)
Mutual labels:  authorization
online-shopping
This is an online shopping project using Spring Boot,Spring web-flow, Spring Rest Services and Hibernate. In this project we also used Spring Security with java and annotation configuration
Stars: ✭ 34 (+78.95%)
Mutual labels:  springframework
iam
企业级的 Go 语言实战项目:认证和授权系统
Stars: ✭ 1,900 (+9900%)
Mutual labels:  authorization

opa-java-spring-client

build-logo

Abstract

build.security provides simple development and management for your organization's authorization policy. opa-java-spring-client is a Spring middleware intended for performing authorization requests against build.security PDP(Policy Decision Point)/OPA.

Data Flow

drawing

Usage

Before you start we recommend completing the onboarding tutorial.


Important note

To simplify the setup process, the following example uses a local build.security PDP instance. If you are already familiar with how to run your PDP, You can also run a PDP on you environment (Dev/Prod, etc).

In that case, don't forget to change the hostname and the port in your code.


Simple usage

Configure the PDP client component by setting the following properties in your application.properties:

pdp.enable=true
pdp.allowOnFailure=false
pdp.port=8181
pdp.hostname=localhost
pdp.policy.path=/authz/allow
pdp.readTimeout.milliseconds=5000
pdp.connectionTimeout.milliseconds=5000
pdp.retry.maxAttempts=2
pdp.retry.backoff.milliseconds=250

Mandatory configuration -

  1. pdp.hostname: The hostname of the Policy Decision Point (PDP)
  2. pdp.port: The port at which the OPA service is running
  3. pdp.policyPath.path: Full path to the policy (including the rule) that decides whether requests should be authorized

How to get your pdp's hostname and port?

Optional configuration

  1. pdp.enable: Boolean. Whether or not to enable interception of requests for authz. Default is true
  2. pdp.interceptAllEndpoints: Boolean. Whether all endpoints should be intercepted, regardless of whether their associated controllers have a an Authorize annotation or not. Default is true
  3. pdp.ignoreEndpoints: Array. Only set when pdp.interceptAllEndpoints is true: a list of endpoints that shouldn't be intercepted for authz.
  4. pdp.ignoreRegex: Array. Only set when pdp.interceptAllEndpoints is true: a list of regex patterns that match endpoints that shouldn't be intercepted for authz.
  5. pdp.allowOnFailure: Boolean. "Fail open" mechanism to allow access to the API in case the policy engine is not reachable. Default is false.
  6. pdp.readTimeout.milliseconds - Integer. Read timeout for requests in milliseconds. Default is 5000
  7. pdp.connectionTimeout.milliseconds - Integer. Connection timeout in milliseconds. Default is 5000
  8. pdp.retry.maxAttempts - Integer. the maximum number of retry attempts in case a failure occurs. Default is 2.
  9. pdp.retry.backoff.milliseconds - Integer. The number of milliseconds to wait between two consecutive retry attempts. Default is 250

Example usage

Register your PDP as a spring interceptor

    @Configuration
    public class Configurer implements WebMvcConfigurer {

        @Autowired
        private PdpInterceptor pdpInterceptor;

        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(pdpInterceptor);
        }
    }

Example implementation in a Spring Controller

    // The Authorize annotation indicates that this request should be be authorized
    // using the PDP request interceptor. The resources supplied in the annotation will be
    // sent on the PDP request as well.
    @Authorize(resources = {"sdk.view"})
    @RequestMapping("/sdk")
    public String sdkExample(HttpServletRequest request) throws Exception {

        // ... Controller logic 
    }

Or instead use PDPClient directly to issue a request with your own input

    @RequestMapping("/sdk")
    public String sdkExample(HttpServletRequest request) throws Exception {
        Map<String, String> headers = new HashMap<String, String>();
        for (Enumeration<String> headerNames = request.getHeaderNames(); headerNames.hasMoreElements(); ) {
            String header = headerNames.nextElement();
            headers.put(header, request.getHeader(header));
        }

        String[] path = request.getRequestURI().replaceAll("^/|/$", "").split("/");

        //define the input for evaluation
        //In your application, you can put anything you'd like on the input for policy evaluation
        Map<String, Object> input = new HashMap<String, Object>();
        input.put("group", "group1");
        input.put("environment", "staging");
        input.put("role", "admin");

        JsonNode node = null;
        try {
            node = pdpClient.getJsonResponse(input);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }

        return node.toPrettyString();
    }

Try it out

Run your PDP (OPA) instance (assuming it runs on localhost:8181) and your spring server(localhost:8080).

PDP Request example

This is what the input received by the PDP would look like.

{
   "input":{
      "request":{
         "scheme":"http",
         "method":"GET",
         "path":"websecurity",
         "query":{
            
         },
         "headers":{
            "host":"localhost:8080",
            "user-agent":"curl/7.64.1",
            "accept":"*/*"
         }
      },
      "resources":{
         "requirements":[
            "websecurity"
         ],
         "attributes":{
            
         }
      },
      "source":{
         "ipAddress":"172.19.0.1",
         "port":0
      },
      "destination":{
         "ipAddress":"172.19.0.2",
         "port":0
      }
   }
}

If everything works well you should receive the following response:

{
    "decision_id":"ef414180-05bd-4817-9634-7d1537d5a657",
    "result":true
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].