|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Tags: no-parallel |
| 3 | + |
| 4 | +CUR_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) |
| 5 | +# shellcheck source=../shell_config.sh |
| 6 | +. "$CUR_DIR"/../shell_config.sh |
| 7 | + |
| 8 | +TEST_USER="03096_role_query_param_user" |
| 9 | +TEST_USER_AUTH="$TEST_USER:" |
| 10 | + |
| 11 | +TEST_ROLE1="03096_role_query_param_role1" |
| 12 | +TEST_ROLE2="03096_role_query_param_role2" |
| 13 | +TEST_ROLE_ENABLED_BY_DEFAULT="03096_role_query_param_role_enabled_by_default" |
| 14 | +TEST_ROLE_NOT_GRANTED="03096_role_query_param_role_not_granted" |
| 15 | +TEST_ROLE_SPECIAL_CHARS="\`03096_role_query_param_@!\\$\`" # = CREATE ROLE `03096_role_query_param_@!\$` |
| 16 | +TEST_ROLE_SPECIAL_CHARS_URLENCODED="03096_role_query_param_%40!%5C%24" |
| 17 | + |
| 18 | +CHANGED_SETTING_NAME="max_result_rows" |
| 19 | +CHANGED_SETTING_VALUE="42" |
| 20 | + |
| 21 | +SHOW_CURRENT_ROLES_QUERY="SELECT role_name FROM system.current_roles ORDER BY role_name ASC" |
| 22 | +SHOW_CHANGED_SETTINGS_QUERY="SELECT name, value FROM system.settings WHERE changed = 1 AND name = '$CHANGED_SETTING_NAME' ORDER BY name ASC" |
| 23 | + |
| 24 | +$CLICKHOUSE_CLIENT -n --query " |
| 25 | +DROP USER IF EXISTS $TEST_USER; |
| 26 | +DROP ROLE IF EXISTS $TEST_ROLE1; |
| 27 | +DROP ROLE IF EXISTS $TEST_ROLE2; |
| 28 | +DROP ROLE IF EXISTS $TEST_ROLE_ENABLED_BY_DEFAULT; |
| 29 | +DROP ROLE IF EXISTS $TEST_ROLE_NOT_GRANTED; |
| 30 | +DROP ROLE IF EXISTS $TEST_ROLE_SPECIAL_CHARS; |
| 31 | +CREATE USER $TEST_USER NOT IDENTIFIED; |
| 32 | +CREATE ROLE $TEST_ROLE_ENABLED_BY_DEFAULT; |
| 33 | +GRANT $TEST_ROLE_ENABLED_BY_DEFAULT TO $TEST_USER; |
| 34 | +SET DEFAULT ROLE $TEST_ROLE_ENABLED_BY_DEFAULT TO $TEST_USER; |
| 35 | +CREATE ROLE $TEST_ROLE1; |
| 36 | +GRANT $TEST_ROLE1 TO $TEST_USER; |
| 37 | +CREATE ROLE $TEST_ROLE2; |
| 38 | +GRANT $TEST_ROLE2 TO $TEST_USER; |
| 39 | +CREATE ROLE $TEST_ROLE_SPECIAL_CHARS; |
| 40 | +GRANT $TEST_ROLE_SPECIAL_CHARS TO $TEST_USER; |
| 41 | +CREATE ROLE $TEST_ROLE_NOT_GRANTED; |
| 42 | +" |
| 43 | + |
| 44 | +echo "### Shows the default role when there are no role parameters" |
| 45 | +$CLICKHOUSE_CURL -u $TEST_USER_AUTH -sS "$CLICKHOUSE_URL" --data-binary "$SHOW_CURRENT_ROLES_QUERY" |
| 46 | + |
| 47 | +echo "### Shows a single role from the query parameters" |
| 48 | +$CLICKHOUSE_CURL -u $TEST_USER_AUTH -sS "$CLICKHOUSE_URL&role=$TEST_ROLE1" --data-binary "$SHOW_CURRENT_ROLES_QUERY" |
| 49 | + |
| 50 | +echo "### Shows multiple roles from the query parameters" |
| 51 | +$CLICKHOUSE_CURL -u $TEST_USER_AUTH -sS "$CLICKHOUSE_URL&role=$TEST_ROLE1&role=$TEST_ROLE2" --data-binary "$SHOW_CURRENT_ROLES_QUERY" |
| 52 | + |
| 53 | +echo "### Sets the default role alongside with another granted one" |
| 54 | +$CLICKHOUSE_CURL -u $TEST_USER_AUTH -sS "$CLICKHOUSE_URL&role=$TEST_ROLE_ENABLED_BY_DEFAULT&role=$TEST_ROLE1" --data-binary "$SHOW_CURRENT_ROLES_QUERY" |
| 55 | + |
| 56 | +echo "### Sets a role with special characters in the name" |
| 57 | +$CLICKHOUSE_CURL -u $TEST_USER_AUTH -sS "$CLICKHOUSE_URL&role=$TEST_ROLE_SPECIAL_CHARS_URLENCODED" --data-binary "$SHOW_CURRENT_ROLES_QUERY" |
| 58 | + |
| 59 | +echo "### Sets a role with special characters in the name with another granted role" |
| 60 | +$CLICKHOUSE_CURL -u $TEST_USER_AUTH -sS "$CLICKHOUSE_URL&role=$TEST_ROLE_SPECIAL_CHARS_URLENCODED&role=$TEST_ROLE1" --data-binary "$SHOW_CURRENT_ROLES_QUERY" |
| 61 | + |
| 62 | +echo "### Sets a role once when it's present in the query parameters multiple times" |
| 63 | +$CLICKHOUSE_CURL -u $TEST_USER_AUTH -sS "$CLICKHOUSE_URL&role=$TEST_ROLE1&role=$TEST_ROLE1" --data-binary "$SHOW_CURRENT_ROLES_QUERY" |
| 64 | + |
| 65 | +echo "### Sets a role when there are other parameters in the query (before the role parameter)" |
| 66 | +$CLICKHOUSE_CURL -u $TEST_USER_AUTH -sS "$CLICKHOUSE_URL&$CHANGED_SETTING_NAME=$CHANGED_SETTING_VALUE&role=$TEST_ROLE1" --data-binary "$SHOW_CURRENT_ROLES_QUERY" |
| 67 | +$CLICKHOUSE_CURL -u $TEST_USER_AUTH -sS "$CLICKHOUSE_URL&$CHANGED_SETTING_NAME=$CHANGED_SETTING_VALUE&role=$TEST_ROLE1" --data-binary "$SHOW_CHANGED_SETTINGS_QUERY" |
| 68 | + |
| 69 | +echo "### Sets a role when there are other parameters in the query (after the role parameter)" |
| 70 | +$CLICKHOUSE_CURL -u $TEST_USER_AUTH -sS "$CLICKHOUSE_URL&role=$TEST_ROLE1&$CHANGED_SETTING_NAME=$CHANGED_SETTING_VALUE" --data-binary "$SHOW_CURRENT_ROLES_QUERY" |
| 71 | +$CLICKHOUSE_CURL -u $TEST_USER_AUTH -sS "$CLICKHOUSE_URL&role=$TEST_ROLE1&$CHANGED_SETTING_NAME=$CHANGED_SETTING_VALUE" --data-binary "$SHOW_CHANGED_SETTINGS_QUERY" |
| 72 | + |
| 73 | +echo "### Sets multiple roles when there are other parameters in the query" |
| 74 | +$CLICKHOUSE_CURL -u $TEST_USER_AUTH -sS "$CLICKHOUSE_URL&role=$TEST_ROLE1&$CHANGED_SETTING_NAME=$CHANGED_SETTING_VALUE&role=$TEST_ROLE2" --data-binary "$SHOW_CURRENT_ROLES_QUERY" |
| 75 | +$CLICKHOUSE_CURL -u $TEST_USER_AUTH -sS "$CLICKHOUSE_URL&role=$TEST_ROLE1&$CHANGED_SETTING_NAME=$CHANGED_SETTING_VALUE&role=$TEST_ROLE2" --data-binary "$SHOW_CHANGED_SETTINGS_QUERY" |
| 76 | + |
| 77 | +echo "### Cannot set a role that does not exist (single parameter)" |
| 78 | +OUT=$($CLICKHOUSE_CURL -u $TEST_USER_AUTH -sS "$CLICKHOUSE_URL&role=aaaaaaaaaaa" --data-binary "$SHOW_CURRENT_ROLES_QUERY") |
| 79 | +echo -ne $OUT | grep -o "Code: 511" || echo "expected code 511, got: $OUT" |
| 80 | +echo -ne $OUT | grep -o "UNKNOWN_ROLE" || echo "expected UNKNOWN_ROLE error, got: $OUT" |
| 81 | + |
| 82 | +echo "### Cannot set a role that does not exist (multiple parameters)" |
| 83 | +OUT=$($CLICKHOUSE_CURL -u $TEST_USER_AUTH -sS "$CLICKHOUSE_URL&role=$TEST_ROLE1&role=aaaaaaaaaaa" --data-binary "$SHOW_CURRENT_ROLES_QUERY") |
| 84 | +echo -ne $OUT | grep -o "Code: 511" || echo "expected code 511, got: $OUT" |
| 85 | +echo -ne $OUT | grep -o "UNKNOWN_ROLE" || echo "expected UNKNOWN_ROLE error, got: $OUT" |
| 86 | + |
| 87 | +echo "### Cannot set a role that is not granted to the user (single parameter)" |
| 88 | +OUT=$($CLICKHOUSE_CURL -u $TEST_USER_AUTH -sS "$CLICKHOUSE_URL&role=$TEST_ROLE_NOT_GRANTED" --data-binary "$SHOW_CURRENT_ROLES_QUERY") |
| 89 | +echo -ne $OUT | grep -o "Code: 512" || echo "expected code 512, got: $OUT" |
| 90 | +echo -ne $OUT | grep -o "SET_NON_GRANTED_ROLE" || echo "expected SET_NON_GRANTED_ROLE error, got: $OUT" |
| 91 | + |
| 92 | +echo "### Cannot set a role that is not granted to the user (multiple parameters)" |
| 93 | +OUT=$($CLICKHOUSE_CURL -u $TEST_USER_AUTH -sS "$CLICKHOUSE_URL&role=$TEST_ROLE1&role=$TEST_ROLE_NOT_GRANTED" --data-binary "$SHOW_CURRENT_ROLES_QUERY") |
| 94 | +echo -ne $OUT | grep -o "Code: 512" || echo "expected code 512, got: $OUT" |
| 95 | +echo -ne $OUT | grep -o "SET_NON_GRANTED_ROLE" || echo "expected SET_NON_GRANTED_ROLE error, got: $OUT" |
| 96 | + |
| 97 | +$CLICKHOUSE_CLIENT -n --query " |
| 98 | +DROP USER $TEST_USER; |
| 99 | +DROP ROLE $TEST_ROLE1; |
| 100 | +DROP ROLE $TEST_ROLE2; |
| 101 | +DROP ROLE $TEST_ROLE_ENABLED_BY_DEFAULT; |
| 102 | +DROP ROLE $TEST_ROLE_NOT_GRANTED; |
| 103 | +DROP ROLE $TEST_ROLE_SPECIAL_CHARS; |
| 104 | +" |
0 commit comments