From 61a4ff454374cd7ff1d31cd133d2ecff3b83241b Mon Sep 17 00:00:00 2001 From: SoledaD208 Date: Wed, 9 Oct 2024 21:08:25 +0700 Subject: [PATCH] issue-671: get ASNI_QUOTES from session sql_mode instead of GLOBAL sql_mode --- plugins/module_utils/user.py | 2 +- .../test_mysql_user/tasks/issue-671.yaml | 94 +++++++++++++++++++ .../targets/test_mysql_user/tasks/main.yml | 4 + 3 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 tests/integration/targets/test_mysql_user/tasks/issue-671.yaml diff --git a/plugins/module_utils/user.py b/plugins/module_utils/user.py index 7b6914f2..307ef6e6 100644 --- a/plugins/module_utils/user.py +++ b/plugins/module_utils/user.py @@ -32,7 +32,7 @@ class InvalidPrivsError(Exception): def get_mode(cursor): - cursor.execute('SELECT @@GLOBAL.sql_mode') + cursor.execute('SELECT @@sql_mode') result = cursor.fetchone() mode_str = result[0] if 'ANSI' in mode_str: diff --git a/tests/integration/targets/test_mysql_user/tasks/issue-671.yaml b/tests/integration/targets/test_mysql_user/tasks/issue-671.yaml new file mode 100644 index 00000000..e3aa7705 --- /dev/null +++ b/tests/integration/targets/test_mysql_user/tasks/issue-671.yaml @@ -0,0 +1,94 @@ +--- +# Due to https://bugs.mysql.com/bug.php?id=115953, in Mysql 8, if ANSI_QUOTES is enabled, +# backticks will be used instead of double quotes to quote functions or procedures name +# the workaround is disable ANSI_QUOTES and run the module. As a consequence, mysql_user +# and mysql_roles will always report "changed" for functions and procedures no matter +# the privileges are granted or not. +# Workaround for the mysql bug 116953 is removing ANSI_QUOTES from the moduule's session sql_mode. +# But because issue 671, ANSI_QUOTES is always got from GLOBAL sql_mode, thus this workaround can't work +# even without the Mysql bug, because sql_mode in session precedes GLOBAL sql_mode. we should +# check for sql_mode in session variable instead of the GLOBAL +- vars: + mysql_parameters: &mysql_params + login_user: '{{ mysql_user }}' + login_password: '{{ mysql_password }}' + login_host: '{{ mysql_host }}' + login_port: '{{ mysql_primary_port }}' + + block: + - name: Issue-671| test setup | drop database + mysql_db: + <<: *mysql_params + name: "{{ item }}" + state: absent + loop: + - foo + - bar + + - name: Issue-671| test setup | create database + mysql_db: + <<: *mysql_params + name: "{{ item }}" + state: present + loop: + - foo + - bar + + - name: Issue-671| enable sql_mode ANSI_QUOTES + mysql_variables: + <<: *mysql_params + variable: sql_mode + value: "ANSI_QUOTES" + mode: persist + + - name: Issue-671| Copy SQL scripts to remote + copy: + src: "{{ item }}" + dest: "{{ remote_tmp_dir }}/{{ item | basename }}" + with_items: + - create-function.sql + - create-procedure.sql + + - name: Issue-671| Create function for test + shell: "{{ mysql_command }} < {{ remote_tmp_dir }}/create-function.sql" + + - name: Issue-671| Create procedure for test + shell: "{{ mysql_command }} < {{ remote_tmp_dir }}/create-procedure.sql" + + - name: Issue-671| Create user with FUNCTION and PROCEDURE privileges + mysql_user: + <<: *mysql_params + name: '{{ user_name_2 }}' + password: '{{ user_password_2 }}' + state: present + priv: 'FUNCTION foo.function:EXECUTE/foo.*:SELECT/PROCEDURE bar.procedure:EXECUTE' + + - name: Issue-671| Grant the privileges again, remove ANSI_QUOTES from the session variable + mysql_user: + <<: *mysql_params + session_vars: + "sql_mode": "" + name: '{{ user_name_2 }}' + password: '{{ user_password_2 }}' + state: present + priv: 'FUNCTION foo.function:EXECUTE/foo.*:SELECT/PROCEDURE bar.procedure:EXECUTE' + register: result + + - name: Issue-671| Assert Create user with FUNCTION and PROCEDURE privileges + assert: + that: + - result is success + - result is not changed + + - name: Issue-671| Test teardown | cleanup databases + mysql_db: + <<: *mysql_params + name: "{{ item }}" + state: absent + loop: + - foo + - bar + + - include_tasks: utils/remove_user.yml + vars: + user_name: "{{ user_name_2 }}" diff --git a/tests/integration/targets/test_mysql_user/tasks/main.yml b/tests/integration/targets/test_mysql_user/tasks/main.yml index e77c4436..4e9046f3 100644 --- a/tests/integration/targets/test_mysql_user/tasks/main.yml +++ b/tests/integration/targets/test_mysql_user/tasks/main.yml @@ -282,6 +282,10 @@ - import_tasks: issue-64560.yaml tags: - issue-64560 + + - import_tasks: issue-671.yaml + tags: + - issue-671 # Test that mysql_user still works with force_context enabled (database set to "mysql") # (https://github.com/ansible-collections/community.mysql/issues/265)