diff --git a/scripts-available/CDB_FederatedServer.sql b/scripts-available/CDB_FederatedServer.sql index 324359f..43401f7 100644 --- a/scripts-available/CDB_FederatedServer.sql +++ b/scripts-available/CDB_FederatedServer.sql @@ -126,16 +126,21 @@ CREATE OR REPLACE FUNCTION @extschema@.__CDB_FS_credentials_to_user_mapping(inpu RETURNS jsonb AS $$ DECLARE - user_mapping jsonb := json_build_object( - 'user_mapping', - jsonb_build_object( 'user', input_config->'credentials'->'username', - 'password', input_config->'credentials'->'password') - ); + mapping jsonb := '{}'::jsonb; BEGIN IF NOT (input_config ? 'credentials') THEN RAISE EXCEPTION 'Credentials are mandatory'; END IF; - RETURN (input_config - 'credentials')::jsonb || user_mapping; + + -- For now, allow not passing username or password + IF input_config->'credentials'->'username' IS NOT NULL THEN + mapping := jsonb_build_object('user', input_config->'credentials'->'username'); + END IF; + IF input_config->'credentials'->'password' IS NOT NULL THEN + mapping := mapping || jsonb_build_object('password', input_config->'credentials'->'password'); + END IF; + + RETURN (input_config - 'credentials')::jsonb || jsonb_build_object('user_mapping', mapping); END $$ LANGUAGE PLPGSQL IMMUTABLE PARALLEL SAFE; @@ -154,6 +159,9 @@ DECLARE }'; server_config jsonb; BEGIN + IF NOT (input_config ? 'server') THEN + RAISE EXCEPTION 'Server information is mandatory'; + END IF; server_config := default_options || to_jsonb(input_config->'server'); RETURN jsonb_set(input_config, '{server}'::text[], server_config); END diff --git a/test/CDB_FederatedServer.sql b/test/CDB_FederatedServer.sql index 58a033b..ffce997 100644 --- a/test/CDB_FederatedServer.sql +++ b/test/CDB_FederatedServer.sql @@ -71,15 +71,64 @@ SELECT '5.1', cartodb.CDB_Federated_Server_Unregister(server := 'doesNotExist':: SELECT '6.1', cartodb.CDB_Federated_Server_Unregister(server := 'myRemote2'::text); SELECT '6.2', cartodb.CDB_Federated_Server_List_Servers(); --- Should show the appropiate output (database, read-write, user, pass) +-- Test empty config +SELECT '7.1', cartodb.CDB_Federated_Server_Register_PG(server := 'empty'::text, config := '{}'); +-- Test without passing credentials +SELECT '7.2', cartodb.CDB_Federated_Server_Register_PG(server := 'empty'::text, config := '{ + "server": { + "dbname": "fdw_target", + "host": "localhost", + "port": @@PGPORT@@, + "extensions": "postgis", + "updatable": "false", + "use_remote_estimate": "true", + "fetch_size": "1000" + } +}'::jsonb); +-- Test with empty credentials +SELECT '7.3', cartodb.CDB_Federated_Server_Register_PG(server := 'empty'::text, config := '{ + "server": { + "dbname": "fdw_target", + "host": "localhost", + "port": @@PGPORT@@, + "extensions": "postgis", + "updatable": "false", + "use_remote_estimate": "true", + "fetch_size": "1000" + }, + "credentials": { } +}'::jsonb); +SELECT '7.4', cartodb.CDB_Federated_Server_List_Servers(); +SELECT '7.5', cartodb.CDB_Federated_Server_Unregister(server := 'empty'::text); +-- Test without without server options +SELECT '7.6', cartodb.CDB_Federated_Server_Register_PG(server := 'empty'::text, config := '{ + "credentials": { + "username": "other_remote_user", + "password": "foobarino" + } +}'::jsonb); --- Test multiple user mappings +-- Should work ok with special characters +SELECT '8.1', cartodb.CDB_Federated_Server_Register_PG(server := 'myRemote" or''not'::text, config := '{ + "server": { + "dbname": "fdw target", + "host": "localhost", + "port": @@PGPORT@@, + "extensions": "postgis", + "updatable": "false", + "use_remote_estimate": "true", + "fetch_size": "1000" + }, + "credentials": { + "username": "fdw user", + "password": "foo barino" + } +}'::jsonb); +SELECT '8.2', cartodb.CDB_Federated_Server_List_Servers(); +SELECT '8.3', cartodb.CDB_Federated_Server_Unregister(server := 'myRemote" or''not'::text); --- Should work ok with special characters in the name - --- Should throw with invalid or NULL config - --- -- List works with multiple and single server +-- Should throw when trying to unregistering a server that doesn't exists +SELECT '8.4', cartodb.CDB_Federated_Server_Unregister(server := 'Does not exist'::text); -- Cleanup \set QUIET on diff --git a/test/CDB_FederatedServer_expect b/test/CDB_FederatedServer_expect index 24c6479..68972b1 100644 --- a/test/CDB_FederatedServer_expect +++ b/test/CDB_FederatedServer_expect @@ -11,3 +11,13 @@ 4.2|(myRemote2,postgres_fdw,localhost,5432,fdw_target,read-only,other_remote_user) ERROR: Server "doesNotExist" does not exist 6.1| +ERROR: Server information is mandatory +ERROR: Credentials are mandatory +7.3| +7.4|(empty,postgres_fdw,localhost,5432,fdw_target,read-only,) +7.5| +ERROR: Server information is mandatory +8.1| +8.2|("myRemote"" or'not",postgres_fdw,localhost,5432,"fdw target",read-only,"fdw user") +8.3| +ERROR: Server "Does not exist" does not exist