Ejemplo n.º 1
0
def get_sentry_client(username,
                      client_class,
                      exempt_host=None,
                      component=None):
    server = None
    if is_ha_enabled():
        servers = _get_server_properties(exempt_host=exempt_host)
        if servers:
            server = random.choice(servers)

    if server is None:
        if HOSTNAME.get() and PORT.get():
            LOG.info(
                'No Sentry servers configured in %s, falling back to libsentry configured host: %s:%s'
                % (_CONF_SENTRY_SERVER_RPC_ADDRESSES, HOSTNAME.get(),
                   PORT.get()))
            server = {'hostname': HOSTNAME.get(), 'port': PORT.get()}
        else:
            raise PopupException(_('No Sentry servers are configured.'))

    if component:
        client = client_class(server['hostname'],
                              server['port'],
                              username,
                              component=component)
    else:
        client = client_class(server['hostname'], server['port'], username)

    return client
Ejemplo n.º 2
0
  def test_get_collections(self):
    client = SentryClient(HOSTNAME.get(), PORT.get(), 'test')
    resp = client.list_sentry_roles_by_group() # Non Sentry Admin can do that
    assert_not_equal(0, resp.status.value, resp)
    assert_true('denied' in resp.status.message, resp)

    resp = client.list_sentry_roles_by_group(groupName='*')
    assert_equal(0, resp.status.value, resp)
Ejemplo n.º 3
0
  def setup_class(cls):

    if not is_live_cluster():
      raise SkipTest()

    cls.client = make_logged_in_client(username='******', is_superuser=False)
    cls.user = User.objects.get(username='******')
    add_to_group('test')
    grant_access("test", "test", "libsentry")

    cls.db = SentryClient(HOSTNAME.get(), PORT.get(), 'test')
Ejemplo n.º 4
0
    def setup_class(cls):

        if not is_live_cluster():
            raise SkipTest()

        cls.client = make_logged_in_client(username='******', is_superuser=False)
        cls.user = User.objects.get(username='******')
        add_to_group('test')
        grant_access("test", "test", "libsentry")

        cls.db = SentryClient(HOSTNAME.get(), PORT.get(), 'test')
Ejemplo n.º 5
0
def _get_client(username):
    if get_sentry_server_ha_enabled():
        servers = _get_server_properties()
        if servers:
            server = random.choice(servers)
        else:
            raise PopupException(_('No Sentry servers are available.'))
    else:
        server = {'hostname': HOSTNAME.get(), 'port': PORT.get()}

    return SentryClient(server['hostname'], server['port'], username)
Ejemplo n.º 6
0
def _get_client(username):
    if get_sentry_server_ha_enabled():
        servers = _get_server_properties()
        if servers:
            server = random.choice(servers)
        else:
            raise PopupException(_("No Sentry servers are available."))
    else:
        server = {"hostname": HOSTNAME.get(), "port": PORT.get()}

    return SentryClient(server["hostname"], server["port"], username)
Ejemplo n.º 7
0
def _get_client(username):
  if get_sentry_server_ha_enabled():
    servers = _get_server_properties()
    if servers:
      server = random.choice(servers)
    else:
      raise PopupException(_('No Sentry servers are available.'))
  else:
    server = {
        'hostname': HOSTNAME.get(),
        'port': PORT.get()
    }

  return SentryClient(server['hostname'], server['port'], username)
Ejemplo n.º 8
0
  def setup_class(cls):

    if not is_live_cluster():
      raise SkipTest('Sentry tests require a live sentry server')

    if not os.path.exists(os.path.join(SENTRY_CONF_DIR.get(), 'sentry-site.xml')):
      raise SkipTest('Could not find sentry-site.xml, skipping sentry tests')

    cls.client = make_logged_in_client(username='******', is_superuser=False)
    cls.user = User.objects.get(username='******')
    add_to_group('test')
    grant_access("test", "test", "libsentry")

    cls.db = SentryClient(HOSTNAME.get(), PORT.get(), 'test')
Ejemplo n.º 9
0
def get_sentry_server(current_host=None):
    '''
  Returns the next Sentry server if current_host is set, or a random server if current_host is None.
    If servers contains a single server, the server will be set to the same current_host.
    If servers is None, attempts to fallback to libsentry configs, else raises exception.
  @param current_host: currently set host, if any
  @return: server dict with hostname and port key/values
  '''
    if is_ha_enabled():
        servers = get_sentry_servers()
        hosts = [s['hostname'] for s in servers]

        next_idx = random.randint(0, len(servers) - 1)
        if current_host is not None and hosts:
            try:
                current_idx = hosts.index(current_host)
                LOG.debug("Current Sentry host, %s, index is: %d." %
                          (current_host, current_idx))
                next_idx = (current_idx + 1) % len(servers)
            except ValueError as e:
                LOG.warn("Current host: %s not found in list of servers: %s" %
                         (current_host, ','.join(hosts)))

        server = servers[next_idx]
        LOG.debug("Returning Sentry host, %s, at next index: %d." %
                  (server['hostname'], next_idx))
    else:
        if HOSTNAME.get() and PORT.get():
            LOG.info(
                'No Sentry servers configured in %s, falling back to libsentry configured host: %s:%s'
                % (_CONF_SENTRY_SERVER_RPC_ADDRESSES, HOSTNAME.get(),
                   PORT.get()))
            server = {'hostname': HOSTNAME.get(), 'port': PORT.get()}
        else:
            raise PopupException(_('No Sentry servers are configured.'))

    return server
Ejemplo n.º 10
0
def get_sentry_client(username,
                      client_class,
                      exempt_host=None,
                      component=None,
                      retries=0,
                      seed=None):
    server = None

    if is_ha_enabled():
        servers = _get_server_properties(exempt_host=exempt_host)
        seed_function = lambda: seed if seed else random.random()

        random.shuffle(servers, seed_function)
        if servers and retries < len(servers):
            server = servers[retries]
        else:
            raise PopupException(
                _('Tried %s Sentry servers HA, none are available.') % retries)
    else:
        if HOSTNAME.get() and PORT.get():
            LOG.info(
                'No Sentry servers configured in %s, falling back to libsentry configured host: %s:%s'
                % (_CONF_SENTRY_SERVER_RPC_ADDRESSES, HOSTNAME.get(),
                   PORT.get()))
            server = {'hostname': HOSTNAME.get(), 'port': PORT.get()}
        else:
            raise PopupException(_('No Sentry servers are configured.'))

    if component:
        client = client_class(server['hostname'],
                              server['port'],
                              username,
                              component=component)
    else:
        client = client_class(server['hostname'], server['port'], username)

    return client
Ejemplo n.º 11
0
def _get_server_properties(exempt_host=None):
  try:
    servers = []
    sentry_servers = get_sentry_server_rpc_addresses()
    for server in sentry_servers:
      host = server
      if ':' in server:
        host, port = server.split(':')
      elif get_sentry_server_rpc_port():
        port = get_sentry_server_rpc_port()
      else:
        port = PORT.get()
      if host != exempt_host:
        servers.append({'hostname': host, 'port': int(port)})
  except Exception, e:
    raise PopupException(_('Error in retrieving Sentry server properties.'), detail=e)
Ejemplo n.º 12
0
    def setup_class(cls):

        if not is_live_cluster():
            raise SkipTest('Sentry tests require a live sentry server')

        if not os.path.exists(
                os.path.join(SENTRY_CONF_DIR.get(), 'sentry-site.xml')):
            raise SkipTest(
                'Could not find sentry-site.xml, skipping sentry tests')

        cls.client = make_logged_in_client(username='******', is_superuser=False)
        cls.user = User.objects.get(username='******')
        add_to_group('test')
        grant_access("test", "test", "libsentry")

        cls.db = SentryClient(HOSTNAME.get(), PORT.get(), 'test')
Ejemplo n.º 13
0
def get_sentry_servers():
    try:
        servers = []
        sentry_servers = get_sentry_server_rpc_addresses()
        for server in sentry_servers:
            host = server
            if ':' in server:
                host, port = server.split(':')
            elif get_sentry_server_rpc_port():
                port = get_sentry_server_rpc_port()
            else:
                port = PORT.get()
            servers.append({'hostname': host, 'port': int(port)})
    except Exception as e:
        raise PopupException(
            _('Error in retrieving Sentry server properties.'), detail=e)

    LOG.debug(
        "Sentry servers are: %s" %
        ', '.join(['%s:%d' % (s['hostname'], s['port']) for s in servers]))
    return servers
Ejemplo n.º 14
0
Archivo: api.py Proyecto: Ile2/hue
def get_api(user):
  return SentryApi(SentryClient(HOSTNAME.get(), PORT.get(), user.username))
Ejemplo n.º 15
0
def get_api(user):
    return SentryApi(SentryClient(HOSTNAME.get(), PORT.get(), user.username))
Ejemplo n.º 16
0
        next_idx = random.randint(0, len(servers) - 1)
        if current_host is not None and hosts:
            try:
                current_idx = hosts.index(current_host)
                LOG.debug("Current Sentry host, %s, index is: %d." %
                          (current_host, current_idx))
                next_idx = (current_idx + 1) % len(servers)
            except ValueError, e:
                LOG.warn("Current host: %s not found in list of servers: %s" %
                         (current_host, ','.join(hosts)))

        server = servers[next_idx]
        LOG.debug("Returning Sentry host, %s, at next index: %d." %
                  (server['hostname'], next_idx))
    else:
        if HOSTNAME.get() and PORT.get():
            LOG.info(
                'No Sentry servers configured in %s, falling back to libsentry configured host: %s:%s'
                % (_CONF_SENTRY_SERVER_RPC_ADDRESSES, HOSTNAME.get(),
                   PORT.get()))
            server = {'hostname': HOSTNAME.get(), 'port': PORT.get()}
        else:
            raise PopupException(_('No Sentry servers are configured.'))

    return server


def get_sentry_servers():
    try:
        servers = []
        sentry_servers = get_sentry_server_rpc_addresses()