Esempio n. 1
0
    def test_get_sdk_root_class(self):
        """
        """
        GASDKLibrary().register_sdk('1', tstdk1)
        self.assertEquals(GASDKLibrary().get_sdk_root_class('1'),
                          tstdk1.GARoot)

        with self.assertRaises(IndexError):
            GASDKLibrary().get_sdk_root_class('2')

        GASDKLibrary().unregister_sdk('1')
Esempio n. 2
0
    def test_execute_model_request_with_valid_session(self):
        """
        """
        GASDKLibrary().register_sdk('default', tstdk)
        core_controller = GACoreController(
            garuda_uuid='test-garuda',
            redis_info={
                'host': '127.0.0.1',
                'port': 6379,
                'db': 6
            },
            additional_controller_classes=[AdditionalController],
            authentication_plugins=[FakeAuthPlugin()])

        request = GARequest(action=GARequest.ACTION_CREATE)
        request.resources = [GAResource(name='enterprise', value=None)]
        request.token = 'toto'

        with patch.object(core_controller.sessions_controller,
                          'get_session',
                          return_value=GASession(garuda_uuid='test-garuda',
                                                 root_object=tstdk.GARoot())):
            result = core_controller.execute_model_request(request)
            self.assertEquals(result.__class__,
                              GAResponseFailure)  # nothing exists that's fine
Esempio n. 3
0
    def test_execute_event_request_with_invalid_session(self):
        """
        """
        GASDKLibrary().register_sdk('default', tstdk)
        core_controller = GACoreController(
            garuda_uuid='test-garuda',
            redis_info={
                'host': '127.0.0.1',
                'port': 6379,
                'db': 6
            },
            additional_controller_classes=[AdditionalController],
            authentication_plugins=[FakeAuthPlugin()])

        request = GARequest(action=GARequest.ACTION_CREATE)
        request.resources = [GAResource(name='enterprise', value=None)]

        with patch.object(core_controller.sessions_controller,
                          'get_session',
                          return_value=None):
            result = core_controller.execute_events_request(request)
            self.assertEquals(result[0], None)
            self.assertEquals(result[1].__class__, GAResponseFailure)
            self.assertEquals(len(result[1].content), 1)
            self.assertEquals(result[1].content[0].type,
                              GAError.TYPE_UNAUTHORIZED)
Esempio n. 4
0
    def test_execute_event_request_with_valid_session(self):
        """
        """
        GASDKLibrary().register_sdk('default', tstdk)
        core_controller = GACoreController(
            garuda_uuid='test-garuda',
            redis_info={
                'host': '127.0.0.1',
                'port': 6379,
                'db': 6
            },
            additional_controller_classes=[AdditionalController],
            authentication_plugins=[FakeAuthPlugin()])

        request = GARequest(action=GARequest.ACTION_CREATE)
        request.resources = [GAResource(name='enterprise', value=None)]

        session = GASession(garuda_uuid='test-garuda')
        session.root_object = NURESTRootObject()
        session.root_object.id = 'toto'

        with patch.object(core_controller.sessions_controller,
                          'get_session',
                          return_value=session):
            result = core_controller.execute_events_request(request)
            self.assertEquals(result[0].__class__, GASession)
            self.assertEquals(result[1], None)
Esempio n. 5
0
 def did_register(self):
     """
     """
     self._sdk = GASDKLibrary().get_sdk('default')
     self._push_controller = self.core_controller.push_controller
     self._storage_controller = self.core_controller.storage_controller
     self._github_operations_controller = self.core_controller.additional_controller(identifier='sd.controller.githuboperations.client')
Esempio n. 6
0
 def test_from_dict(self):
     """
     """
     data = {
         'entities': [{
             'description': None,
             'zipcode': None,
             'parentType': None,
             'lastUpdatedDate': None,
             'parentID': None,
             'owner': None,
             'creationDate': None,
             'ID': None,
             'name': 'enterprise1'
         }],
         'entityType':
         'enterprise',
         'type':
         'CREATE',
         'updateMechanism':
         'DEFAULT',
         'eventReceivedTime':
         None
     }
     GASDKLibrary().register_sdk('default', tstdk)
     event = GAPushEvent.from_dict(data)
     self.assertEqual(event.entity.name, 'enterprise1')
    def ready(self):
        """
        """
        self._sdk = GASDKLibrary().get_sdk('default')
        self._storage_controller = self.core_controller.storage_controller
        self._push_controller = self.core_controller.push_controller
        self._specification_exporter = SDSpecificationExporter(
            storage_controller=self._storage_controller,
            push_controller=self._push_controller,
            sdk=self._sdk)
        self._specification_importer = SDSpecificationImporter(
            storage_controller=self._storage_controller,
            push_controller=self._push_controller,
            sdk=self._sdk)

        self.subscribe(channel='github-operation:new',
                       handler=self._on_github_operation)
Esempio n. 8
0
    def did_register(self):
        """
        """
        self.sdk = GASDKLibrary().get_sdk(self.sdk_identifier)

        if self.db_initialization_function:
            self.db_initialization_function(
                db=self.db,
                root_object_class=self.sdk.SDKInfo.root_object_class())
Esempio n. 9
0
    def run(self):
        """
        """
        self._api_prefix = GASDKLibrary().get_sdk('default').SDKInfo.api_prefix()

        logger.info("Listening to inbound connection on %s:%d" % (self._host, self._port))

        logger.info("Starting gunicorn with %s workers" % self._number_of_workers)

        try:
            self._server.run()
        except:
            pass
Esempio n. 10
0
    def test_authenticate_with_wrong_access(self):
        """
        """
        GASDKLibrary().register_sdk('default', tstdk)

        request = GARequest(action=GARequest.ACTION_READ)
        request.resources = [GAResource('note-good', None)]
        request.token = 'token'
        session = GASession()
        auth_plugin = GASimpleAuthenticationPlugin()
        auth_info = auth_plugin.authenticate(request=request, session=session)

        self.assertIsNone(auth_info)
Esempio n. 11
0
 def setUpClass(cls):
     """
     """
     GASDKLibrary().register_sdk('default', tstdk)
     cls.plugin = FakeStoragePlugin()
     cls.core_controller = GACoreController(garuda_uuid='test-garuda',
                                            redis_info={
                                                'host': '127.0.0.1',
                                                'port': '6379',
                                                'db': 5
                                            },
                                            storage_plugins=[cls.plugin])
     cls.storage_controller = cls.core_controller.storage_controller
Esempio n. 12
0
    def test_authenticate_without_auth_function(self):
        """
        """
        GASDKLibrary().register_sdk('default', tstdk)

        request = GARequest(action=GARequest.ACTION_READ)
        request.resources = [GAResource('root', None)]
        request.token = 'token'
        session = GASession()
        auth_plugin = GASimpleAuthenticationPlugin()
        auth_info = auth_plugin.authenticate(request=request, session=session)

        self.assertEquals(auth_info.__class__, tstdk.GARoot)
Esempio n. 13
0
    def test_register_sdk(self):
        """
        """
        GASDKLibrary().register_sdk('1', tstdk1)
        GASDKLibrary().register_sdk('2', tstdk2)

        self.assertEquals(GASDKLibrary().get_sdk('1'), tstdk1)
        self.assertEquals(GASDKLibrary().get_sdk('2'), tstdk2)

        GASDKLibrary().unregister_sdk('1')
        GASDKLibrary().unregister_sdk('2')

        with self.assertRaises(IndexError):
            GASDKLibrary().get_sdk('1')

        with self.assertRaises(IndexError):
            GASDKLibrary().get_sdk('2')
Esempio n. 14
0
    def authenticate(self, request, session):
        """
        """
        root_object_class = GASDKLibrary().get_sdk(
            'default').SDKInfo.root_object_class()
        root_api = root_object_class.rest_name

        if request.resources[0].name != root_api:
            return None

        if self._auth_function:
            return self._auth_function(
                request=request,
                session=session,
                root_object_class=root_object_class,
                storage_controller=self.core_controller.storage_controller)
        else:
            auth = root_object_class()
            auth.api_key = session.uuid
            auth.password = None
            return auth
Esempio n. 15
0
    def test_authenticate_with_auth_function(self):
        """
        """
        GASDKLibrary().register_sdk('default', tstdk)

        o_request = GARequest(action=GARequest.ACTION_READ)
        o_request.resources = [GAResource('root', None)]
        o_request.token = 'token'
        o_session = GASession()

        def auth_function(request, session, root_object_class,
                          storage_controller):
            self.assertEquals(request, o_request)
            self.assertEquals(session, o_session)
            self.assertEquals(root_object_class, tstdk.GARoot)
            self.assertEquals(storage_controller, 'fake_storage_controller')

        auth_plugin = GASimpleAuthenticationPlugin()
        auth_plugin.core_controller = FakeCoreController()
        auth_plugin._auth_function = auth_function
        auth_plugin.authenticate(request=o_request, session=o_session)
Esempio n. 16
0
 def test_singleton(self):
     """
     """
     lib1 = GASDKLibrary()
     lib2 = GASDKLibrary()
     self.assertEquals(lib1, lib2)
    def setUpClass(cls):
        """
        Model:

        e1  --- u1  --- a1
            +-- u2  --- a2
            +-- u3

        e2  --- u4  --- a3
            +-- u5  --- a4
                    --- a5

        will be testing diverse permissions on that model for self.e0
        """
        GASDKLibrary().register_sdk('default', tstdk)

        cls.mongo_plugin = GAMongoStoragePlugin(db_name='permissions_test',
                                                sdk_identifier='default')
        cls.redis_permissions_plugin = GARedisPermissionsPlugin()

        cls.core_controller = GACoreController(
            garuda_uuid='test-garuda',
            redis_info={
                'host': '127.0.0.1',
                'port': '6379',
                'db': 7
            },
            storage_plugins=[cls.mongo_plugin],
            permission_plugins=[cls.redis_permissions_plugin])

        cls.storage_controller = cls.core_controller.storage_controller

        cls.core_controller.redis.flushdb()

        cls.core_controller.start()
        cls.permissions_controller = cls.core_controller.permissions_controller

        cls.e0 = tstdk.GAEnterprise(username='******')
        cls.storage_controller.create(user_identifier='fake',
                                      resource=cls.e0,
                                      parent=None)

        cls.e1 = tstdk.GAEnterprise(name='e1')
        cls.e2 = tstdk.GAEnterprise(name='e2')

        cls.u1 = tstdk.GAUser(username='******')
        cls.u2 = tstdk.GAUser(username='******')
        cls.u3 = tstdk.GAUser(username='******')
        cls.u4 = tstdk.GAUser(username='******')
        cls.u5 = tstdk.GAUser(username='******')

        cls.a1 = tstdk.GAUser(street='a1')
        cls.a2 = tstdk.GAUser(street='a2')
        cls.a3 = tstdk.GAUser(street='a3')
        cls.a4 = tstdk.GAUser(street='a4')
        cls.a5 = tstdk.GAUser(street='a5')

        cls.storage_controller.create(user_identifier='fake',
                                      resource=cls.e1,
                                      parent=None)
        cls.storage_controller.create(user_identifier='fake',
                                      resource=cls.u1,
                                      parent=cls.e1)
        cls.storage_controller.create(user_identifier='fake',
                                      resource=cls.u2,
                                      parent=cls.e1)
        cls.storage_controller.create(user_identifier='fake',
                                      resource=cls.u3,
                                      parent=cls.e1)
        cls.storage_controller.create(user_identifier='fake',
                                      resource=cls.a1,
                                      parent=cls.u1)
        cls.storage_controller.create(user_identifier='fake',
                                      resource=cls.a2,
                                      parent=cls.u2)

        cls.storage_controller.create(user_identifier='fake',
                                      resource=cls.e2,
                                      parent=None)
        cls.storage_controller.create(user_identifier='fake',
                                      resource=cls.u4,
                                      parent=cls.e2)
        cls.storage_controller.create(user_identifier='fake',
                                      resource=cls.u5,
                                      parent=cls.e2)
        cls.storage_controller.create(user_identifier='fake',
                                      resource=cls.a3,
                                      parent=cls.u4)
        cls.storage_controller.create(user_identifier='fake',
                                      resource=cls.a4,
                                      parent=cls.u5)
        cls.storage_controller.create(user_identifier='fake',
                                      resource=cls.a5,
                                      parent=cls.u5)
 def setUpClass(cls):
     """
     """
     GASDKLibrary().register_sdk('default', tstdk)
     cls.auth_plugin = GAOwnerPermissionsPlugin()
Esempio n. 19
0
 def tearDown(self):
     """
     """
     GASDKLibrary().unregister_sdk('default')
Esempio n. 20
0
 def setUp(self):
     """
     """
     GASDKLibrary().register_sdk('default', tstdk)
Esempio n. 21
0
 def did_register(self):
     """
     """
     self._sdk = GASDKLibrary().get_sdk('default')
     self._permissions_controller = self.core_controller.permissions_controller
class SDGitHubOperationsController(GAController):
    def __init__(self, core_controller):
        """
        """
        super(SDGitHubOperationsController,
              self).__init__(core_controller=core_controller)

        self._storage_controller = None
        self._push_controller = None
        self._sdk = None

    @classmethod
    def identifier(cls):
        """
        """
        return 'sd.controller.githuboperations'

    def ready(self):
        """
        """
        self._sdk = GASDKLibrary().get_sdk('default')
        self._storage_controller = self.core_controller.storage_controller
        self._push_controller = self.core_controller.push_controller
        self._specification_exporter = SDSpecificationExporter(
            storage_controller=self._storage_controller,
            push_controller=self._push_controller,
            sdk=self._sdk)
        self._specification_importer = SDSpecificationImporter(
            storage_controller=self._storage_controller,
            push_controller=self._push_controller,
            sdk=self._sdk)

        self.subscribe(channel='github-operation:new',
                       handler=self._on_github_operation)

    def start(self):
        """
        """
        self.start_listening_to_events()

    def stop(self):
        """
        """
        self.stop_listening_to_events()

    # PRIVATES

    def _on_github_operation(self, data):
        """
        """

        try:
            info = msgpack.unpackb(data)
            action = info['action']
            garuda_uuid = info['garuda_uuid']
            session_username = info['session_username']

            # let initiator handle the request
            if garuda_uuid != self.core_controller.garuda_uuid:
                return

            repository = self._sdk.SDRepository(data=info['repository'])

            if action == 'checkout_repository':

                job = self._sdk.SDJob(data=info['job'])

                self._peform_checkout_repository(
                    repository=repository,
                    job=job,
                    session_username=session_username)

            elif action == 'commit_specification':

                klass = self._sdk.SDSpecification if info[
                    'specification_type'] == self._sdk.SDSpecification.rest_name else self._sdk.SDAbstract

                specification = klass(data=info['specification'])

                self._perform_commit_specification(
                    repository=repository,
                    specification=specification,
                    commit_message=info['commit_message'],
                    session_username=session_username)

            elif action == 'rename_specification':

                klass = self._sdk.SDSpecification if info[
                    'specification_type'] == self._sdk.SDSpecification.rest_name else self._sdk.SDAbstract

                specification = klass(data=info['specification'])

                self._perform_rename_specification(
                    repository=repository,
                    specification=specification,
                    old_name=info['old_name'],
                    commit_message=info['commit_message'],
                    session_username=session_username)

            elif action == 'delete_specification':

                klass = self._sdk.SDSpecification if info[
                    'specification_type'] == self._sdk.SDSpecification.rest_name else self._sdk.SDAbstract
                specification = klass(data=info['specification'])

                self._perform_delete_specification(
                    repository=repository,
                    specification=specification,
                    commit_message=info['commit_message'],
                    session_username=session_username)

            elif action == 'commit_apiinfo':

                apiinfo = self._sdk.SDAPIInfo(data=info['apiinfo'])

                self._perform_commit_apiinfo(
                    repository=repository,
                    apiinfo=apiinfo,
                    commit_message=info['commit_message'],
                    session_username=session_username)

            elif action == 'commit_monolitheconfig':

                monolitheconfig = self._sdk.SDMonolitheConfig(
                    data=info['monolitheconfig'])

                self._perform_commit_monolitheconfig(
                    repository=repository,
                    monolitheconfig=monolitheconfig,
                    commit_message=info['commit_message'],
                    session_username=session_username)

            elif action == 'merge_upstream_master':

                job = self._sdk.SDJob(data=info['job'])

                self._perform_merge_upstream_master(
                    repository=repository,
                    job=job,
                    commit_message=info['commit_message'],
                    session_username=session_username)

        except Exception as ex:
            logger.error("Exception while executing git operation: %s" % ex)

    def _peform_checkout_repository(self, repository, job, session_username):
        """
        """
        repository.status = 'PULLING'
        self._storage_controller.update(user_identifier=session_username,
                                        resource=repository)
        self._push_controller.push_events(events=[
            GAPushEvent(action=GARequest.ACTION_UPDATE, entity=repository)
        ])

        try:
            manager = self._get_repository_manager_for_repository(
                repository=repository, session_username=session_username)

            self._specification_importer.clean_repository(
                repository=repository, session_username=session_username)
            self._specification_importer.import_repository_info(
                repository=repository,
                manager=manager,
                session_username=session_username)
            self._specification_importer.import_apiinfo(
                repository=repository,
                manager=manager,
                session_username=session_username)
            self._specification_importer.import_abstracts(
                repository=repository,
                manager=manager,
                session_username=session_username)
            self._specification_importer.import_specifications(
                repository=repository,
                manager=manager,
                session_username=session_username)
            self._specification_importer.import_monolitheconfig(
                repository=repository,
                manager=manager,
                session_username=session_username)

            job.progress = 1.0
            job.status = 'SUCCESS'

            repository.status = 'READY'

        except Exception as ex:
            logger.error("Unable to checkout repository: %s" % ex)
            job.progress = 1.0
            job.status = 'FAILED'
            job.result = 'Unable to pull repository.\n\nReason:\n%s' % ex

            repository.status = 'ERROR'

        finally:

            self._storage_controller.update(user_identifier=session_username,
                                            resource=job)
            self._push_controller.push_events(events=[
                GAPushEvent(action=GARequest.ACTION_UPDATE, entity=job)
            ])

            self._storage_controller.update(user_identifier=session_username,
                                            resource=repository)
            self._push_controller.push_events(events=[
                GAPushEvent(action=GARequest.ACTION_UPDATE, entity=repository)
            ])

    def _perform_merge_upstream_master(self, repository, job, commit_message,
                                       session_username):
        """
        """
        repository.status = 'MERGING'
        self._storage_controller.update(user_identifier=session_username,
                                        resource=repository)
        self._push_controller.push_events(events=[
            GAPushEvent(action=GARequest.ACTION_UPDATE, entity=repository)
        ])

        try:

            manager = self._get_repository_manager_for_repository(
                repository=repository, session_username=session_username)
            manager.merge_upstream_master(
                local_branch=repository.branch,
                upstream_branch="master" if repository.parent_branch is None
                else repository.parent_branch,
                commit_message="Merged upstream master branch")
            job.progress = 1.0
            job.status = 'SUCCESS'

            repository.status = 'NEEDS_PULL'

        except Exception as ex:

            logger.error('Unable to merge repository from upstream: %s' % ex)
            job.progress = 1.0
            job.status = 'FAILED'
            job.result = 'Unable to merge repository.\n\nReason:\n%s' % ex

            repository.status = 'ERROR'

        finally:

            self._storage_controller.update(user_identifier=session_username,
                                            resource=job)
            self._push_controller.push_events(events=[
                GAPushEvent(action=GARequest.ACTION_UPDATE, entity=job)
            ])

            self._storage_controller.update(user_identifier=session_username,
                                            resource=repository)
            self._push_controller.push_events(events=[
                GAPushEvent(action=GARequest.ACTION_UPDATE, entity=repository)
            ])

    def _perform_commit_specification(self, repository, specification,
                                      commit_message, session_username):
        """
        """
        manager = self._get_repository_manager_for_repository(
            repository=repository, session_username=session_username)
        mono_spec = self._specification_exporter.export_specification(
            specification=specification, session_username=session_username)

        self._set_specification_syncing(specification=specification,
                                        syncing=True,
                                        session_username=session_username)
        manager.save_specification(specification=mono_spec,
                                   message=commit_message,
                                   branch=repository.branch)
        self._set_specification_syncing(specification=specification,
                                        syncing=False,
                                        session_username=session_username)

    def _perform_rename_specification(self, repository, specification,
                                      old_name, commit_message,
                                      session_username):
        """
        """
        manager = self._get_repository_manager_for_repository(
            repository=repository, session_username=session_username)
        mono_spec = self._specification_exporter.export_specification(
            specification=specification, session_username=session_username)

        self._set_specification_syncing(specification=specification,
                                        syncing=True,
                                        session_username=session_username)
        manager.rename_specification(specification=mono_spec,
                                     old_name=old_name,
                                     message=commit_message,
                                     branch=repository.branch)
        self._set_specification_syncing(specification=specification,
                                        syncing=False,
                                        session_username=session_username)

    def _perform_delete_specification(self, repository, specification,
                                      commit_message, session_username):
        """
        """
        manager = self._get_repository_manager_for_repository(
            repository=repository, session_username=session_username)
        mono_spec = self._specification_exporter.export_specification(
            specification=specification, session_username=session_username)

        manager.delete_specification(specification=mono_spec,
                                     message=commit_message,
                                     branch=repository.branch)

    def _perform_commit_apiinfo(self, repository, apiinfo, commit_message,
                                session_username):
        """
        """
        manager = self._get_repository_manager_for_repository(
            repository=repository, session_username=session_username)

        manager.save_apiinfo(version=apiinfo.version,
                             root_api=apiinfo.root,
                             prefix=apiinfo.prefix,
                             message=commit_message,
                             branch=repository.branch)

    def _perform_commit_monolitheconfig(self, repository, monolitheconfig,
                                        commit_message, session_username):
        """
        """
        manager = self._get_repository_manager_for_repository(
            repository=repository, session_username=session_username)
        parser = self._specification_exporter.export_monolithe_config(
            monolitheconfig=monolitheconfig)
        manager.save_monolithe_config(monolithe_config_parser=parser,
                                      message=commit_message,
                                      branch=repository.branch)

    def _set_specification_syncing(self, specification, syncing,
                                   session_username):
        """
        """
        specification.syncing = syncing
        self._storage_controller.update(user_identifier=session_username,
                                        resource=specification)
        self._push_controller.push_events(events=[
            GAPushEvent(action=GARequest.ACTION_UPDATE, entity=specification)
        ])

    def _get_repository_manager_for_repository(self, repository,
                                               session_username):
        """
        """
        response = self._storage_controller.get(
            user_identifier=session_username,
            resource_name=self._sdk.SDToken.rest_name,
            identifier=repository.associated_token_id)
        github_token = response.data.value

        repo_manager = RepositoryManager(monolithe_config=None,
                                         api_url=repository.url,
                                         login_or_token=github_token,
                                         password=None,
                                         organization=repository.organization,
                                         repository=repository.repository,
                                         repository_path=repository.path)
        return repo_manager
 def did_register(self):
     """
     """
     self._sdk = GASDKLibrary().get_sdk('default')
     self._storage_controller = self.core_controller.storage_controller
     self._push_controller = self.core_controller.push_controller