Database Backups and Security · Lesson 60 of 112

Database Users, Roles and Permissions

What you will learn

  • Authentication and authorisation
  • The principle of least privilege
  • What users, roles and privileges are
  • How to limit access with a read-only connection and a view

1. Who are you, and what may you do

Authentication proves who someone is (a user name and password, a key). Authorisation decides what that person may do. A database needs both.

The safest rule is least privilege: every person and every program gets only the access it needs. A report that only reads data should not be able to delete it. A website that only inserts orders does not need to drop tables.

2. Users, roles and privileges

Server databases such as PostgreSQL have their own users and permissions:

  • A privilege is a permission, such as reading (SELECT), adding (INSERT), changing (UPDATE) or removing (DELETE) rows in a table.
  • A role or user is an account that can hold privileges.
  • Giving a privilege is done with GRANT, and taking it away with REVOKE.

The PostgreSQL documentation describes GRANT as defining access privileges, and lists privileges including SELECT, INSERT, UPDATE and DELETE. A statement in that style looks like this (it is shown, not run, because SQLite has no users):

GRANT SELECT ON orders TO reporting;

Meaning: the role reporting may read the orders table and do nothing else. The exact commands, role rules and defaults differ between database products, so read the manual of the one you use before relying on them.

Good practice:

  • Give each application its own account, not the administrator account.
  • Use roles for groups of people (for example "reporting") rather than setting every person up by hand.
  • Remove accounts and privileges when people leave.
  • Use strong, unique passwords and never put them in code that is shared.

3. SQLite: no users, but read-only and views

SQLite has no user accounts; whoever can open the file has the access the file system allows. You can still limit what a program can do.

This lesson runs on the sample database you chose in lesson 4.1 (use the Example data picker at the top). Run the examples from top to bottom; Reset data starts again.

Make the connection read-only. A program can open a database file read-only (in Python: sqlite3.connect("file:c.db?mode=ro", uri=True)). Inside SQL, PRAGMA query_only = ON does the same for the rest of the session:

PRAGMA query_only = ON;
(done)
INSERT INTO categories (name) VALUES ('Pets');
OperationalError: attempt to write a readonly database
PRAGMA query_only = ON;
(done)
INSERT INTO branches (name, city) VALUES ('Hilltop', 'Oslo');
OperationalError: attempt to write a readonly database
PRAGMA query_only = ON;
(done)
INSERT INTO departments (name) VALUES ('Music');
OperationalError: attempt to write a readonly database

Reading still works, and PRAGMA query_only = OFF turns writing back on.

PRAGMA query_only = OFF;
(done)
PRAGMA query_only = OFF;
(done)
PRAGMA query_only = OFF;
(done)

Show only some columns with a view (lesson 4.7). A view is a saved query. The table has private columns; the view shows only the safe ones:

The customers table has e-mail addresses and phone numbers. The view shows only the id, the first name and the city:

CREATE VIEW public_customers AS SELECT customer_id, first_name, city FROM customers;
SELECT * FROM public_customers ORDER BY customer_id LIMIT 3;
customer_id  first_name  city
-----------  ----------  -----
          1  Vik         Leeds
          2  Wanda       Leeds
          3  Dana        Cairo
(3 rows)

The customers table has birth dates. The view shows only the id, the name and the city:

CREATE VIEW public_customers AS SELECT customer_id, full_name, city FROM customers;
SELECT * FROM public_customers ORDER BY customer_id LIMIT 3;
customer_id  full_name    city
-----------  -----------  -------
          1  Chen Park    Lima
          2  Rafi Uddin   Manila
          3  Grace Kamau  Toronto
(3 rows)

The students table has birth dates, e-mail addresses and guardians' names. The view shows only the id and the names:

CREATE VIEW public_students AS SELECT student_id, first_name, last_name FROM students;
SELECT * FROM public_students ORDER BY student_id LIMIT 3;
student_id  first_name  last_name
----------  ----------  ---------
         1  Vera        Khan
         2  Elena       Haque
         3  Chloe       Garcia
(3 rows)

A program that only queries the view never sees the private columns. (In SQLite this limits what your own code asks for; it is not a security wall against someone who can open the file itself. Server databases can enforce it with privileges.)

Worked example: three accounts for one shop

Account Used by Access
shop_web the website read products; add orders; no deleting
reporting the owner's charts read only
admin the owner, for changes full access, used rarely and never by the website

If the website has a bug or is attacked, the damage is limited to what shop_web can do.

Common mistakes

  • Using the administrator account in the application.
  • Sharing one password among many people.
  • Never removing old accounts.
  • Mixing up authentication and authorisation.

Key takeaways

  • Authentication proves who you are; authorisation limits what you can do.
  • Give the least access that gets the job done.
  • Server databases use users or roles with GRANT and REVOKE.
  • In SQLite, use read-only connections and views to limit what code can do.

Practice

1. What is the difference between authentication and authorisation?

Show answer

Authentication proves who you are; authorisation decides what you may do.

2. What is the principle of least privilege?

Show answer

Give each person and program only the access it needs.

3. What does GRANT SELECT ON orders TO reporting; allow?

Show answer

The role reporting can read rows from the orders table (and, by this statement, nothing more).

4. Why should a website not use the administrator account?

Show answer

A bug or attack on the website would then have full power over the database.

5. How can a SQLite program be stopped from writing?

Show answer

Open it read-only (mode=ro); writes then fail with attempt to write a readonly database.

6. What did the public_users view hide?

Show answer

The email and is_admin columns.