def post(self, request, env_template_id, path, body):
        """It adds a service into a template

        :param request: The operation request.
        :param env_template_id: the env template ID where the
        service belongs to.
        :param path: The path
        :param body: the information about the service
        :return: the service description.
        """
        secure_data = token_sanitizer.TokenSanitizer().sanitize(body)
        LOG.debug('Applications:Post <EnvTempId: {env_id}, Path: {path}, '
                  'Body: {body}>'.format(env_id=env_template_id,
                                         body=secure_data,
                                         path=path))

        post_data = core_services.CoreServices.post_application_data
        try:
            result = post_data(env_template_id, body, path)
        except (KeyError, ValueError):
            msg = _('The template does not exist {templ_id}').format(
                templ_id=env_template_id)
            LOG.exception(msg)
            raise exc.HTTPNotFound(msg)
        return result
Esempio n. 2
0
    def execute(task):
        s_task = token_sanitizer.TokenSanitizer().sanitize(task)
        LOG.info('Starting processing task: {task_desc}'.format(
            task_desc=jsonutils.dumps(s_task)))

        result = None
        reporter = status_reporter.StatusReporter(task['id'])

        try:
            task_executor = TaskExecutor(task, reporter)
            result = task_executor.execute()
            return result
        finally:
            s_result = token_sanitizer.TokenSanitizer().sanitize(result)
            LOG.info('Finished processing task: {task_desc}'.format(
                task_desc=jsonutils.dumps(s_result)))
class TokenSanitizerTests(base.MuranoTestCase):
    sanitizer = token_sanitizer.TokenSanitizer()

    def test_dict_with_one_value(self):
        source = {'token': 'value'}
        value = self.sanitizer.sanitize(source)
        self.assertEqual(value['token'], self.sanitizer.message)

    def test_dict_with_few_value(self):
        source = {'token': 'value', 'pass': '******', 'TrustId': 'value'}
        value = self.sanitizer.sanitize(source)

        self.assertEqual(value['token'], self.sanitizer.message)
        self.assertEqual(value['pass'], self.sanitizer.message)
        self.assertEqual(value['TrustId'], self.sanitizer.message)

    def test_dict_with_nested_dict(self):
        source = {'obj': {'pass': '******'}}
        value = self.sanitizer.sanitize(source)
        self.assertEqual(value['obj']['pass'], self.sanitizer.message)

    def test_dict_with_nested_list(self):
        source = {'obj': [{'pass': '******'}]}
        value = self.sanitizer.sanitize(source)
        self.assertEqual(value['obj'][0]['pass'], self.sanitizer.message)

    def test_leave_out_other_values(self):
        source = {'obj': ['value']}
        value = self.sanitizer.sanitize(source)
        self.assertEqual(value['obj'][0], 'value')
Esempio n. 4
0
    def post(self, request, environment_id, path, body):
        secure_data = token_sanitizer.TokenSanitizer().sanitize(body)
        LOG.debug('Services:Post <EnvId: {0}, Path: {2}, '
                  'Body: {1}>'.format(environment_id, secure_data, path))

        post_data = core_services.CoreServices.post_data
        session_id = request.context.session
        try:
            result = post_data(environment_id, session_id, body, path)
        except (KeyError, ValueError):
            raise exc.HTTPNotFound
        return result
Esempio n. 5
0
    def call_static_action(cls, context, task):
        s_task = token_sanitizer.TokenSanitizer().sanitize(task)
        LOG.info('Starting execution of static action: '
                 '{task_desc}'.format(task_desc=jsonutils.dumps(s_task)))

        result = None
        reporter = status_reporter.StatusReporter(task['id'])

        try:
            task_executor = StaticActionExecutor(task, reporter)
            result = task_executor.execute()
            return result
        finally:
            LOG.info('Finished execution of static action: '
                     '{task_desc}'.format(task_desc=jsonutils.dumps(result)))
Esempio n. 6
0
    def _push(self, object_store=None):
        template = copy.deepcopy(self._template)
        s_template = token_sanitizer.TokenSanitizer().sanitize(template)
        LOG.debug(
            'Pushing: {template}'.format(template=json.dumps(s_template)))
        object_store = object_store or helpers.get_object_store()
        while True:
            try:
                with helpers.with_object_store(object_store):
                    current_status = self._get_status()
                    resources = template.get('Resources') or template.get(
                        'resources')
                    if current_status == 'NOT_FOUND':
                        if resources is not None:
                            token_client = self._get_token_client()
                            token_client.stacks.create(
                                stack_name=self._name,
                                parameters=self._parameters,
                                template=template,
                                files=self._files,
                                environment=self._hot_environment,
                                disable_rollback=True,
                                tags=self._tags)

                            self._wait_state(
                                lambda status: status == 'CREATE_COMPLETE')
                    else:
                        if resources is not None:
                            self._client.stacks.update(
                                stack_id=self._name,
                                parameters=self._parameters,
                                files=self._files,
                                environment=self._hot_environment,
                                template=template,
                                disable_rollback=True,
                                tags=self._tags)
                            self._wait_state(
                                lambda status: status == 'UPDATE_COMPLETE',
                                True)
                        else:
                            self.delete()
            except heat_exc.HTTPConflict as e:
                LOG.warning('Conflicting operation: {msg}'.format(msg=e))
                eventlet.sleep(3)
            else:
                break

        self._applied = self._template == template
Esempio n. 7
0
def update_task(action, session, task, unit):
    session.state = state.SessionState.deploying
    task_info = models.Task()
    task_info.environment_id = session.environment_id
    objects = session.description.get('Objects', None)
    if objects:
        task_info.description = token_sanitizer.TokenSanitizer().sanitize(
            dict(session.description.get('Objects')))
    task_info.action = task['action']
    status = models.Status()
    status.text = 'Action {0} is scheduled'.format(action)
    status.level = 'info'
    task_info.statuses.append(status)
    with unit.begin():
        unit.add(session)
        unit.add(task_info)
Esempio n. 8
0
    def post(self, request, environment_id, path, body=None):
        if not body:
            msg = _('Request body is empty: please, provide'
                    ' application object model')
            LOG.error(msg)
            raise exc.HTTPBadRequest(msg)
        secure_data = token_sanitizer.TokenSanitizer().sanitize(body)
        LOG.debug('Services:Post <EnvId: {env_id}, Path: {path}, '
                  'Body: {body}>'.format(env_id=environment_id,
                                         body=secure_data,
                                         path=path))

        post_data = core_services.CoreServices.post_data
        session_id = request.context.session
        try:
            result = post_data(environment_id, session_id, body, path)
        except (KeyError, ValueError):
            raise exc.HTTPNotFound
        return result
Esempio n. 9
0
    def handle_task(context, task):
        s_task = token_sanitizer.TokenSanitizer().sanitize(task)
        LOG.info(
            _('Starting processing task: {task_desc}').format(
                task_desc=anyjson.dumps(s_task)))

        result = task['model']
        try:
            task_executor = TaskExecutor(task)
            result = task_executor.execute()
        except Exception as e:
            LOG.exception('Error during task execution for tenant %s',
                          task['tenant_id'])
            msg_env = Environment(task['id'])
            reporter = status_reporter.StatusReporter()
            reporter.initialize(msg_env)
            reporter.report_error(msg_env, str(e))
        finally:
            rpc.api().process_result(result, task['id'])
Esempio n. 10
0
def _patch_description(description):
    if not description:
        description = {}
    description['services'] = description.pop('applications', [])
    return token_sanitizer.TokenSanitizer().sanitize(description)
Esempio n. 11
0
    def process_result(context, result, environment_id):
        secure_result = token_sanitizer.TokenSanitizer().sanitize(result)
        LOG.debug('Got result from orchestration '
                  'engine:\n{0}'.format(secure_result))

        model = result['model']
        action_result = result.get('action', {})

        unit = session.get_session()
        environment = unit.query(models.Environment).get(environment_id)

        if not environment:
            LOG.warning(_LW('Environment result could not be handled, '
                            'specified environment not found in database'))
            return

        if model['Objects'] is None and model.get('ObjectsCopy', {}) is None:
            environments.EnvironmentServices.remove(environment_id)
            return

        environment.description = model
        if environment.description['Objects'] is not None:
            environment.description['Objects']['services'] = \
                environment.description['Objects'].pop('applications', [])
            # environment.networking = result.get('networking', {})
            action_name = 'Deployment'
            deleted = False
        else:
            action_name = 'Deletion'
            deleted = True
        environment.version += 1
        environment.save(unit)

        # close deployment
        deployment = get_last_deployment(unit, environment.id)
        deployment.finished = timeutils.utcnow()
        deployment.result = action_result

        num_errors = unit.query(models.Status)\
            .filter_by(level='error', task_id=deployment.id).count()
        num_warnings = unit.query(models.Status)\
            .filter_by(level='warning', task_id=deployment.id).count()

        final_status_text = action_name + ' finished'
        if num_errors:
            final_status_text += " with errors"

        elif num_warnings:
            final_status_text += " with warnings"

        status = models.Status()
        status.task_id = deployment.id
        status.text = final_status_text
        status.level = 'info'
        deployment.statuses.append(status)
        deployment.save(unit)

        # close session
        conf_session = unit.query(models.Session).filter_by(
            **{'environment_id': environment.id,
               'state': states.SessionState.DEPLOYING if not deleted
               else states.SessionState.DELETING}).first()
        if num_errors > 0:
            conf_session.state = \
                states.SessionState.DELETE_FAILURE if deleted else \
                states.SessionState.DEPLOY_FAILURE
        else:
            conf_session.state = states.SessionState.DEPLOYED
        conf_session.save(unit)

        # output application tracking information
        message = _LI('EnvId: {0} TenantId: {1} Status: {2} Apps: {3}').format(
            environment.id,
            environment.tenant_id,
            _('Failed') if num_errors + num_warnings > 0 else _('Successful'),
            ', '.join(map(
                lambda a: a['?']['type'],
                model['Objects']['services']
            ))
        )
        LOG.info(message)
Esempio n. 12
0
    def process_result(context, result, environment_id):
        secure_result = token_sanitizer.TokenSanitizer().sanitize(result)
        LOG.debug('Got result from orchestration '
                  'engine:\n{0}'.format(secure_result))

        unit = session.get_session()
        environment = unit.query(models.Environment).get(environment_id)

        if not environment:
            LOG.warning(
                _('Environment result could not be handled, specified '
                  'environment was not found in database'))
            return

        if result['Objects'] is None and result.get('ObjectsCopy', {}) is None:
            environments.EnvironmentServices.remove(environment_id)
            return

        environment.description = result
        if environment.description['Objects'] is not None:
            environment.description['Objects']['services'] = \
                environment.description['Objects'].pop('applications', [])
            # environment.networking = result.get('networking', {})
            action_name = 'Deployment'
            deleted = False
        else:
            action_name = 'Deletion'
            deleted = True
        environment.version += 1
        environment.save(unit)

        #close deployment
        deployment = get_last_deployment(unit, environment.id)
        deployment.finished = timeutils.utcnow()

        num_errors = unit.query(models.Status)\
            .filter_by(level='error', task_id=deployment.id).count()
        num_warnings = unit.query(models.Status)\
            .filter_by(level='warning', task_id=deployment.id).count()

        final_status_text = action_name + ' finished'
        if num_errors:
            final_status_text += " with errors"

        elif num_warnings:
            final_status_text += " with warnings"

        status = models.Status()
        status.task_id = deployment.id
        status.text = final_status_text
        status.level = 'info'
        deployment.statuses.append(status)
        deployment.save(unit)

        #close session
        conf_session = unit.query(models.Session).filter_by(
            **{
                'environment_id': environment.id,
                'state': 'deploying' if not deleted else 'deleting'
            }).first()
        if num_errors > 0:
            conf_session.state = \
                sessions.SessionState.DELETE_FAILURE if deleted else \
                sessions.SessionState.DEPLOY_FAILURE
        else:
            conf_session.state = sessions.SessionState.DEPLOYED
        conf_session.save(unit)
Esempio n. 13
0
    def process_result(context, result, environment_id):
        secure_result = token_sanitizer.TokenSanitizer().sanitize(result)
        LOG.debug('Got result from orchestration '
                  'engine:\n{result}'.format(result=secure_result))

        model = result['model']
        action_result = result.get('action', {})

        unit = session.get_session()
        environment = unit.query(models.Environment).get(environment_id)

        if not environment:
            LOG.warning('Environment result could not be handled, '
                        'specified environment not found in database')
            return

        if model['Objects'] is None and model.get('ObjectsCopy', {}) is None:
            environments.EnvironmentServices.remove(environment_id)
            return

        environment.description = model
        if environment.description['Objects'] is not None:
            environment.description['Objects']['services'] = \
                environment.description['Objects'].pop('applications', [])
            action_name = 'Deployment'
            deleted = False
        else:
            action_name = 'Deletion'
            deleted = True
        environment.version += 1
        environment.save(unit)

        # close deployment
        deployment = get_last_deployment(unit, environment.id)
        deployment.finished = timeutils.utcnow()
        deployment.result = action_result

        num_errors = unit.query(models.Status)\
            .filter_by(level='error', task_id=deployment.id).count()
        num_warnings = unit.query(models.Status)\
            .filter_by(level='warning', task_id=deployment.id).count()

        final_status_text = action_name + ' finished'
        if num_errors:
            final_status_text += " with errors"

        elif num_warnings:
            final_status_text += " with warnings"

        status = models.Status()
        status.task_id = deployment.id
        status.text = final_status_text
        status.level = 'info'
        deployment.statuses.append(status)
        deployment.save(unit)

        # close session
        conf_session = unit.query(models.Session).filter_by(
            **{
                'environment_id':
                environment.id,
                'state':
                states.SessionState.DEPLOYING if not deleted else states.
                SessionState.DELETING
            }).first()
        if num_errors > 0 or result['action'].get('isException'):
            conf_session.state = \
                states.SessionState.DELETE_FAILURE if deleted else \
                states.SessionState.DEPLOY_FAILURE
        else:
            conf_session.state = states.SessionState.DEPLOYED
        conf_session.save(unit)

        # output application tracking information
        services = []
        objects = model['Objects']
        if objects:
            services = objects.get('services')
        if num_errors + num_warnings > 0:
            LOG.warning('EnvId: {env_id} TenantId: {tenant_id} Status: '
                        'Failed Apps: {services}'.format(
                            env_id=environment.id,
                            tenant_id=environment.tenant_id,
                            services=services))
        else:
            LOG.info('EnvId: {env_id} TenantId: {tenant_id} Status: '
                     'Successful Apps: {services}'.format(
                         env_id=environment.id,
                         tenant_id=environment.tenant_id,
                         services=services))
            if action_name == 'Deployment':
                env = environment.to_dict()
                env["deployment_started"] = deployment.started
                env["deployment_finished"] = deployment.finished
                status_reporter.get_notifier().report('environment.deploy.end',
                                                      env)