Exemple #1
0
 def _apply_mapping_filter(self, identity_provider, protocol, assertion):
     mapping = self.federation_api.get_mapping_from_idp_and_protocol(
         identity_provider, protocol)
     rules = jsonutils.loads(mapping['rules'])
     LOG.debug('using the following rules: %s', rules)
     rule_processor = utils.RuleProcessor(rules)
     mapped_properties = rule_processor.process(assertion)
     utils.validate_groups(mapped_properties['group_ids'], mapping['id'],
                           self.identity_api)
     return mapped_properties
Exemple #2
0
    def test_whitelist_pass_through(self):
        mapping = mapping_fixtures.MAPPING_GROUPS_WHITELIST_PASS_THROUGH
        rp = mapping_utils.RuleProcessor(mapping['rules'])
        assertion = mapping_fixtures.DEVELOPER_ASSERTION
        mapped_properties = rp.process(assertion)
        self.assertValidMappedUserObject(mapped_properties)

        self.assertEqual('developacct', mapped_properties['user']['name'])
        self.assertEqual('Developer',
                         mapped_properties['group_names'][0]['name'])
Exemple #3
0
    def test_type_not_in_assertion(self):
        """Test that if the remote "type" is not in the assertion it fails."""
        mapping = mapping_fixtures.MAPPING_GROUPS_WHITELIST_PASS_THROUGH
        rp = mapping_utils.RuleProcessor(mapping['rules'])
        assertion = {uuid.uuid4().hex: uuid.uuid4().hex}
        mapped_properties = rp.process(assertion)
        self.assertValidMappedUserObject(mapped_properties)

        self.assertNotIn('id', mapped_properties['user'])
        self.assertNotIn('name', mapped_properties['user'])
Exemple #4
0
    def test_rule_engine_blacklist_direct_group_mapping_missing_domain(self):
        """Test if the local rule is rejected upon missing domain value

        This is a variation with a ``blacklist`` filter.

        """
        mapping = mapping_fixtures.MAPPING_GROUPS_BLACKLIST_MISSING_DOMAIN
        assertion = mapping_fixtures.EMPLOYEE_ASSERTION_MULTIPLE_GROUPS
        rp = mapping_utils.RuleProcessor(mapping['rules'])
        self.assertRaises(exception.ValidationError, rp.process, assertion)
Exemple #5
0
 def test_local_user_local_domain(self):
     """Test that local users can have non-service domains assigned."""
     mapping = mapping_fixtures.MAPPING_LOCAL_USER_LOCAL_DOMAIN
     rp = mapping_utils.RuleProcessor(mapping['rules'])
     assertion = mapping_fixtures.CONTRACTOR_ASSERTION
     mapped_properties = rp.process(assertion)
     self.assertIsNotNone(mapped_properties)
     self.assertValidMappedUserObject(
         mapped_properties,
         user_type='local',
         domain_id=mapping_fixtures.LOCAL_DOMAIN)
Exemple #6
0
    def main(cls):
        from keystone.contrib.federation import utils as mapping_engine
        if not CONF.command.engine_debug:
            mapping_engine.LOG.logger.setLevel('WARN')

        rules = MappingEngineTester.read_rules(CONF.command.rules)
        mapping_engine.validate_mapping_structure(rules)

        assertion = MappingEngineTester.read_file(CONF.command.input)
        assertion = MappingEngineTester.normalize_assertion(assertion)
        rp = mapping_engine.RuleProcessor(rules['rules'])
        print(jsonutils.dumps(rp.process(assertion), indent=2))
Exemple #7
0
    def test_mapping_federated_domain_specified(self):
        """Test mapping engine when domain 'ephemeral' is explicitely set.

        For that, we use mapping rule MAPPING_EPHEMERAL_USER and assertion
        EMPLOYEE_ASSERTION

        """
        mapping = mapping_fixtures.MAPPING_EPHEMERAL_USER
        rp = mapping_utils.RuleProcessor(mapping['rules'])
        assertion = mapping_fixtures.EMPLOYEE_ASSERTION
        mapped_properties = rp.process(assertion)
        self.assertIsNotNone(mapped_properties)
        self.assertValidMappedUserObject(mapped_properties)
Exemple #8
0
    def test_set_ephemeral_domain_to_ephemeral_users(self):
        """Test auto assigning service domain to ephemeral users.

        Test that ephemeral users will always become members of federated
        service domain. The check depends on ``type`` value which must be set
        to ``ephemeral`` in case of ephemeral user.

        """
        mapping = mapping_fixtures.MAPPING_EPHEMERAL_USER_LOCAL_DOMAIN
        rp = mapping_utils.RuleProcessor(mapping['rules'])
        assertion = mapping_fixtures.CONTRACTOR_ASSERTION
        mapped_properties = rp.process(assertion)
        self.assertIsNotNone(mapped_properties)
        self.assertValidMappedUserObject(mapped_properties)
Exemple #9
0
    def test_rule_engine_fails_after_discarding_nonstring(self):
        """Check whether RuleProcessor discards non string objects.

        Expect RuleProcessor to discard non string object, which
        is required for a correct rule match. RuleProcessor will result with
        empty list of groups.

        """
        mapping = mapping_fixtures.MAPPING_SMALL
        rp = mapping_utils.RuleProcessor(mapping['rules'])
        assertion = mapping_fixtures.CONTRACTOR_MALFORMED_ASSERTION
        mapped_properties = rp.process(assertion)
        self.assertValidMappedUserObject(mapped_properties)
        self.assertIsNone(mapped_properties['user'].get('name'))
        self.assertListEqual(list(), mapped_properties['group_ids'])
Exemple #10
0
    def test_rule_engine_no_groups_allowed(self):
        """Should return user mapped to no groups.

        The EMPLOYEE_ASSERTION should successfully have a match
        in MAPPING_GROUPS_WHITELIST, but 'whitelist' should filter out
        the group values from the assertion and thus map to no groups.

        """
        mapping = mapping_fixtures.MAPPING_GROUPS_WHITELIST
        assertion = mapping_fixtures.EMPLOYEE_ASSERTION
        rp = mapping_utils.RuleProcessor(mapping['rules'])
        mapped_properties = rp.process(assertion)
        self.assertIsNotNone(mapped_properties)
        self.assertListEqual(mapped_properties['group_names'], [])
        self.assertListEqual(mapped_properties['group_ids'], [])
        self.assertEqual('tbo', mapped_properties['user']['name'])
Exemple #11
0
    def test_rule_engine_no_regex_match(self):
        """Should deny authorization, the email of the tester won't match.

        This will not match since the email in the assertion will fail
        the regex test. It is set to match any @example.com address.
        But the incoming value is set to [email protected].
        RuleProcessor should raise exception.Unauthorized exception.

        """

        mapping = mapping_fixtures.MAPPING_LARGE
        assertion = mapping_fixtures.BAD_TESTER_ASSERTION
        rp = mapping_utils.RuleProcessor(mapping['rules'])

        self.assertRaises(exception.Unauthorized,
                          rp.process, assertion)
Exemple #12
0
    def test_rule_engine_no_regex_match(self):
        """Should deny authorization, the email of the tester won't match.

        This will not match since the email in the assertion will fail
        the regex test. It is set to match any @example.com address.
        But the incoming value is set to [email protected].
        RuleProcessor should return list of empty group_ids.

        """
        mapping = mapping_fixtures.MAPPING_LARGE
        assertion = mapping_fixtures.BAD_TESTER_ASSERTION
        rp = mapping_utils.RuleProcessor(mapping['rules'])
        mapped_properties = rp.process(assertion)

        self.assertValidMappedUserObject(mapped_properties)
        self.assertIsNone(mapped_properties['user'].get('name'))
        self.assertListEqual(list(), mapped_properties['group_ids'])
Exemple #13
0
    def test_rule_engine_not_any_of_regex_verify_fail(self):
        """Should deny authorization.

        The email in the assertion will fail the regex test.
        It is set to reject any @example.org address, but the
        incoming value is set to [email protected].
        RuleProcessor should return list of empty group_ids.

        """
        mapping = mapping_fixtures.MAPPING_DEVELOPER_REGEX
        assertion = mapping_fixtures.BAD_DEVELOPER_ASSERTION
        rp = mapping_utils.RuleProcessor(mapping['rules'])
        mapped_properties = rp.process(assertion)

        self.assertValidMappedUserObject(mapped_properties)
        self.assertIsNone(mapped_properties['user'].get('name'))
        self.assertListEqual(list(), mapped_properties['group_ids'])
Exemple #14
0
    def _handle_unscoped_token(self, context, auth_payload):
        assertion = context['environment']

        identity_provider = auth_payload['identity_provider']
        protocol = auth_payload['protocol']

        mapping = self.federation_api.get_mapping_from_idp_and_protocol(
            identity_provider, protocol)
        rules = jsonutils.loads(mapping['rules'])
        rule_processor = utils.RuleProcessor(rules)
        mapped_properties = rule_processor.process(assertion)
        return {
            'user_id': parse.quote(mapped_properties['name']),
            'group_ids': mapped_properties['group_ids'],
            federation.IDENTITY_PROVIDER: identity_provider,
            federation.PROTOCOL: protocol
        }
    def test_rule_engine_not_any_of_many_rules(self):
        """Should return group EMPLOYEE_GROUP_ID.

        The EMPLOYEE_ASSERTION should successfully have a match in
        MAPPING_SMALL. This will test the case where many remote
        rules must be matched, including a `not_any_of`.

        """

        mapping = mapping_fixtures.MAPPING_SMALL
        assertion = mapping_fixtures.EMPLOYEE_ASSERTION
        rp = mapping_utils.RuleProcessor(mapping['rules'])
        values = rp.process(assertion)

        group_ids = values.get('group_ids')
        name = values.get('name')

        self.assertIsNone(name)
        self.assertIn(mapping_fixtures.EMPLOYEE_GROUP_ID, group_ids)
    def test_rule_engine_no_regex_match(self):
        """Should return no values, the email of the tester won't match.

        This will not match since the email in the assertion will fail
        the regex test. It is set to match any @example.com address.
        But the incoming value is set to [email protected].

        """

        mapping = mapping_fixtures.MAPPING_LARGE
        assertion = mapping_fixtures.BAD_TESTER_ASSERTION
        rp = mapping_utils.RuleProcessor(mapping['rules'])
        values = rp.process(assertion)

        group_ids = values.get('group_ids')
        name = values.get('name')

        self.assertIsNone(name)
        self.assertEqual(group_ids, [])
Exemple #17
0
    def _rule_engine_regex_match_and_many_groups(self, assertion):
        """Should return group DEVELOPER_GROUP_ID and TESTER_GROUP_ID.

        A helper function injecting assertion passed as an argument.
        Expect DEVELOPER_GROUP_ID and TESTER_GROUP_ID in the results.

        """
        mapping = mapping_fixtures.MAPPING_LARGE
        rp = mapping_utils.RuleProcessor(mapping['rules'])
        values = rp.process(assertion)

        user_name = assertion.get('UserName')
        group_ids = values.get('group_ids')
        name = values.get('user', {}).get('name')

        self.assertValidMappedUserObject(values)
        self.assertEqual(user_name, name)
        self.assertIn(mapping_fixtures.DEVELOPER_GROUP_ID, group_ids)
        self.assertIn(mapping_fixtures.TESTER_GROUP_ID, group_ids)
    def test_rule_engine_not_any_of_and_direct_mapping(self):
        """Should return user's name and email.

        The CUSTOMER_ASSERTION should successfully have a match in
        MAPPING_LARGE. This will test the case where a requirement
        has `not_any_of`, and direct mapping to a username, no group.

        """

        mapping = mapping_fixtures.MAPPING_LARGE
        assertion = mapping_fixtures.CUSTOMER_ASSERTION
        rp = mapping_utils.RuleProcessor(mapping['rules'])
        values = rp.process(assertion)

        user_name = mapping_fixtures.CUSTOMER_ASSERTION.get('UserName')
        group_ids = values.get('group_ids')
        name = values.get('name')

        self.assertEqual(name, user_name)
        self.assertEqual(group_ids, [])
Exemple #19
0
    def test_rule_engine_not_any_of_regex_verify_pass(self):
        """Should return group DEVELOPER_GROUP_ID.

        The DEVELOPER_ASSERTION should successfully have a match in
        MAPPING_DEVELOPER_REGEX. This will test the case where many
        remote rules must be matched, including a `not_any_of`, with
        regex set to True.

        """
        mapping = mapping_fixtures.MAPPING_DEVELOPER_REGEX
        assertion = mapping_fixtures.DEVELOPER_ASSERTION
        rp = mapping_utils.RuleProcessor(mapping['rules'])
        values = rp.process(assertion)

        self.assertValidMappedUserObject(values)
        user_name = assertion.get('UserName')
        group_ids = values.get('group_ids')
        name = values.get('user', {}).get('name')

        self.assertEqual(user_name, name)
        self.assertIn(mapping_fixtures.DEVELOPER_GROUP_ID, group_ids)
    def test_rule_engine_regex_match_and_many_groups(self):
        """Should return group DEVELOPER_GROUP_ID and TESTER_GROUP_ID.

        The TESTER_ASSERTION should successfully have a match in
        MAPPING_LARGE. This will test a successful regex match
        for an `any_one_of` evaluation type, and will have many
        groups returned.

        """

        mapping = mapping_fixtures.MAPPING_LARGE
        assertion = mapping_fixtures.TESTER_ASSERTION
        rp = mapping_utils.RuleProcessor(mapping['rules'])
        values = rp.process(assertion)

        group_ids = values.get('group_ids')
        name = values.get('name')

        self.assertIsNone(name)
        self.assertIn(mapping_fixtures.DEVELOPER_GROUP_ID, group_ids)
        self.assertIn(mapping_fixtures.TESTER_GROUP_ID, group_ids)
Exemple #21
0
    def test_rule_engine_any_one_of_and_direct_mapping(self):
        """Should return user's name and group id EMPLOYEE_GROUP_ID.

        The ADMIN_ASSERTION should successfully have a match in MAPPING_LARGE.
        They will test the case where `any_one_of` is valid, and there is
        a direct mapping for the users name.

        """
        mapping = mapping_fixtures.MAPPING_LARGE
        assertion = mapping_fixtures.ADMIN_ASSERTION
        rp = mapping_utils.RuleProcessor(mapping['rules'])
        values = rp.process(assertion)

        fn = assertion.get('FirstName')
        ln = assertion.get('LastName')
        full_name = '%s %s' % (fn, ln)
        group_ids = values.get('group_ids')
        user_name = values.get('user', {}).get('name')

        self.assertIn(mapping_fixtures.EMPLOYEE_GROUP_ID, group_ids)
        self.assertEqual(full_name, user_name)
Exemple #22
0
    def test_create_user_object_with_bad_mapping(self):
        """Test if user object is created even with bad mapping.

        User objects will be created by mapping engine always as long as there
        is corresponding local rule.  This test shows, that even with assertion
        where no group names nor ids are matched, but there is 'blind' rule for
        mapping user, such object will be created.

        In this test MAPPING_EHPEMERAL_USER expects UserName set to jsmith
        whereas value from assertion is 'tbo'.

        """
        mapping = mapping_fixtures.MAPPING_EPHEMERAL_USER
        rp = mapping_utils.RuleProcessor(mapping['rules'])
        assertion = mapping_fixtures.CONTRACTOR_ASSERTION
        mapped_properties = rp.process(assertion)
        self.assertIsNotNone(mapped_properties)
        self.assertValidMappedUserObject(mapped_properties)

        self.assertNotIn('id', mapped_properties['user'])
        self.assertNotIn('name', mapped_properties['user'])
Exemple #23
0
    def test_rule_engine_regex_many_groups(self):
        """Should return group CONTRACTOR_GROUP_ID.

        The TESTER_ASSERTION should successfully have a match in
        MAPPING_TESTER_REGEX. This will test the case where many groups
        are in the assertion, and a regex value is used to try and find
        a match.

        """
        mapping = mapping_fixtures.MAPPING_TESTER_REGEX
        assertion = mapping_fixtures.TESTER_ASSERTION
        rp = mapping_utils.RuleProcessor(mapping['rules'])
        values = rp.process(assertion)

        self.assertValidMappedUserObject(values)
        user_name = assertion.get('UserName')
        group_ids = values.get('group_ids')
        name = values.get('user', {}).get('name')

        self.assertEqual(user_name, name)
        self.assertIn(mapping_fixtures.TESTER_GROUP_ID, group_ids)
Exemple #24
0
    def test_rule_engine_any_one_of_many_rules(self):
        """Should return group CONTRACTOR_GROUP_ID.

        The CONTRACTOR_ASSERTION should successfully have a match in
        MAPPING_SMALL. This will test the case where many rules
        must be matched, including an `any_one_of`, and a direct
        mapping.

        """

        mapping = mapping_fixtures.MAPPING_SMALL
        assertion = mapping_fixtures.CONTRACTOR_ASSERTION
        rp = mapping_utils.RuleProcessor(mapping['rules'])
        values = rp.process(assertion)

        user_name = assertion.get('UserName')
        group_ids = values.get('group_ids')
        name = values.get('name')

        self.assertEqual(user_name, name)
        self.assertIn(mapping_fixtures.CONTRACTOR_GROUP_ID, group_ids)
Exemple #25
0
    def test_user_identifications_name(self):
        """Test varius mapping options and how users are identified.

        This test calls mapped.setup_username() for propagating user object.

        Test plan:
        - Check if the user has proper domain ('federated') set
        - Check if the user has property type set ('ephemeral')
        - Check if user's name is properly mapped from the assertion
        - Check if user's id is properly set and equal to name, as it was not
        explicitely specified in the mapping.

        """
        mapping = mapping_fixtures.MAPPING_USER_IDS
        rp = mapping_utils.RuleProcessor(mapping['rules'])
        assertion = mapping_fixtures.CONTRACTOR_ASSERTION
        mapped_properties = rp.process(assertion)
        self.assertIsNotNone(mapped_properties)
        self.assertValidMappedUserObject(mapped_properties)
        mapped.setup_username({}, mapped_properties)
        self.assertEqual('jsmith', mapped_properties['user']['id'])
        self.assertEqual('jsmith', mapped_properties['user']['name'])
 def evaluate(self, idp_id, protocol_id, assertion_data):
     mapping = self.get_mapping_from_idp_and_protocol(idp_id, protocol_id)
     rules = mapping['rules']
     rule_processor = utils.RuleProcessor(rules)
     mapped_properties = rule_processor.process(assertion_data)
     return mapped_properties, mapping['id']