Beispiel #1
0
def test_get_attribute_value():
    request_json = {
        "subject": {
            "id": "a",
            "attributes": {
                "firstName": "Carl",
                "lastName": "Right"
            }
        },
        "resource": {
            "id": "a",
            "attributes": {
                "name": "Calendar"
            }
        },
        "action": {
            "id": "",
            "attributes": {}
        },
        "context": {}
    }
    request = Request.from_json(request_json)
    ctx = EvaluationContext(request)
    provider = RequestAttributeProvider(request)

    assert request_json["subject"]["attributes"]["firstName"] == provider.get_attribute_value("subject", "$.firstName",
                                                                                              ctx)
    assert request_json["subject"]["attributes"]["lastName"] == provider.get_attribute_value("subject", "$.lastName",
                                                                                             ctx)
    assert provider.get_attribute_value("subject", "$.test", ctx) is None
    assert request_json["resource"]["attributes"]["name"] == provider.get_attribute_value("resource", "$.name", ctx)
    assert provider.get_attribute_value("resource", "$.test", ctx) is None
    assert provider.get_attribute_value("action", "$.test", ctx) is None
    assert provider.get_attribute_value("context", "$.test", ctx) is None
Beispiel #2
0
def test_invalid_attribute_path_error():
    request_json = {
        "subject": {
            "id": "a",
            "attributes": {
                "firstName": "Carl",
                "lastName": "Right"
            }
        },
        "resource": {
            "id": "a",
            "attributes": {
                "name": "Calendar"
            }
        },
        "action": {
            "id": "",
            "attributes": {}
        },
        "context": {}
    }
    request = Request.from_json(request_json)
    ctx = EvaluationContext(request)
    provider = RequestAttributeProvider(request)
    with pytest.raises(InvalidAttributePathError):
        provider.get_attribute_value("subject", ")", ctx)
Beispiel #3
0
def test_attribute_value_raises():
    request_json = {
        "subject": {
            "id": "a",
            "attributes": {
                "firstName": "Carl",
                "lastName": "Right"
            }
        },
        "resource": {
            "id": "a",
            "attributes": {
                "name": "Calendar"
            }
        },
        "action": {
            "id": "",
            "attributes": {}
        },
        "context": {}
    }
    request = Request.from_json(request_json)
    context = EvaluationContext(request)
    with pytest.raises(InvalidAccessControlElementError):
        _ = context.attribute_value

    context.ace = "test"
    with pytest.raises(InvalidAccessControlElementError):
        _ = context.attribute_value

    context.ace = "subject"
    context.attribute_path = ")"
    with pytest.raises(InvalidAttributePathError):
        _ = context.attribute_value
Beispiel #4
0
def test_create():
    request_json = {
        "subject": {
            "id": "a",
            "attributes": {
                "firstName": "Carl",
                "lastName": "Right"
            }
        },
        "resource": {
            "id": "a",
            "attributes": {
                "name": "Calendar"
            }
        },
        "action": {
            "id": "",
            "attributes": {}
        },
        "context": {}
    }
    request = AccessRequest.from_json(request_json)

    assert request_json["subject"]["id"] == request._subject_id
    assert request_json["resource"]["id"] == request._resource_id
    assert request_json["action"]["id"] == request._action_id

    # Check backward compatibility
    request = Request.from_json(request_json)

    assert request_json["subject"]["id"] == request._subject_id
    assert request_json["resource"]["id"] == request._resource_id
    assert request_json["action"]["id"] == request._action_id
Beispiel #5
0
def test_get_attribute_value():
    request_json = {
        "subject": {
            "id": "a",
            "attributes": {
                "firstName": "Carl",
                "lastName": "Right",
                "middleName": ""
            }
        },
        "resource": {
            "id": "a",
            "attributes": {
                "name": "Calendar"
            }
        },
        "action": {
            "id": "",
            "attributes": {}
        },
        "context": {}
    }
    request = Request.from_json(request_json)
    context = EvaluationContext(request, providers=[EmailAttributeProvider()])
    assert context.get_attribute_value("subject", "$.firstName") == "Carl"
    assert context.get_attribute_value("subject", "$.middleName") == ""
    assert context.get_attribute_value("subject",
                                       "$.email") == "*****@*****.**"
    assert context.get_attribute_value("resource", "$.name") == "Calendar"
    assert context.get_attribute_value("context", "$.ip") is None
Beispiel #6
0
def test_attribute_provider_infinite_recursion():
    request_json = {
        "subject": {
            "id": "a",
            "attributes": {
                "firstName": "Carl",
                "lastName": "Right"
            }
        },
        "resource": {
            "id": "a",
            "attributes": {
                "name": "Calendar"
            }
        },
        "action": {
            "id": "",
            "attributes": {}
        },
        "context": {}
    }
    request = Request.from_json(request_json)
    context = EvaluationContext(request, providers=[FaultyAttributeProvider()])
    assert context.get_attribute_value("subject", "$.email") is None

    context = EvaluationContext(request,
                                providers=[
                                    FaultyAttributeProvider(),
                                    EmailAttributeProvider(),
                                    FaultyAttributeProvider(),
                                    EmailAttributeProvider(),
                                    FaultyAttributeProvider()
                                ])
    assert context.get_attribute_value("subject", "$.age") is None
Beispiel #7
0
def test_is_satisfied(rules_json, result):
    request_json = {
        "subject": {
            "id": "a",
            "attributes": {
                "firstName": "Carl",
                "lastName": "Right"
            }
        },
        "resource": {
            "id": "a",
            "attributes": {
                "name": "Calendar"
            }
        },
        "action": {
            "id": "",
            "attributes": {}
        },
        "context": {}
    }
    request = Request.from_json(request_json)
    ctx = EvaluationContext(request)
    rules = RulesSchema().load(rules_json)
    assert rules.is_satisfied(ctx) == result
Beispiel #8
0
def test_match(targets_json, result):
    request_json = {
        "subject": {
            "id": "abc",
            "attributes": {
                "firstName": "Carl",
                "lastName": "Right"
            }
        },
        "resource": {
            "id": "12",
            "attributes": {
                "name": "Calendar"
            }
        },
        "action": {
            "id": ">",
            "attributes": {}
        },
        "context": {}
    }
    request = Request.from_json(request_json)
    ctx = EvaluationContext(request)
    targets = TargetsSchema().load(targets_json)
    assert targets.match(ctx) == result
Beispiel #9
0
 def test_is_satisfied(self, condition, what, result):
     request = Request(subject={"attributes": {
         "what": what
     }},
                       resource={},
                       action={},
                       context={})
     ctx = EvaluationContext(request)
     ctx.ace = "subject"
     ctx.attribute_path = "$.what"
     assert condition.is_satisfied(ctx) == result
Beispiel #10
0
def test_create():
    request_json = {
        "subject": {
            "id": "a",
            "attributes": {
                "firstName": "Carl",
                "lastName": "Right",
                "middleName": ""
            }
        },
        "resource": {
            "id": "a",
            "attributes": {
                "name": "Calendar"
            }
        },
        "action": {
            "id": "",
            "attributes": {}
        },
        "context": {}
    }
    request = Request.from_json(request_json)
    context = EvaluationContext(request)
    assert context.subject_id == request._subject_id
    assert context.resource_id == request._resource_id
    assert context.action_id == request._action_id
    assert context._other_providers == []
    assert context.ace is None
    assert context.attribute_path is None

    context.ace = "subject"
    context.attribute_path = "$.firstName"
    assert context.attribute_value == "Carl"
    context.attribute_path = "$.lastName"
    assert context.attribute_value == "Right"
    context.attribute_path = "$.middleName"
    assert context.attribute_value == ""

    context.ace = "resource"
    context.attribute_path = "$.name"
    assert context.attribute_value == "Calendar"
Beispiel #11
0
def test_find_for_target(st, request_json, num):
    st.add(Policy.from_json({"uid": "1",
                             "rules": {},
                             "targets": {},
                             "effect": "deny"}))
    st.add(Policy.from_json({"uid": "2",
                             "rules": {},
                             "targets": {"subject_id": "ab*"},
                             "effect": "deny"}))
    st.add(Policy.from_json({"uid": "3",
                             "rules": {},
                             "targets": {"subject_id": "a*b"},
                             "effect": "deny"}))
    st.add(Policy.from_json({"uid": "4",
                             "rules": {},
                             "targets": {"subject_id": "ab*c"},
                             "effect": "deny"}))

    request = Request.from_json(request_json)
    found = st.get_for_target(request._subject_id, request._resource_id, request._action_id)
    found = list(found)
    assert num == len(found)
Beispiel #12
0
 def test_fits(self, desc, policy_json, request_json, result):
     ctx = EvaluationContext(Request.from_json(request_json))
     policy = Policy.from_json(policy_json)
     assert policy.fits(ctx) == result
Beispiel #13
0
def test_create_error(request_json):
    with pytest.raises(RequestCreateError):
        Request.from_json(request_json)