Skip to content
Snippets Groups Projects
Commit ccc1bd64 authored by Marcin's avatar Marcin
Browse files

only used state providers impact db hash

parent 2478963a
No related branches found
No related tags found
6 merge requests!627merge in fix for get_current_block_age,!626Fix get_current_block_age function to avoid healthcheck fails,!622merge develop to master,!599merge ( with merge commit) develop to master,!597Merge develop to master for release,!590issue #259 automaticly get tables to db hash computation
...@@ -81,15 +81,15 @@ POSTGRES_EXTENSION_DIR='@POSTGRES_SHAREDIR@/extension' ...@@ -81,15 +81,15 @@ POSTGRES_EXTENSION_DIR='@POSTGRES_SHAREDIR@/extension'
DB_NAME_AFTER_UPDATE="upd_${DB_NAME}" # truncate before postgres will do this DB_NAME_AFTER_UPDATE="upd_${DB_NAME}" # truncate before postgres will do this
DB_NAME_AFTER_UPDATE="${DB_NAME_AFTER_UPDATE:0:63}" DB_NAME_AFTER_UPDATE="${DB_NAME_AFTER_UPDATE:0:63}"
save_table_schema() { save_table_schema() {
psql_do -d "$DB_NAME" -o before_update_columns.txt -q -t -A -c "SELECT table_name, table_columns FROM hive-update.calculate_schema_hash()" psql_do -d "$DB_NAME" -o before_update_columns.txt -q -t -A -c "SELECT table_name, table_columns FROM hive_update.calculate_schema_hash()"
psql_do -d "$DB_NAME" -o before_update_constraints.txt -q -t -A -c "SELECT table_name, table_constraints FROM hive-update.calculate_schema_hash()" psql_do -d "$DB_NAME" -o before_update_constraints.txt -q -t -A -c "SELECT table_name, table_constraints FROM hive_update.calculate_schema_hash()"
psql_do -d "$DB_NAME" -o before_update_indexes.txt -q -t -A -c "SELECT table_name, table_indexes FROM hive-update.calculate_schema_hash()" psql_do -d "$DB_NAME" -o before_update_indexes.txt -q -t -A -c "SELECT table_name, table_indexes FROM hive_update.calculate_schema_hash()"
psql_do -d "$DB_NAME" -o before_update_providers.txt -q -t -A -c "SELECT provider, hash FROM hive.calculate_state_provider_hashes()" psql_do -d "$DB_NAME" -o before_update_providers.txt -q -t -A -c "SELECT provider, hash FROM hive_update.calculate_state_provider_hashes(enum_range(NULL::hafd.state_providers))"
psql_do -d "$DB_NAME_AFTER_UPDATE" -o after_update_columns.txt -q -t -A -c "SELECT table_name, table_columns FROM hive-update.calculate_schema_hash()" psql_do -d "$DB_NAME_AFTER_UPDATE" -o after_update_columns.txt -q -t -A -c "SELECT table_name, table_columns FROM hive_update.calculate_schema_hash()"
psql_do -d "$DB_NAME_AFTER_UPDATE" -o after_update_constraings.txt -q -t -A -c "SELECT table_name, table_constraints FROM hive-update.calculate_schema_hash()" psql_do -d "$DB_NAME_AFTER_UPDATE" -o after_update_constraings.txt -q -t -A -c "SELECT table_name, table_constraints FROM hive_update.calculate_schema_hash()"
psql_do -d "$DB_NAME_AFTER_UPDATE" -o after_update_indexes.txt -q -t -A -c "SELECT table_name, table_indexes FROM hive-update.calculate_schema_hash()" psql_do -d "$DB_NAME_AFTER_UPDATE" -o after_update_indexes.txt -q -t -A -c "SELECT table_name, table_indexes FROM hive_update.calculate_schema_hash()"
psql_do -d "$DB_NAME_AFTER_UPDATE" -o after_update_providers.txt -q -t -A -c "SELECT provider, hash FROM hive.calculate_state_provider_hashes()" psql_do -d "$DB_NAME_AFTER_UPDATE" -o after_update_providers.txt -q -t -A -c "SELECT provider, hash FROM hive_update.calculate_state_provider_hashes(enum_range(NULL::hafd.state_providers))"
} }
verify_table_schema() { verify_table_schema() {
...@@ -98,8 +98,9 @@ verify_table_schema() { ...@@ -98,8 +98,9 @@ verify_table_schema() {
psql_do -a -d "$DB_NAME_AFTER_UPDATE" -c 'CREATE EXTENSION hive_fork_manager CASCADE;' psql_do -a -d "$DB_NAME_AFTER_UPDATE" -c 'CREATE EXTENSION hive_fork_manager CASCADE;'
psql_do -d "$DB_NAME_AFTER_UPDATE" -q -t -A -f "$SCRIPTPATH/update.sql" psql_do -d "$DB_NAME_AFTER_UPDATE" -q -t -A -f "$SCRIPTPATH/update.sql"
BEFORE_UPDATE=$(psql_do -d "$DB_NAME" -t -A -c "SELECT hive_update.create_database_hash()") STATE_PROVIDERS=$(psql_do -d "$DB_NAME" -t -A -c "SELECT hive_update.get_used_state_providers()")
AFTER_UPDATE=$(psql_do -d "$DB_NAME_AFTER_UPDATE" -t -A -c "SELECT hive_update.create_database_hash()") BEFORE_UPDATE=$(psql_do -d "$DB_NAME" -t -A -c "SELECT hive_update.create_database_hash('${STATE_PROVIDERS}'::hafd.state_providers[])")
AFTER_UPDATE=$(psql_do -d "$DB_NAME_AFTER_UPDATE" -t -A -c "SELECT hive_update.create_database_hash('${STATE_PROVIDERS}'::hafd.state_providers[])")
if [ "$BEFORE_UPDATE" = "$AFTER_UPDATE" ]; then if [ "$BEFORE_UPDATE" = "$AFTER_UPDATE" ]; then
echo "The table schema is correct, verification completed." echo "The table schema is correct, verification completed."
echo "Dropping temporary database" echo "Dropping temporary database"
...@@ -110,7 +111,7 @@ verify_table_schema() { ...@@ -110,7 +111,7 @@ verify_table_schema() {
echo "COLUMNS" echo "COLUMNS"
diff --suppress-common-lines before_update_columns.txt after_update_columns.txt || true diff --suppress-common-lines before_update_columns.txt after_update_columns.txt || true
echo "PROVIDERS" echo "PROVIDERS"
ate diff --suppress-common-lines before_update_providers.txt after_update_providers.txt || true diff --suppress-common-lines before_update_providers.txt after_update_providers.txt || true
echo "Dropping temporary database" echo "Dropping temporary database"
psql_do -a -d postgres -c "DROP DATABASE IF EXISTS $DB_NAME_AFTER_UPDATE;" psql_do -a -d postgres -c "DROP DATABASE IF EXISTS $DB_NAME_AFTER_UPDATE;"
find -type f -name '*.txt' > /dev/null 2>&1 find -type f -name '*.txt' > /dev/null 2>&1
......
...@@ -299,7 +299,7 @@ END; ...@@ -299,7 +299,7 @@ END;
$BODY$; $BODY$;
DROP FUNCTION IF EXISTS hive_update.calculate_state_provider_hashes; DROP FUNCTION IF EXISTS hive_update.calculate_state_provider_hashes;
CREATE FUNCTION hive_update.calculate_state_provider_hashes() CREATE FUNCTION hive_update.calculate_state_provider_hashes( include_providers hafd.state_providers[] )
RETURNS SETOF hive_update.state_provider_and_hash RETURNS SETOF hive_update.state_provider_and_hash
LANGUAGE plpgsql LANGUAGE plpgsql
STABLE STABLE
...@@ -310,7 +310,7 @@ BEGIN ...@@ -310,7 +310,7 @@ BEGIN
SELECT SELECT
sp.* as provider sp.* as provider
, hive_update.calculate_state_provider_hash(sp.*) as hash , hive_update.calculate_state_provider_hash(sp.*) as hash
FROM unnest(enum_range(NULL::hafd.state_providers)) as sp; FROM unnest(include_providers) as sp;
END; END;
$BODY$; $BODY$;
...@@ -335,7 +335,7 @@ $BODY$ ...@@ -335,7 +335,7 @@ $BODY$
; ;
DROP FUNCTION IF EXISTS hive_update.create_database_hash; DROP FUNCTION IF EXISTS hive_update.create_database_hash;
CREATE FUNCTION hive_update.create_database_hash() CREATE FUNCTION hive_update.create_database_hash(include_providers hafd.state_providers[] = enum_range(NULL::hafd.state_providers))
RETURNS UUID RETURNS UUID
LANGUAGE plpgsql LANGUAGE plpgsql
STABLE STABLE
...@@ -346,11 +346,29 @@ DECLARE ...@@ -346,11 +346,29 @@ DECLARE
_provider_hashes TEXT; _provider_hashes TEXT;
BEGIN BEGIN
SELECT string_agg(table_schema, ' | ') FROM hive_update.calculate_schema_hash() INTO _tmp; SELECT string_agg(table_schema, ' | ') FROM hive_update.calculate_schema_hash() INTO _tmp;
SELECT string_agg(provider || hash, ' | ') FROM hive_update.calculate_state_provider_hashes() INTO _provider_hashes; SELECT string_agg(provider || hash, ' | ') FROM hive_update.calculate_state_provider_hashes(include_providers) INTO _provider_hashes;
IF _provider_hashes IS NOT NULL THEN
_tmp = _tmp || _provider_hashes; _tmp = _tmp || _provider_hashes;
END IF;
RETURN MD5(_tmp)::uuid; RETURN MD5(_tmp)::uuid;
END; END;
$BODY$ $BODY$
; ;
DROP FUNCTION IF EXISTS hive_update.get_used_state_providers;
CREATE FUNCTION hive_update.get_used_state_providers()
RETURNS hafd.state_providers[]
LANGUAGE plpgsql
STABLE
AS
$BODY$
DECLARE
__result hafd.state_providers[];
BEGIN
SELECT ARRAY_AGG( DISTINCT sp.state_provider ) INTO __result
FROM hafd.state_providers_registered sp;
RETURN COALESCE( __result, ARRAY[]::hafd.state_providers[] );
END;
$BODY$
;
...@@ -328,6 +328,7 @@ ADD_TEST( NAME test_update_script ...@@ -328,6 +328,7 @@ ADD_TEST( NAME test_update_script
ADD_SQL_FUNCTIONAL_TEST( state_providers/import_state_provider_negative_non_context_owner_create_table.sql ) ADD_SQL_FUNCTIONAL_TEST( state_providers/import_state_provider_negative_non_context_owner_create_table.sql )
ADD_SQL_FUNCTIONAL_TEST( state_providers/switch_state_provider_to_forkable.sql ) ADD_SQL_FUNCTIONAL_TEST( state_providers/switch_state_provider_to_forkable.sql )
ADD_SQL_FUNCTIONAL_TEST( state_providers/hash.sql ) ADD_SQL_FUNCTIONAL_TEST( state_providers/hash.sql )
ADD_SQL_FUNCTIONAL_TEST( state_providers/no_hash.sql )
ADD_SQL_FUNCTIONAL_TEST( state_providers/update_state_provider_runtime.sql ) ADD_SQL_FUNCTIONAL_TEST( state_providers/update_state_provider_runtime.sql )
ADD_SQL_FUNCTIONAL_TEST( authorization/alice_access_to_bob_negative.sql ) ADD_SQL_FUNCTIONAL_TEST( authorization/alice_access_to_bob_negative.sql )
......
...@@ -10,24 +10,26 @@ DECLARE ...@@ -10,24 +10,26 @@ DECLARE
__all_before_hashes TEXT; __all_before_hashes TEXT;
__all_after_hashes TEXT; __all_after_hashes TEXT;
__database_hash_before TEXT; __database_hash_before TEXT;
__database_hash_before1 TEXT;
__database_hash_after TEXT; __database_hash_after TEXT;
BEGIN BEGIN
ASSERT ( SELECT 1 FROM hive_update.calculate_state_provider_hashes() WHERE provider = 'ACCOUNTS' ) IS NOT NULL ASSERT ( SELECT 1 FROM hive_update.calculate_state_provider_hashes(enum_range(NULL::hafd.state_providers)) WHERE provider = 'ACCOUNTS' ) IS NOT NULL
, 'ACCOUNTS not hashed'; , 'ACCOUNTS not hashed';
ASSERT ( SELECT 1 FROM hive_update.calculate_state_provider_hashes() WHERE provider = 'KEYAUTH' ) IS NOT NULL ASSERT ( SELECT 1 FROM hive_update.calculate_state_provider_hashes(enum_range(NULL::hafd.state_providers)) WHERE provider = 'KEYAUTH' ) IS NOT NULL
, 'KEYAUTH not hashed'; , 'KEYAUTH not hashed';
ASSERT ( SELECT 1 FROM hive_update.calculate_state_provider_hashes() WHERE provider = 'METADATA' ) IS NOT NULL ASSERT ( SELECT 1 FROM hive_update.calculate_state_provider_hashes(enum_range(NULL::hafd.state_providers)) WHERE provider = 'METADATA' ) IS NOT NULL
, 'METADATA not hashed'; , 'METADATA not hashed';
ASSERT ( SELECT COUNT(*) FROM hive_update.calculate_state_provider_hashes() ) = 3 ASSERT ( SELECT COUNT(*) FROM hive_update.calculate_state_provider_hashes(enum_range(NULL::hafd.state_providers)) ) = 3
, 'More than 3 known providers are hashed'; , 'More than 3 known providers are hashed';
SELECT STRING_AGG( hash, '|') FROM hive_update.calculate_state_provider_hashes() INTO __all_before_hashes; SELECT STRING_AGG( hash, '|') FROM hive_update.calculate_state_provider_hashes(enum_range(NULL::hafd.state_providers)) INTO __all_before_hashes;
SELECT * FROM hive_update.calculate_state_provider_hash( 'KEYAUTH'::hafd.state_providers ) INTO __keyauth_before_hash; SELECT * FROM hive_update.calculate_state_provider_hash( 'KEYAUTH'::hafd.state_providers ) INTO __keyauth_before_hash;
SELECT hive_update.create_database_hash() INTO __database_hash_before; SELECT hive_update.create_database_hash() INTO __database_hash_before;
SELECT hive_update.create_database_hash(ARRAY['METADATA']::hafd.state_providers[]) INTO __database_hash_before1;
EXECUTE format( 'CREATE OR REPLACE FUNCTION hive.start_provider_keyauth( _context hafd.context_name ) EXECUTE format( 'CREATE OR REPLACE FUNCTION hive.start_provider_keyauth( _context hafd.context_name )
RETURNS TEXT[] RETURNS TEXT[]
...@@ -41,12 +43,13 @@ BEGIN ...@@ -41,12 +43,13 @@ BEGIN
$$ $$
;'); ;');
SELECT STRING_AGG( hash, '|') FROM hive_update.calculate_state_provider_hashes() INTO __all_after_hashes; SELECT STRING_AGG( hash, '|') FROM hive_update.calculate_state_provider_hashes(enum_range(NULL::hafd.state_providers)) INTO __all_after_hashes;
SELECT * FROM hive_update.calculate_state_provider_hash( 'KEYAUTH'::hafd.state_providers ) INTO __keyauth_after_hash; SELECT * FROM hive_update.calculate_state_provider_hash( 'KEYAUTH'::hafd.state_providers ) INTO __keyauth_after_hash;
SELECT hive_update.create_database_hash() INTO __database_hash_after; SELECT hive_update.create_database_hash() INTO __database_hash_after;
ASSERT __all_after_hashes != __all_before_hashes, 'Hashes not changed after modification'; ASSERT __all_after_hashes != __all_before_hashes, 'Hashes not changed after modification';
ASSERT __keyauth_after_hash != __keyauth_before_hash, 'Hash not changed after modification'; ASSERT __keyauth_after_hash != __keyauth_before_hash, 'Hash not changed after modification';
ASSERT __database_hash_before != __database_hash_after, 'DB Hash not changed after modification'; ASSERT __database_hash_before != __database_hash_after, 'DB Hash not changed after modification';
ASSERT __database_hash_before1 != __database_hash_after, 'DB Hash not changed after modification 1';
END; END;
$BODY$; $BODY$;
\ No newline at end of file
-- check if there is no registered state_provider, then hash is not computed
CREATE OR REPLACE PROCEDURE haf_admin_test_then()
LANGUAGE 'plpgsql'
AS
$BODY$
DECLARE
__database_hash_before TEXT;
__database_hash_after TEXT;
BEGIN
ASSERT ( SELECT 1 FROM hive_update.calculate_state_provider_hashes( ARRAY['KEYAUTH', 'METADATA']::hafd.state_providers[] ) WHERE provider = 'ACCOUNTS' ) IS NULL
, 'ACCOUNTS hashed';
ASSERT ( SELECT 1 FROM hive_update.calculate_state_provider_hashes( ARRAY['KEYAUTH', 'METADATA']::hafd.state_providers[] ) WHERE provider = 'KEYAUTH' ) IS NOT NULL
, 'KEYAUTH not hashed';
ASSERT ( SELECT 1 FROM hive_update.calculate_state_provider_hashes( ARRAY['KEYAUTH', 'METADATA']::hafd.state_providers[] ) WHERE provider = 'METADATA' ) IS NOT NULL
, 'METADATA not hashed';
ASSERT ( SELECT hive_update.calculate_state_provider_hashes( ARRAY[]::hafd.state_providers[] ) ) IS NULL, 'NOT NULL returned for empty state providers';
SELECT hive_update.create_database_hash(ARRAY['METADATA']::hafd.state_providers[]) INTO __database_hash_before;
EXECUTE format( 'CREATE OR REPLACE FUNCTION hive.start_provider_keyauth( _context hafd.context_name )
RETURNS TEXT[]
LANGUAGE plpgsql
VOLATILE
AS
$$
BEGIN
RETURN '''';
END;
$$
;');
SELECT hive_update.create_database_hash(ARRAY['METADATA']::hafd.state_providers[]) INTO __database_hash_after;
ASSERT __database_hash_after = __database_hash_before, 'Unused state provider has impact on database hash';
END;
$BODY$;
\ No newline at end of file
...@@ -33,6 +33,7 @@ prepare_sql_script() { ...@@ -33,6 +33,7 @@ prepare_sql_script() {
prepare_database() { prepare_database() {
"$SCRIPTS_DIR/setup_db.sh" --haf-db-name="$UPDATE_DB_NAME" "$@" "$SCRIPTS_DIR/setup_db.sh" --haf-db-name="$UPDATE_DB_NAME" "$@"
exec_sql "SELECT hive.initialize_extension_data();"
} }
update_database() { update_database() {
...@@ -241,4 +242,38 @@ prepare_database ...@@ -241,4 +242,38 @@ prepare_database
exec_sql "CREATE FUNCTION hafd.bad_function() RETURNS VOID VOLATILE AS '/lib/postgresql/${POSTGRES_VERSION}/lib/tablefunc.so', 'crosstab' language c;" exec_sql "CREATE FUNCTION hafd.bad_function() RETURNS VOID VOLATILE AS '/lib/postgresql/${POSTGRES_VERSION}/lib/tablefunc.so', 'crosstab' language c;"
failswith 3 update_database failswith 3 update_database
printf "\nTEST: Change keyauth state provider, but not use it. This should pass\n"
prepare_database
exec_sql "CREATE OR REPLACE FUNCTION hive.start_provider_keyauth( _context hafd.context_name )
RETURNS TEXT[]
LANGUAGE plpgsql
VOLATILE
AS
\$\$
BEGIN
RETURN '';
END;
\$\$
;"
update_database
printf "\nTEST: Change keyauth state provider, and not use it. This should fail\n"
prepare_database
exec_sql "CREATE SCHEMA workaround;
SELECT hive.app_create_context('workaround', 'workaround');
SELECT hive.app_state_provider_import( 'KEYAUTH', 'workaround' );
"
exec_sql "CREATE OR REPLACE FUNCTION hive.start_provider_keyauth( _context hafd.context_name )
RETURNS TEXT[]
LANGUAGE plpgsql
VOLATILE
AS
\$\$
BEGIN
RETURN '';
END;
\$\$
;"
failswith 1 update_database
echo "Succeeded" echo "Succeeded"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment