Beispiel #1
0
    def get_user(self, context, username, hostname):
        """Return a single user matching the criteria.

        The username and hostname parameter are strings.

        The return value is a dictionary in the following form:

            {"_name": "", "_host": None, "_password": None,
             "_databases": [{"_name": ""}, ...]}

        Where "_databases" is a list of databases the user has access to.
        """
        results = pgutil.query(
            pgutil.UserQuery.get(name=username),
            timeout=30,
        )
        results = tuple(results)
        if len(results) < 1:
            return None

        return {
            "_name": results[0][0],
            "_host": None,
            "_password": None,
            "_databases": self.list_access(context, username, None),
        }
Beispiel #2
0
    def is_root_enabled(self, context):
        """Return True if there is a superuser account enabled.
        """
        results = pgutil.query(pgutil.UserQuery.list_root(), timeout=30)

        # There should be only one superuser (Trove's administrative account).
        return len(results) > 1 or (results[0] != self.ADMIN_USER)
    def _run_pre_backup(self):
        self.backup_label = self.base_filename
        r = pgutil.query("SELECT pg_start_backup('%s', true)" %
                         self.backup_label)
        self.start_segment = r[0][0]

        r = pgutil.query("SELECT pg_xlogfile_name('%s')" % self.start_segment)
        self.start_wal_file = r[0][0]

        r = pgutil.query("SELECT pg_stop_backup()")
        self.stop_segment = r[0][0]

        # We have to hack this because self.command is
        # initialized in the base class before we get here, which is
        # when we will know exactly what WAL files we want to archive
        self.command = self._cmd()
Beispiel #4
0
 def _get_users(self, context):
     """Return all non-system Postgres users on the instance."""
     results = pgutil.query(
         pgutil.UserQuery.list(ignore=cfg.get_ignored_users()),
         timeout=30,
     )
     return [self._build_user(context, row[0].strip()) for row in results]
Beispiel #5
0
 def _get_users(self, context):
     """Return all non-system Postgres users on the instance."""
     results = pgutil.query(
         pgutil.UserQuery.list(ignore=cfg.get_ignored_users()),
         timeout=30,
     )
     return [self._build_user(context, row[0].strip()) for row in results]
    def _run_pre_backup(self):
        self.backup_label = self.base_filename
        r = pgutil.query("SELECT pg_start_backup('%s', true)" %
                         self.backup_label)
        self.start_segment = r[0][0]

        r = pgutil.query("SELECT pg_xlogfile_name('%s')" % self.start_segment)
        self.start_wal_file = r[0][0]

        r = pgutil.query("SELECT pg_stop_backup()")
        self.stop_segment = r[0][0]

        # We have to hack this because self.command is
        # initialized in the base class before we get here, which is
        # when we will know exactly what WAL files we want to archive
        self.command = self._cmd()
Beispiel #7
0
    def get_user(self, context, username, hostname):
        """Return a single user matching the criteria.

        The username and hostname parameter are strings.

        The return value is a dictionary in the following form:

            {"_name": "", "_host": None, "_password": None,
             "_databases": [{"_name": ""}, ...]}

        Where "_databases" is a list of databases the user has access to.
        """
        results = pgutil.query(
            pgutil.UserQuery.get(name=username),
            timeout=30,
        )
        results = tuple(results)
        if len(results) < 1:
            return None

        return {
            "_name": results[0][0],
            "_host": None,
            "_password": None,
            "_databases": self.list_access(context, username, None),
        }
Beispiel #8
0
    def user_exists(self, username):
        """Return whether a given user exists on the instance."""
        results = pgutil.query(
            pgutil.UserQuery.get(name=username),
            timeout=30,
        )

        return bool(results)
Beispiel #9
0
    def user_exists(self, username):
        """Return whether a given user exists on the instance."""
        results = pgutil.query(
            pgutil.UserQuery.get(name=username),
            timeout=30,
        )

        return bool(results)
Beispiel #10
0
 def _get_databases_for(self, username):
     """Return all Postgres databases accessible by a given user."""
     results = pgutil.query(
         pgutil.AccessQuery.list(user=username),
         timeout=30,
     )
     return [models.PostgreSQLSchema(
         row[0].strip(), character_set=row[1], collate=row[2])
         for row in results]
Beispiel #11
0
 def _get_databases(self):
     """Return all non-system Postgres databases on the instance."""
     results = pgutil.query(
         pgutil.DatabaseQuery.list(ignore=IGNORE_DBS_LIST),
         timeout=30,
     )
     return [models.PostgreSQLSchema(
         row[0].strip(), character_set=row[1], collate=row[2])
         for row in results]
Beispiel #12
0
    def is_root_enabled(self, context):
        """Return True if there is a superuser account enabled.
        """
        results = pgutil.query(
            pgutil.UserQuery.list_root(),
            timeout=30,
        )

        # There should be only one superuser (Trove's administrative account).
        return len(results) > 1 or (results[0][0] != self.ADMIN_USER)
Beispiel #13
0
 def _get_databases_for(self, username):
     """Return all Postgres databases accessible by a given user."""
     results = pgutil.query(
         pgutil.AccessQuery.list(user=username),
         timeout=30,
     )
     return [
         models.PostgreSQLSchema(row[0].strip(),
                                 character_set=row[1],
                                 collate=row[2]) for row in results
     ]
Beispiel #14
0
    def list_users(
            self,
            context,
            limit=None,
            marker=None,
            include_marker=False,
    ):
        """List all users on the instance along with their access permissions.

        Return value is a list of dictionaries in the following form:

            [{"_name": "", "_password": None, "_host": None,
              "_databases": [{"_name": ""}, ...]}, ...]
        """
        results = pgutil.query(
            pgutil.UserQuery.list(ignore=cfg.get_ignored_users(
                manager='postgresql')),
            timeout=30,
        )
        # Convert results into dictionaries.
        results = (
            {
                '_name': r[0].strip(),
                '_password': None,
                '_host': None,
                '_databases': self.list_access(context, r[0], None),
            }
            for r in results
        )

        # Force __iter__ of generator until marker found.
        if marker is not None:
            try:
                item = next(results)
                while item['_name'] != marker:
                    item = next(results)
            except StopIteration:
                pass

        remainder = None
        if limit is not None:
            remainder = results
            results = itertools.islice(results, limit)

        results = tuple(results)

        next_marker = None
        if remainder is not None:
            try:
                next_marker = next(remainder)
            except StopIteration:
                pass

        return results, next_marker
Beispiel #15
0
    def list_users(
            self,
            context,
            limit=None,
            marker=None,
            include_marker=False,
    ):
        """List all users on the instance along with their access permissions.

        Return value is a list of dictionaries in the following form:

            [{"_name": "", "_password": None, "_host": None,
              "_databases": [{"_name": ""}, ...]}, ...]
        """
        results = pgutil.query(
            pgutil.UserQuery.list(ignore=cfg.get_ignored_users(
                manager='postgresql')),
            timeout=30,
        )
        # Convert results into dictionaries.
        results = (
            {
                '_name': r[0].strip(),
                '_password': None,
                '_host': None,
                '_databases': self.list_access(context, r[0], None),
            }
            for r in results
        )

        # Force __iter__ of generator until marker found.
        if marker is not None:
            try:
                item = results.next()
                while item['_name'] != marker:
                    item = results.next()
            except StopIteration:
                pass

        remainder = None
        if limit is not None:
            remainder = results
            results = itertools.islice(results, limit)

        results = tuple(results)

        next_marker = None
        if remainder is not None:
            try:
                next_marker = remainder.next()
            except StopIteration:
                pass

        return results, next_marker
Beispiel #16
0
 def _get_databases(self):
     """Return all non-system Postgres databases on the instance."""
     results = pgutil.query(
         pgutil.DatabaseQuery.list(ignore=IGNORE_DBS_LIST),
         timeout=30,
     )
     return [
         models.PostgreSQLSchema(row[0].strip(),
                                 character_set=row[1],
                                 collate=row[2]) for row in results
     ]
Beispiel #17
0
    def _find_user(self, context, username):
        """Lookup a user with a given username.
        Return a new Postgres user instance or raise if no match is found.
        """
        results = pgutil.query(
            pgutil.UserQuery.get(name=username),
            timeout=30,
        )

        if results:
            return self._build_user(context, username)

        return None
Beispiel #18
0
    def is_root_enabled(self, context):
        """Return True if there is a superuser account enabled.

        This ignores the built-in superuser of postgres and the potential
        system administration superuser of os_admin.
        """
        results = pgutil.query(
            pgutil.UserQuery.list_root(ignore=IGNORE_USERS_LIST),
            timeout=30,
        )
        # Reduce iter of iters to iter of single values.
        results = (r[0] for r in results)
        return len(tuple(results)) > 0
Beispiel #19
0
    def _find_user(self, context, username):
        """Lookup a user with a given username.
        Return a new Postgres user instance or raise if no match is found.
        """
        results = pgutil.query(
            pgutil.UserQuery.get(name=username),
            timeout=30,
        )

        if results:
            return self._build_user(context, username)

        raise exception.UserNotFound()
Beispiel #20
0
    def is_root_enabled(self, context):
        """Return True if there is a superuser account enabled.

        This ignores the built-in superuser of postgres and the potential
        system administration superuser of os_admin.
        """
        results = pgutil.query(
            pgutil.UserQuery.list_root(ignore=IGNORE_USERS_LIST),
            timeout=30,
        )
        # Reduce iter of iters to iter of single values.
        results = (r[0] for r in results)
        return len(tuple(results)) > 0
Beispiel #21
0
    def list_databases(
        self,
        context,
        limit=None,
        marker=None,
        include_marker=False,
    ):
        """List databases created on this instance.

        Return value is a list of dictionaries in the following form:

            [{"_name": "", "_character_set": "", "_collate": ""}, ...]
        """
        results = pgutil.query(
            pgutil.DatabaseQuery.list(ignore=cfg.get_ignored_dbs(
                manager='postgresql')),
            timeout=30,
        )
        # Convert results to dictionaries.
        results = ({
            '_name': r[0].strip(),
            '_character_set': r[1],
            '_collate': r[2]
        } for r in results)
        # Force __iter__ of generator until marker found.
        if marker is not None:
            try:
                item = results.next()
                while item['_name'] != marker:
                    item = results.next()
            except StopIteration:
                pass

        remainder = None
        if limit is not None:
            remainder = results
            results = itertools.islice(results, limit)

        results = tuple(results)

        next_marker = None
        if remainder is not None:
            try:
                next_marker = remainder.next()
            except StopIteration:
                pass

        return results, next_marker
Beispiel #22
0
    def list_databases(
            self,
            context,
            limit=None,
            marker=None,
            include_marker=False,
    ):
        """List databases created on this instance.

        Return value is a list of dictionaries in the following form:

            [{"_name": "", "_character_set": "", "_collate": ""}, ...]
        """
        results = pgutil.query(
            pgutil.DatabaseQuery.list(ignore=cfg.get_ignored_dbs(
                manager='postgresql')),
            timeout=30,
        )
        # Convert results to dictionaries.
        results = (
            {'_name': r[0].strip(), '_character_set': r[1], '_collate': r[2]}
            for r in results
        )
        # Force __iter__ of generator until marker found.
        if marker is not None:
            try:
                item = results.next()
                while item['_name'] != marker:
                    item = results.next()
            except StopIteration:
                pass

        remainder = None
        if limit is not None:
            remainder = results
            results = itertools.islice(results, limit)

        results = tuple(results)

        next_marker = None
        if remainder is not None:
            try:
                next_marker = remainder.next()
            except StopIteration:
                pass

        return results, next_marker
Beispiel #23
0
    def list_access(self, context, username, hostname):
        """List database for which the given user as access.

        The username and hostname parameters are strings.

        Return value is a list of dictionaries in the following form:

            [{"_name": "", "_collate": None, "_character_set": None}, ...]
        """
        results = pgutil.query(
            pgutil.AccessQuery.list(user=username),
            timeout=30,
        )

        # Convert to dictionaries.
        results = (
            {'_name': r[0].strip(), '_collate': None, '_character_set': None}
            for r in results
        )
        return tuple(results)
Beispiel #24
0
    def list_access(self, context, username, hostname):
        """List database for which the given user as access.

        The username and hostname parameters are strings.

        Return value is a list of dictionaries in the following form:

            [{"_name": "", "_collate": None, "_character_set": None}, ...]
        """
        results = pgutil.query(
            pgutil.AccessQuery.list(user=username),
            timeout=30,
        )

        # Convert to dictionaries.
        results = ({
            '_name': r[0].strip(),
            '_collate': None,
            '_character_set': None
        } for r in results)
        return tuple(results)
Beispiel #25
0
 def pg_is_in_recovery(self):
     """Wrapper for pg_is_in_recovery() for detecting a server in
     standby mode
     """
     r = pgutil.query("SELECT pg_is_in_recovery()")
     return r[0][0]
Beispiel #26
0
 def pg_last_xlog_replay_location(self):
     """Wrapper for pg_last_xlog_replay_location()
      For use on standby servers
      """
     r = pgutil.query("SELECT pg_last_xlog_replay_location()")
     return r[0][0]
Beispiel #27
0
 def pg_current_xlog_location(self):
     """Wrapper for pg_current_xlog_location()
     Cannot be used against a running slave
     """
     r = pgutil.query("SELECT pg_current_xlog_location()")
     return r[0][0]
Beispiel #28
0
 def pg_current_xlog_location(self):
     """Wrapper for pg_current_xlog_location()
     Cannot be used against a running slave
     """
     r = pgutil.query("SELECT pg_current_xlog_location()")
     return r[0][0]
Beispiel #29
0
 def pg_last_xlog_replay_location(self):
     """Wrapper for pg_last_xlog_replay_location()
      For use on standby servers
      """
     r = pgutil.query("SELECT pg_last_xlog_replay_location()")
     return r[0][0]
Beispiel #30
0
 def pg_is_in_recovery(self):
     """Wrapper for pg_is_in_recovery() for detecting a server in
     standby mode
     """
     r = pgutil.query("SELECT pg_is_in_recovery()")
     return r[0][0]