{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "899bb06e", "metadata": {}, "outputs": [], "source": [ "import agh_db_lectures\n", "agh_db_lectures.prepare_notebook_for_sql()" ] }, { "cell_type": "markdown", "id": "33596512", "metadata": {}, "source": [ "# Privilege Management" ] }, { "cell_type": "markdown", "id": "bf1306b9", "metadata": {}, "source": [ "Consider the following scenarios.\n", "\n", "> Multiple applications, each with its own database (i.e., catalog) are served by the same DBMS. Every application manages its own database by, e.g., creating tables and migrating the schema to new versions on updates.\n", "\n", "> A database for an enterprise is designed first and then multiple applications are developed to use it. Certain employees (e.g., administrators, managers, analysts) are also given direct (SQL) access to the database." ] }, { "cell_type": "markdown", "id": "f7530a98", "metadata": {}, "source": [ "_notes_\n", "\n", "In the first scenario, each application can have its own user account with the DBMS. Privileges can be configured so that the application is only able to access its own catalog.\n", "\n", "In the second scenario the privileges can be used on a more granular level to grant access to particular tables or particular (kinds of) operations. The concept of user roles becomes useful." ] }, { "cell_type": "markdown", "id": "1c12176f", "metadata": {}, "source": [ "## Managing Users in MariaDB" ] }, { "cell_type": "code", "execution_count": null, "id": "bdeab499", "metadata": {}, "outputs": [], "source": [ "%sql mysql:///mysql?unix_socket=/var/run/mysql/mysql.sock" ] }, { "cell_type": "markdown", "id": "9c022728", "metadata": {}, "source": [ "_notes_\n", "\n", "We are connecting to database `mysql` (which exists by default)." ] }, { "cell_type": "code", "execution_count": null, "id": "d140d6d2", "metadata": { "scrolled": false }, "outputs": [], "source": [ "%%sql\n", "SELECT Host, User, Password FROM user;" ] }, { "cell_type": "markdown", "id": "a5dc4212", "metadata": {}, "source": [ "### User Creation and Privilege Assignment" ] }, { "cell_type": "code", "execution_count": null, "id": "50756371", "metadata": {}, "outputs": [], "source": [ "%%sql\n", "CREATE USER theodore@127.0.0.1 IDENTIFIED BY 'PingVim'" ] }, { "cell_type": "code", "execution_count": null, "id": "f7874366", "metadata": {}, "outputs": [], "source": [ "%%sql\n", "DROP USER theodore@127.0.0.1" ] }, { "cell_type": "markdown", "id": "8bf6c6a0", "metadata": {}, "source": [ "**Relevant Postgres differences**:\n", "- `@host` → `pg_hba.conf`\n", "- `IDENTIFIED BY` → `WITH PASSWORD`\n", "\n", "e.g.,\n", "\n", "```sql\n", "-- No `@', just the Postgres username.\n", "CREATE USER theodore WITH PASSWORD 'datenbank';\n", "```" ] }, { "cell_type": "code", "execution_count": null, "id": "ab47c31e", "metadata": {}, "outputs": [], "source": [ "!cat /var/lib/postgresql/data/pg_hba.conf" ] }, { "cell_type": "markdown", "id": "ec82a017", "metadata": {}, "source": [ "**Back to MariaDB**" ] }, { "cell_type": "code", "execution_count": null, "id": "1af0faf8", "metadata": {}, "outputs": [], "source": [ "%%sql\n", "SELECT PASSWORD('PingVim')" ] }, { "cell_type": "code", "execution_count": null, "id": "7b77ac4f", "metadata": {}, "outputs": [], "source": [ "%%sql\n", "CREATE OR REPLACE USER theodore@127.0.0.1\n", "IDENTIFIED BY PASSWORD '*749882C224A9E49BADB6492AAAE881F371BB7C4E'" ] }, { "cell_type": "markdown", "id": "b30d03ec", "metadata": {}, "source": [ "**Relevant Postgres difference**: `IDENTIFIED BY PASSWORD` → `WITH ENCRYPTED PASSWORD`" ] }, { "cell_type": "code", "execution_count": null, "id": "4f7b5f5c", "metadata": {}, "outputs": [], "source": [ "%%sql\n", "CREATE USER theodore@\"192.168.134.0/255.255.255.0\"\n", "IDENTIFIED BY 'S3cur3 Pa$$word3'" ] }, { "cell_type": "markdown", "id": "6d0f1a68", "metadata": {}, "source": [ "_notes_\n", "\n", "Whenever in doubt, we can quote the host or user part." ] }, { "cell_type": "markdown", "id": "bbb4986f", "metadata": {}, "source": [ "```shell\n", "mariadb --port=23306 --host=127.0.0.1 \\\n", " -p'PingVim' --user=theodore mysql\n", "```" ] }, { "cell_type": "markdown", "id": "438c7ff6", "metadata": {}, "source": [ "_notes_\n", "\n", "Login attempt from a shell.\n", "\n", "No right to use any database (including the `mysql` database)." ] }, { "cell_type": "markdown", "id": "599050af", "metadata": {}, "source": [ "```shell\n", "mariadb --port=23306 --host=127.0.0.1 \\\n", " -p'PingVim' --user=theodore\n", "```" ] }, { "cell_type": "markdown", "id": "712a6f9c", "metadata": {}, "source": [ "_notes_\n", "\n", "Can connect to the database server as such, though." ] }, { "cell_type": "code", "execution_count": null, "id": "9ca0c65b", "metadata": {}, "outputs": [], "source": [ "%%sql\n", "GRANT SELECT ON agh_it_wordpress.wp_comments TO theodore@127.0.0.1" ] }, { "cell_type": "markdown", "id": "76b4c46f", "metadata": {}, "source": [ "```sql\n", "SELECT comment_ID,\n", " SUBSTRING(comment_content, 1, 20),\n", " comment_approved\n", "FROM wp_comments;\n", "```" ] }, { "cell_type": "markdown", "id": "33544d5e", "metadata": {}, "source": [ "_notes_\n", "\n", "Works (when logged in as `theodore` in the shell)." ] }, { "cell_type": "markdown", "id": "93135616", "metadata": {}, "source": [ "```sql\n", "SELECT * FROM wp_posts;\n", "```\n", "\n", "```sql\n", "UPDATE wp_comments SET comment_approved = 1;\n", "```" ] }, { "cell_type": "markdown", "id": "13ae71ee", "metadata": {}, "source": [ "_notes_\n", "\n", "But these fail." ] }, { "cell_type": "code", "execution_count": null, "id": "edb09e41", "metadata": {}, "outputs": [], "source": [ "%%sql\n", "REVOKE SELECT ON agh_it_wordpress.wp_comments FROM theodore@127.0.0.1" ] }, { "cell_type": "markdown", "id": "fdc642a1", "metadata": {}, "source": [ "_notes_\n", "\n", "This way privileges can be taken away from a user." ] }, { "cell_type": "code", "execution_count": null, "id": "0d82b524", "metadata": {}, "outputs": [], "source": [ "%%sql\n", "GRANT SELECT ON agh_it_wordpress.wp_comments TO theodore@127.0.0.1" ] }, { "cell_type": "markdown", "id": "692924e2", "metadata": {}, "source": [ "**Relevant Postgres difference**: `ON some_db.some_table` → `ON TABLE some_table`\n", "\n", "```sql\n", "-- Must be connected to the right DB.\n", "GRANT SELECT ON TABLE products TO theodore;\n", "REVOKE SELECT ON TABLE products FROM theodore;\n", "```\n", "\n", "**Back to MariaDB**" ] }, { "cell_type": "markdown", "id": "29300c97", "metadata": {}, "source": [ "_note_\n", "\n", "He is going to need it later." ] }, { "cell_type": "code", "execution_count": null, "id": "41746938", "metadata": {}, "outputs": [], "source": [ "%%sql\n", "CREATE OR REPLACE USER pancratius@127.0.0.1 IDENTIFIED VIA pam" ] }, { "cell_type": "markdown", "id": "5aef4ede", "metadata": {}, "source": [ "_notes_\n", "\n", "Plugins can be used to authenticate users differently. For example, authentication through external mechanisms is possible. The `pam` authentication plugin would use similar mechanisms as used in the (UNIX-like) host operating system.\n", "\n", "Plugins — as one would expect — are pluggable. Hence they need to be loaded before they can be used." ] }, { "cell_type": "code", "execution_count": null, "id": "4a3b697e", "metadata": {}, "outputs": [], "source": [ "%%sql\n", "INSTALL SONAME 'auth_pam'" ] }, { "cell_type": "markdown", "id": "0e3c3714", "metadata": {}, "source": [ "_notes_\n", "\n", "The above command loads the required module (a corresponding MariaDB configuration option also exists)." ] }, { "cell_type": "code", "execution_count": null, "id": "18f92e92", "metadata": {}, "outputs": [], "source": [ "%%sql\n", "CREATE OR REPLACE USER pancratius@127.0.0.1 IDENTIFIED VIA pam" ] }, { "cell_type": "markdown", "id": "c4b12d35", "metadata": {}, "source": [ "_notes_\n", "\n", "Plugin can now be used.\n", "\n", "Also, recall that Postgres used `pg_hba.conf` to specify who can authenticate with which method." ] }, { "cell_type": "code", "execution_count": null, "id": "c6b193c7", "metadata": { "scrolled": false }, "outputs": [], "source": [ "%%sql\n", "SELECT Host, User, Password, plugin, authentication_string\n", "FROM mysql.user;" ] }, { "cell_type": "markdown", "id": "12598183", "metadata": {}, "source": [ "_notes_\n", "\n", "Use of `pam` authentication would be difficult to demonstrate given our server is running in an unprivileged container. Let's assign `pancratius@\"127.0.0.1\"` a casual password." ] }, { "cell_type": "code", "execution_count": null, "id": "8618beba", "metadata": {}, "outputs": [], "source": [ "%%sql\n", "ALTER USER pancratius@\"127.0.0.1\"\n", "IDENTIFIED BY 'MUCH passw0rd, VERY secur!ty'" ] }, { "cell_type": "markdown", "id": "66bf3ea6", "metadata": {}, "source": [ "_notes_\n", "\n", "Users can share their privileges with others provided that they have the `GRANT` privilege." ] }, { "cell_type": "code", "execution_count": null, "id": "e71ea97e", "metadata": {}, "outputs": [], "source": [ "%%sql\n", "GRANT GRANT OPTION ON agh_it_wordpress.wp_comments\n", "TO theodore@127.0.0.1" ] }, { "cell_type": "markdown", "id": "d3c5059a", "metadata": {}, "source": [ "_notes_\n", "\n", "After this `GRANT`, `theodore@127.0.0.1` can pass forward the `SELECT` privilege on `wp_comments`." ] }, { "cell_type": "code", "execution_count": null, "id": "c3cc49b2", "metadata": { "scrolled": true }, "outputs": [], "source": [ "%%sql\n", "SHOW GRANTS FOR theodore@127.0.0.1" ] }, { "cell_type": "markdown", "id": "7d164a5e", "metadata": {}, "source": [ "_notes_\n", "\n", "Instead use `information_schema.role_table_grants` or others under Postgres." ] }, { "cell_type": "markdown", "id": "5d693336", "metadata": {}, "source": [ "```sql\n", "GRANT SELECT ON agh_it_wordpress.wp_comments TO pancratius@127.0.0.1;\n", "```" ] }, { "cell_type": "markdown", "id": "45a7bf00", "metadata": {}, "source": [ "_notes_\n", "\n", "Perform the above as `theodore@127.0.0.1`." ] }, { "cell_type": "markdown", "id": "92b87a7b", "metadata": {}, "source": [ "```shell\n", "mariadb --port=23306 --host=127.0.0.1 \\\n", " -p'MUCH passw0rd, VERY secur!ty' --user=pancratius \\\n", " agh_it_wordpress\n", "```" ] }, { "cell_type": "markdown", "id": "5e4dcd96", "metadata": {}, "source": [ "_notes_\n", "\n", "`pancratius@127.0.0.1` can now use that database & table." ] }, { "cell_type": "markdown", "id": "70ce0207", "metadata": {}, "source": [ "#### A Major Difference in Postgres\n", "\n", "In Postgres, when a privilege is revoked from a user, it gets revoked from those who got it from that user, in a cascade fashion.\n", "\n", "```sql\n", "REVOKE SELECT ON products FROM eugenia CASCADE\n", "```" ] }, { "cell_type": "markdown", "id": "193dc4d2", "metadata": {}, "source": [ "_notes_\n", "\n", "`CASCADE` is necessary. Otherwise, if dependent privileges exist, the `REVOKE` command shall fail.\n", "\n", "MariaDB currently does not have the notion of dependent privileges." ] }, { "cell_type": "markdown", "id": "bd2c5b84", "metadata": {}, "source": [ "### Privilege Types and Examples" ] }, { "cell_type": "markdown", "id": "6df9f5b8", "metadata": {}, "source": [ "#### The Global Level\n", "##### MariaDB\n", "- `ALL PRIVILEGES ON *.*`\n", "- `CREATE USER ON *.*`\n", "##### Postgres\n", "NA (but: `ALTER USER some_user WITH SUPERUSER`)\n", "\n", "#### The Database Level\n", "#### MariaDB\n", "- `CREATE ON theodore_db.*` (user can create `theodore_db`)\n", "- `DROP ON agh_it_wordpress.*` (user can drop `agh_it_wordpress`)\n", "#### Postgres\n", "- `CREATE ON DATABASE agh_it_northwind` (user can crete **schemas**)\n", "- **no** `DROP` privilege (only owners and superusers can `DROP`)\n", "\n", "#### The Table Level\n", "##### MariaDB\n", "- `DELETE ON agh_it_wordpress.wp_comments`\n", "- `UPDATE ON wp_comments` (when already connected to the right db)\n", "##### Postgres\n", "- `DELETE ON TABLE orders`\n", "- `UPDATE ON products` (`TABLE` is implied)\n", "- `ALL PRIVILEGES ON ALL TABLES IN SCHEMA public`\n", "\n", "#### The Column Level\n", "##### MariaDB\n", "- `UPDATE (comment_approved) ON agh_it_wordpress.wp_comments`\n", "##### Postgres\n", "- `UPDATE (phone) ON customers`\n", "\n", "#### Others\n", "\n", "Function, procedure, proxy, etc. privileges." ] }, { "cell_type": "markdown", "id": "615343d4", "metadata": {}, "source": [ "## User Roles" ] }, { "cell_type": "code", "execution_count": null, "id": "8081dcee", "metadata": {}, "outputs": [], "source": [ "%%sql\n", "CREATE ROLE user_viewer" ] }, { "cell_type": "code", "execution_count": null, "id": "46e4343f", "metadata": {}, "outputs": [], "source": [ "%%sql\n", "SELECT Host, User, Password, is_role\n", "FROM user" ] }, { "cell_type": "code", "execution_count": null, "id": "d69be888", "metadata": {}, "outputs": [], "source": [ "%%sql\n", "SELECT User AS role FROM user WHERE is_role = 'Y'" ] }, { "cell_type": "code", "execution_count": null, "id": "d569ffde", "metadata": {}, "outputs": [], "source": [ "%%sql\n", "CREATE ROLE IF NOT EXISTS user_viewer;" ] }, { "cell_type": "code", "execution_count": null, "id": "dba71140", "metadata": {}, "outputs": [], "source": [ "%%sql\n", "GRANT SELECT (ID, user_login, user_email, user_registered)\n", "ON agh_it_wordpress.wp_users\n", "TO user_viewer" ] }, { "cell_type": "code", "execution_count": null, "id": "163cc947", "metadata": {}, "outputs": [], "source": [ "%%sql\n", "GRANT user_viewer TO theodore@127.0.0.1" ] }, { "cell_type": "markdown", "id": "d28327ac", "metadata": {}, "source": [ "```sql\n", "SELECT user_login FROM wp_users\n", "```" ] }, { "cell_type": "markdown", "id": "33500d9f", "metadata": {}, "source": [ "_notes_\n", "\n", "As `theodore@127.0.0.1`.\n", "\n", "Still denied." ] }, { "cell_type": "code", "execution_count": null, "id": "ab228d5a", "metadata": {}, "outputs": [], "source": [ "%%sql\n", "SELECT CURRENT_ROLE();" ] }, { "cell_type": "markdown", "id": "446512af", "metadata": {}, "source": [ "**Relevant differences in Postgres**: no `()`\n", "\n", "```sql\n", "SELECT CURRENT_ROLE;\n", "```\n", "\n", "**Back to MariaDB**" ] }, { "cell_type": "markdown", "id": "125995f8", "metadata": {}, "source": [ "```sql\n", "SET ROLE user_viewer;\n", "\n", "SELECT CURRENT_ROLE();\n", "\n", "SELECT ID, user_login, user_email, user_registered\n", "FROM wp_users;\n", "\n", "SET ROLE NONE;\n", "\n", "SELECT CURRENT_ROLE();\n", "```" ] }, { "cell_type": "markdown", "id": "86417f15", "metadata": {}, "source": [ "_notes_\n", "\n", "A role must be activated before being used." ] }, { "cell_type": "markdown", "id": "2804c756", "metadata": {}, "source": [ "### The `PUBLIC` Role" ] }, { "cell_type": "code", "execution_count": null, "id": "61708e12", "metadata": {}, "outputs": [], "source": [ "%%sql\n", "REVOKE SELECT ON agh_it_wordpress.wp_comments FROM theodore@127.0.0.1;\n", "\n", "GRANT SELECT ON agh_it_wordpress.wp_comments to PUBLIC" ] }, { "cell_type": "markdown", "id": "c8cb7d4a", "metadata": {}, "source": [ "```sql\n", "SELECT comment_author, comment_date, comment_content FROM wp_comments;\n", "```" ] }, { "cell_type": "markdown", "id": "d814d5d9", "metadata": {}, "source": [ "_notes_\n", "\n", "Still works (as `theodore@127.0.0.1`)." ] }, { "cell_type": "markdown", "id": "13503b07", "metadata": {}, "source": [ "### Multiple Roles" ] }, { "cell_type": "code", "execution_count": null, "id": "499e4cab", "metadata": {}, "outputs": [], "source": [ "%%sql\n", "CREATE OR REPLACE ROLE site_reporter;" ] }, { "cell_type": "code", "execution_count": null, "id": "5fe76dd6", "metadata": {}, "outputs": [], "source": [ "%%sql\n", "GRANT site_reporter TO theodore@127.0.0.1" ] }, { "cell_type": "markdown", "id": "45f851e5", "metadata": {}, "source": [ "```sql\n", "SET ROLE user_viewer;\n", "\n", "SET ROLE site_reporter;\n", "\n", "SELECT CURRENT_ROLE(); -- How many?\n", "```" ] }, { "cell_type": "markdown", "id": "2bb96a8c", "metadata": {}, "source": [ "_notes_\n", "\n", "In MariaDB only one role can be active at a time (not counting the special role `PUBLIC`)." ] }, { "cell_type": "markdown", "id": "9f7b4ce0", "metadata": {}, "source": [ "**Oracle's MySQL difference**: the opposite behavior\n", "\n", "/(against the SQL standard)/\n", "\n", "**Back to MariaDB**" ] }, { "cell_type": "markdown", "id": "d2908767", "metadata": {}, "source": [ "### Roles in Roles" ] }, { "cell_type": "code", "execution_count": null, "id": "d92bdae4", "metadata": {}, "outputs": [], "source": [ "%%sql\n", "GRANT SELECT ON agh_it_wordpress.wp_posts TO site_reporter;" ] }, { "cell_type": "code", "execution_count": null, "id": "4582a552", "metadata": {}, "outputs": [], "source": [ "%%sql\n", "GRANT user_viewer TO site_reporter;" ] }, { "cell_type": "code", "execution_count": null, "id": "5f01add7", "metadata": {}, "outputs": [], "source": [ "%%sql\n", "REVOKE user_viewer FROM theodore@127.0.0.1;" ] }, { "cell_type": "markdown", "id": "c92d7151", "metadata": {}, "source": [ "```sql\n", "SET ROLE site_reporter; -- Works.\n", "\n", "SET ROLE user_viewer; -- Fails, even with `site_reporter' active.\n", "\n", "SELECT ID, user_login, user_email, user_registered\n", "FROM wp_users; -- Works, with just `site_reporter' active.\n", "```" ] }, { "cell_type": "markdown", "id": "4907dc1d", "metadata": {}, "source": [ "_notes_\n", "\n", "Theodore now cannot set current role to `user_viewer`, but can set to `site_reporter` and, as such, still use all the privileges of `user_viewer`, `PUBLIC`, and `reporter`." ] }, { "cell_type": "markdown", "id": "668b7824", "metadata": {}, "source": [ "**Major Postgres difference**: settability & inheritability configurable\n", "\n", "| |MariaDB|Postgres|\n", "|:--|:--|:--|\n", "|`SET ROLE some_role_i_was_directly_granted` | allowed | configurable (`WITH SET OPTION`) |\n", "|`SET ROLE other_role_my_current_role_has` | forbidden | configurable (`WITH SET OPTION`) |\n", "| automatically use privileges of current role's role| yes | configurable (`WITH INHERIT OPTION`) |\n", "| automatically use privileges of my user's role| no | configurable (`WITH INHERIT OPTION`) |" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "/gnu/store/a03fcf7xcfmd4nplyla6pmrkn4fsnaxd-python-3.12.12/bin/python3", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.12" } }, "nbformat": 4, "nbformat_minor": 5 }