예제 #1
0
def ssh_execute(ssh, cmd, process_input=None,
                addl_env=None, check_exit_code=True):
    sanitized_cmd = strutils.mask_password(cmd)
    LOG.debug('Running cmd (SSH): %s', sanitized_cmd)
    if addl_env:
        raise InvalidArgumentError(_('Environment not supported over SSH'))

    if process_input:
        # This is (probably) fixable if we need it...
        raise InvalidArgumentError(_('process_input not supported over SSH'))

    stdin_stream, stdout_stream, stderr_stream = ssh.exec_command(cmd)
    channel = stdout_stream.channel

    # NOTE(justinsb): This seems suspicious...
    # ...other SSH clients have buffering issues with this approach
    stdout = stdout_stream.read()
    sanitized_stdout = strutils.mask_password(stdout)
    stderr = stderr_stream.read()
    sanitized_stderr = strutils.mask_password(stderr)

    stdin_stream.close()

    exit_status = channel.recv_exit_status()

    # exit_status == -1 if no exit code was returned
    if exit_status != -1:
        LOG.debug('Result was %s' % exit_status)
        if check_exit_code and exit_status != 0:
            raise ProcessExecutionError(exit_code=exit_status,
                                        stdout=sanitized_stdout,
                                        stderr=sanitized_stderr,
                                        cmd=sanitized_cmd)

    return (sanitized_stdout, sanitized_stderr)
예제 #2
0
def ssh_execute(ssh, cmd, process_input=None,
                addl_env=None, check_exit_code=True):
    sanitized_cmd = strutils.mask_password(cmd)
    LOG.debug('Running cmd (SSH): %s', sanitized_cmd)
    if addl_env:
        raise InvalidArgumentError(_('Environment not supported over SSH'))

    if process_input:
        # This is (probably) fixable if we need it...
        raise InvalidArgumentError(_('process_input not supported over SSH'))

    stdin_stream, stdout_stream, stderr_stream = ssh.exec_command(cmd)
    channel = stdout_stream.channel

    # NOTE(justinsb): This seems suspicious...
    # ...other SSH clients have buffering issues with this approach
    stdout = stdout_stream.read()
    sanitized_stdout = strutils.mask_password(stdout)
    stderr = stderr_stream.read()
    sanitized_stderr = strutils.mask_password(stderr)

    stdin_stream.close()

    exit_status = channel.recv_exit_status()

    # exit_status == -1 if no exit code was returned
    if exit_status != -1:
        LOG.debug('Result was %s' % exit_status)
        if check_exit_code and exit_status != 0:
            raise ProcessExecutionError(exit_code=exit_status,
                                        stdout=sanitized_stdout,
                                        stderr=sanitized_stderr,
                                        cmd=sanitized_cmd)

    return (sanitized_stdout, sanitized_stderr)
예제 #3
0
    def create(self, req, body, tenant_id):
        # TODO(hub-cap): turn this into middleware
        LOG.info(_LI("Creating a database instance for tenant '%s'"),
                 tenant_id)
        LOG.debug("req : '%s'\n\n", strutils.mask_password(req))
        LOG.debug("body : '%s'\n\n", strutils.mask_password(body))
        context = req.environ[wsgi.CONTEXT_KEY]
        datastore_args = body['instance'].get('datastore', {})
        datastore, datastore_version = (
            datastore_models.get_datastore_version(**datastore_args))
        image_id = datastore_version.image_id
        name = body['instance']['name']
        flavor_ref = body['instance']['flavorRef']
        flavor_id = utils.get_id_from_href(flavor_ref)

        configuration = self._configuration_parse(context, body)
        databases = populate_validated_databases(
            body['instance'].get('databases', []))
        database_names = [database.get('_name', '') for database in databases]
        users = None
        try:
            users = populate_users(body['instance'].get('users', []),
                                   database_names)
        except ValueError as ve:
            raise exception.BadRequest(msg=ve)

        if 'volume' in body['instance']:
            volume_size = int(body['instance']['volume']['size'])
        else:
            volume_size = None

        if 'restorePoint' in body['instance']:
            backupRef = body['instance']['restorePoint']['backupRef']
            backup_id = utils.get_id_from_href(backupRef)
        else:
            backup_id = None

        availability_zone = body['instance'].get('availability_zone')
        nics = body['instance'].get('nics')

        slave_of_id = body['instance'].get('replica_of',
                                           # also check for older name
                                           body['instance'].get('slave_of'))
        replica_count = body['instance'].get('replica_count')
        instance = models.Instance.create(context, name, flavor_id,
                                          image_id, databases, users,
                                          datastore, datastore_version,
                                          volume_size, backup_id,
                                          availability_zone, nics,
                                          configuration, slave_of_id,
                                          replica_count=replica_count)

        view = views.InstanceDetailView(instance, req=req)
        return wsgi.Result(view.data(), 200)
예제 #4
0
 def create(self, req, body, tenant_id, instance_id):
     """Creates a set of users."""
     LOG.info(_("Creating users for instance '%s'") % instance_id)
     LOG.info(_("req : '%s'\n\n") % strutils.mask_password(req))
     LOG.info(_("body : '%s'\n\n") % strutils.mask_password(body))
     context = req.environ[wsgi.CONTEXT_KEY]
     users = body['users']
     try:
         model_users = populate_users(users)
         models.User.create(context, instance_id, model_users)
     except (ValueError, AttributeError) as e:
         raise exception.BadRequest(msg=str(e))
     return wsgi.Result(None, 202)
 def test_json_message(self):
     payload = """body: {"changePassword": {"adminPass": "******"}}"""
     expected = """body: {"changePassword": {"adminPass": "******"}}"""
     self.assertEqual(expected, strutils.mask_password(payload))
     payload = """body: {"rescue": {"admin_pass": "******"}}"""
     expected = """body: {"rescue": {"admin_pass": "******"}}"""
     self.assertEqual(expected, strutils.mask_password(payload))
     payload = """body: {"rescue": {"admin_password": "******"}}"""
     expected = """body: {"rescue": {"admin_password": "******"}}"""
     self.assertEqual(expected, strutils.mask_password(payload))
     payload = """body: {"rescue": {"password": "******"}}"""
     expected = """body: {"rescue": {"password": "******"}}"""
     self.assertEqual(expected, strutils.mask_password(payload))
 def test_json_message(self):
     payload = """body: {"changePassword": {"adminPass": "******"}}"""
     expected = """body: {"changePassword": {"adminPass": "******"}}"""
     self.assertEqual(expected, strutils.mask_password(payload))
     payload = """body: {"rescue": {"admin_pass": "******"}}"""
     expected = """body: {"rescue": {"admin_pass": "******"}}"""
     self.assertEqual(expected, strutils.mask_password(payload))
     payload = """body: {"rescue": {"admin_password": "******"}}"""
     expected = """body: {"rescue": {"admin_password": "******"}}"""
     self.assertEqual(expected, strutils.mask_password(payload))
     payload = """body: {"rescue": {"password": "******"}}"""
     expected = """body: {"rescue": {"password": "******"}}"""
     self.assertEqual(expected, strutils.mask_password(payload))
예제 #7
0
 def create(self, req, body, tenant_id, instance_id):
     """Creates a set of users."""
     LOG.info(_("Creating users for instance '%s'") % instance_id)
     LOG.info(_("req : '%s'\n\n") % strutils.mask_password(req))
     LOG.info(_("body : '%s'\n\n") % strutils.mask_password(body))
     context = req.environ[wsgi.CONTEXT_KEY]
     users = body['users']
     try:
         model_users = populate_users(users)
         models.User.create(context, instance_id, model_users)
     except (ValueError, AttributeError) as e:
         raise exception.BadRequest(msg=str(e))
     return wsgi.Result(None, 202)
예제 #8
0
    def synchronize(self):
        LOG.debug("Synchronizing running datasources")

        cage = d6cage.d6Cage()
        datasources = self.datasource_mgr.get_datasources(filter_secret=False)

        # Look for datasources in the db, but not in the cage.
        for configured_ds in datasources:
            active_ds = cage.service_object(configured_ds['name'])

            if active_ds is not None:
                if not configured_ds['enabled']:
                    LOG.info('Datasource %s now disabled, just delete it.',
                             configured_ds['name'])
                    self.datasource_mgr.delete_datasource(configured_ds['id'],
                                                          update_db=False)
                    continue

                active_config = cage.getservice(name=configured_ds['name'])
                if not self._config_eq(configured_ds, active_config):
                    LOG.debug('configured and active disagree: (%s) %s %s',
                              strutils.mask_password(active_ds),
                              strutils.mask_password(configured_ds),
                              strutils.mask_password(active_config))

                    LOG.info('Reloading datasource: %s',
                             strutils.mask_password(configured_ds))
                    self.datasource_mgr.delete_datasource(configured_ds['id'],
                                                          update_db=False)
                    self.datasource_mgr.add_datasource(
                        configured_ds,
                        update_db=False)
            else:
                if configured_ds['enabled']:
                    LOG.info('Configured datasource is not active, adding: %s',
                             strutils.mask_password(configured_ds))
                    self.datasource_mgr.add_datasource(configured_ds,
                                                       update_db=False)
                else:
                    LOG.info('Configured datasource is not active but ' +
                             'disabled, not adding: %s',
                             strutils.mask_password(configured_ds))

        # Look for datasources in the cage, but not in the db.  This
        # need not compare the configuration, because the above
        # comparison would have already checked the configuration.
        configured_dicts = dict((ds['name'], ds) for ds in datasources)
        LOG.debug("configured dicts: %s",
                  strutils.mask_password(configured_dicts))
        LOG.debug("active services: %s",
                  strutils.mask_password(cage.getservices()))
        for name, service in cage.getservices().items():
            LOG.debug('active datasource: %s', service['name'])
            if (service['type'] == 'datasource_driver' and
                    not configured_dicts.get(service['name'], None)):
                LOG.info('Active datasource is not configured, removing: %s',
                         service['name'])
                cage.deleteservice(service['name'])
                engine = cage.service_object('engine')
                engine.delete_policy(service['name'])
 def test_json(self):
     # Test 'adminPass' w/o spaces
     payload = """{'adminPass':'******'}"""
     expected = """{'adminPass':'******'}"""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'adminPass' with spaces
     payload = """{ 'adminPass' : 'mypassword' }"""
     expected = """{ 'adminPass' : '***' }"""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'admin_pass' w/o spaces
     payload = """{'admin_pass':'******'}"""
     expected = """{'admin_pass':'******'}"""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'admin_pass' with spaces
     payload = """{ 'admin_pass' : 'mypassword' }"""
     expected = """{ 'admin_pass' : '***' }"""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'admin_password' w/o spaces
     payload = """{'admin_password':'******'}"""
     expected = """{'admin_password':'******'}"""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'admin_password' with spaces
     payload = """{ 'admin_password' : 'mypassword' }"""
     expected = """{ 'admin_password' : '***' }"""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'password' w/o spaces
     payload = """{'password':'******'}"""
     expected = """{'password':'******'}"""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'password' with spaces
     payload = """{ 'password' : 'mypassword' }"""
     expected = """{ 'password' : '***' }"""
     self.assertEqual(expected, strutils.mask_password(payload))
 def test_json(self):
     # Test 'adminPass' w/o spaces
     payload = """{'adminPass':'******'}"""
     expected = """{'adminPass':'******'}"""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'adminPass' with spaces
     payload = """{ 'adminPass' : 'mypassword' }"""
     expected = """{ 'adminPass' : '***' }"""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'admin_pass' w/o spaces
     payload = """{'admin_pass':'******'}"""
     expected = """{'admin_pass':'******'}"""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'admin_pass' with spaces
     payload = """{ 'admin_pass' : 'mypassword' }"""
     expected = """{ 'admin_pass' : '***' }"""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'admin_password' w/o spaces
     payload = """{'admin_password':'******'}"""
     expected = """{'admin_password':'******'}"""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'admin_password' with spaces
     payload = """{ 'admin_password' : 'mypassword' }"""
     expected = """{ 'admin_password' : '***' }"""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'password' w/o spaces
     payload = """{'password':'******'}"""
     expected = """{'password':'******'}"""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'password' with spaces
     payload = """{ 'password' : 'mypassword' }"""
     expected = """{ 'password' : '***' }"""
     self.assertEqual(expected, strutils.mask_password(payload))
예제 #11
0
 def update_all(self, req, body, tenant_id, instance_id):
     """Change the password of one or more users."""
     LOG.info(_("Updating user passwords for instance '%s'") % instance_id)
     LOG.info(_("req : '%s'\n\n") % strutils.mask_password(req))
     context = req.environ[wsgi.CONTEXT_KEY]
     users = body['users']
     model_users = []
     for user in users:
         try:
             mu = guest_models.MySQLUser()
             mu.name = user['name']
             mu.host = user.get('host')
             mu.password = user['password']
             found_user = models.User.load(context, instance_id, mu.name,
                                           mu.host)
             if not found_user:
                 user_and_host = mu.name
                 if mu.host:
                     user_and_host += '@' + mu.host
                 raise exception.UserNotFound(uuid=user_and_host)
             model_users.append(mu)
         except (ValueError, AttributeError) as e:
             raise exception.BadRequest(msg=str(e))
     models.User.change_password(context, instance_id, model_users)
     return wsgi.Result(None, 202)
예제 #12
0
 def update_all(self, req, body, tenant_id, instance_id):
     """Change the password of one or more users."""
     LOG.info(_("Updating user passwords for instance '%s'") % instance_id)
     LOG.info(_("req : '%s'\n\n") % strutils.mask_password(req))
     context = req.environ[wsgi.CONTEXT_KEY]
     users = body['users']
     model_users = []
     for user in users:
         try:
             mu = guest_models.MySQLUser()
             mu.name = user['name']
             mu.host = user.get('host')
             mu.password = user['password']
             found_user = models.User.load(context, instance_id,
                                           mu.name, mu.host)
             if not found_user:
                 user_and_host = mu.name
                 if mu.host:
                     user_and_host += '@' + mu.host
                 raise exception.UserNotFound(uuid=user_and_host)
             model_users.append(mu)
         except (ValueError, AttributeError) as e:
             raise exception.BadRequest(msg=str(e))
     models.User.change_password(context, instance_id, model_users)
     return wsgi.Result(None, 202)
    def test_xml_message(self):
        payload = """<?xml version="1.0" encoding="UTF-8"?>
<rebuild
    xmlns="http://docs.openstack.org/compute/api/v1.1"
    name="foobar"
    imageRef="http://openstack.example.com/v1.1/32278/images/70a599e0-31e7"
    accessIPv4="1.2.3.4"
    accessIPv6="fe80::100"
    adminPass="******">
  <metadata>
    <meta key="My Server Name">Apache1</meta>
  </metadata>
</rebuild>"""
        expected = """<?xml version="1.0" encoding="UTF-8"?>
<rebuild
    xmlns="http://docs.openstack.org/compute/api/v1.1"
    name="foobar"
    imageRef="http://openstack.example.com/v1.1/32278/images/70a599e0-31e7"
    accessIPv4="1.2.3.4"
    accessIPv6="fe80::100"
    adminPass="******">
  <metadata>
    <meta key="My Server Name">Apache1</meta>
  </metadata>
</rebuild>"""
        self.assertEqual(expected, strutils.mask_password(payload))
        payload = """<?xml version="1.0" encoding="UTF-8"?>
<rescue xmlns="http://docs.openstack.org/compute/api/v1.1"
    admin_pass="******"/>"""
        expected = """<?xml version="1.0" encoding="UTF-8"?>
<rescue xmlns="http://docs.openstack.org/compute/api/v1.1"
    admin_pass="******"/>"""
        self.assertEqual(expected, strutils.mask_password(payload))
        payload = """<?xml version="1.0" encoding="UTF-8"?>
<rescue xmlns="http://docs.openstack.org/compute/api/v1.1"
    admin_password="******"/>"""
        expected = """<?xml version="1.0" encoding="UTF-8"?>
<rescue xmlns="http://docs.openstack.org/compute/api/v1.1"
    admin_password="******"/>"""
        self.assertEqual(expected, strutils.mask_password(payload))
        payload = """<?xml version="1.0" encoding="UTF-8"?>
<rescue xmlns="http://docs.openstack.org/compute/api/v1.1"
    password="******"/>"""
        expected = """<?xml version="1.0" encoding="UTF-8"?>
<rescue xmlns="http://docs.openstack.org/compute/api/v1.1"
    password="******"/>"""
        self.assertEqual(expected, strutils.mask_password(payload))
    def test_xml_message(self):
        payload = """<?xml version="1.0" encoding="UTF-8"?>
<rebuild
    xmlns="http://docs.openstack.org/compute/api/v1.1"
    name="foobar"
    imageRef="http://openstack.example.com/v1.1/32278/images/70a599e0-31e7"
    accessIPv4="1.2.3.4"
    accessIPv6="fe80::100"
    adminPass="******">
  <metadata>
    <meta key="My Server Name">Apache1</meta>
  </metadata>
</rebuild>"""
        expected = """<?xml version="1.0" encoding="UTF-8"?>
<rebuild
    xmlns="http://docs.openstack.org/compute/api/v1.1"
    name="foobar"
    imageRef="http://openstack.example.com/v1.1/32278/images/70a599e0-31e7"
    accessIPv4="1.2.3.4"
    accessIPv6="fe80::100"
    adminPass="******">
  <metadata>
    <meta key="My Server Name">Apache1</meta>
  </metadata>
</rebuild>"""
        self.assertEqual(expected, strutils.mask_password(payload))
        payload = """<?xml version="1.0" encoding="UTF-8"?>
<rescue xmlns="http://docs.openstack.org/compute/api/v1.1"
    admin_pass="******"/>"""
        expected = """<?xml version="1.0" encoding="UTF-8"?>
<rescue xmlns="http://docs.openstack.org/compute/api/v1.1"
    admin_pass="******"/>"""
        self.assertEqual(expected, strutils.mask_password(payload))
        payload = """<?xml version="1.0" encoding="UTF-8"?>
<rescue xmlns="http://docs.openstack.org/compute/api/v1.1"
    admin_password="******"/>"""
        expected = """<?xml version="1.0" encoding="UTF-8"?>
<rescue xmlns="http://docs.openstack.org/compute/api/v1.1"
    admin_password="******"/>"""
        self.assertEqual(expected, strutils.mask_password(payload))
        payload = """<?xml version="1.0" encoding="UTF-8"?>
<rescue xmlns="http://docs.openstack.org/compute/api/v1.1"
    password="******"/>"""
        expected = """<?xml version="1.0" encoding="UTF-8"?>
<rescue xmlns="http://docs.openstack.org/compute/api/v1.1"
    password="******"/>"""
        self.assertEqual(expected, strutils.mask_password(payload))
예제 #15
0
    def edit(self, req, id, body, tenant_id):
        """
        Updates the instance to set or unset one or more attributes.
        """
        LOG.info(_LI("Editing instance for tenant id %s."), tenant_id)
        LOG.debug("req: %s", strutils.mask_password(req))
        LOG.debug("body: %s", strutils.mask_password(body))
        context = req.environ[wsgi.CONTEXT_KEY]

        instance = models.Instance.load(context, id)

        args = {}
        args['detach_replica'] = 'slave_of' in body['instance']
        if 'name' in body['instance']:
            args['name'] = body['instance']['name']
        if 'configuration' in body['instance']:
            args['configuration_id'] = self._configuration_parse(context, body)
        self._modify_instance(instance, **args)
        return wsgi.Result(None, 202)
예제 #16
0
파일: auth.py 프로젝트: CMSS-BCRDB/RDSV1.0
 def authorize(self, request, tenant_id, roles):
     match_for_tenant = self.tenant_scoped_url.match(request.path_info)
     if (match_for_tenant and
             tenant_id == match_for_tenant.group('tenant_id')):
         LOG.debug(strutils.mask_password(
                   _("Authorized tenant '%(tenant_id)s' request: "
                     "%(request)s") %
                   {'tenant_id': tenant_id, 'request': request}))
         return True
     msg = _("User with tenant id %s cannot access this resource")
     LOG.debug(msg % tenant_id)
     raise webob.exc.HTTPForbidden(msg)
예제 #17
0
파일: wsgi.py 프로젝트: k4veri/keystone
    def __call__(self, req):
        if not hasattr(LOG, "isEnabledFor") or LOG.isEnabledFor(LOG.debug):
            LOG.debug("%s %s %s", ("*" * 20), "REQUEST ENVIRON", ("*" * 20))
            for key, value in req.environ.items():
                LOG.debug("%s = %s", key, strutils.mask_password(value))
            LOG.debug("")
            LOG.debug("%s %s %s", ("*" * 20), "REQUEST BODY", ("*" * 20))
            for line in req.body_file:
                LOG.debug("%s", strutils.mask_password(line))
            LOG.debug("")

        resp = req.get_response(self.application)
        if not hasattr(LOG, "isEnabledFor") or LOG.isEnabledFor(LOG.debug):
            LOG.debug("%s %s %s", ("*" * 20), "RESPONSE HEADERS", ("*" * 20))
            for (key, value) in six.iteritems(resp.headers):
                LOG.debug("%s = %s", key, value)
            LOG.debug("")

        resp.app_iter = self.print_generator(resp.app_iter)

        return resp
예제 #18
0
    def __call__(self, req):
        if not hasattr(LOG, 'isEnabledFor') or LOG.isEnabledFor(LOG.debug):
            LOG.debug('%s %s %s', ('*' * 20), 'REQUEST ENVIRON', ('*' * 20))
            for key, value in req.environ.items():
                LOG.debug('%s = %s', key, strutils.mask_password(value))
            LOG.debug('')
            LOG.debug('%s %s %s', ('*' * 20), 'REQUEST BODY', ('*' * 20))
            for line in req.body_file:
                LOG.debug('%s', strutils.mask_password(line))
            LOG.debug('')

        resp = req.get_response(self.application)
        if not hasattr(LOG, 'isEnabledFor') or LOG.isEnabledFor(LOG.debug):
            LOG.debug('%s %s %s', ('*' * 20), 'RESPONSE HEADERS', ('*' * 20))
            for (key, value) in six.iteritems(resp.headers):
                LOG.debug('%s = %s', key, value)
            LOG.debug('')

        resp.app_iter = self.print_generator(resp.app_iter)

        return resp
예제 #19
0
파일: volume.py 프로젝트: davehe/nova
 def _run_iscsiadm(self, iscsi_properties, iscsi_command, **kwargs):
     check_exit_code = kwargs.pop('check_exit_code', 0)
     (out, err) = utils.execute('iscsiadm', '-m', 'node', '-T',
                                iscsi_properties['target_iqn'],
                                '-p', iscsi_properties['target_portal'],
                                *iscsi_command, run_as_root=True,
                                check_exit_code=check_exit_code)
     msg = ('iscsiadm %(command)s: stdout=%(out)s stderr=%(err)s' %
            {'command': iscsi_command, 'out': out, 'err': err})
     # NOTE(bpokorny): iscsi_command can contain passwords so we need to
     # sanitize the password in the message.
     LOG.debug(strutils.mask_password(msg))
     return (out, err)
예제 #20
0
파일: auth.py 프로젝트: stanleykao72/trove
 def authorize(self, request, tenant_id, roles):
     match_for_tenant = self.tenant_scoped_url.match(request.path_info)
     if (match_for_tenant
             and tenant_id == match_for_tenant.group('tenant_id')):
         LOG.debug(
             strutils.mask_password(
                 _("Authorized tenant '%(tenant_id)s' request: "
                   "%(request)s") % {
                       'tenant_id': tenant_id,
                       'request': request
                   }))
         return True
     msg = _("User with tenant id %s cannot access this resource")
     LOG.debug(msg % tenant_id)
     raise webob.exc.HTTPForbidden(msg)
 def test_xml(self):
     # Test 'adminPass' w/o spaces
     payload = """<adminPass>mypassword</adminPass>"""
     expected = """<adminPass>***</adminPass>"""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'adminPass' with spaces
     payload = """<adminPass>
                     mypassword
                  </adminPass>"""
     expected = """<adminPass>***</adminPass>"""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'admin_pass' w/o spaces
     payload = """<admin_pass>mypassword</admin_pass>"""
     expected = """<admin_pass>***</admin_pass>"""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'admin_pass' with spaces
     payload = """<admin_pass>
                     mypassword
                  </admin_pass>"""
     expected = """<admin_pass>***</admin_pass>"""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'admin_password' w/o spaces
     payload = """<admin_password>mypassword</admin_password>"""
     expected = """<admin_password>***</admin_password>"""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'admin_password' with spaces
     payload = """<admin_password>
                     mypassword
                  </admin_password>"""
     expected = """<admin_password>***</admin_password>"""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'password' w/o spaces
     payload = """<password>mypassword</password>"""
     expected = """<password>***</password>"""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'password' with spaces
     payload = """<password>
                     mypassword
                  </password>"""
     expected = """<password>***</password>"""
     self.assertEqual(expected, strutils.mask_password(payload))
 def test_xml(self):
     # Test 'adminPass' w/o spaces
     payload = """<adminPass>mypassword</adminPass>"""
     expected = """<adminPass>***</adminPass>"""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'adminPass' with spaces
     payload = """<adminPass>
                     mypassword
                  </adminPass>"""
     expected = """<adminPass>***</adminPass>"""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'admin_pass' w/o spaces
     payload = """<admin_pass>mypassword</admin_pass>"""
     expected = """<admin_pass>***</admin_pass>"""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'admin_pass' with spaces
     payload = """<admin_pass>
                     mypassword
                  </admin_pass>"""
     expected = """<admin_pass>***</admin_pass>"""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'admin_password' w/o spaces
     payload = """<admin_password>mypassword</admin_password>"""
     expected = """<admin_password>***</admin_password>"""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'admin_password' with spaces
     payload = """<admin_password>
                     mypassword
                  </admin_password>"""
     expected = """<admin_password>***</admin_password>"""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'password' w/o spaces
     payload = """<password>mypassword</password>"""
     expected = """<password>***</password>"""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'password' with spaces
     payload = """<password>
                     mypassword
                  </password>"""
     expected = """<password>***</password>"""
     self.assertEqual(expected, strutils.mask_password(payload))
예제 #23
0
 def _run_iscsiadm(self, iscsi_properties, iscsi_command, **kwargs):
     check_exit_code = kwargs.pop('check_exit_code', 0)
     (out, err) = utils.execute('iscsiadm',
                                '-m',
                                'node',
                                '-T',
                                iscsi_properties['target_iqn'],
                                '-p',
                                iscsi_properties['target_portal'],
                                *iscsi_command,
                                run_as_root=True,
                                check_exit_code=check_exit_code)
     msg = ('iscsiadm %(command)s: stdout=%(out)s stderr=%(err)s' % {
         'command': iscsi_command,
         'out': out,
         'err': err
     })
     # NOTE(bpokorny): iscsi_command can contain passwords so we need to
     # sanitize the password in the message.
     LOG.debug(strutils.mask_password(msg))
     return (out, err)
예제 #24
0
 def update(self, req, body, tenant_id, instance_id, id):
     """Change attributes for one user."""
     LOG.info(_("Updating user attributes for instance '%s'") % instance_id)
     LOG.info(_("req : '%s'\n\n") % strutils.mask_password(req))
     context = req.environ[wsgi.CONTEXT_KEY]
     id = correct_id_with_req(id, req)
     username, hostname = unquote_user_host(id)
     user = None
     user_attrs = body['user']
     try:
         user = models.User.load(context, instance_id, username, hostname)
     except (ValueError, AttributeError) as e:
         raise exception.BadRequest(msg=str(e))
     if not user:
         raise exception.UserNotFound(uuid=id)
     try:
         models.User.update_attributes(context, instance_id, username,
                                       hostname, user_attrs)
     except (ValueError, AttributeError) as e:
         raise exception.BadRequest(msg=str(e))
     return wsgi.Result(None, 202)
예제 #25
0
 def update(self, req, body, tenant_id, instance_id, id):
     """Change attributes for one user."""
     LOG.info(_("Updating user attributes for instance '%s'") % instance_id)
     LOG.info(_("req : '%s'\n\n") % strutils.mask_password(req))
     context = req.environ[wsgi.CONTEXT_KEY]
     id = correct_id_with_req(id, req)
     username, hostname = unquote_user_host(id)
     user = None
     user_attrs = body['user']
     try:
         user = models.User.load(context, instance_id, username, hostname)
     except (ValueError, AttributeError) as e:
         raise exception.BadRequest(msg=str(e))
     if not user:
         raise exception.UserNotFound(uuid=id)
     try:
         models.User.update_attributes(context, instance_id, username,
                                       hostname, user_attrs)
     except (ValueError, AttributeError) as e:
         raise exception.BadRequest(msg=str(e))
     return wsgi.Result(None, 202)
예제 #26
0
    def _process_stack(self, request, action, action_args,
                       content_type, body, accept):
        """Implement the processing stack."""

        # Get the implementing method
        try:
            meth, extensions = self.get_method(request, action,
                                               content_type, body)
        except (AttributeError, TypeError):
            return Fault(webob.exc.HTTPNotFound())
        except KeyError as ex:
            msg = _("There is no such action: %s") % ex.args[0]
            return Fault(webob.exc.HTTPBadRequest(explanation=msg))
        except exception.MalformedRequestBody:
            msg = _("Malformed request body")
            return Fault(webob.exc.HTTPBadRequest(explanation=msg))

        if body:
            msg = _("Action: '%(action)s', calling method: %(meth)s, body: "
                    "%(body)s") % {'action': action,
                                   'body': unicode(body, 'utf-8'),
                                   'meth': str(meth)}
            LOG.debug(strutils.mask_password(msg))
        else:
            LOG.debug("Calling method '%(meth)s'",
                      {'meth': str(meth)})

        # Now, deserialize the request body...
        try:
            contents = {}
            if self._should_have_body(request):
                # allow empty body with PUT and POST
                if request.content_length == 0:
                    contents = {'body': None}
                else:
                    contents = self.deserialize(meth, content_type, body)
        except exception.InvalidContentType:
            msg = _("Unsupported Content-Type")
            return Fault(webob.exc.HTTPBadRequest(explanation=msg))
        except exception.MalformedRequestBody:
            msg = _("Malformed request body")
            return Fault(webob.exc.HTTPBadRequest(explanation=msg))

        # Update the action args
        action_args.update(contents)

        project_id = action_args.pop("project_id", None)
        context = request.environ.get('nova.context')
        # Following check ,we are doing in quotas.py
        """if (context and project_id and (project_id != context.project_id)):
            msg = _("Malformed request URL: URL's project_id '%(project_id)s'"
                    " doesn't match Context's project_id"
                    " '%(context_project_id)s'") % \
                    {'project_id': project_id,
                     'context_project_id': context.project_id}
            return Fault(webob.exc.HTTPBadRequest(explanation=msg))"""

        # Run pre-processing extensions
        response, post = self.pre_process_extensions(extensions,
                                                     request, action_args)

        if not response:
            try:
                with ResourceExceptionHandler():
                    action_result = self.dispatch(meth, request, action_args)
            except Fault as ex:
                response = ex

        if not response:
            # No exceptions; convert action_result into a
            # ResponseObject
            resp_obj = None
            if type(action_result) is dict or action_result is None:
                resp_obj = ResponseObject(action_result)
            elif isinstance(action_result, ResponseObject):
                resp_obj = action_result
            else:
                response = action_result

            # Run post-processing extensions
            if resp_obj:
                # Do a preserialize to set up the response object
                serializers = getattr(meth, 'wsgi_serializers', {})
                resp_obj._bind_method_serializers(serializers)
                if hasattr(meth, 'wsgi_code'):
                    resp_obj._default_code = meth.wsgi_code
                resp_obj.preserialize(accept, self.default_serializers)

                # Process post-processing extensions
                response = self.post_process_extensions(post, resp_obj,
                                                        request, action_args)

            if resp_obj and not response:
                response = resp_obj.serialize(request, accept,
                                              self.default_serializers)

        if hasattr(response, 'headers'):

            for hdr, val in response.headers.items():
                # Headers must be utf-8 strings
                response.headers[hdr] = utils.utf8(str(val))

            if not request.api_version_request.is_null():
                response.headers[API_VERSION_REQUEST_HEADER] = \
                    request.api_version_request.get_string()
                response.headers['Vary'] = API_VERSION_REQUEST_HEADER

        return response
예제 #27
0
def _safe_log(log_func, msg, msg_data):
    """Sanitizes the msg_data field before logging."""
    return log_func(msg, strutils.mask_password(six.text_type(msg_data)))
예제 #28
0
    def create(self, req, body, tenant_id):
        # TODO(hub-cap): turn this into middleware
        LOG.info(_LI("Creating a database instance for tenant '%s'"),
                 tenant_id)
        LOG.debug("req : '%s'\n\n", strutils.mask_password(req))
        LOG.debug("body : '%s'\n\n", strutils.mask_password(body))
        context = req.environ[wsgi.CONTEXT_KEY]
        datastore_args = body['instance'].get('datastore', {})
        datastore, datastore_version = (
            datastore_models.get_datastore_version(**datastore_args))
        image_id = datastore_version.image_id
        name = body['instance']['name']
        flavor_ref = body['instance']['flavorRef']
        flavor_id = utils.get_id_from_href(flavor_ref)

        configuration = self._configuration_parse(context, body)
        databases = populate_validated_databases(
            body['instance'].get('databases', []))
        database_names = [database.get('_name', '') for database in databases]
        users = None
        try:
            users = populate_users(body['instance'].get('users', []),
                                   database_names)
        except ValueError as ve:
            raise exception.BadRequest(msg=ve)

        if 'volume' in body['instance']:
            volume_size = int(body['instance']['volume']['size'])
        else:
            volume_size = None

        if 'restorePoint' in body['instance']:
            backupRef = body['instance']['restorePoint']['backupRef']
            backup_id = utils.get_id_from_href(backupRef)
        else:
            backup_id = None

        availability_zone = body['instance'].get('availability_zone')
        nics = body['instance'].get('nics')
        #rds-start
        if CONF.private_network:
            from trove.common.remote import create_neutron_client
            from neutronclient.common import exceptions as neutron_exceptions
            client = create_neutron_client(context)
            try:
                net_info = client.list_networks(name=CONF.private_network)
                private_id = net_info['networks'][0]['id']
            except neutron_exceptions.NotFound:
                raise neutron_exceptions.NotFound((_("failed geting private"
                                                     " net id for: %s") %
                                                     CONF.private_network))
            else:
                nics.insert(1,{'net-id': private_id})
                LOG.info(_("attach network is %s") % nics)
        #rds-end
			

        slave_of_id = body['instance'].get('replica_of',
                                           # also check for older name
                                           body['instance'].get('slave_of'))
        replica_count = body['instance'].get('replica_count')
        instance = models.Instance.create(context, name, flavor_id,
                                          image_id, databases, users,
                                          datastore, datastore_version,
                                          volume_size, backup_id,
                                          availability_zone, nics,
                                          configuration, slave_of_id,
                                          replica_count=replica_count)

        view = views.InstanceDetailView(instance, req=req)
        return wsgi.Result(view.data(), 200)
예제 #29
0
def execute(*cmd, **kwargs):
    """Helper method to shell out and execute a command through subprocess.

    Allows optional retry.

    :param cmd:             Passed to subprocess.Popen.
    :type cmd:              string
    :param process_input:   Send to opened process.
    :type process_input:    string
    :param env_variables:   Environment variables and their values that
                            will be set for the process.
    :type env_variables:    dict
    :param check_exit_code: Single bool, int, or list of allowed exit
                            codes.  Defaults to [0].  Raise
                            :class:`ProcessExecutionError` unless
                            program exits with one of these code.
    :type check_exit_code:  boolean, int, or [int]
    :param delay_on_retry:  True | False. Defaults to True. If set to True,
                            wait a short amount of time before retrying.
    :type delay_on_retry:   boolean
    :param attempts:        How many times to retry cmd.
    :type attempts:         int
    :param run_as_root:     True | False. Defaults to False. If set to True,
                            the command is prefixed by the command specified
                            in the root_helper kwarg.
    :type run_as_root:      boolean
    :param root_helper:     command to prefix to commands called with
                            run_as_root=True
    :type root_helper:      string
    :param shell:           whether or not there should be a shell used to
                            execute this command. Defaults to false.
    :type shell:            boolean
    :param loglevel:        log level for execute commands.
    :type loglevel:         int.  (Should be logging.DEBUG or logging.INFO)
    :returns:               (stdout, stderr) from process execution
    :raises:                :class:`UnknownArgumentError` on
                            receiving unknown arguments
    :raises:                :class:`ProcessExecutionError`
    """

    process_input = kwargs.pop('process_input', None)
    env_variables = kwargs.pop('env_variables', None)
    check_exit_code = kwargs.pop('check_exit_code', [0])
    ignore_exit_code = False
    delay_on_retry = kwargs.pop('delay_on_retry', True)
    attempts = kwargs.pop('attempts', 1)
    run_as_root = kwargs.pop('run_as_root', False)
    root_helper = kwargs.pop('root_helper', '')
    shell = kwargs.pop('shell', False)
    loglevel = kwargs.pop('loglevel', logging.DEBUG)

    if isinstance(check_exit_code, bool):
        ignore_exit_code = not check_exit_code
        check_exit_code = [0]
    elif isinstance(check_exit_code, int):
        check_exit_code = [check_exit_code]

    if kwargs:
        raise UnknownArgumentError(_('Got unknown keyword args: %r') % kwargs)

    if run_as_root and hasattr(os, 'geteuid') and os.geteuid() != 0:
        if not root_helper:
            raise NoRootWrapSpecified(
                message=_('Command requested root, but did not '
                          'specify a root helper.'))
        cmd = shlex.split(root_helper) + list(cmd)

    cmd = map(str, cmd)
    sanitized_cmd = strutils.mask_password(' '.join(cmd))

    while attempts > 0:
        attempts -= 1
        try:
            LOG.log(loglevel, _('Running cmd (subprocess): %s'), sanitized_cmd)
            _PIPE = subprocess.PIPE  # pylint: disable=E1101

            if os.name == 'nt':
                preexec_fn = None
                close_fds = False
            else:
                preexec_fn = _subprocess_setup
                close_fds = True

            obj = subprocess.Popen(cmd,
                                   stdin=_PIPE,
                                   stdout=_PIPE,
                                   stderr=_PIPE,
                                   close_fds=close_fds,
                                   preexec_fn=preexec_fn,
                                   shell=shell,
                                   env=env_variables)
            result = None
            for _i in six.moves.range(20):
                # NOTE(russellb) 20 is an arbitrary number of retries to
                # prevent any chance of looping forever here.
                try:
                    if process_input is not None:
                        result = obj.communicate(process_input)
                    else:
                        result = obj.communicate()
                except OSError as e:
                    if e.errno in (errno.EAGAIN, errno.EINTR):
                        continue
                    raise
                break
            obj.stdin.close()  # pylint: disable=E1101
            _returncode = obj.returncode  # pylint: disable=E1101
            LOG.log(loglevel, _('Result was %s'), _returncode)
            if not ignore_exit_code and _returncode not in check_exit_code:
                (stdout, stderr) = result
                sanitized_stdout = strutils.mask_password(stdout)
                sanitized_stderr = strutils.mask_password(stderr)
                raise ProcessExecutionError(exit_code=_returncode,
                                            stdout=sanitized_stdout,
                                            stderr=sanitized_stderr,
                                            cmd=sanitized_cmd)
            return result
        except ProcessExecutionError:
            if not attempts:
                raise
            else:
                LOG.log(loglevel, _('%r failed. Retrying.'), sanitized_cmd)
                if delay_on_retry:
                    greenthread.sleep(random.randint(20, 200) / 100.0)
        finally:
            # NOTE(termie): this appears to be necessary to let the subprocess
            #               call clean something up in between calls, without
            #               it two execute calls in a row hangs the second one
            greenthread.sleep(0)
 def test_xml_attribute(self):
     # Test 'adminPass' w/o spaces
     payload = """adminPass='******'"""
     expected = """adminPass='******'"""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'adminPass' with spaces
     payload = """adminPass = '******'"""
     expected = """adminPass = '******'"""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'adminPass' with double quotes
     payload = """adminPass = "******""""
     expected = """adminPass = "******""""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'admin_pass' w/o spaces
     payload = """admin_pass='******'"""
     expected = """admin_pass='******'"""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'admin_pass' with spaces
     payload = """admin_pass = '******'"""
     expected = """admin_pass = '******'"""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'admin_pass' with double quotes
     payload = """admin_pass = "******""""
     expected = """admin_pass = "******""""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'admin_password' w/o spaces
     payload = """admin_password='******'"""
     expected = """admin_password='******'"""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'admin_password' with spaces
     payload = """admin_password = '******'"""
     expected = """admin_password = '******'"""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'admin_password' with double quotes
     payload = """admin_password = "******""""
     expected = """admin_password = "******""""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'password' w/o spaces
     payload = """password='******'"""
     expected = """password='******'"""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'password' with spaces
     payload = """password = '******'"""
     expected = """password = '******'"""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'password' with double quotes
     payload = """password = "******""""
     expected = """password = "******""""
     self.assertEqual(expected, strutils.mask_password(payload))
예제 #31
0
def execute(*cmd, **kwargs):
    """Helper method to shell out and execute a command through subprocess.

    Allows optional retry.

    :param cmd:             Passed to subprocess.Popen.
    :type cmd:              string
    :param process_input:   Send to opened process.
    :type process_input:    string
    :param env_variables:   Environment variables and their values that
                            will be set for the process.
    :type env_variables:    dict
    :param check_exit_code: Single bool, int, or list of allowed exit
                            codes.  Defaults to [0].  Raise
                            :class:`ProcessExecutionError` unless
                            program exits with one of these code.
    :type check_exit_code:  boolean, int, or [int]
    :param delay_on_retry:  True | False. Defaults to True. If set to True,
                            wait a short amount of time before retrying.
    :type delay_on_retry:   boolean
    :param attempts:        How many times to retry cmd.
    :type attempts:         int
    :param run_as_root:     True | False. Defaults to False. If set to True,
                            the command is prefixed by the command specified
                            in the root_helper kwarg.
    :type run_as_root:      boolean
    :param root_helper:     command to prefix to commands called with
                            run_as_root=True
    :type root_helper:      string
    :param shell:           whether or not there should be a shell used to
                            execute this command. Defaults to false.
    :type shell:            boolean
    :param loglevel:        log level for execute commands.
    :type loglevel:         int.  (Should be logging.DEBUG or logging.INFO)
    :returns:               (stdout, stderr) from process execution
    :raises:                :class:`UnknownArgumentError` on
                            receiving unknown arguments
    :raises:                :class:`ProcessExecutionError`
    """

    process_input = kwargs.pop('process_input', None)
    env_variables = kwargs.pop('env_variables', None)
    check_exit_code = kwargs.pop('check_exit_code', [0])
    ignore_exit_code = False
    delay_on_retry = kwargs.pop('delay_on_retry', True)
    attempts = kwargs.pop('attempts', 1)
    run_as_root = kwargs.pop('run_as_root', False)
    root_helper = kwargs.pop('root_helper', '')
    shell = kwargs.pop('shell', False)
    loglevel = kwargs.pop('loglevel', logging.DEBUG)

    if isinstance(check_exit_code, bool):
        ignore_exit_code = not check_exit_code
        check_exit_code = [0]
    elif isinstance(check_exit_code, int):
        check_exit_code = [check_exit_code]

    if kwargs:
        raise UnknownArgumentError(_('Got unknown keyword args: %r') % kwargs)

    if run_as_root and hasattr(os, 'geteuid') and os.geteuid() != 0:
        if not root_helper:
            raise NoRootWrapSpecified(
                message=_('Command requested root, but did not '
                          'specify a root helper.'))
        cmd = shlex.split(root_helper) + list(cmd)

    cmd = map(str, cmd)
    sanitized_cmd = strutils.mask_password(' '.join(cmd))

    while attempts > 0:
        attempts -= 1
        try:
            LOG.log(loglevel, _('Running cmd (subprocess): %s'), sanitized_cmd)
            _PIPE = subprocess.PIPE  # pylint: disable=E1101

            if os.name == 'nt':
                preexec_fn = None
                close_fds = False
            else:
                preexec_fn = _subprocess_setup
                close_fds = True

            obj = subprocess.Popen(cmd,
                                   stdin=_PIPE,
                                   stdout=_PIPE,
                                   stderr=_PIPE,
                                   close_fds=close_fds,
                                   preexec_fn=preexec_fn,
                                   shell=shell,
                                   env=env_variables)
            result = None
            for _i in six.moves.range(20):
                # NOTE(russellb) 20 is an arbitrary number of retries to
                # prevent any chance of looping forever here.
                try:
                    if process_input is not None:
                        result = obj.communicate(process_input)
                    else:
                        result = obj.communicate()
                except OSError as e:
                    if e.errno in (errno.EAGAIN, errno.EINTR):
                        continue
                    raise
                break
            obj.stdin.close()  # pylint: disable=E1101
            _returncode = obj.returncode  # pylint: disable=E1101
            LOG.log(loglevel, 'Result was %s' % _returncode)
            if not ignore_exit_code and _returncode not in check_exit_code:
                (stdout, stderr) = result
                sanitized_stdout = strutils.mask_password(stdout)
                sanitized_stderr = strutils.mask_password(stderr)
                raise ProcessExecutionError(exit_code=_returncode,
                                            stdout=sanitized_stdout,
                                            stderr=sanitized_stderr,
                                            cmd=sanitized_cmd)
            return result
        except ProcessExecutionError:
            if not attempts:
                raise
            else:
                LOG.log(loglevel, _('%r failed. Retrying.'), sanitized_cmd)
                if delay_on_retry:
                    greenthread.sleep(random.randint(20, 200) / 100.0)
        finally:
            # NOTE(termie): this appears to be necessary to let the subprocess
            #               call clean something up in between calls, without
            #               it two execute calls in a row hangs the second one
            greenthread.sleep(0)
def execute(*cmd, **kwargs):
    """Helper method to shell out and execute a command through subprocess.

    Allows optional retry.

    :param cmd:             Passed to subprocess.Popen.
    :type cmd:              string
    :param process_input:   Send to opened process.
    :type process_input:    string
    :param env_variables:   Environment variables and their values that
                            will be set for the process.
    :type env_variables:    dict
    :param check_exit_code: Single bool, int, or list of allowed exit
                            codes.  Defaults to [0].  Raise
                            :class:`ProcessExecutionError` unless
                            program exits with one of these code.
    :type check_exit_code:  boolean, int, or [int]
    :param delay_on_retry:  True | False. Defaults to True. If set to True,
                            wait a short amount of time before retrying.
    :type delay_on_retry:   boolean
    :param attempts:        How many times to retry cmd.
    :type attempts:         int
    :param run_as_root:     True | False. Defaults to False. If set to True,
                            the command is prefixed by the command specified
                            in the root_helper kwarg.
    :type run_as_root:      boolean
    :param root_helper:     command to prefix to commands called with
                            run_as_root=True
    :type root_helper:      string
    :param shell:           whether or not there should be a shell used to
                            execute this command. Defaults to false.
    :type shell:            boolean
    :param loglevel:        log level for execute commands.
    :type loglevel:         int.  (Should be logging.DEBUG or logging.INFO)
    :param log_errors:      Should stdout and stderr be logged on error?
                            Possible values are None=default,
                            LOG_FINAL_ERROR, or LOG_ALL_ERRORS. None
                            implies no logging on errors. The values
                            LOG_FINAL_ERROR and LOG_ALL_ERRORS are
                            relevant when multiple attempts of command
                            execution are requested using the
                            'attempts' parameter. If LOG_FINAL_ERROR
                            is specified then only log an error on the
                            last attempt, and LOG_ALL_ERRORS requires
                            logging on each occurence of an error.
    :type log_errors:       integer.
    :returns:               (stdout, stderr) from process execution
    :raises:                :class:`UnknownArgumentError` on
                            receiving unknown arguments
    :raises:                :class:`ProcessExecutionError`
    :raises:                :class:`OSError`
    """

    process_input = kwargs.pop('process_input', None)
    env_variables = kwargs.pop('env_variables', None)
    check_exit_code = kwargs.pop('check_exit_code', [0])
    ignore_exit_code = False
    delay_on_retry = kwargs.pop('delay_on_retry', True)
    attempts = kwargs.pop('attempts', 1)
    run_as_root = kwargs.pop('run_as_root', False)
    root_helper = kwargs.pop('root_helper', '')
    shell = kwargs.pop('shell', False)
    loglevel = kwargs.pop('loglevel', logging.DEBUG)
    log_errors = kwargs.pop('log_errors', None)

    if isinstance(check_exit_code, bool):
        ignore_exit_code = not check_exit_code
        check_exit_code = [0]
    elif isinstance(check_exit_code, int):
        check_exit_code = [check_exit_code]

    if kwargs:
        raise UnknownArgumentError(_('Got unknown keyword args: %r') % kwargs)

    if log_errors not in [None, LOG_ALL_ERRORS, LOG_FINAL_ERROR]:
        raise InvalidArgumentError(_('Got invalid arg log_errors: %r') %
                                   log_errors)

    if run_as_root and hasattr(os, 'geteuid') and os.geteuid() != 0:
        if not root_helper:
            raise NoRootWrapSpecified(
                message=_('Command requested root, but did not '
                          'specify a root helper.'))
        cmd = shlex.split(root_helper) + list(cmd)

    cmd = map(str, cmd)
    sanitized_cmd = strutils.mask_password(' '.join(cmd))

    while attempts > 0:
        attempts -= 1
        try:
            LOG.log(loglevel, _('Running cmd (subprocess): %s'), sanitized_cmd)
            _PIPE = subprocess.PIPE  # pylint: disable=E1101

            if os.name == 'nt':
                preexec_fn = None
                close_fds = False
            else:
                preexec_fn = _subprocess_setup
                close_fds = True

            obj = subprocess.Popen(cmd,
                                   stdin=_PIPE,
                                   stdout=_PIPE,
                                   stderr=_PIPE,
                                   close_fds=close_fds,
                                   preexec_fn=preexec_fn,
                                   shell=shell,
                                   env=env_variables)

            result = obj.communicate(process_input)

            obj.stdin.close()  # pylint: disable=E1101
            _returncode = obj.returncode  # pylint: disable=E1101
            LOG.log(loglevel, 'Result was %s' % _returncode)
            if not ignore_exit_code and _returncode not in check_exit_code:
                (stdout, stderr) = result
                sanitized_stdout = strutils.mask_password(stdout)
                sanitized_stderr = strutils.mask_password(stderr)
                raise ProcessExecutionError(exit_code=_returncode,
                                            stdout=sanitized_stdout,
                                            stderr=sanitized_stderr,
                                            cmd=sanitized_cmd)
            return result

        except (ProcessExecutionError, OSError) as err:
            # if we want to always log the errors or if this is
            # the final attempt that failed and we want to log that.
            if log_errors == LOG_ALL_ERRORS or (
                    log_errors == LOG_FINAL_ERROR and not attempts):
                if isinstance(err, ProcessExecutionError):
                    format = _('%(desc)r\ncommand: %(cmd)r\n'
                               'exit code: %(code)r\nstdout: %(stdout)r\n'
                               'stderr: %(stderr)r')
                    LOG.log(loglevel, format, {"desc": err.description,
                                               "cmd": err.cmd,
                                               "code": err.exit_code,
                                               "stdout": err.stdout,
                                               "stderr": err.stderr})
                else:
                    format = _('Got an OSError\ncommand: %(cmd)r\n'
                               'errno: %(errno)r')
                    LOG.log(loglevel, format, {"cmd": sanitized_cmd,
                                               "errno": err.errno})

            if not attempts:
                LOG.log(loglevel, _('%r failed. Not Retrying.'),
                        sanitized_cmd)
                raise
            else:
                LOG.log(loglevel, _('%r failed. Retrying.'),
                        sanitized_cmd)
                if delay_on_retry:
                    greenthread.sleep(random.randint(20, 200) / 100.0)
        finally:
            # NOTE(termie): this appears to be necessary to let the subprocess
            #               call clean something up in between calls, without
            #               it two execute calls in a row hangs the second one
            greenthread.sleep(0)
def execute(*cmd, **kwargs):
    """Helper method to shell out and execute a command through subprocess.

    Allows optional retry.

    :param cmd:             Passed to subprocess.Popen.
    :type cmd:              string
    :param process_input:   Send to opened process.
    :type process_input:    string
    :param env_variables:   Environment variables and their values that
                            will be set for the process.
    :type env_variables:    dict
    :param check_exit_code: Single bool, int, or list of allowed exit
                            codes.  Defaults to [0].  Raise
                            :class:`ProcessExecutionError` unless
                            program exits with one of these code.
    :type check_exit_code:  boolean, int, or [int]
    :param delay_on_retry:  True | False. Defaults to True. If set to True,
                            wait a short amount of time before retrying.
    :type delay_on_retry:   boolean
    :param attempts:        How many times to retry cmd.
    :type attempts:         int
    :param run_as_root:     True | False. Defaults to False. If set to True,
                            the command is prefixed by the command specified
                            in the root_helper kwarg.
    :type run_as_root:      boolean
    :param root_helper:     command to prefix to commands called with
                            run_as_root=True
    :type root_helper:      string
    :param shell:           whether or not there should be a shell used to
                            execute this command. Defaults to false.
    :type shell:            boolean
    :param loglevel:        log level for execute commands.
    :type loglevel:         int.  (Should be logging.DEBUG or logging.INFO)
    :param log_errors:      Should stdout and stderr be logged on error?
                            Possible values are None=default,
                            LOG_FINAL_ERROR, or LOG_ALL_ERRORS. None
                            implies no logging on errors. The values
                            LOG_FINAL_ERROR and LOG_ALL_ERRORS are
                            relevant when multiple attempts of command
                            execution are requested using the
                            'attempts' parameter. If LOG_FINAL_ERROR
                            is specified then only log an error on the
                            last attempt, and LOG_ALL_ERRORS requires
                            logging on each occurence of an error.
    :type log_errors:       integer.
    :returns:               (stdout, stderr) from process execution
    :raises:                :class:`UnknownArgumentError` on
                            receiving unknown arguments
    :raises:                :class:`ProcessExecutionError`
    :raises:                :class:`OSError`
    """

    process_input = kwargs.pop('process_input', None)
    env_variables = kwargs.pop('env_variables', None)
    check_exit_code = kwargs.pop('check_exit_code', [0])
    ignore_exit_code = False
    delay_on_retry = kwargs.pop('delay_on_retry', True)
    attempts = kwargs.pop('attempts', 1)
    run_as_root = kwargs.pop('run_as_root', False)
    root_helper = kwargs.pop('root_helper', '')
    shell = kwargs.pop('shell', False)
    loglevel = kwargs.pop('loglevel', logging.DEBUG)
    log_errors = kwargs.pop('log_errors', None)

    if isinstance(check_exit_code, bool):
        ignore_exit_code = not check_exit_code
        check_exit_code = [0]
    elif isinstance(check_exit_code, int):
        check_exit_code = [check_exit_code]

    if kwargs:
        raise UnknownArgumentError(_('Got unknown keyword args: %r') % kwargs)

    if log_errors not in [None, LOG_ALL_ERRORS, LOG_FINAL_ERROR]:
        raise InvalidArgumentError(
            _('Got invalid arg log_errors: %r') % log_errors)

    if run_as_root and hasattr(os, 'geteuid') and os.geteuid() != 0:
        if not root_helper:
            raise NoRootWrapSpecified(
                message=_('Command requested root, but did not '
                          'specify a root helper.'))
        cmd = shlex.split(root_helper) + list(cmd)

    cmd = map(str, cmd)
    sanitized_cmd = strutils.mask_password(' '.join(cmd))

    while attempts > 0:
        attempts -= 1
        try:
            LOG.log(loglevel, _('Running cmd (subprocess): %s'), sanitized_cmd)
            _PIPE = subprocess.PIPE  # pylint: disable=E1101

            if os.name == 'nt':
                preexec_fn = None
                close_fds = False
            else:
                preexec_fn = _subprocess_setup
                close_fds = True

            obj = subprocess.Popen(cmd,
                                   stdin=_PIPE,
                                   stdout=_PIPE,
                                   stderr=_PIPE,
                                   close_fds=close_fds,
                                   preexec_fn=preexec_fn,
                                   shell=shell,
                                   env=env_variables)

            result = obj.communicate(process_input)

            obj.stdin.close()  # pylint: disable=E1101
            _returncode = obj.returncode  # pylint: disable=E1101
            LOG.log(loglevel, 'Result was %s' % _returncode)
            if not ignore_exit_code and _returncode not in check_exit_code:
                (stdout, stderr) = result
                sanitized_stdout = strutils.mask_password(stdout)
                sanitized_stderr = strutils.mask_password(stderr)
                raise ProcessExecutionError(exit_code=_returncode,
                                            stdout=sanitized_stdout,
                                            stderr=sanitized_stderr,
                                            cmd=sanitized_cmd)
            return result

        except (ProcessExecutionError, OSError) as err:
            # if we want to always log the errors or if this is
            # the final attempt that failed and we want to log that.
            if log_errors == LOG_ALL_ERRORS or (log_errors == LOG_FINAL_ERROR
                                                and not attempts):
                if isinstance(err, ProcessExecutionError):
                    format = _('%(desc)r\ncommand: %(cmd)r\n'
                               'exit code: %(code)r\nstdout: %(stdout)r\n'
                               'stderr: %(stderr)r')
                    LOG.log(
                        loglevel, format, {
                            "desc": err.description,
                            "cmd": err.cmd,
                            "code": err.exit_code,
                            "stdout": err.stdout,
                            "stderr": err.stderr
                        })
                else:
                    format = _('Got an OSError\ncommand: %(cmd)r\n'
                               'errno: %(errno)r')
                    LOG.log(loglevel, format, {
                        "cmd": sanitized_cmd,
                        "errno": err.errno
                    })

            if not attempts:
                LOG.log(loglevel, _('%r failed. Not Retrying.'), sanitized_cmd)
                raise
            else:
                LOG.log(loglevel, _('%r failed. Retrying.'), sanitized_cmd)
                if delay_on_retry:
                    greenthread.sleep(random.randint(20, 200) / 100.0)
        finally:
            # NOTE(termie): this appears to be necessary to let the subprocess
            #               call clean something up in between calls, without
            #               it two execute calls in a row hangs the second one
            greenthread.sleep(0)
    def test_mask_password(self):
        payload = "test = 'password'  :   'aaaaaa'"
        expected = "test = 'password'  :   '111'"
        self.assertEqual(expected, strutils.mask_password(payload,
                                                          secret='111'))

        payload = 'mysqld --password "aaaaaa"'
        expected = 'mysqld --password "****"'
        self.assertEqual(expected,
                         strutils.mask_password(payload, secret='****'))

        payload = 'mysqld --password aaaaaa'
        expected = 'mysqld --password ???'
        self.assertEqual(expected, strutils.mask_password(payload,
                                                          secret='???'))

        payload = 'mysqld --password = "******"'
        expected = 'mysqld --password = "******"'
        self.assertEqual(expected,
                         strutils.mask_password(payload, secret='****'))

        payload = "mysqld --password = '******'"
        expected = "mysqld --password = '******'"
        self.assertEqual(expected,
                         strutils.mask_password(payload, secret='****'))

        payload = "mysqld --password = aaaaaa"
        expected = "mysqld --password = ****"
        self.assertEqual(expected,
                         strutils.mask_password(payload, secret='****'))

        payload = "test = password =   aaaaaa"
        expected = "test = password =   111"
        self.assertEqual(expected, strutils.mask_password(payload,
                                                          secret='111'))

        payload = "test = password=   aaaaaa"
        expected = "test = password=   111"
        self.assertEqual(expected, strutils.mask_password(payload,
                                                          secret='111'))

        payload = "test = password =aaaaaa"
        expected = "test = password =111"
        self.assertEqual(expected, strutils.mask_password(payload,
                                                          secret='111'))

        payload = "test = password=aaaaaa"
        expected = "test = password=111"
        self.assertEqual(expected, strutils.mask_password(payload,
                                                          secret='111'))

        payload = 'test = "original_password" : "aaaaaaaaa"'
        expected = 'test = "original_password" : "***"'
        self.assertEqual(expected, strutils.mask_password(payload))

        payload = 'test = "param1" : "value"'
        expected = 'test = "param1" : "value"'
        self.assertEqual(expected, strutils.mask_password(payload))

        payload = """{'adminPass':'******'}"""
        payload = six.text_type(payload)
        expected = """{'adminPass':'******'}"""
        self.assertEqual(expected, strutils.mask_password(payload))

        payload = ("test = 'node.session.auth.password','-v','mypassword',"
                   "'nomask'")
        expected = ("test = 'node.session.auth.password','-v','***',"
                    "'nomask'")
        self.assertEqual(expected, strutils.mask_password(payload))

        payload = ("test = 'node.session.auth.password', '--password', "
                   "'mypassword', 'nomask'")
        expected = ("test = 'node.session.auth.password', '--password', "
                    "'***', 'nomask'")
        self.assertEqual(expected, strutils.mask_password(payload))

        payload = ("test = 'node.session.auth.password', '--password', "
                   "'mypassword'")
        expected = ("test = 'node.session.auth.password', '--password', "
                    "'***'")
        self.assertEqual(expected, strutils.mask_password(payload))

        payload = "test = node.session.auth.password -v mypassword nomask"
        expected = "test = node.session.auth.password -v *** nomask"
        self.assertEqual(expected, strutils.mask_password(payload))

        payload = ("test = node.session.auth.password --password mypassword "
                   "nomask")
        expected = ("test = node.session.auth.password --password *** "
                    "nomask")
        self.assertEqual(expected, strutils.mask_password(payload))

        payload = ("test = node.session.auth.password --password mypassword")
        expected = ("test = node.session.auth.password --password ***")
        self.assertEqual(expected, strutils.mask_password(payload))

        payload = "test = cmd --password my\xe9\x80\x80pass"
        expected = ("test = cmd --password ***")
        self.assertEqual(expected, strutils.mask_password(payload))
 def test_xml_attribute(self):
     # Test 'adminPass' w/o spaces
     payload = """adminPass='******'"""
     expected = """adminPass='******'"""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'adminPass' with spaces
     payload = """adminPass = '******'"""
     expected = """adminPass = '******'"""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'adminPass' with double quotes
     payload = """adminPass = "******""""
     expected = """adminPass = "******""""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'admin_pass' w/o spaces
     payload = """admin_pass='******'"""
     expected = """admin_pass='******'"""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'admin_pass' with spaces
     payload = """admin_pass = '******'"""
     expected = """admin_pass = '******'"""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'admin_pass' with double quotes
     payload = """admin_pass = "******""""
     expected = """admin_pass = "******""""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'admin_password' w/o spaces
     payload = """admin_password='******'"""
     expected = """admin_password='******'"""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'admin_password' with spaces
     payload = """admin_password = '******'"""
     expected = """admin_password = '******'"""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'admin_password' with double quotes
     payload = """admin_password = "******""""
     expected = """admin_password = "******""""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'password' w/o spaces
     payload = """password='******'"""
     expected = """password='******'"""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'password' with spaces
     payload = """password = '******'"""
     expected = """password = '******'"""
     self.assertEqual(expected, strutils.mask_password(payload))
     # Test 'password' with double quotes
     payload = """password = "******""""
     expected = """password = "******""""
     self.assertEqual(expected, strutils.mask_password(payload))
예제 #36
0
파일: wsgi.py 프로젝트: FengYang/nova
    def _process_stack(self, request, action, action_args,
                       content_type, body, accept):
        """Implement the processing stack."""

        # Get the implementing method
        try:
            meth, extensions = self.get_method(request, action,
                                               content_type, body)
        except (AttributeError, TypeError):
            return Fault(webob.exc.HTTPNotFound())
        except KeyError as ex:
            msg = _("There is no such action: %s") % ex.args[0]
            return Fault(webob.exc.HTTPBadRequest(explanation=msg))
        except exception.MalformedRequestBody:
            msg = _("Malformed request body")
            return Fault(webob.exc.HTTPBadRequest(explanation=msg))

        if body:
            msg = _("Action: '%(action)s', calling method: %(meth)s, body: "
                    "%(body)s") % {'action': action,
                                   'body': unicode(body, 'utf-8'),
                                   'meth': str(meth)}
            LOG.debug(strutils.mask_password(msg))
        else:
            LOG.debug("Calling method '%(meth)s'",
                      {'meth': str(meth)})

        # Now, deserialize the request body...
        try:
            contents = {}
            if self._should_have_body(request):
                # allow empty body with PUT and POST
                if request.content_length == 0:
                    contents = {'body': None}
                else:
                    contents = self.deserialize(meth, content_type, body)
        except exception.InvalidContentType:
            msg = _("Unsupported Content-Type")
            return Fault(webob.exc.HTTPBadRequest(explanation=msg))
        except exception.MalformedRequestBody:
            msg = _("Malformed request body")
            return Fault(webob.exc.HTTPBadRequest(explanation=msg))

        # Update the action args
        action_args.update(contents)

        project_id = action_args.pop("project_id", None)
        context = request.environ.get('nova.context')
        if (context and project_id and (project_id != context.project_id)):
            msg = _("Malformed request URL: URL's project_id '%(project_id)s'"
                    " doesn't match Context's project_id"
                    " '%(context_project_id)s'") % \
                    {'project_id': project_id,
                     'context_project_id': context.project_id}
            return Fault(webob.exc.HTTPBadRequest(explanation=msg))

        # Run pre-processing extensions
        response, post = self.pre_process_extensions(extensions,
                                                     request, action_args)

        if not response:
            try:
                with ResourceExceptionHandler():
                    action_result = self.dispatch(meth, request, action_args)
            except Fault as ex:
                response = ex

        if not response:
            # No exceptions; convert action_result into a
            # ResponseObject
            resp_obj = None
            if type(action_result) is dict or action_result is None:
                resp_obj = ResponseObject(action_result)
            elif isinstance(action_result, ResponseObject):
                resp_obj = action_result
            else:
                response = action_result

            # Run post-processing extensions
            if resp_obj:
                # Do a preserialize to set up the response object
                serializers = getattr(meth, 'wsgi_serializers', {})
                resp_obj._bind_method_serializers(serializers)
                if hasattr(meth, 'wsgi_code'):
                    resp_obj._default_code = meth.wsgi_code
                resp_obj.preserialize(accept, self.default_serializers)

                # Process post-processing extensions
                response = self.post_process_extensions(post, resp_obj,
                                                        request, action_args)

            if resp_obj and not response:
                response = resp_obj.serialize(request, accept,
                                              self.default_serializers)

        if hasattr(response, 'headers'):

            for hdr, val in response.headers.items():
                # Headers must be utf-8 strings
                response.headers[hdr] = utils.utf8(str(val))

        return response
    def test_mask_password(self):
        payload = "test = 'password'  :   'aaaaaa'"
        expected = "test = 'password'  :   '111'"
        self.assertEqual(expected,
                         strutils.mask_password(payload, secret='111'))

        payload = 'mysqld --password "aaaaaa"'
        expected = 'mysqld --password "****"'
        self.assertEqual(expected,
                         strutils.mask_password(payload, secret='****'))

        payload = 'mysqld --password aaaaaa'
        expected = 'mysqld --password ???'
        self.assertEqual(expected,
                         strutils.mask_password(payload, secret='???'))

        payload = 'mysqld --password = "******"'
        expected = 'mysqld --password = "******"'
        self.assertEqual(expected,
                         strutils.mask_password(payload, secret='****'))

        payload = "mysqld --password = '******'"
        expected = "mysqld --password = '******'"
        self.assertEqual(expected,
                         strutils.mask_password(payload, secret='****'))

        payload = "mysqld --password = aaaaaa"
        expected = "mysqld --password = ****"
        self.assertEqual(expected,
                         strutils.mask_password(payload, secret='****'))

        payload = "test = password =   aaaaaa"
        expected = "test = password =   111"
        self.assertEqual(expected,
                         strutils.mask_password(payload, secret='111'))

        payload = "test = password=   aaaaaa"
        expected = "test = password=   111"
        self.assertEqual(expected,
                         strutils.mask_password(payload, secret='111'))

        payload = "test = password =aaaaaa"
        expected = "test = password =111"
        self.assertEqual(expected,
                         strutils.mask_password(payload, secret='111'))

        payload = "test = password=aaaaaa"
        expected = "test = password=111"
        self.assertEqual(expected,
                         strutils.mask_password(payload, secret='111'))

        payload = 'test = "original_password" : "aaaaaaaaa"'
        expected = 'test = "original_password" : "***"'
        self.assertEqual(expected, strutils.mask_password(payload))

        payload = 'test = "param1" : "value"'
        expected = 'test = "param1" : "value"'
        self.assertEqual(expected, strutils.mask_password(payload))

        payload = """{'adminPass':'******'}"""
        payload = six.text_type(payload)
        expected = """{'adminPass':'******'}"""
        self.assertEqual(expected, strutils.mask_password(payload))

        payload = ("test = 'node.session.auth.password','-v','mypassword',"
                   "'nomask'")
        expected = ("test = 'node.session.auth.password','-v','***',"
                    "'nomask'")
        self.assertEqual(expected, strutils.mask_password(payload))

        payload = ("test = 'node.session.auth.password', '--password', "
                   "'mypassword', 'nomask'")
        expected = ("test = 'node.session.auth.password', '--password', "
                    "'***', 'nomask'")
        self.assertEqual(expected, strutils.mask_password(payload))

        payload = ("test = 'node.session.auth.password', '--password', "
                   "'mypassword'")
        expected = ("test = 'node.session.auth.password', '--password', "
                    "'***'")
        self.assertEqual(expected, strutils.mask_password(payload))

        payload = "test = node.session.auth.password -v mypassword nomask"
        expected = "test = node.session.auth.password -v *** nomask"
        self.assertEqual(expected, strutils.mask_password(payload))

        payload = ("test = node.session.auth.password --password mypassword "
                   "nomask")
        expected = ("test = node.session.auth.password --password *** "
                    "nomask")
        self.assertEqual(expected, strutils.mask_password(payload))

        payload = ("test = node.session.auth.password --password mypassword")
        expected = ("test = node.session.auth.password --password ***")
        self.assertEqual(expected, strutils.mask_password(payload))

        payload = "test = cmd --password my\xe9\x80\x80pass"
        expected = ("test = cmd --password ***")
        self.assertEqual(expected, strutils.mask_password(payload))