def test_resource(self): #Use a JWT token as id_token id_token = self.getJWT() #Mock resource ID resource_id = "20248583" #Create default policy status, self.policyID = self.createTestPolicy(id_token, resource_id) self.assertEqual(status, 200) print("Create policy: Policy created with id: "+self.policyID) del status print("=======================") print("") with open('../tests/examples/request_template.json') as json_file: data = json.load(json_file) subject, action, resource = parser.load_request(data) resource_id = resource.attributes[0]['Value'] action = action.attributes[0]['Value'] dict_values = {} for i in range(0, len(subject.attributes)): dict_values[subject.attributes[i]['AttributeId']] = subject.attributes[i]['Value'] print("Checking policy with a XACML") j = policies_operations.validate_complete_policies(resource_id, action, dict_values) self.assertEqual(j[0][0], True) del j print("=======================") print("")
def test_pdp_validate_delegation_policies( self, mock_validate_rule_with_conditions_false, raise_for_status=None): with open('../tests/examples/request_template_delegation.json' ) as json_file: data = json.load(json_file) subject, action_rsrc, resource = parser.load_request(data) resource_id = resource.attributes[0]['Value'] action = action_rsrc.attributes[0]['Value'] dict_values = {} for i in range(0, len(subject.attributes)): dict_values[subject.attributes[i] ['AttributeId']] = subject.attributes[i]['Value'] mock_resp = mock.Mock() mock_resp.raise_for_status = mock.Mock() if raise_for_status: mock_resp.raise_for_status.side_effect = raise_for_status decisions = policies_operations.validate_complete_policies( resource_id, action, dict_values) self.assertEqual( type(decisions), dict, "Should return a dictionary with the result decisions")
def test_pdp_validate_rule_with_conditions_true( self, mock_validate_rule_with_conditions_true, raise_for_status=None): with open('../tests/examples/request_template.json') as json_file: data = json.load(json_file) subject, action, resource = parser.load_request(data) resource_id = "123456" action = action.attributes[0]['Value'] dict_values = {} for i in range(0, len(subject.attributes)): dict_values[subject.attributes[i] ['AttributeId']] = subject.attributes[i]['Value'] dict_values['attemps'] = 5 mock_resp = mock.Mock() mock_resp.raise_for_status = mock.Mock() if raise_for_status: mock_resp.raise_for_status.side_effect = raise_for_status j = policies_operations.validate_complete_policies( resource_id, action, dict_values) self.assertEqual(j, True, "Validate conditional policy rule should be true")
def test_pdp_validate_access_policies_action_false( self, mock_access_policies_resourceid_true_user_name_false, raise_for_status=None): with open('../tests/examples/request_template.json') as json_file: data = json.load(json_file) subject, action, resource = parser.load_request(data) resource_id = resource.attributes[0]['Value'] action = "edit" dict_values = {} for i in range(0, len(subject.attributes)): dict_values[subject.attributes[i] ['AttributeId']] = subject.attributes[i]['Value'] dict_values['userName'] = "******" mock_resp = mock.Mock() mock_resp.raise_for_status = mock.Mock() if raise_for_status: mock_resp.raise_for_status.side_effect = raise_for_status j = policies_operations.validate_complete_policies( resource_id, action, dict_values) self.assertEqual(j, False, "Validate access policies 3 should be false")
def test_resource(self): #Use a JWT token as id_token id_token = self.getJWT() #Mock resource ID resource_id = "20248592" #Insert delegate = "http://localhost:6667/policy/validate" #Create default policy status, self.policyID = self.createTestFederatedPolicy( id_token, resource_id, delegate) self.assertEqual(status, 200) print("Create policy: Policy created with id: " + self.policyID) del status print("=======================") print("") with open('../tests/examples/request_template_delegation.json' ) as json_file: data = json.load(json_file) subject, action, resource = parser.load_request(data) resource_id = resource.attributes[0]['Value'] action = action.attributes[0]['Value'] dict_values = {} for i in range(0, len(subject.attributes)): dict_values[subject.attributes[i] ['AttributeId']] = subject.attributes[i]['Value'] print("Checking policy for delegation decision signature") j = policies_operations.validate_complete_policies( resource_id, action, dict_values) self.assertEqual(j[0][0], None) self.assertEqual(str(j[0][1]), delegate) del j print("=======================") print("")
def validate_resource(): xacml = request.json if not validate_json(xacml): print("Invalid xacml received") return "Valid JSON data is required", 400 subject, action_rsrc, resource = parser.load_request(xacml) resource_id = resource.attributes[0]['Value'] user_name = subject.attributes[0]['Value'] action = action_rsrc.attributes[0]['Value'] #To be expanded when implementing more complex policies if "Issuer" in subject.attributes[0].keys(): issuer = subject.attributes[0]['Issuer'] else: issuer = None handler_status_issuer = None if issuer is not None: handler_status_issuer, handler_issuer_message = ScimHandler.get_instance( ).modifyAuthServerUrl(issuer) #For now it serves only as a check if the user attributes were reachable on the AS #handler_user_attributes uses this schema: https://gluu.org/docs/gluu-server/4.1/api-guide/scim-api/#/definitions/User # Call to be used later in development try: if handler_status_issuer == 200 or (issuer is None): handler_status, handler_user_attributes = ScimHandler.get_instance( ).getUserAttributes(user_name) if not isinstance(handler_user_attributes, dict): print(handler_user_attributes + ", using attributes from the XACML request instead...") handler_user_attributes = {} for i in range(0, len(subject.attributes)): handler_user_attributes[subject.attributes[i][ 'AttributeId']] = subject.attributes[i]['Value'] if issuer is None: handler_user_attributes['issuer'] = validate_auth_server_url() else: handler_user_attributes['issuer'] = issuer else: handler_user_attributes = {} raise Exception() except Exception as e: print("Error While retrieving the user attributes") # Pending: Complete when xacml receives several resources if isinstance(resource_id, list): for resource_from_list in resource.attributes[0]['Value']: result_validation, delegate_addr = policies_operations.validate_complete_policies( resource_from_list, action, handler_user_attributes) if result_validation: break else: decisions = policies_operations.validate_complete_policies( resource_id, action, handler_user_attributes) resp, status = response_handler.generate_response(decisions, subject.attributes, action_rsrc.attributes, resource.attributes, request.base_url) return json.dumps(resp, cls=ClassEncoder), status