From fd97c8e56c776508527a997dd6a29442ebf13421 Mon Sep 17 00:00:00 2001 From: Adam Miller Date: Thu, 11 Oct 2018 12:51:40 -0500 Subject: [PATCH] fix yum proxy username/password handling (#46291) Fixes #46249 Signed-off-by: Adam Miller --- lib/ansible/modules/packaging/os/yum.py | 19 ++-- test/integration/targets/yum/tasks/main.yml | 2 + test/integration/targets/yum/tasks/proxy.yml | 96 ++++++++++++++++++++ 3 files changed, 111 insertions(+), 6 deletions(-) create mode 100644 test/integration/targets/yum/tasks/proxy.yml diff --git a/lib/ansible/modules/packaging/os/yum.py b/lib/ansible/modules/packaging/os/yum.py index de6e2a8c8cc..2fcc682a455 100644 --- a/lib/ansible/modules/packaging/os/yum.py +++ b/lib/ansible/modules/packaging/os/yum.py @@ -701,20 +701,27 @@ class YumModule(YumDnf): # setting system proxy environment and saving old, if exists my = self.yum_base() namepass = "" + proxy_url = "" scheme = ["http", "https"] old_proxy_env = [os.getenv("http_proxy"), os.getenv("https_proxy")] try: if my.conf.proxy: if my.conf.proxy_username: namepass = namepass + my.conf.proxy_username + proxy_url = my.conf.proxy if my.conf.proxy_password: namepass = namepass + ":" + my.conf.proxy_password - namepass = namepass + '@' - for item in scheme: - os.environ[item + "_proxy"] = re.sub( - r"(http://)", - r"\1" + namepass, my.conf.proxy - ) + elif '@' in my.conf.proxy: + namepass = my.conf.proxy.split('@')[0].split('//')[-1] + proxy_url = my.conf.proxy.replace("{0}@".format(namepass), "") + + if namepass: + namepass = namepass + '@' + for item in scheme: + os.environ[item + "_proxy"] = re.sub( + r"(http://)", + r"\1" + namepass, proxy_url + ) yield except yum.Errors.YumBaseError: raise diff --git a/test/integration/targets/yum/tasks/main.yml b/test/integration/targets/yum/tasks/main.yml index 9e08dcf2a33..6733857fe88 100644 --- a/test/integration/targets/yum/tasks/main.yml +++ b/test/integration/targets/yum/tasks/main.yml @@ -69,3 +69,5 @@ - include: 'yum_group_remove.yml' when: - (ansible_distribution in ['RedHat', 'CentOS', 'ScientificLinux'] and ansible_distribution_major_version|int > 6) + +- include: 'proxy.yml' diff --git a/test/integration/targets/yum/tasks/proxy.yml b/test/integration/targets/yum/tasks/proxy.yml new file mode 100644 index 00000000000..c506c5e563e --- /dev/null +++ b/test/integration/targets/yum/tasks/proxy.yml @@ -0,0 +1,96 @@ +- name: test yum proxy settings + block: + - yum: + name: 'https://s3.amazonaws.com/ansible-ci-files/test/integration/targets/yum/tinyproxy-1.10.0-3.el7.x86_64.rpm' + state: installed + + - lineinfile: + path: /etc/tinyproxy/tinyproxy.conf + line: "BasicAuth testuser testpassword" + state: present + + # systemd doesn't play nice with this in a container for some reason + - shell: tinyproxy + changed_when: false + + - lineinfile: + path: /etc/yum.conf + line: "proxy=http://testuser:testpassword@127.0.0.1:8888" + state: present + + - yum: + name: 'https://s3.amazonaws.com/ansible-ci-files/test/integration/targets/yum/ninvaders-0.1.1-18.el7.x86_64.rpm' + state: installed + register: yum_proxy_result + + - assert: + that: + - "yum_proxy_result.changed" + - "'msg' in yum_proxy_result" + - "'rc' in yum_proxy_result" + + - yum: + name: ninvaders + state: absent + + - lineinfile: + path: /etc/yum.conf + line: "proxy=http://testuser:testpassword@127.0.0.1:8888" + state: absent + + - lineinfile: + path: /etc/yum.conf + line: "proxy=http://127.0.0.1:8888" + state: present + + - lineinfile: + path: /etc/yum.conf + line: "proxy_username=testuser" + state: present + + - lineinfile: + path: /etc/yum.conf + line: "proxy_password=testpassword" + state: present + + - yum: + name: 'https://s3.amazonaws.com/ansible-ci-files/test/integration/targets/yum/ninvaders-0.1.1-18.el7.x86_64.rpm' + state: installed + register: yum_proxy_result + + - assert: + that: + - "yum_proxy_result.changed" + - "'msg' in yum_proxy_result" + - "'rc' in yum_proxy_result" + + always: + - yum: + name: tinyproxy + state: absent + + - yum: + name: ninvaders + state: absent + + - lineinfile: + path: /etc/yum.conf + line: "proxy=http://testuser:testpassword@127.0.0.1:8888" + state: absent + + - lineinfile: + path: /etc/yum.conf + line: "proxy=http://127.0.0.1:8888" + state: absent + + - lineinfile: + path: /etc/yum.conf + line: "proxy_username=testuser" + state: absent + + - lineinfile: + path: /etc/yum.conf + line: "proxy_password=testpassword" + state: absent + when: + - (ansible_distribution in ['RedHat', 'CentOS', 'ScientificLinux'] and ansible_distribution_major_version|int == 7)