Exemple #1
0
    def test_get_summary(self):
        res_org = ResourceMock('1', {'id': 'test'}, 'organization', 'resource')
        res_proj1 = ResourceMock('2', {'id': 'test'}, 'project', 'resource',
                                 res_org)
        res_proj1.set_iam_policy({'id': 'test'})
        res_proj1.set_billing_info({'id': 'test'})
        res_buc1 = ResourceMock('5', {'id': 'test'}, 'bucket', 'resource',
                                res_proj1)
        res_proj2 = ResourceMock('6', {'id': 'test'}, 'project', 'resource',
                                 res_org)
        res_buc2 = ResourceMock('7', {'id': 'test'}, 'bucket', 'resource',
                                res_proj2)
        res_obj2 = ResourceMock('8', {'id': 'test'}, 'object', 'resource',
                                res_buc2)
        resources = [
            res_org, res_proj1, res_buc1, res_proj2, res_buc2, res_obj2]

        storage = Storage(self.session, self.engine)
        inv_index_id = storage.open()
        for resource in resources:
            storage.write(resource)
        storage.commit()
        # add more resource data that belongs to a different inventory index
        storage = Storage(self.session, self.engine)
        storage.open()
        for resource in resources:
            storage.write(resource)
        storage.commit()

        inv_index = self.session.query(InventoryIndex).get(inv_index_id)
        expected = {'bucket': 2, 'object': 1, 'organization': 1, 'project': 2}
        inv_summary = inv_index.get_summary(self.session)
        self.assertEqual(expected, inv_summary)
class GetUserEmailsTest(ForsetiTestCase):
    """Test the storage_helpers module."""
    def setUp(self):
        self.engine = create_test_engine()
        _session_maker = sessionmaker()
        self.session = _session_maker(bind=self.engine)
        initialize(self.engine)
        res_user1 = ResourceMock('1', {
            'primaryEmail': '*****@*****.**',
            'suspended': False
        }, 'gsuite_user', 'resource')
        res_user2 = ResourceMock('2', {
            'primaryEmail': '*****@*****.**',
            'suspended': False
        }, 'gsuite_user', 'resource')
        res_user3 = ResourceMock('3', {
            'primaryEmail': '*****@*****.**',
            'suspended': False
        }, 'gsuite_user', 'resource')
        self.resources = [res_user1, res_user2, res_user3]
        self.storage = Storage(self.session)
        _ = self.storage.open()
        for resource in self.resources:
            self.storage.write(resource)
        self.storage.commit()
        self.service_config = TestServiceConfig()

    #pylint: disable=C0301,W9016,W9015,W0613
    @mock.patch(
        'google.cloud.forseti.scanner.scanners.external_project_access_scanner._get_inventory_storage'
    )
    @mock.patch(
        'google.cloud.forseti.services.inventory.storage.DataAccess.get_latest_inventory_index_id'
    )
    def test_get_emails(self, mock_get_latest_inv_ndx_id, mock_storage):
        """Test retrieving e-mails from storage"""

        expected_emails = [
            u'*****@*****.**', u'*****@*****.**', u'*****@*****.**'
        ]
        mock_storage.return_value = self.storage
        emails = epas.get_user_emails(self.service_config)
        self.assertListEqual(emails, expected_emails)
Exemple #3
0
    def test_basic(self):
        """Test storing a few resources, then iterate."""

        initialize(self.engine)
        scoped_sessionmaker = db.create_scoped_sessionmaker(self.engine)

        res_org = ResourceMock('1', {'id': 'test'}, 'organization', 'resource')
        res_org.set_access_policy(MOCK_ACCESS_POLICY)
        res_org.set_org_policy(MOCK_ORG_POLICY)

        res_proj1 = ResourceMock('2', {'id': 'test'}, 'project', 'resource',
                                 res_org)
        res_proj1.set_iam_policy({'id': 'test'})
        res_proj1.set_billing_info({'id': 'test'})
        res_buc1 = ResourceMock('3', {'id': 'test'}, 'bucket', 'resource',
                                res_proj1)
        res_proj2 = ResourceMock('4', {'id': 'test'}, 'project', 'resource',
                                 res_org)
        res_buc2 = ResourceMock('5', {'id': 'test'}, 'bucket', 'resource',
                                res_proj2)
        res_obj2 = ResourceMock('6', {'id': 'test'}, 'object', 'resource',
                                res_buc2)

        resources = [
            res_org,
            res_proj1,
            res_buc1,
            res_proj2,
            res_buc2,
            res_obj2
        ]

        with scoped_sessionmaker() as session:
            with Storage(session, self.engine) as storage:
                for resource in resources:
                    storage.write(resource)
                storage.commit()
                inventory_index_id = storage.inventory_index.id
                self.assertEqual(3,
                                 len(self.reduced_inventory(
                                     session,
                                     inventory_index_id,
                                     ['organization', 'bucket'])),
                                 'Only 1 organization and 2 buckets')

                self.assertEqual(6,
                                 len(self.reduced_inventory(session,
                                                            inventory_index_id,
                                                            [])),
                                 'No types should yield empty list')

                access_policy = self.reduced_inventory(
                    session, inventory_index_id, ['organization'],
                    Categories.access_policy)
                self.assertEqual(1, len(access_policy),
                                 'Access Policy not found in inventory.')

                org_policy = self.reduced_inventory(
                    session, inventory_index_id, ['organization'],
                    Categories.org_policy)
                self.assertEqual(1, len(org_policy),
                                 'Org Policy not found in inventory.')

        with scoped_sessionmaker() as session:
            storage = Storage(session, self.engine)
            _ = storage.open()
            for resource in resources:
                storage.write(resource)
            storage.commit()
            inventory_index_id = storage.inventory_index.id
            self.assertEqual(3,
                             len(self.reduced_inventory(
                                 session,
                                 inventory_index_id,
                                 ['organization', 'bucket'])),
                             'Only 1 organization and 2 buckets')

            self.assertEqual(6,
                             len(self.reduced_inventory(session,
                                                        inventory_index_id,
                                                        [])),
                             'No types should yield empty list')