From f49a46b5f10ea262a8071776dd1c9336d291afa4 Mon Sep 17 00:00:00 2001
From: Arthur BOUDREAULT <arthur@lydra.fr>
Date: Tue, 28 Feb 2023 17:19:16 +0100
Subject: [PATCH] fix: (YNH) sudo without password for admins

---
 roles/ynh_config/README-FR.md                          | 10 ++++++++++
 roles/ynh_config/README.md                             | 10 ++++++++++
 roles/ynh_config/defaults/main.yml                     |  3 ++-
 roles/ynh_config/tasks/main.yml                        |  5 ++---
 .../{sshd_configuration.yml => ssh_configuration.yml}  |  7 +++++++
 5 files changed, 31 insertions(+), 4 deletions(-)
 rename roles/ynh_config/tasks/{sshd_configuration.yml => ssh_configuration.yml} (88%)

diff --git a/roles/ynh_config/README-FR.md b/roles/ynh_config/README-FR.md
index e23b072..defa612 100644
--- a/roles/ynh_config/README-FR.md
+++ b/roles/ynh_config/README-FR.md
@@ -61,6 +61,16 @@ 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/)). Vous pouvez aussi indiquer cette configuration dans votre fichier d'inventaire sinon Ansible ne pourra plus se connecter à votre serveur. (plus d'infos [ici](https://docs.ansible.com/ansible/latest/reference_appendices/faq.html#how-do-i-handle-different-machines-needing-different-user-accounts-or-ports-to-log-in-with)).
 
+
+### Utilisation de sudo sans mot de passe
+
+A partir de Yunohost 11.1, un nouveau groupe d'administrateurs est créé sur l'instance. Il s'agit d'un groupe Unix qui est intégré à YunoHost et son LDAP. Tous les utilisateurs dans ce groupe auront accès à la console d'administration en ligne YunoHost mais pourront également se connecter en SSH et utiliser la commande sudo (pour prendre temporairement les droits root).
+Par défaut, l'utilisateur doit taper son mot de passe pour utiliser la commande sudo mais il est possible de désactiver cette vérification depuis l'interface web (`outils` > `Paramètres de YunoHost` > `Permettre aux administrateurs d'utiliser 'sudo' sans retaper leur mot de passe`) ou en modifiant la variable `ynh_sudo_passwordless` à `true` dans votre fichier de variables Ansible. Plus d'informations disponibles [ici](https://forum.yunohost.org/t/yunohost-11-1-release-sortie-de-yunohost-11-1/23378#sudo-sans-mot-de-passe-16).
+
+```yml
+ynh_sudo_passwordless: false
+```
+
 ## Dépendances
 
 Aucune.
diff --git a/roles/ynh_config/README.md b/roles/ynh_config/README.md
index e072a1c..6158190 100644
--- a/roles/ynh_config/README.md
+++ b/roles/ynh_config/README.md
@@ -61,6 +61,16 @@ 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/)). You can indicate that configuration in your inventory file otherwise Ansible won't be able to connect to your server. (More info [here](https://docs.ansible.com/ansible/latest/reference_appendices/faq.html#how-do-i-handle-different-machines-needing-different-user-accounts-or-ports-to-log-in-with)).
 
+
+### Using sudo without password
+
+Starting with Yunohost 11.1, a new administrator group is created on the instance. This is a Unix group that is integrated with YunoHost and its LDAP. All users in this group will have access to the YunoHost online administration console but will also be able to connect via SSH and use the sudo command (to temporarily take root rights).
+By default, the user must type their password to use the sudo command but it is possible to disable this check from the web interface (`tools` > `YunoHost settings` > `Allow admins to use ‘sudo’ without re-typing their passwords`) or by changing the `ynh_sudo_passwordless` variable to `true` in your Ansible variables file. More information available [here](https://forum.yunohost.org/t/yunohost-11-1-release-sortie-de-yunohost-11-1/23378#passwordless-sudo-4).
+
+``yml
+ynh_sudo_passwordless: false
+```
+
 ## Dependencies
 
 None.
diff --git a/roles/ynh_config/defaults/main.yml b/roles/ynh_config/defaults/main.yml
index 1359ca3..b4a053f 100644
--- a/roles/ynh_config/defaults/main.yml
+++ b/roles/ynh_config/defaults/main.yml
@@ -39,4 +39,5 @@ ynh_autoupdate:
   # system: True
   # dest_script: "/usr/local/bin/"
 
-# ynh_ssh_port: "22"
+ynh_ssh_port:          "22"
+ynh_sudo_passwordless: false
diff --git a/roles/ynh_config/tasks/main.yml b/roles/ynh_config/tasks/main.yml
index 7f5af93..4fc696b 100644
--- a/roles/ynh_config/tasks/main.yml
+++ b/roles/ynh_config/tasks/main.yml
@@ -33,9 +33,8 @@
     - yunohost
     - update
 
-- name: Configure Yunohost SSH port
-  ansible.builtin.include_tasks: sshd_configuration.yml
-  when: ynh_ssh_port
+- name: Configure Yunohost SSH
+  ansible.builtin.include_tasks: ssh_configuration.yml
   tags:
     - yunohost
     - ssh
diff --git a/roles/ynh_config/tasks/sshd_configuration.yml b/roles/ynh_config/tasks/ssh_configuration.yml
similarity index 88%
rename from roles/ynh_config/tasks/sshd_configuration.yml
rename to roles/ynh_config/tasks/ssh_configuration.yml
index 3b7970d..a98b797 100644
--- a/roles/ynh_config/tasks/sshd_configuration.yml
+++ b/roles/ynh_config/tasks/ssh_configuration.yml
@@ -24,3 +24,10 @@
   tags:
     - yunohost
     - ssh
+
+- name: Enable sudo without password for admins
+  ansible.builtin.command:
+    "yunohost settings set security.password.passwordless_sudo -v {{ ynh_sudo_passwordless }}"
+  tags:
+    - yunohost
+    - ssh
-- 
GitLab