Add capability to customize test database name
This patchset gives the deployer the capability to define their own test database name for testing postgresql/mariadb user backup/restore. It also gives them the capability to not create the user by leaving the user name blank/null in the values.yaml. Change-Id: I8d824bd4d3ad5d402a8a21baa7c42befcf66898d
This commit is contained in:
@@ -3,7 +3,6 @@
|
||||
set -e +x
|
||||
|
||||
IFS=', ' read -re -a BACKUP_RESTORE_NAMESPACE_ARRAY <<< "$BACKUP_RESTORE_NAMESPACE_LIST"
|
||||
TEST_DB_USER="${TEST_DB_NAME}_user"
|
||||
TEST_TABLE="test_table1"
|
||||
|
||||
function database_cmd() {
|
||||
@@ -46,16 +45,20 @@ EOF
|
||||
INSERT INTO ${TEST_TABLE} VALUES ( 'name1', '1' );
|
||||
EOF
|
||||
|
||||
# Create a test user if it has not been created before.
|
||||
if ${PSQL} -tc "SELECT rolname FROM pg_roles WHERE rolname='${TEST_DB_USER}';" | grep ${TEST_DB_USER}; then
|
||||
echo "Test user already exists in namespace ${NAMESPACE}"
|
||||
else
|
||||
${PSQL} -tc "CREATE ROLE ${TEST_DB_USER};"
|
||||
echo "Test user created in namespace ${NAMESPACE}."
|
||||
fi
|
||||
if [[ -n ${TEST_DB_USER} ]]; then
|
||||
# Create a test user if it has not been created before.
|
||||
if ${PSQL} -tc "SELECT rolname FROM pg_roles WHERE rolname='${TEST_DB_USER}';" | grep ${TEST_DB_USER}; then
|
||||
echo "Test user already exists in namespace ${NAMESPACE}"
|
||||
else
|
||||
${PSQL} -tc "CREATE ROLE ${TEST_DB_USER};"
|
||||
echo "Test user created in namespace ${NAMESPACE}."
|
||||
fi
|
||||
|
||||
# Note, if the GRANT is already there, the following command will not fail,
|
||||
# so no need to check existence first.
|
||||
${PSQL} -tc "GRANT ALL PRIVILEGES ON DATABASE ${TEST_DB_NAME} TO ${TEST_DB_USER};"
|
||||
echo "Test user is granted permissions to the test database in namespace ${NAMESPACE}."
|
||||
# Note, if the GRANT is already there, the following command will not fail,
|
||||
# so no need to check existence first.
|
||||
${PSQL} -tc "GRANT ALL PRIVILEGES ON DATABASE ${TEST_DB_NAME} TO ${TEST_DB_USER};"
|
||||
echo "Test user is granted permissions to the test database in namespace ${NAMESPACE}."
|
||||
else
|
||||
echo "No test user configured to access test database in namespace ${NAMESPACE}"
|
||||
fi
|
||||
done
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
#!/bin/bash
|
||||
|
||||
TEST_DB_USER="${TEST_DB_NAME}_user"
|
||||
|
||||
function database_cmd() {
|
||||
NAMESPACE=$1
|
||||
|
||||
@@ -147,14 +145,18 @@ function create_user_grants() {
|
||||
CREATE_GRANTS_ARGS=("$@")
|
||||
NAMESPACE=${CREATE_GRANTS_ARGS[1]}
|
||||
|
||||
DB_CMD=$(database_cmd ${NAMESPACE})
|
||||
if [[ -n ${TEST_DB_USER} ]]; then
|
||||
DB_CMD=$(database_cmd ${NAMESPACE})
|
||||
|
||||
# If the test user and grants do not exist already,
|
||||
# give the test user privilege to access the test database
|
||||
if ${DB_CMD} -tc "SELECT rolname FROM pg_roles WHERE rolname='${TEST_DB_USER}';" | grep ${TEST_DB_USER}; then
|
||||
${DB_CMD} -tc "GRANT ALL PRIVILEGES ON DATABASE ${TEST_DB_NAME} TO ${TEST_DB_USER};"
|
||||
# If the test user and grants do not exist already,
|
||||
# give the test user privilege to access the test database
|
||||
if ${DB_CMD} -tc "SELECT rolname FROM pg_roles WHERE rolname='${TEST_DB_USER}';" | grep ${TEST_DB_USER}; then
|
||||
${DB_CMD} -tc "GRANT ALL PRIVILEGES ON DATABASE ${TEST_DB_NAME} TO ${TEST_DB_USER};"
|
||||
else
|
||||
echo "Test user does not exist in namespace ${NAMESPACE}"
|
||||
fi
|
||||
else
|
||||
echo "Test user does not exist in namespace ${NAMESPACE}"
|
||||
echo "Test user was not deployed in namespace ${NAMESPACE}"
|
||||
fi
|
||||
}
|
||||
|
||||
@@ -171,43 +173,47 @@ function query_user() {
|
||||
QUERY_ARGS=("$@")
|
||||
NAMESPACE=${QUERY_ARGS[1]}
|
||||
|
||||
DB_CMD=$(database_cmd ${NAMESPACE})
|
||||
if [[ -n ${TEST_DB_USER} ]]; then
|
||||
DB_CMD=$(database_cmd ${NAMESPACE})
|
||||
|
||||
# Sub-command to retrieve the test user
|
||||
DB_ARGS="\du ${TEST_DB_USER}"
|
||||
# Sub-command to retrieve the test user
|
||||
DB_ARGS="\du ${TEST_DB_USER}"
|
||||
|
||||
# Execute the command to query for the test user
|
||||
# Result should look like this: (assuming TEST_DB_NAME = test)
|
||||
# List of roles
|
||||
# Role name | Attributes | Member of
|
||||
# -------------------------+--------------+-----------
|
||||
# test_user | Cannot login | {}
|
||||
USERS=$(${DB_CMD} -tc ${DB_ARGS} | grep ${TEST_DB_USER} | wc -l)
|
||||
if [[ ${USERS} -ne 1 ]]; then
|
||||
# There should only be one user
|
||||
echo "${TEST_DB_USER} does not exist"
|
||||
return
|
||||
# Execute the command to query for the test user
|
||||
# Result should look like this: (assuming TEST_DB_NAME = test)
|
||||
# List of roles
|
||||
# Role name | Attributes | Member of
|
||||
# -------------------------+--------------+-----------
|
||||
# test_user | Cannot login | {}
|
||||
USERS=$(${DB_CMD} -tc ${DB_ARGS} | grep ${TEST_DB_USER} | wc -l)
|
||||
if [[ ${USERS} -ne 1 ]]; then
|
||||
# There should only be one user
|
||||
echo "${TEST_DB_USER} does not exist"
|
||||
return
|
||||
fi
|
||||
|
||||
# Sub-command to retrieve the grants for the test database
|
||||
DB_ARGS="\l+ ${TEST_DB_NAME}"
|
||||
|
||||
# Execute the command to query the grants for the test user.
|
||||
# Result should look like this: (assuming TEST_DB_NAME = test)
|
||||
# List of databases
|
||||
# Name | Owner | Encoding | Collate | Ctype | Access privileges | Size | Tablespace | Description
|
||||
# --------------------+----------+----------+------------+------------+--------------------------------------+---------+------------+-------------
|
||||
# test | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =Tc/postgres +| 7087 kB | pg_default |
|
||||
# | | | | | postgres=CTc/postgres +| | |
|
||||
# | | | | | test_user=CTc/postgres | | |
|
||||
GRANTS=$(${DB_CMD} -tc ${DB_ARGS} | grep "${TEST_DB_USER}=CTc" | wc -l)
|
||||
if [[ ${GRANTS} -ne 1 ]]; then
|
||||
# There should only be 1 GRANT statement for this user
|
||||
echo "${TEST_DB_USER} does not have the correct grants"
|
||||
return
|
||||
fi
|
||||
|
||||
echo "${TEST_DB_USER} exists and has the correct grants."
|
||||
else
|
||||
echo "Test user was not deployed in namespace ${NAMESPACE}"
|
||||
fi
|
||||
|
||||
# Sub-command to retrieve the grants for the test database
|
||||
DB_ARGS="\l+ ${TEST_DB_NAME}"
|
||||
|
||||
# Execute the command to query the grants for the test user.
|
||||
# Result should look like this: (assuming TEST_DB_NAME = test)
|
||||
# List of databases
|
||||
# Name | Owner | Encoding | Collate | Ctype | Access privileges | Size | Tablespace | Description
|
||||
# --------------------+----------+----------+------------+------------+--------------------------------------+---------+------------+-------------
|
||||
# test | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =Tc/postgres +| 7087 kB | pg_default |
|
||||
# | | | | | postgres=CTc/postgres +| | |
|
||||
# | | | | | test_user=CTc/postgres | | |
|
||||
GRANTS=$(${DB_CMD} -tc ${DB_ARGS} | grep "${TEST_DB_USER}=CTc" | wc -l)
|
||||
if [[ ${GRANTS} -ne 1 ]]; then
|
||||
# There should only be 1 GRANT statement for this user
|
||||
echo "${TEST_DB_USER} does not have the correct grants"
|
||||
return
|
||||
fi
|
||||
|
||||
echo "${TEST_DB_USER} exists and has the correct grants."
|
||||
}
|
||||
|
||||
# Params: <namespace>
|
||||
@@ -220,13 +226,17 @@ function delete_user_grants() {
|
||||
DELETE_GRANTS_ARGS=("$@")
|
||||
NAMESPACE=${DELETE_GRANTS_ARGS[1]}
|
||||
|
||||
DB_CMD=$(database_cmd ${NAMESPACE})
|
||||
if [[ -n ${TEST_DB_USER} ]]; then
|
||||
DB_CMD=$(database_cmd ${NAMESPACE})
|
||||
|
||||
# Execute the commands to delete the grants.
|
||||
if $DB_CMD -tc "SELECT rolname FROM pg_roles WHERE rolname='${TEST_DB_USER}';" | grep ${TEST_DB_USER}; then
|
||||
${DB_CMD} -tc "REVOKE ALL PRIVILEGES ON DATABASE ${TEST_DB_NAME} FROM ${TEST_DB_USER};"
|
||||
# Execute the commands to delete the grants.
|
||||
if $DB_CMD -tc "SELECT rolname FROM pg_roles WHERE rolname='${TEST_DB_USER}';" | grep ${TEST_DB_USER}; then
|
||||
${DB_CMD} -tc "REVOKE ALL PRIVILEGES ON DATABASE ${TEST_DB_NAME} FROM ${TEST_DB_USER};"
|
||||
else
|
||||
echo "Test user does not exist in namespace ${NAMESPACE}"
|
||||
fi
|
||||
else
|
||||
echo "Test user does not exist in namespace ${NAMESPACE}"
|
||||
echo "Test user was not deployed in namespace ${NAMESPACE}"
|
||||
fi
|
||||
}
|
||||
|
||||
@@ -309,5 +319,3 @@ function delete_backups() {
|
||||
done
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -129,6 +129,10 @@ spec:
|
||||
{{- if .Values.manifests.create_test_database }}
|
||||
- name: TEST_DB_NAME
|
||||
value: {{ .Values.conf.postgresql_backup_restore.test_database_name | quote }}
|
||||
{{- if .Values.conf.postgresql_backup_restore.test_database_user }}
|
||||
- name: TEST_DB_USER
|
||||
value: {{ .Values.conf.postgresql_backup_restore.test_database_user | quote }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
volumeMounts:
|
||||
- name: postgresql-utility-bin
|
||||
|
||||
@@ -109,6 +109,7 @@ conf:
|
||||
postgresql_backup_restore:
|
||||
enabled_namespaces: "openstack"
|
||||
test_database_name: "test_database"
|
||||
test_database_user: "test_database_user"
|
||||
secrets:
|
||||
rgw_secret: postgresql-backup-user
|
||||
conf_secret: postgresql-backup-restore
|
||||
|
||||
Reference in New Issue
Block a user