From 89da094cdcc155c3f9aa4fcfb56d6fc19816fb58 Mon Sep 17 00:00:00 2001
From: Arthur BOUDREAULT <arthur@lydra.fr>
Date: Thu, 18 Aug 2022 14:53:26 +0200
Subject: [PATCH] feat(ynh_config): add the posibility to change SSH Port

---
 roles/ynh_config/README-FR.md   | 10 ++++++++++
 roles/ynh_config/README.md      | 11 +++++++++++
 roles/ynh_config/tasks/main.yml | 20 +++++++++++++++++++-
 3 files changed, 40 insertions(+), 1 deletion(-)

diff --git a/roles/ynh_config/README-FR.md b/roles/ynh_config/README-FR.md
index bc959f0..d35a625 100644
--- a/roles/ynh_config/README-FR.md
+++ b/roles/ynh_config/README-FR.md
@@ -50,6 +50,16 @@ Si des mises à jour sont disponibles, elles sont faites automatiquement. En cas
 
 Pour en savoir plus sur le fonctionnement des mises à jour dans Yunohost vous pouvez vous rendre [ici](https://yunohost.org/fr/update). Le changelog des versions de Yunohost est aussi disponible [ici](https://forum.yunohost.org/tag/ynh_release).
 
+### Modification du port SSH
+
+Parmi les paramètres proposés dans YunoHost, il est possible de modifier le port SSH. Vous devez juste créer la variable `ynh_ssh_port` et le rôle se chargera d'aller récupérer le port SSH et de le modifier si nécessaire. Il va aussi effectuer les changements appropriés pour fail2ban et le firewall interne de YunoHost.
+Si votre instance YunoHost est derrière un firewall applicatif ou propre à votre fournisseur cloud, il faudra également ouvrir le groupe de sécurité approprié.
+
+```yml
+ynh_ssh_port: "812"
+```
+⚠️ Attention, à partir du moment où le port SSH est modifié, la prochaine fois que vous voudrez vous connecter en SSH sur le serveur YunoHost, il faudra renseigner le port SSH utilisé (par exemple `ssh -p 812 username@hostname`). Vous pouvez également externaliser cette configuration vers un fichier de configuration SSH (plus d'infos [ici](https://linuxize.com/post/using-the-ssh-config-file/)).
+
 ## Dépendances
 
 Aucune.
diff --git a/roles/ynh_config/README.md b/roles/ynh_config/README.md
index 829ccc5..665e700 100644
--- a/roles/ynh_config/README.md
+++ b/roles/ynh_config/README.md
@@ -50,6 +50,17 @@ If available, updates are done automatically. In case of problems following an a
 
 To learn more about how updates work in Yunohost you can go [here](https://yunohost.org/fr/update). The changelog of Yunohost versions is also available [here](https://forum.yunohost.org/tag/ynh_release).
 
+### SSH port modification
+
+Among the settings available in YunoHost, it is possible to change the SSH port. You just have to create the `ynh_ssh_port` variable and the role will retrieve the SSH port and modify it if necessary. It will also perform the adequate modifications regarding fail2ban and the internal firewall of YunoHost.
+In your YunoHost host is behind a firewall, you may consider creating the appropriate security group.
+
+``yml
+ynh_ssh_port: "812"
+```
+
+⚠️ Be careful, from the moment the SSH port is modified, the next time you want to connect to the YunoHost server with SSH, you will have to specify the SSH port to be used (for example `ssh -p 812 username@hostname`). You can also externalize this configuration to an SSH configuration file (more info [here](https://linuxize.com/post/using-the-ssh-config-file/)).
+
 ## Dependencies
 
 None.
diff --git a/roles/ynh_config/tasks/main.yml b/roles/ynh_config/tasks/main.yml
index e9d1561..b7f44f3 100644
--- a/roles/ynh_config/tasks/main.yml
+++ b/roles/ynh_config/tasks/main.yml
@@ -26,9 +26,27 @@
     - yunohost
     - smtp
 
-- name: Configures Yunohost autoupdate
+- name: Configure Yunohost autoupdate
   ansible.builtin.include_tasks: autoupdate.yml
   when: ynh_autoupdate.scheduled
   tags:
     - yunohost
     - update
+
+- name: Change SSH port
+  block:
+    - name: Get current SSH setting
+      ansible.builtin.command:
+        yunohost settings get security.ssh.port
+      register: _ynh_ssh_current_value
+      changed_when: False
+
+    - name: Set new SSH setting
+      ansible.builtin.command:
+        "yunohost settings set security.ssh.port -v {{ ynh_ssh_port }}"
+      when: _ynh_ssh_current_value != ynh_ssh_port
+      changed_when: False
+  when: ynh_ssh_port
+  tags:
+    - yunohost
+    - ssh
-- 
GitLab