From 2a4ecd4850c5e2cff7560002756a4b681dbcac3a Mon Sep 17 00:00:00 2001 From: Gonzalo Riestra Date: Wed, 20 Feb 2019 10:38:00 +0100 Subject: [PATCH 1/6] Add CDB_Username() function --- scripts-available/CDB_Username.sql | 13 +++++++++++++ .../{CDB_Conf.sql => 910-CDB_Conf.sql} | 0 scripts-enabled/920-CDB_Username.sql | 1 + test/CDB_Username.sql | 17 +++++++++++++++++ test/CDB_Username_expect | 3 +++ 5 files changed, 34 insertions(+) create mode 100644 scripts-available/CDB_Username.sql rename scripts-enabled/{CDB_Conf.sql => 910-CDB_Conf.sql} (100%) create mode 120000 scripts-enabled/920-CDB_Username.sql create mode 100644 test/CDB_Username.sql create mode 100644 test/CDB_Username_expect diff --git a/scripts-available/CDB_Username.sql b/scripts-available/CDB_Username.sql new file mode 100644 index 0000000..9a747ed --- /dev/null +++ b/scripts-available/CDB_Username.sql @@ -0,0 +1,13 @@ +-- Function returning the username of the provided user +CREATE OR REPLACE FUNCTION _CDB_Username(pg_user TEXT) +RETURNS text +AS $$ + SELECT CDB_Conf_GetConf(CONCAT('api_keys_', pg_user))->>'username'; +$$ LANGUAGE SQL STRICT IMMUTABLE PARALLEL SAFE SECURITY DEFINER; + +-- Function returning the username of the current user +CREATE OR REPLACE FUNCTION CDB_Username() +RETURNS text +AS $$ + SELECT _CDB_Username(current_user); +$$ LANGUAGE SQL STABLE PARALLEL SAFE; diff --git a/scripts-enabled/CDB_Conf.sql b/scripts-enabled/910-CDB_Conf.sql similarity index 100% rename from scripts-enabled/CDB_Conf.sql rename to scripts-enabled/910-CDB_Conf.sql diff --git a/scripts-enabled/920-CDB_Username.sql b/scripts-enabled/920-CDB_Username.sql new file mode 120000 index 0000000..8c0abca --- /dev/null +++ b/scripts-enabled/920-CDB_Username.sql @@ -0,0 +1 @@ +../scripts-available/CDB_Username.sql \ No newline at end of file diff --git a/test/CDB_Username.sql b/test/CDB_Username.sql new file mode 100644 index 0000000..795f689 --- /dev/null +++ b/test/CDB_Username.sql @@ -0,0 +1,17 @@ +SELECT current_user; -- postgres +SELECT CDB_Username(); -- (NULL) + +-- Connect with admin +\set QUIET on +\o log/test.log +SELECT current_database() AS current_database; +\gset +SELECT SUBSTRING (:'current_database', 19, 36) AS user_id; +\gset +SELECT rolname AS admin_user FROM pg_roles where rolname LIKE ('%' || :'user_id'); +\gset +\c :current_database :admin_user +\o +\set QUIET off + +SELECT CDB_Username(); -- admin \ No newline at end of file diff --git a/test/CDB_Username_expect b/test/CDB_Username_expect new file mode 100644 index 0000000..5336c89 --- /dev/null +++ b/test/CDB_Username_expect @@ -0,0 +1,3 @@ +postgres + +admin From ffaf5e4400d5e29b0d453ad01e074f6c04aaa59b Mon Sep 17 00:00:00 2001 From: Gonzalo Riestra Date: Wed, 20 Feb 2019 10:38:16 +0100 Subject: [PATCH 2/6] Bump to 0.25.0 --- Makefile | 3 ++- NEWS.md | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 0bc7ec6..2bbe527 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ # cartodb/Makefile EXTENSION = cartodb -EXTVERSION = 0.24.1 +EXTVERSION = 0.25.0 SED = sed AWK = awk @@ -93,6 +93,7 @@ UPGRADABLE = \ 0.23.2 \ 0.24.0 \ 0.24.1 \ + 0.25.0 \ $(EXTVERSION)dev \ $(EXTVERSION)next \ $(END) diff --git a/NEWS.md b/NEWS.md index 17ec9c2..13687fd 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,6 @@ +0.25.0 (XXXX-XX-XX) +* Add `CDB_Username` to get the cartodb username from the current PostgreSQL user + 0.24.1 (2019-01-02) * Drop functions removed in 0.12 (#341) * Travis: Test with PostgreSQL 9.5, 10 and 11. From 6d122462bbe64b4b3e070f2311020f64bd8b7b76 Mon Sep 17 00:00:00 2001 From: Gonzalo Riestra Date: Wed, 20 Feb 2019 13:34:11 +0100 Subject: [PATCH 3/6] decouple tests from cartodb --- test/CDB_Username.sql | 29 ++++++++++++++++++----------- test/CDB_Username_expect | 3 ++- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/test/CDB_Username.sql b/test/CDB_Username.sql index 795f689..1d187e2 100644 --- a/test/CDB_Username.sql +++ b/test/CDB_Username.sql @@ -1,17 +1,24 @@ SELECT current_user; -- postgres SELECT CDB_Username(); -- (NULL) --- Connect with admin +-- Add the role fulano with an api_key and connect with it \set QUIET on -\o log/test.log -SELECT current_database() AS current_database; -\gset -SELECT SUBSTRING (:'current_database', 19, 36) AS user_id; -\gset -SELECT rolname AS admin_user FROM pg_roles where rolname LIKE ('%' || :'user_id'); -\gset -\c :current_database :admin_user -\o +CREATE ROLE fulano LOGIN; +GRANT USAGE ON SCHEMA cartodb TO fulano; +GRANT EXECUTE ON FUNCTION CDB_Username() TO fulano; +GRANT EXECUTE ON FUNCTION _CDB_Username(text) TO fulano; +INSERT INTO cdb_conf (key, value) VALUES ('api_keys_fulano', '{"username": "fulanito", "permissions":[]}'); +SET ROLE fulano; \set QUIET off -SELECT CDB_Username(); -- admin \ No newline at end of file +SELECT current_user; -- fulano +SELECT CDB_Username(); -- fulanito + +-- Remove fulano +\set QUIET on +SET ROLE postgres; +REVOKE USAGE ON SCHEMA cartodb FROM fulano; +REVOKE EXECUTE ON FUNCTION CDB_Username() FROM fulano; +REVOKE EXECUTE ON FUNCTION _CDB_Username(text) FROM fulano; +DROP ROLE fulano; +\set QUIET off diff --git a/test/CDB_Username_expect b/test/CDB_Username_expect index 5336c89..fe1ea5b 100644 --- a/test/CDB_Username_expect +++ b/test/CDB_Username_expect @@ -1,3 +1,4 @@ postgres -admin +fulano +fulanito From 0568b36a9022c9a4437d29717ea2b6e8c9936f16 Mon Sep 17 00:00:00 2001 From: Gonzalo Riestra Date: Wed, 20 Feb 2019 14:45:57 +0100 Subject: [PATCH 4/6] use session_user instead of current_user --- scripts-available/CDB_Username.sql | 13 +++---------- test/CDB_Username.sql | 14 ++++++-------- 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/scripts-available/CDB_Username.sql b/scripts-available/CDB_Username.sql index 9a747ed..9c6a5bc 100644 --- a/scripts-available/CDB_Username.sql +++ b/scripts-available/CDB_Username.sql @@ -1,13 +1,6 @@ --- Function returning the username of the provided user -CREATE OR REPLACE FUNCTION _CDB_Username(pg_user TEXT) -RETURNS text -AS $$ - SELECT CDB_Conf_GetConf(CONCAT('api_keys_', pg_user))->>'username'; -$$ LANGUAGE SQL STRICT IMMUTABLE PARALLEL SAFE SECURITY DEFINER; - --- Function returning the username of the current user +-- Returns the cartodb username of the current PostgreSQL session CREATE OR REPLACE FUNCTION CDB_Username() RETURNS text AS $$ - SELECT _CDB_Username(current_user); -$$ LANGUAGE SQL STABLE PARALLEL SAFE; + SELECT CDB_Conf_GetConf(CONCAT('api_keys_', session_user))->>'username'; +$$ LANGUAGE SQL STABLE PARALLEL SAFE SECURITY DEFINER; diff --git a/test/CDB_Username.sql b/test/CDB_Username.sql index 1d187e2..ed344d6 100644 --- a/test/CDB_Username.sql +++ b/test/CDB_Username.sql @@ -1,24 +1,22 @@ -SELECT current_user; -- postgres +SELECT session_user; -- postgres SELECT CDB_Username(); -- (NULL) --- Add the role fulano with an api_key and connect with it +-- Add the role fulano with api_key and connect with it \set QUIET on CREATE ROLE fulano LOGIN; GRANT USAGE ON SCHEMA cartodb TO fulano; GRANT EXECUTE ON FUNCTION CDB_Username() TO fulano; -GRANT EXECUTE ON FUNCTION _CDB_Username(text) TO fulano; INSERT INTO cdb_conf (key, value) VALUES ('api_keys_fulano', '{"username": "fulanito", "permissions":[]}'); -SET ROLE fulano; +SET SESSION AUTHORIZATION fulano; \set QUIET off -SELECT current_user; -- fulano +SELECT session_user; -- fulano SELECT CDB_Username(); -- fulanito -- Remove fulano \set QUIET on -SET ROLE postgres; +SET SESSION AUTHORIZATION postgres; REVOKE USAGE ON SCHEMA cartodb FROM fulano; REVOKE EXECUTE ON FUNCTION CDB_Username() FROM fulano; -REVOKE EXECUTE ON FUNCTION _CDB_Username(text) FROM fulano; DROP ROLE fulano; -\set QUIET off +\set QUIET off \ No newline at end of file From 917a975baaa8026a82d0eeb9ec7b365dffc2387d Mon Sep 17 00:00:00 2001 From: Javier Torres Date: Thu, 21 Feb 2019 15:20:24 +0100 Subject: [PATCH 5/6] Tabs vs spaces --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 2bbe527..fa9ffb6 100644 --- a/Makefile +++ b/Makefile @@ -93,7 +93,7 @@ UPGRADABLE = \ 0.23.2 \ 0.24.0 \ 0.24.1 \ - 0.25.0 \ + 0.25.0 \ $(EXTVERSION)dev \ $(EXTVERSION)next \ $(END) From a904b101a39873598537ef9a9e9833ab33b3f493 Mon Sep 17 00:00:00 2001 From: Gonzalo Riestra Date: Fri, 22 Feb 2019 08:39:17 +0100 Subject: [PATCH 6/6] update news --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 13687fd..9c803b2 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,4 @@ -0.25.0 (XXXX-XX-XX) +0.25.0 (2019-02-22) * Add `CDB_Username` to get the cartodb username from the current PostgreSQL user 0.24.1 (2019-01-02)