Database Administration: User Management & Database Tuning

Phase 4


Overview

In this phase, we add features to the FabFlixs database to allow for different user groups (each with appropriate permissions) and to tune the database.


References


What to Turn In

In one ZIP file, labeled with your team name, your updated employee access system, your tuning report and a README file explaining how to compile, load, and otherwise prepare your system for use, and how to run it; make sure the file's extension is ZIP.


Terminology

Attributes: permissions that can be given to, or taken away from, a PostgreSQL ROLE, managed with the CREATE ROLE and ALTER ROLE commands.

Privileges: access and use permissions to the database and its objects (tables, functions, langauges, etc.) that can be given to, or taken from, a PostgreSQL ROLE, managed with the GRANT and REVOKE commands.


User Groups; Permissions Management

Required is adding user groups and to manage them with the "back-end program" (the program first developed in Phase 1); those groups and their capabilities are

The superuser and communication security

A superuser is a user, a role with the login privilege, that has all privileges, including the privilege of creating other roles, including users. To create FabFlixs user groups, the database must first have at least one superuser, which it does: the superuser 'postgres', created when you installed PostgreSQL.

If you wish to create another superuser, say one called root, log into the database as postgres and issue the SQL command

CREATE ROLE root WITH PASSWORD 'rootpassword' SUPERUSER CREATEROLE CREATEDB LOGIN;

make sure you have this testing environment "locked down" so that only authorized computers can get acess to FabFlixs. The default installation of PostgreSQL allows only localhost access, typically what is desired at this stage. If you changed that access (e.g., in Phase 0), double check to ensure that only authorized computers will get to FabFlixs (see Enabling remote computers to communicate with the PostgreSQL server, in Phase 0, for details.

User and group Management

You are required to implement the new Permissions Management feature in the backend program. It allows a user in the DB Staff group to view users and user groups along with their attributes and privileges. It allows a DBA to not only view these settings, but to change them. The DBA can also add and remove users, move users to another group and change attributes and privileges; s/he cannot, however, add or remove groups. More specifically, the feature provides these capabilities:

  1. Create new users and place them in a group: DBA only.

  2. A DBA can create a new user and add her/him to one of the three user groups, or move an existing user to a group of which s/he is not currently a member. A new member of a group should have all and only the permissions of that group. Since these groups comprise all staff, and group permissions are cumulative (that is, since DBA has all the permisssions for DB Staff, and DB Staff has all the permissions of FabFlixs Staff), a user must be a member of a group, and of only one group. This implies that when a user is moved to a new group, that user is removed from her/his current group.)

  3. Modify group attributes: DBA only.

    A member of the DBA group can alter the attributes and privileges of any of the three user groups. As a wrong setting can seriously compromise database security, this feature must have verification checks passed (e.g., "Are you sure?"-type questions answered yes) before the change is made.

  4. Remove users: DBA only.

    Deletes a user from the FabFlixs system (including removing said from its group); a verfication check is required.

  5. Display roles and their attributes and privileges (DBA and DB Staff only)

    The staff member can repeatedly choose to view the attributes and privileges of any user or any particular group, until a command is given to exit the feature. The display of information must, of course, be easy for a DBA or DB Staff (that is, someone with their assumed expertise) to read and understand. (Yes, it is the case that a user's attributes and privileges are completely determined by her/his group, and so one could just display a user's group and issue another command to view the group's attributes and privileges if need be—but that is cumbersome. Displaying all information about a user at once is more efficacious.)

The three user groups are permanent; therefore, there should be no way within this function to delete a group.

To enforce the security these functions implement, the arbitrary SQL command function must now be limited. Required is that only DBAs can use the feature. Desired, but not required for full credit (which means that if you do it you can earn credit that can offset points lost for other reasons) is a more nuanced restriction: DBAs can issue any command, but DB Staff and FabFlixs Staff can only issue those arbitrary SQL commands that are consistent with the permissions and access of those groups, as described above.

The exact look and feel of the Permissions Management feature is up to you, but it should be consistent with the interface used in the rest of the program.

Regardless of the interface, the Permissions Management feature should act like its own mini-system: the user enters into it, sees a menu of choices, picks and executes a choice, then goes back to the menu; this continues until some sort of exit command is issued to take the user out of Permissions Management and back to the main FabFlixs menu.

The back-end user should only "see" those functions and features her/his group are allowed to employ. For instance, if the user who logged into this system is DB Staff, that user only sees menu options that allow for viewing of permissions information and specifically does not see options that would add, delete, or modify this information; if the user is FabFlixs staff, s/he does not see a menu option for the permissions subsystem, for the arbitrary SQL function, or for any other function s/he is not allowed to use.

Some hints and suggestions:

Some Technical Points

To display the information required in the above-described functions, or to show us during your demonstration that changes made to roles by your program are actually appearing in the database, you will need to access certain of PostgreSQL's internal tables of metadata, called catalogs (see the references, above). In particular, you will likely find useful information in the following catalogs:

The pg_class and pg_proc tables have information about both system and user tables, and both standard and user PostgreSQL functions, respectively. We are only interested in user-created tables and functions, i.e., your interface should allow authorized users to change user privileges only on user-created tables and functions: These all have the namespace public.

Do not directly change the information in these catalogs, as you could (quite easily) break the database. Instead, call upon GRANT, REVOKE, ALTER, CREATE and DROP as appropriate; those commands will update the catalogs.

You will need to log in as a superuser if you want unfettered access to all the roles, along with their attributes and privileges. In psql, you can use the \dg command to display roles and the \z command to display information about existing privileges. pgAdmin also can display this information.

When accessing a function, you need to provide its name and its argument types; as postgreSQL allows function overloading, the arguments are needed to uniquely define which function is being used. So, for instance, one says

GRANT EXECUTE ON FUNCTION concat(varchar, varchar) TO dbstaff ;

One way to find the arguments of a function is to issue

select proname, nspname, proargtypes 
from pg_proc, pg_namespace 
where pg_proc.pronamespace = pg_namespace.oid and
pg_namespace.nspname='public' ;

which returns a table such as

 
      proname        | nspname |               proargtypes 
----------------------+---------+------------------------------------------------- 
add_one               | public  |                                              23 
add_one               | public  |                                       1043 1043 
concat                | public  |                                       1043 1043 
plpgsql_call_handler  | public  | 
add_movie             | public  | 1043 23 1043 1043 1043 1015 1015 1015 1182 1015
(5 rows) 

Now you can look up the argtype of interest, in our case, code 1043 from concat:

		select typname from pg_type where oid=1043;

which returns

typname
--------- 
varchar
(1 row)

Here's a SELECT statement that will provide a table of just user-defined functions:

select proname, nspname
from pg_proc, pg_namespace 
where pg_proc.pronamespace = pg_namespace.oid and pg_namespace.nspname='public' ;

(No, it's not straight-forward, but so it goes...)

Similar queries can be used to identify public tables.


Tuning

As movies and related information are added to the FabFlixs database, customer browsing and searching times may increase to unacceptable levels. So, here we create a large test database, reflective of how big the FabFlixs database is likely to become, and determine if indexing certain database tables is warranted.

Desired, but not required for full credit, are tests with multiple users searching the database simultaneously. This can be done by modifying your back-end system to repeatedly issue a SQL command and then using multiple such back-end systems simultaneously. You can also do it by writing a multi-threaded Java program where each thread queries the database. If you are interested in doing even more sophisticated testing, contact the instructor.


Demonstration

Prior to your demonstration, have at least three users in each of the three user groups.

During the demonstration, first show us that the three default roles have the specified attributes and privileges and that each has at least three users. Further, show us the capabilities of your Permissions Management feature; what you do in what order is up to you, but be sure to demonstrate Permissions Management capabilities thoroughly. While showing us these features, also show us that changes you made using the program really did end up in the database. For instance, you could have an open window with pgAdmin running and, after doing some work using the Permissions Management feature, issue pgAdmin commands to show us that the changes were indeed made to the database.

Also, briefly explain the results of your tuning tests and your recommendations regarding indexing of the database.

Written Chen Li as "Project 4" for ICS185 Fall 2005.
Revised by Norman Jacobson for the Spring 2005 offering of ICS185, May 2005.
Revised by Norman Jacobson with assistance from Michael Sirivianos, for the Winter 2006 offering of ICS185,
  to include FabFlix user groups, and to reflect the use of postgres 8.1, including the use of "roles," February 2006.
Minor revisions by Norman Jacobson primarily to reflect 8.1 syntax, and to improve clarity, for CS122B Spring 2007, May 2007.
  Includes some updates made in the CS122B Winter 2007 version of this exercise.
Minor edits by Norman Jacobson, September and November 2007.
Revised to clarify requirements and meanings of 'attributes' and 'privileges,' by Norman Jacobson, December 2007.
Revised to reflect using PostgreSQL 8.3.3 under Windows to to correct minor typos, by Norman Jacobson, September 2008.
Revised to include READ.ME requirement, by Norman Jacobson, December 2008.
Revised to suggest System.nanoTime() for timings; some minor polishing, by Norman Jacobson, March 2009.