From 6383c82328f32a6221fa7dd88fc92910966b5457 Mon Sep 17 00:00:00 2001
From: Alexei Znamensky <103110+russoz@users.noreply.github.com>
Date: Fri, 23 Dec 2022 09:34:21 +1300
Subject: [PATCH] ssh_config: fixed sanity (#5720)

* ssh_config: fix sanity checks

* fixed mod utils and removed sanity ignores

* update BOTMETA

* add changelog fragment

* Update plugins/module_utils/ssh.py

Co-authored-by: Felix Fontein <felix@fontein.de>

Co-authored-by: Felix Fontein <felix@fontein.de>
---
 .github/BOTMETA.yml                           |  2 ++
 .../5720-ssh_config-plugin-sanity.yml         |  2 ++
 plugins/module_utils/ssh.py                   | 21 +++++++++++++++++++
 plugins/modules/ssh_config.py                 |  6 ++----
 tests/sanity/ignore-2.11.txt                  |  1 -
 tests/sanity/ignore-2.12.txt                  |  1 -
 tests/sanity/ignore-2.13.txt                  |  1 -
 tests/sanity/ignore-2.14.txt                  |  1 -
 tests/sanity/ignore-2.15.txt                  |  1 -
 9 files changed, 27 insertions(+), 9 deletions(-)
 create mode 100644 changelogs/fragments/5720-ssh_config-plugin-sanity.yml
 create mode 100644 plugins/module_utils/ssh.py

diff --git a/.github/BOTMETA.yml b/.github/BOTMETA.yml
index 0a0cded0c1..047d537895 100644
--- a/.github/BOTMETA.yml
+++ b/.github/BOTMETA.yml
@@ -323,6 +323,8 @@ files:
   $module_utils/scaleway.py:
     labels: cloud scaleway
     maintainers: $team_scaleway
+  $module_utils/ssh.py:
+    maintainers: russoz
   $module_utils/storage/hpe3par/hpe3par.py:
     maintainers: farhan7500 gautamphegde
   $module_utils/utm_utils.py:
diff --git a/changelogs/fragments/5720-ssh_config-plugin-sanity.yml b/changelogs/fragments/5720-ssh_config-plugin-sanity.yml
new file mode 100644
index 0000000000..19d57ea145
--- /dev/null
+++ b/changelogs/fragments/5720-ssh_config-plugin-sanity.yml
@@ -0,0 +1,2 @@
+minor_changes:
+  - ssh_config - refactor code to module util to fix sanity check (https://github.com/ansible-collections/community.general/pull/5720).
diff --git a/plugins/module_utils/ssh.py b/plugins/module_utils/ssh.py
new file mode 100644
index 0000000000..082839e26d
--- /dev/null
+++ b/plugins/module_utils/ssh.py
@@ -0,0 +1,21 @@
+# -*- coding: utf-8 -*-
+# Copyright (c) 2015, Björn Andersson
+# Copyright (c) 2021, Ansible Project
+# Copyright (c) 2021, Abhijeet Kasurde <akasurde@redhat.com>
+# Copyright (c) 2022, Alexei Znamensky <russoz@gmail.com>
+# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
+# SPDX-License-Identifier: GPL-3.0-or-later
+
+from __future__ import absolute_import, division, print_function
+__metaclass__ = type
+
+
+import os
+
+
+def determine_config_file(user, config_file):
+    if user:
+        config_file = os.path.join(os.path.expanduser('~%s' % user), '.ssh', 'config')
+    elif config_file is None:
+        config_file = '/etc/ssh/ssh_config'
+    return config_file
diff --git a/plugins/modules/ssh_config.py b/plugins/modules/ssh_config.py
index cb028ac8e5..f2fa8aa457 100644
--- a/plugins/modules/ssh_config.py
+++ b/plugins/modules/ssh_config.py
@@ -169,6 +169,7 @@ except ImportError:
 
 from ansible.module_utils.basic import AnsibleModule, missing_required_lib
 from ansible.module_utils.common.text.converters import to_native
+from ansible_collections.community.general.plugins.module_utils.ssh import determine_config_file
 
 
 class SSHConfig():
@@ -188,10 +189,7 @@ class SSHConfig():
         self.config.load()
 
     def check_ssh_config_path(self):
-        if self.user:
-            self.config_file = os.path.join(os.path.expanduser('~%s' % self.user), '.ssh', 'config')
-        elif self.config_file is None:
-            self.config_file = '/etc/ssh/ssh_config'
+        self.config_file = determine_config_file(self.user, self.config_file)
 
         # See if the identity file exists or not, relative to the config file
         if os.path.exists(self.config_file) and self.identity_file is not None:
diff --git a/tests/sanity/ignore-2.11.txt b/tests/sanity/ignore-2.11.txt
index e49b216f6e..77738afd40 100644
--- a/tests/sanity/ignore-2.11.txt
+++ b/tests/sanity/ignore-2.11.txt
@@ -23,7 +23,6 @@ plugins/modules/rax_files_objects.py use-argspec-type-path
 plugins/modules/rax_files.py validate-modules:parameter-state-invalid-choice
 plugins/modules/rax.py use-argspec-type-path # fix needed
 plugins/modules/rhevm.py validate-modules:parameter-state-invalid-choice
-plugins/modules/ssh_config.py use-argspec-type-path # Required since module uses other methods to specify path
 plugins/modules/xfconf.py validate-modules:return-syntax-error
 plugins/modules/yarn.py use-argspec-type-path
 tests/integration/targets/django_manage/files/base_test/simple_project/p1/manage.py compile-2.6   # django generated code
diff --git a/tests/sanity/ignore-2.12.txt b/tests/sanity/ignore-2.12.txt
index 0cf9b73046..61494a1ab1 100644
--- a/tests/sanity/ignore-2.12.txt
+++ b/tests/sanity/ignore-2.12.txt
@@ -18,6 +18,5 @@ plugins/modules/rax_files_objects.py use-argspec-type-path
 plugins/modules/rax_files.py validate-modules:parameter-state-invalid-choice
 plugins/modules/rax.py use-argspec-type-path # fix needed
 plugins/modules/rhevm.py validate-modules:parameter-state-invalid-choice
-plugins/modules/ssh_config.py use-argspec-type-path # Required since module uses other methods to specify path
 plugins/modules/xfconf.py validate-modules:return-syntax-error
 plugins/modules/yarn.py use-argspec-type-path
diff --git a/tests/sanity/ignore-2.13.txt b/tests/sanity/ignore-2.13.txt
index 0cf9b73046..61494a1ab1 100644
--- a/tests/sanity/ignore-2.13.txt
+++ b/tests/sanity/ignore-2.13.txt
@@ -18,6 +18,5 @@ plugins/modules/rax_files_objects.py use-argspec-type-path
 plugins/modules/rax_files.py validate-modules:parameter-state-invalid-choice
 plugins/modules/rax.py use-argspec-type-path # fix needed
 plugins/modules/rhevm.py validate-modules:parameter-state-invalid-choice
-plugins/modules/ssh_config.py use-argspec-type-path # Required since module uses other methods to specify path
 plugins/modules/xfconf.py validate-modules:return-syntax-error
 plugins/modules/yarn.py use-argspec-type-path
diff --git a/tests/sanity/ignore-2.14.txt b/tests/sanity/ignore-2.14.txt
index efdb15f969..6b9026caea 100644
--- a/tests/sanity/ignore-2.14.txt
+++ b/tests/sanity/ignore-2.14.txt
@@ -19,7 +19,6 @@ plugins/modules/rax_files_objects.py use-argspec-type-path
 plugins/modules/rax_files.py validate-modules:parameter-state-invalid-choice
 plugins/modules/rax.py use-argspec-type-path # fix needed
 plugins/modules/rhevm.py validate-modules:parameter-state-invalid-choice
-plugins/modules/ssh_config.py use-argspec-type-path # Required since module uses other methods to specify path
 plugins/modules/udm_user.py import-3.11  # Uses deprecated stdlib library 'crypt'
 plugins/modules/xfconf.py validate-modules:return-syntax-error
 plugins/modules/yarn.py use-argspec-type-path
diff --git a/tests/sanity/ignore-2.15.txt b/tests/sanity/ignore-2.15.txt
index efdb15f969..6b9026caea 100644
--- a/tests/sanity/ignore-2.15.txt
+++ b/tests/sanity/ignore-2.15.txt
@@ -19,7 +19,6 @@ plugins/modules/rax_files_objects.py use-argspec-type-path
 plugins/modules/rax_files.py validate-modules:parameter-state-invalid-choice
 plugins/modules/rax.py use-argspec-type-path # fix needed
 plugins/modules/rhevm.py validate-modules:parameter-state-invalid-choice
-plugins/modules/ssh_config.py use-argspec-type-path # Required since module uses other methods to specify path
 plugins/modules/udm_user.py import-3.11  # Uses deprecated stdlib library 'crypt'
 plugins/modules/xfconf.py validate-modules:return-syntax-error
 plugins/modules/yarn.py use-argspec-type-path