buildsecurity/symfony-opa

Symfony 中间件,为传入请求添加 Open Policy Agent 授权。

v8.0.1 2021-02-25 06:31 UTC

This package is not auto-updated.

Last update: 2024-09-20 04:15:29 UTC


README

build-logo

摘要

build.security 为您组织的授权策略提供简单的开发和管理工作。opa-symfony-middleware 是一个 PHP Symfony 中间件,用于针对 build.security PDP(策略决策点)/OPA 执行授权请求。

此包是为 PHP v8.0 及以上版本和 Symfony v4.22 及以上版本构建的。

数据流

drawing

使用方法

在开始之前,我们建议您完成入门教程。

重要提示

为了简化设置过程,以下示例使用本地 build.security PDP 实例。如果您已经熟悉如何运行您的 PDP,您也可以在您的环境中(Dev/Prod 等)运行 PDP。

在这种情况下,不要忘记更改代码中的 hostnameport

简单使用

在您的 Symfony 应用程序目录中

composer require buildsecurity/symfony-opa

编辑您的 PDP 配置文件(services.yaml)- 这将定义如何向 PDP 发送请求

parameters:
    pdp.port: 8181
    pdp.hostname: https://
    pdp.policy.path: /authz/allow
    pdp.readTimeout.milliseconds: 5000
    pdp.connectionTimeout.milliseconds: 5000
    pdp.retry.maxAttempts: 2
    pdp.retry.backoff.milliseconds: 250

services.yaml 中注册 OpenPolicyAgent 服务

services:
    # Make the PDP configuration to the OpenPolicyAgent service.
    BuildSecurity\OpenPolicyAgentBundle\OpenPolicyAgent:
        arguments:
            $pdp_config:
                port: '%env(default:pdp.port:PDP_PORT)%'
                hostname: '%env(default:pdp.hostname:PDP_HOSTNAME)%'
                policy.path: '%env(default:pdp.policy.path:PDP_POLICY_PATH)%'
                readTimeout.milliseconds: '%env(default:pdp.readTimeout.milliseconds:PDP_READ_TIMEOUT_MS)%'
                connectionTimeout.milliseconds: '%env(default:pdp.connectionTimeout.milliseconds:PDP_CONNECTION_TIMEOUT_MS)%'
                retry.maxAttempts: '%env(default:pdp.retry.maxAttempts:PDP_RETRY_MAX_ATTEMPTS)%'
                retry.backoff.milliseconds: '%env(default:pdp.retry.backoff.milliseconds:PDP_RETRY_BACKOFF_MS)%'

必选配置

  1. hostname:策略决策点(PDP)的主机名
  2. port:OPA 服务运行在的端口
  3. policyPath:决定是否授权请求的策略(包括规则)的完整路径

当在您的 Symfony 服务器环境中添加时,以下环境变量(PDP_HOSTNAMEPDP_PORTPDP_POLICY_PATHPDP_READ_TIMEOUT_MSPDP_CONNECTION_TIMEOUT_MSPDP_RETRY_MAX_ATTEMPTSPDP_RETRY_BACKOFF_MS)将覆盖此服务配置。

可选配置

  1. allowOnFailure:布尔值。如果策略引擎不可达,允许访问 API 的“开启失败”机制。默认值为 false。
  2. includeBody:布尔值。是否将请求数据传递给策略引擎。默认值为 true。
  3. includeHeaders:布尔值。是否将请求头传递给策略引擎。默认值为 true。
  4. timeout:布尔值。在请求被放弃并宣布失败之前等待的时间。默认值为 1000ms。
  5. enable:布尔值。是否为特定请求咨询策略引擎。默认值为 true。
示例

要将授权中间件添加到控制器方法,只需用 Authorize 属性装饰它即可。

<?php
namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;

// This is the attribute that the middleware looks for.
use BuildSecurity\OpenPolicyAgentBundle\Authorize;

// You can see some_method has been decorated using the
// Authorize attribute. The decoration resources, ['foo', 'bar']
// will be made available in the input to the OPA request.
class SomeController
{
    #[Authorize('foo', 'bar')]
    public function some_method(): Response
    {
        return new Response(
            '<html><body>Authorized!</body></html>'
        );
    }
}

更多示例 请点击此处

PDP 请求示例

这是 PDP 收到的输入的样子。

{
    "input":{
        "request":{
            "headers":{
                "host":[
                    "localhost:8000"
                ],
                "user-agent":[
                    "curl\/7.74.0"
                ],
                "content-length":[
                    "0"
                ],
                "accept":[
                    "*\/*"
                ],
                "user":[
                    "charlie"
                ],
                "x-forwarded-for":[
                    "::1"
                ],
                "accept-encoding":[
                    "gzip"
                ],
                "content-type":[
                    ""
                ],
                "mod-rewrite":[
                    "On"
                ],
                "x-php-ob-level":[
                    "1"
                ]
            },
            "method":"POST",
            "path":"\/blog\/bob\/some-blog",
            "query":[
                
            ],
            "scheme":"http"
        },
        "resources":{
            "attributes":{
                "user":"bob",
                "blog_slug":"some-blog"
            },
            "requirements":[
                "blog.create"
            ]
        }
    }
}

如果一切正常,您应该会收到以下响应:+

{
    "decision_id":"ef414180-05bd-4817-9634-7d1537d5a657",
    "result":true
}