From 0c34cdd0f78dbb413e1ec067cb84b000bc465e6f Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Mon, 23 Oct 2017 11:40:54 -0700 Subject: [PATCH] fix #Requires to be valid in real Powershell (#31858) * module_utils #Requires should not have .psm1 extension if "real" Powershell will ever execute them * updated validate-modules to enforce this * added check to disallow multi-module syntax on Ansible.ModuleUtils #Requires --- lib/ansible/modules/windows/win_acl.ps1 | 4 +- .../modules/windows/win_audit_rule.ps1 | 4 +- lib/ansible/modules/windows/win_command.ps1 | 4 +- lib/ansible/modules/windows/win_copy.ps1 | 2 +- .../modules/windows/win_dotnet_ngen.ps1 | 4 +- .../modules/windows/win_environment.ps1 | 2 +- .../modules/windows/win_eventlog_entry.ps1 | 2 +- lib/ansible/modules/windows/win_file.ps1 | 2 +- lib/ansible/modules/windows/win_hotfix.ps1 | 2 +- .../modules/windows/win_mapped_drive.ps1 | 2 +- lib/ansible/modules/windows/win_owner.ps1 | 4 +- .../modules/windows/win_power_plan.ps1 | 2 +- lib/ansible/modules/windows/win_regedit.ps1 | 2 +- lib/ansible/modules/windows/win_share.ps1 | 4 +- lib/ansible/modules/windows/win_shell.ps1 | 4 +- lib/ansible/modules/windows/win_shortcut.ps1 | 2 +- lib/ansible/modules/windows/win_toast.ps1 | 2 +- lib/ansible/modules/windows/win_uri.ps1 | 2 +- .../modules/windows/win_user_right.ps1 | 2 +- lib/ansible/modules/windows/win_wait_for.ps1 | 2 +- test/sanity/validate-modules/main.py | 38 ++++++++++++++++--- 21 files changed, 60 insertions(+), 32 deletions(-) diff --git a/lib/ansible/modules/windows/win_acl.ps1 b/lib/ansible/modules/windows/win_acl.ps1 index a67f6d753d..dc27132b8a 100644 --- a/lib/ansible/modules/windows/win_acl.ps1 +++ b/lib/ansible/modules/windows/win_acl.ps1 @@ -4,8 +4,8 @@ # Copyright 2015, Hans-Joachim Kliemeck # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) -#Requires -Module Ansible.ModuleUtils.Legacy.psm1 -#Requires -Module Ansible.ModuleUtils.SID.psm1 +#Requires -Module Ansible.ModuleUtils.Legacy +#Requires -Module Ansible.ModuleUtils.SID # win_acl module (File/Resources Permission Additions/Removal) diff --git a/lib/ansible/modules/windows/win_audit_rule.ps1 b/lib/ansible/modules/windows/win_audit_rule.ps1 index 4eb3ed163c..0d3ebf57b3 100644 --- a/lib/ansible/modules/windows/win_audit_rule.ps1 +++ b/lib/ansible/modules/windows/win_audit_rule.ps1 @@ -3,8 +3,8 @@ # Copyright: (c) 2017, Noah Sparks # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) -#Requires -Module Ansible.ModuleUtils.Legacy.psm1 -#Requires -Module Ansible.ModuleUtils.SID.psm1 +#Requires -Module Ansible.ModuleUtils.Legacy +#Requires -Module Ansible.ModuleUtils.SID $params = Parse-Args -arguments $args -supports_check_mode $true $check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false diff --git a/lib/ansible/modules/windows/win_command.ps1 b/lib/ansible/modules/windows/win_command.ps1 index da6b4f62b9..9cdd125df3 100644 --- a/lib/ansible/modules/windows/win_command.ps1 +++ b/lib/ansible/modules/windows/win_command.ps1 @@ -4,8 +4,8 @@ # Copyright (c) 2017 Ansible Project # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) -#Requires -Module Ansible.ModuleUtils.Legacy.psm1 -#Requires -Module Ansible.ModuleUtils.CommandUtil.psm1 +#Requires -Module Ansible.ModuleUtils.Legacy +#Requires -Module Ansible.ModuleUtils.CommandUtil # TODO: add check mode support diff --git a/lib/ansible/modules/windows/win_copy.ps1 b/lib/ansible/modules/windows/win_copy.ps1 index 2af02105fe..18607d8c21 100644 --- a/lib/ansible/modules/windows/win_copy.ps1 +++ b/lib/ansible/modules/windows/win_copy.ps1 @@ -5,7 +5,7 @@ # Copyright (c) 2017 Ansible Project # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) -#Requires -Module Ansible.ModuleUtils.Legacy.psm1 +#Requires -Module Ansible.ModuleUtils.Legacy $ErrorActionPreference = 'Stop' diff --git a/lib/ansible/modules/windows/win_dotnet_ngen.ps1 b/lib/ansible/modules/windows/win_dotnet_ngen.ps1 index e7ec31939f..ce2dab5080 100644 --- a/lib/ansible/modules/windows/win_dotnet_ngen.ps1 +++ b/lib/ansible/modules/windows/win_dotnet_ngen.ps1 @@ -5,8 +5,8 @@ # Copyright (c) 2017 Ansible Project # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) -#Requires -Module Ansible.ModuleUtils.Legacy.psm1 -#Requires -Module Ansible.ModuleUtils.CommandUtil.psm1 +#Requires -Module Ansible.ModuleUtils.Legacy +#Requires -Module Ansible.ModuleUtils.CommandUtil $ErrorActionPreference = 'Stop' diff --git a/lib/ansible/modules/windows/win_environment.ps1 b/lib/ansible/modules/windows/win_environment.ps1 index 48479231c7..0eb7374dec 100644 --- a/lib/ansible/modules/windows/win_environment.ps1 +++ b/lib/ansible/modules/windows/win_environment.ps1 @@ -3,7 +3,7 @@ # Copyright: (c) 2015, Jon Hawkesworth (@jhawkesworth) # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) -#Requires -Module Ansible.ModuleUtils.Legacy.psm1 +#Requires -Module Ansible.ModuleUtils.Legacy $ErrorActionPreference = "Stop" diff --git a/lib/ansible/modules/windows/win_eventlog_entry.ps1 b/lib/ansible/modules/windows/win_eventlog_entry.ps1 index e40bdea0a4..82bf55b736 100644 --- a/lib/ansible/modules/windows/win_eventlog_entry.ps1 +++ b/lib/ansible/modules/windows/win_eventlog_entry.ps1 @@ -3,7 +3,7 @@ # (c) 2017, Andrew Saraceni # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) -#Requires -Module Ansible.ModuleUtils.Legacy.psm1 +#Requires -Module Ansible.ModuleUtils.Legacy $ErrorActionPreference = "Stop" diff --git a/lib/ansible/modules/windows/win_file.ps1 b/lib/ansible/modules/windows/win_file.ps1 index f6398b8334..7c08ac5ad6 100644 --- a/lib/ansible/modules/windows/win_file.ps1 +++ b/lib/ansible/modules/windows/win_file.ps1 @@ -4,7 +4,7 @@ # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) -#Requires -Module Ansible.ModuleUtils.Legacy.psm1 +#Requires -Module Ansible.ModuleUtils.Legacy $ErrorActionPreference = "Stop" diff --git a/lib/ansible/modules/windows/win_hotfix.ps1 b/lib/ansible/modules/windows/win_hotfix.ps1 index 478d58be74..8abfd5f46c 100644 --- a/lib/ansible/modules/windows/win_hotfix.ps1 +++ b/lib/ansible/modules/windows/win_hotfix.ps1 @@ -4,7 +4,7 @@ # Copyright (c) 2017 Ansible Project # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) -#Requires -Module Ansible.ModuleUtils.Legacy.psm1 +#Requires -Module Ansible.ModuleUtils.Legacy $ErrorActionPreference = "Stop" diff --git a/lib/ansible/modules/windows/win_mapped_drive.ps1 b/lib/ansible/modules/windows/win_mapped_drive.ps1 index ed5ac57603..4a3f4ae2dc 100644 --- a/lib/ansible/modules/windows/win_mapped_drive.ps1 +++ b/lib/ansible/modules/windows/win_mapped_drive.ps1 @@ -4,7 +4,7 @@ # Copyright (c) 2017 Ansible Project # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) -#Requires -Module Ansible.ModuleUtils.Legacy.psm1 +#Requires -Module Ansible.ModuleUtils.Legacy $ErrorActionPreference = 'Stop' diff --git a/lib/ansible/modules/windows/win_owner.ps1 b/lib/ansible/modules/windows/win_owner.ps1 index 5be18df595..6cd5b187f5 100644 --- a/lib/ansible/modules/windows/win_owner.ps1 +++ b/lib/ansible/modules/windows/win_owner.ps1 @@ -2,8 +2,8 @@ # Copyright 2015, Hans-Joachim Kliemeck # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) -#Requires -Module Ansible.ModuleUtils.Legacy.psm1 -#Requires -Module Ansible.ModuleUtils.SID.psm1 +#Requires -Module Ansible.ModuleUtils.Legacy +#Requires -Module Ansible.ModuleUtils.SID $result = @{ changed = $false diff --git a/lib/ansible/modules/windows/win_power_plan.ps1 b/lib/ansible/modules/windows/win_power_plan.ps1 index ef4990feba..6022b447b8 100644 --- a/lib/ansible/modules/windows/win_power_plan.ps1 +++ b/lib/ansible/modules/windows/win_power_plan.ps1 @@ -3,7 +3,7 @@ # Copyright (c) 2017 Ansible Project # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) -#Requires -Module Ansible.ModuleUtils.Legacy.psm1 +#Requires -Module Ansible.ModuleUtils.Legacy $params = Parse-Args -arguments $args -supports_check_mode $true $check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false diff --git a/lib/ansible/modules/windows/win_regedit.ps1 b/lib/ansible/modules/windows/win_regedit.ps1 index 28d7bc117b..76654b13e0 100644 --- a/lib/ansible/modules/windows/win_regedit.ps1 +++ b/lib/ansible/modules/windows/win_regedit.ps1 @@ -5,7 +5,7 @@ # (c) 2017, Jordan Borean # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) -#Requires -Module Ansible.ModuleUtils.Legacy.psm1 +#Requires -Module Ansible.ModuleUtils.Legacy $ErrorActionPreference = "Stop" diff --git a/lib/ansible/modules/windows/win_share.ps1 b/lib/ansible/modules/windows/win_share.ps1 index 572e78b78c..50c94899a3 100644 --- a/lib/ansible/modules/windows/win_share.ps1 +++ b/lib/ansible/modules/windows/win_share.ps1 @@ -2,8 +2,8 @@ # Copyright 2015, Hans-Joachim Kliemeck # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) -#Requires -Module Ansible.ModuleUtils.Legacy.psm1 -#Requires -Module Ansible.ModuleUtils.SID.psm1 +#Requires -Module Ansible.ModuleUtils.Legacy +#Requires -Module Ansible.ModuleUtils.SID #Functions Function NormalizeAccounts { diff --git a/lib/ansible/modules/windows/win_shell.ps1 b/lib/ansible/modules/windows/win_shell.ps1 index ee7172c826..0e293b5bba 100644 --- a/lib/ansible/modules/windows/win_shell.ps1 +++ b/lib/ansible/modules/windows/win_shell.ps1 @@ -4,8 +4,8 @@ # Copyright (c) 2017 Ansible Project # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) -#Requires -Module Ansible.ModuleUtils.Legacy.psm1 -#Requires -Module Ansible.ModuleUtils.CommandUtil.psm1 +#Requires -Module Ansible.ModuleUtils.Legacy +#Requires -Module Ansible.ModuleUtils.CommandUtil # TODO: add check mode support diff --git a/lib/ansible/modules/windows/win_shortcut.ps1 b/lib/ansible/modules/windows/win_shortcut.ps1 index 421276e4c0..e4e996c5ec 100644 --- a/lib/ansible/modules/windows/win_shortcut.ps1 +++ b/lib/ansible/modules/windows/win_shortcut.ps1 @@ -5,7 +5,7 @@ # Based on: http://powershellblogger.com/2016/01/create-shortcuts-lnk-or-url-files-with-powershell/ -#Requires -Module Ansible.ModuleUtils.Legacy.psm1 +#Requires -Module Ansible.ModuleUtils.Legacy $ErrorActionPreference = "Stop" diff --git a/lib/ansible/modules/windows/win_toast.ps1 b/lib/ansible/modules/windows/win_toast.ps1 index 6f1dd5d956..6208f0d75a 100644 --- a/lib/ansible/modules/windows/win_toast.ps1 +++ b/lib/ansible/modules/windows/win_toast.ps1 @@ -5,7 +5,7 @@ # Copyright (c) 2017 Ansible Project # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) -#Requires -Module Ansible.ModuleUtils.Legacy.psm1 +#Requires -Module Ansible.ModuleUtils.Legacy $ErrorActionPreference = "Stop" diff --git a/lib/ansible/modules/windows/win_uri.ps1 b/lib/ansible/modules/windows/win_uri.ps1 index 78c1760598..d4ac4ee174 100644 --- a/lib/ansible/modules/windows/win_uri.ps1 +++ b/lib/ansible/modules/windows/win_uri.ps1 @@ -4,7 +4,7 @@ # Copyright: (c) 2017, Dag Wieers (@dagwieers) # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) -#Requires -Module Ansible.ModuleUtils.Legacy.psm1 +#Requires -Module Ansible.ModuleUtils.Legacy $ErrorActionPreference = "Stop" diff --git a/lib/ansible/modules/windows/win_user_right.ps1 b/lib/ansible/modules/windows/win_user_right.ps1 index 452f7c77dd..97f74ca268 100644 --- a/lib/ansible/modules/windows/win_user_right.ps1 +++ b/lib/ansible/modules/windows/win_user_right.ps1 @@ -4,7 +4,7 @@ # Copyright (c) 2017 Ansible Project # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) -#Requires -Module Ansible.ModuleUtils.Legacy.psm1 +#Requires -Module Ansible.ModuleUtils.Legacy $ErrorActionPreference = 'Stop' $params = Parse-Args $args -supports_check_mode $true diff --git a/lib/ansible/modules/windows/win_wait_for.ps1 b/lib/ansible/modules/windows/win_wait_for.ps1 index 068736c582..280e7480d1 100644 --- a/lib/ansible/modules/windows/win_wait_for.ps1 +++ b/lib/ansible/modules/windows/win_wait_for.ps1 @@ -4,7 +4,7 @@ # Copyright (c) 2017 Ansible Project # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) -#Requires -Module Ansible.ModuleUtils.Legacy.psm1 +#Requires -Module Ansible.ModuleUtils.Legacy $ErrorActionPreference = "Stop" diff --git a/test/sanity/validate-modules/main.py b/test/sanity/validate-modules/main.py index 4d2f69696e..e9790f666c 100755 --- a/test/sanity/validate-modules/main.py +++ b/test/sanity/validate-modules/main.py @@ -635,13 +635,41 @@ class ModuleValidator(Validator): line=import_line ) - def _find_ps_replacers(self): - ps_module_util_template = '#Requires -Module Ansible.ModuleUtils.' - if ps_module_util_template not in self.text and REPLACER_WINDOWS not in self.text: + def _validate_ps_replacers(self): + # loop all (for/else + error) + # get module list for each + # check "shape" of each module name + + module_requires = r'(?im)^#\s*requires\s+\-module(?:s?)\s*(Ansible\.ModuleUtils\..+)' + found_requires = False + + for req_stmt in re.finditer(module_requires, self.text): + found_requires = True + # this will bomb on dictionary format - "don't do that" + module_list = [x.strip() for x in req_stmt.group(1).split(',')] + if len(module_list) > 1: + self.reporter.error( + path=self.object_path, + code=210, + msg='Ansible.ModuleUtils requirements do not support multiple modules per statement: "%s"' % req_stmt.group(0) + ) + continue + + module_name = module_list[0] + + if module_name.lower().endswith('.psm1'): + self.reporter.error( + path=self.object_path, + code=211, + msg='Module #Requires should not end in .psm1: "%s"' % module_name + ) + + # also accept the legacy #POWERSHELL_COMMON replacer signal + if not found_requires and REPLACER_WINDOWS not in self.text: self.reporter.error( path=self.object_path, code=207, - msg='"%s" not found in module' % ps_module_util_template + msg='No Ansible.ModuleUtils module requirements/imports found' ) def _find_ps_docs_py_file(self): @@ -1087,7 +1115,7 @@ class ModuleValidator(Validator): self._ensure_imports_below_docs(doc_info, first_callable) if self._powershell_module(): - self._find_ps_replacers() + self._validate_ps_replacers() self._find_ps_docs_py_file() self._check_for_gpl3_header()