使用 MongoDB 将 XACML 文件存储在 JSON 中用于 Authzforce

Storing XACML file in JSON using MongoDB for Authzforce

我想像您在 README 中提到的那样使用 authzforce-ce-core-pdp-engine jar 文件实现 PDP 引擎,但 XML 中的策略文件除外应该是动态的。主要思想类似于文件共享系统,因为一个用户可以将多个文件共享给其他用户,每个文件可能有不同的策略。我正在考虑将策略文件存储在某种数据库中,例如 MySQL 或 MongoDB,PDP 将引用它并根据请求决定授予或拒绝访问权限。

我发现pdp核心引擎支持MongoDB,如here所述。

这是我的 pdp 配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Testing parameter 'maxPolicySetRefDepth' -->
<pdp xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://authzforce.github.io/core/xmlns/pdp/6.0" xmlns:ext="http://authzforce.github.io/core/xmlns/test/3" version="6.0.0">
   <refPolicyProvider id="refPolicyProvider" xsi:type="ext:MongoDBBasedPolicyProvider" serverHost="localhost" serverPort="27017" dbName="testXACML" collectionName="policies" />
   <rootPolicyProvider id="rootPolicyProvider" xsi:type="StaticRefBasedRootPolicyProvider">
      <policyRef>root-rbac-policyset</policyRef>
   </rootPolicyProvider>
</pdp>

所以现在的问题是我如何存储策略 XML 文件,因为它需要存储在 JSON 和 MongoDB 中?我尝试使用 JSON maven dependency, but I have a problem of converting back to XML. For example with the policy XML file like this 将 XML 转换为 JSON 它将创建如下所示的 JSON 文件:

{"Policy": {
    "xmlns": "urn:oasis:names:tc:xacml:3.0:core:schema:wd-17",
    "Target": "",
    "Description": "Policy for Conformance Test IIA001.",
    "Version": 1,
    "xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
    "RuleCombiningAlgId": "urn:oasis:names:tc:xacml:3.0:rule-combining-algorithm:deny-overrides",
    "Rule": {
        "Target": {"AnyOf": [
            {"AllOf": {"Match": {
                "AttributeValue": {
                    "DataType": "http://www.w3.org/2001/XMLSchema#string",
                    "content": "Julius Hibbert"
                },
                "AttributeDesignator": {
                    "Category": "urn:oasis:names:tc:xacml:1.0:subject-category:access-subject",
                    "AttributeId": "urn:oasis:names:tc:xacml:1.0:subject:subject-id",
                    "MustBePresent": false,
                    "DataType": "http://www.w3.org/2001/XMLSchema#string"
                },
                "MatchId": "urn:oasis:names:tc:xacml:1.0:function:string-equal"
            }}},
            {"AllOf": {"Match": {
                "AttributeValue": {
                    "DataType": "http://www.w3.org/2001/XMLSchema#anyURI",
                    "content": "http://medico.com/record/patient/BartSimpson"
                },
                "AttributeDesignator": {
                    "Category": "urn:oasis:names:tc:xacml:3.0:attribute-category:resource",
                    "AttributeId": "urn:oasis:names:tc:xacml:1.0:resource:resource-id",
                    "MustBePresent": false,
                    "DataType": "http://www.w3.org/2001/XMLSchema#anyURI"
                },
                "MatchId": "urn:oasis:names:tc:xacml:1.0:function:anyURI-equal"
            }}},
            {"AllOf": [
                {"Match": {
                    "AttributeValue": {
                        "DataType": "http://www.w3.org/2001/XMLSchema#string",
                        "content": "read"
                    },
                    "AttributeDesignator": {
                        "Category": "urn:oasis:names:tc:xacml:3.0:attribute-category:action",
                        "AttributeId": "urn:oasis:names:tc:xacml:1.0:action:action-id",
                        "MustBePresent": false,
                        "DataType": "http://www.w3.org/2001/XMLSchema#string"
                    },
                    "MatchId": "urn:oasis:names:tc:xacml:1.0:function:string-equal"
                }},
                {"Match": {
                    "AttributeValue": {
                        "DataType": "http://www.w3.org/2001/XMLSchema#string",
                        "content": "write"
                    },
                    "AttributeDesignator": {
                        "Category": "urn:oasis:names:tc:xacml:3.0:attribute-category:action",
                        "AttributeId": "urn:oasis:names:tc:xacml:1.0:action:action-id",
                        "MustBePresent": false,
                        "DataType": "http://www.w3.org/2001/XMLSchema#string"
                    },
                    "MatchId": "urn:oasis:names:tc:xacml:1.0:function:string-equal"
                }}
            ]}
        ]},
        "Description": "Julius Hibbert can read or write Bart Simpson's medical record.",
        "RuleId": "urn:oasis:names:tc:xacml:2.0:conformance-test:IIA1:rule",
        "Effect": "Permit"
    },
    "PolicyId": "urn:oasis:names:tc:xacml:2.0:conformance-test:IIA1:policy"
}}

但是当我尝试将其转换回 XML 时,它会变成完全不同的 XML 文件。那么现在如何将 XML 文件存储在 MongoDB 中?还有如何确保pdp engine core能够找到正确的策略进行比较?我看到在 README 中提到了 json 适配器,例如 this 但我不确定如何正常实现它。

XACML 策略目前没有 JSON 格式。 OASIS XACML 技术委员会目前正在考虑这一点。沃特福德理工学院的伯纳德·巴特勒 (Bernard Butler) 确实做了一些初步翻译,这可能对您有价值。

我目前唯一能想到的其他选择是围绕政策创建一个 JSON 包装器,例如

{
    "policy":"the xml policy contents escaped as valid json value or in base64"
}

我在 AuthzForce's github. In a nutshell, David is mostly right about the format (xml content stored as JSON string). More precisely, for AuthzForce MongoDB policy Provider, you have to store policies as shown by the part of the unit test class's setupBeforeClass method that populates the database with test policies. You'll see that we use the Jongo library (using Jackson object mapping behind the curtains) to map PolicyPOJO Java objects to JSON in the Mongodb collection. So from the PolicyPOJO class 上回答了这个问题,你可以猜到 JSON 中策略的存储格式:它是一个 JSON 对象,包含以下内容字段(键值对):

  • "id"(字符串):策略(集)ID
  • "version"(字符串):策略(集)版本
  • "type"(字符串):策略(集)类型,即'{urn:oasis:names:tc:xacml:3.0:core:schema:wd-17}Policy'(resp.' {urn:oasis:names:tc:xacml:3.0:core:schema:wd-17}PolicySet') for XACML 3.0 Policy (resp. PolicySet)
  • "content"(字符串):实际策略(集)的 XML 文档作为字符串(纯文本)

xml 内容由 Java 库 (Jongo/Jackson) 自动正确转义以适合 JSON 字符串。但是,如果您使用另一个 library/language,请确保也是如此。