Automation in the context of multifactor authentication/fr: Difference between revisions

From Alliance Doc
Jump to navigation Jump to search
(Updating to match new version of source page)
(Updating to match new version of source page)
Line 75: Line 75:
{{Command|rsync -a datadir/a robot:scratch/testdata}}
{{Command|rsync -a datadir/a robot:scratch/testdata}}


<div lang="en" dir="ltr" class="mw-content-ltr">
= IPv4 vs IPv6 issue =
= IPv4 vs IPv6 issue =
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
When connecting to the robot node the SSH client on your computer may choose to use the '''IPv6 addressing''' over the older '''IPv4'''.
When connecting to the robot node the SSH client on your computer may choose to use the '''IPv6 addressing''' over the older '''IPv4'''.
This seems to be more probably in Windows environment.  
This seems to be more probably in Windows environment.  
If this is the case you have to make sure that the IP address mask you put in the <code>restrict,from=</code> field of the key
If this is the case you have to make sure that the IP address mask you put in the <code>restrict,from=</code> field of the key
matches the type your computer will be using when connecting to the node.
matches the type your computer will be using when connecting to the node.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
You can check your addresses using this web site: https://test-ipv6.com/ .
You can check your addresses using this web site: https://test-ipv6.com/ .
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
* An IPv4 address would look like '''199.241.166.5'''.
* An IPv4 address would look like '''199.241.166.5'''.
* An IPv6 address could be similar to '''2620:123:7002:4::5'''.
* An IPv6 address could be similar to '''2620:123:7002:4::5'''.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
The possible problem is that if you put the IPv4 address mask, '''199.241.166.*''' into the CCDB SSH key, and  
The possible problem is that if you put the IPv4 address mask, '''199.241.166.*''' into the CCDB SSH key, and  
your SSH client will be connecting the the robot node using IPv6 address, the source address will not match the mask in the key
your SSH client will be connecting the the robot node using IPv6 address, the source address will not match the mask in the key
and the key will not be accepted by the robot node.  
and the key will not be accepted by the robot node.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
=== How to identify the problem ===
=== How to identify the problem ===
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
If you are having difficulties to make the SSH connection to a robot node working.
If you are having difficulties to make the SSH connection to a robot node working.
Try this test command:
Try this test command:
  ssh -i ~/.ssh/automation_key -vvv username@robot.graham.alliancecan.ca "ls -l"  
  ssh -i ~/.ssh/automation_key -vvv username@robot.graham.alliancecan.ca "ls -l"
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
This command tries to connect to the robot node on Graham cluster and execute the <code>ls -l</code> command  
This command tries to connect to the robot node on Graham cluster and execute the <code>ls -l</code> command  
using the <code>~/.ssh/automation_key</code> SSH key.
using the <code>~/.ssh/automation_key</code> SSH key.
Then it prints the list of files in your home directory on Graham to screen.
Then it prints the list of files in your home directory on Graham to screen.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
This command will produce a lot of debug output due to the <code>-vvv</code> option (be Very Very Verbose).
This command will produce a lot of debug output due to the <code>-vvv</code> option (be Very Very Verbose).
Look for the '''Connecting to...''' message there.
Look for the '''Connecting to...''' message there.
If it says something like this:
If it says something like this:
  debug1: Connecting to robot.graham.alliancecan.ca [199.241.166.5] port 22.
  debug1: Connecting to robot.graham.alliancecan.ca [199.241.166.5] port 22.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
it means the IPv4 is being used.
it means the IPv4 is being used.
If the message is similar to  
If the message is similar to  
  debug1: Connecting to robot.graham.alliancecan.ca [2620:123:7002:4::5] port 22.
  debug1: Connecting to robot.graham.alliancecan.ca [2620:123:7002:4::5] port 22.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
then IPv6 is being used to make the connection.
then IPv6 is being used to make the connection.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
=== Possible solutions ===
=== Possible solutions ===
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
* You can make the SSH client to '''explicitly use either IPv4 or IPv6''' using the <code>-4</code> and <code>-6</code> options, respectively, to match the format you used for the key in CCDB.
* You can make the SSH client to '''explicitly use either IPv4 or IPv6''' using the <code>-4</code> and <code>-6</code> options, respectively, to match the format you used for the key in CCDB.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
* You can try using an '''IP address instead of the name''' to point to the robot node. Using Graham example, try using the  
* You can try using an '''IP address instead of the name''' to point to the robot node. Using Graham example, try using the  
: <code>ssh -i ~/.ssh/automation_key -vvv username@199.241.166.5 "ls -l"</code>
: <code>ssh -i ~/.ssh/automation_key -vvv username@199.241.166.5 "ls -l"</code>
: instead, to force SSH to use the IPv4 addresses.
: instead, to force SSH to use the IPv4 addresses.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
* You can try to '''disable the IPv6 addressing''' for your system, to make sure that only IPv4 is used.
* You can try to '''disable the IPv6 addressing''' for your system, to make sure that only IPv4 is used.
: Currently, there should not be any negative impact on your system.  
: Currently, there should not be any negative impact on your system. However, Microsoft does not recommend this, and this should be your '''last resort''' method, if nothing else works.
: How to disable IPv6 will depend on your system and the operating system.
: How to disable IPv6 will depend on your system and the operating system.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
= Automation using Python and Paramiko =
= Automation using Python and Paramiko =
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
If you are using the [https://www.paramiko.org/index.html Paramiko Python module] to automate your workflow, this is how you can make it work with the robot nodes:
If you are using the [https://www.paramiko.org/index.html Paramiko Python module] to automate your workflow, this is how you can make it work with the robot nodes:
<source lang=python>
<source lang=python>
Line 134: Line 167:
import paramiko
import paramiko
# ====================================================================================================
# ====================================================================================================
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
key = paramiko.Ed25519Key.from_private_key_file("/home/username/.ssh/cc_allowed")
key = paramiko.Ed25519Key.from_private_key_file("/home/username/.ssh/cc_allowed")
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
user = "username"
user = "username"
host = "robot.graham.alliancecan.ca"
host = "robot.graham.alliancecan.ca"
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
ssh = paramiko.SSHClient()
ssh = paramiko.SSHClient()
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
# If the host is not known, it is OK.
# If the host is not known, it is OK.
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
ssh.connect(hostname=host, username=user, pkey=key)
ssh.connect(hostname=host, username=user, pkey=key)
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
cmd = "ls -l"
cmd = "ls -l"
stdin, stdout, stderr = ssh.exec_command(cmd)
stdin, stdout, stderr = ssh.exec_command(cmd)
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
print("".join(stdout.readlines()))
print("".join(stdout.readlines()))
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
ssh.close()
ssh.close()
# ====================================================================================================
# ====================================================================================================
Line 158: Line 207:
executes the <code>ls -l</code> command to get the list of files.
executes the <code>ls -l</code> command to get the list of files.
Then prints the list to the screen.
Then prints the list to the screen.
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
Note, that it is important to install '''paramiko''' with the
Note, that it is important to install '''paramiko''' with the
  $ pip install paramiko[all]
  $ pip install paramiko[all]
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
command. This will make sure that the support for the '''Ed25519''' key type will also be installed.
command. This will make sure that the support for the '''Ed25519''' key type will also be installed.
</div>

Revision as of 19:55, 8 May 2024

Other languages:

Les flux de travail qui établissent sans intervention humaine des connexions à nos grappes ne peuvent pas utiliser un deuxième facteur. Avec l'authentification multifacteur obligatoire, vous devez demander l'accès à un des nœuds spéciaux qui sont réservés pour les flux automatisés. Un deuxième facteur ne sera pas requis, mais sachez que les fonctionnalités de ces nœuds sont limitées par rapport à celles des nœuds de connexion réguliers en termes de type d'authentification requise et aussi des types d'actions à exécuter.

Mesures de sécurité accrues

Accès sur demande seulement

Pour avoir accès aux nœuds spéciaux d'automatisation, écrivez au soutien technique. Décrivez le type d'automatisation, listez les commandes qui seront exécutées ainsi que les outils ou les bibliothèques pour gérer l'automatisation.

Accès via certaines clés SSH avec contraintes

L'accès aux nœuds d'automatisation se fait uniquement via les clés SSH téléversées dans CCDB. Les clés inscrites dans un fichier .ssh/authorized_keys ne sont pas acceptées. De plus, les clés SSH doivent respecter les contraintes suivantes.

restrict

Cette contrainte désactive la redirection de port (port forwarding), la redirection d'agent (agent forwarding) et la redirection X11. Le pseudo télétype (PTY) est aussi désactivé, puisqu'il bloquerait la plupart des flux interactifs. Nous posons ces conditions parce que les nœuds spéciaux ne doivent pas être utilisés pour les processus interactifs ou de longue durée; dans ces cas, il faut utiliser les nœuds réguliers.

from="pattern-list"

Cette contrainte fait en sorte que la clé ne peut être utilisée qu'à partir d'adresses IP qui respectent le pattern et non par d'autres ordinateurs. La liste des patterns doit être uniquement composée d'adresses IP qui spécifient la classe du réseau, le réseau et le sous-réseau, soit les trois premiers éléments de l'adresse. Par exemple, x.y.*.* ne serait pas acceptée, mais x.y.z.* le serait. Prenez note que l'adresse IP doit être une adresse publique; ainsi, 10.0.0.0 – 10.255.255.255, 172.16.0.0 – 172.31.255.255 et 192.168.0.0 – 192.168.255.255 seraient incorrectes. Pour connaître votre adresse IP publique, allez sur le site What Is My IP Address? ou lancez la commande curl ifconfig.me de l'interpréteur (shell).

command="COMMAND"

Cette contrainte exécute la commande COMMAND lors de la connexion. Ceci vous permet de définir les seules commandes qui peuvent être utilisées avec la clé.

Scripts enveloppants pour la commande command=

Cette commande permet de définir toutes les commandes, mais elle est le plus utile quand vous avez un script enveloppant qui accepte ou refuse les commandes qui sont appelées. Vous pouvez écrire vos propres scripts, mais nous en avons préparé certains pour des cas qui se présentent fréquemment. Ces scripts se trouvent dans ce dépôt git.

  • /cvmfs/soft.computecanada.ca/custom/bin/computecanada/allowed_commands/transfer_commands.sh permet uniquement les commandes de transfert de fichiers, comme scp, sftp ou rsync
  • /cvmfs/soft.computecanada.ca/custom/bin/computecanada/allowed_commands/archiving_commands.sh permet les commmandes d'archivage de fichiers, comme gzip, tar ou dar
  • /cvmfs/soft.computecanada.ca/custom/bin/computecanada/allowed_commands/file_commands.sh permet la commande de manipulation de fichiers, comme mv, cp ou rm
  • /cvmfs/soft.computecanada.ca/custom/bin/computecanada/allowed_commands/git_commands.sh permet la commande git
  • /cvmfs/soft.computecanada.ca/custom/bin/computecanada/allowed_commands/slurm_commands.sh permet certaines commandes Slurm, comme squeue, sbatch
  • /cvmfs/soft.computecanada.ca/custom/bin/computecanada/allowed_commands/allowed_commands.sh permet toutes les commandes ci-dessus

Exemples de clés SSH acceptées

Les clés doivent respecter les trois conditions décrites ci-dessus. En voici quelques exemples qui seraient valides pour le transfert de fichiers avec scp, sftp ou rsync :

restrict,from="216.18.209.*",command="/cvmfs/soft.computecanada.ca/custom/bin/computecanada/allowed_commands/transfer_commands.sh" ssh-ed25519 AAAAC3NzaC1lZDI1NTE6AACAIExK9iTTDGsyqKKzduA46DvIJ9oFKZ/WN5memqG9Invw

Le prochain exemple permettrait uniquement des commandes Slurm (squeue, scancel, sbatch, scontrol, sq).

restrict,from="216.18.209.*",command="/cvmfs/soft.computecanada.ca/custom/bin/computecanada/allowed_commands/slurm_commands.sh" ssh-ed25519 AAAAC3NzaC1lZDI1NTE6AACAIExK9iTTDGsyqKKzduA46DvIJ9oFKZ/WN5memqG9Invw


Attention

Avant d’ajouter la clé SSH dans votre compte CCDB, assurez-vous que les contraintes soient indiquées comme dans les exemples précédents.



Nœuds d'automatisation, par grappe

Pour vous connecter à un nœud d'automatisation, utilisez les adresses suivantes :

  • Cedar : robot.cedar.alliancecan.ca
  • Graham : robot.graham.alliancecan.ca
  • Béluga : robot.beluga.alliancecan.ca
  • Narval : robot.narval.alliancecan.ca
  • Niagara : robot.niagara.alliancecan.ca

Ne pas se tromper de clé

Si vous avez plusieurs clés, assurez-vous d'utiliser la bonne. Ceci peut se faire avec des paramètres passés à la commande, comme dans les exemples ci-dessous.

Avec ssh ou scp,

Question.png
[name@server ~]$ ssh -i .ssh/private_key_to_use ...
Question.png
[name@server ~]$ scp -i .ssh/private_key_to_use ...

Avec rsync,

Question.png
[name@server ~]$ rsync -e "ssh -i .ssh/private_key_to_use" ...

Il est souvent beaucoup plus pratique d'inclure ces paramètres dans votre fichier ~/.ssh/config pour qu'ils soient pris en compte quand le client SSH est invoqué. Par exemple :

host robot
 hostname robot.cluster.alliancecan.ca
 user myrobot
 identityfile ~/.ssh/my-robot-key
 identitiesonly yes
 requesttty no

Ceci signifie que les deux types de commandes suivantes feront ce que vous voulez.

Question.png
[name@server ~]$ ssh robot /usr/bin/ls
Question.png
[name@server ~]$ rsync -a datadir/a robot:scratch/testdata

IPv4 vs IPv6 issue

When connecting to the robot node the SSH client on your computer may choose to use the IPv6 addressing over the older IPv4. This seems to be more probably in Windows environment. If this is the case you have to make sure that the IP address mask you put in the restrict,from= field of the key matches the type your computer will be using when connecting to the node.

You can check your addresses using this web site: https://test-ipv6.com/ .

  • An IPv4 address would look like 199.241.166.5.
  • An IPv6 address could be similar to 2620:123:7002:4::5.

The possible problem is that if you put the IPv4 address mask, 199.241.166.* into the CCDB SSH key, and your SSH client will be connecting the the robot node using IPv6 address, the source address will not match the mask in the key and the key will not be accepted by the robot node.

How to identify the problem

If you are having difficulties to make the SSH connection to a robot node working. Try this test command:

ssh -i ~/.ssh/automation_key -vvv username@robot.graham.alliancecan.ca "ls -l"

This command tries to connect to the robot node on Graham cluster and execute the ls -l command using the ~/.ssh/automation_key SSH key. Then it prints the list of files in your home directory on Graham to screen.

This command will produce a lot of debug output due to the -vvv option (be Very Very Verbose). Look for the Connecting to... message there. If it says something like this:

debug1: Connecting to robot.graham.alliancecan.ca [199.241.166.5] port 22.

it means the IPv4 is being used. If the message is similar to

debug1: Connecting to robot.graham.alliancecan.ca [2620:123:7002:4::5] port 22.

then IPv6 is being used to make the connection.

Possible solutions

  • You can make the SSH client to explicitly use either IPv4 or IPv6 using the -4 and -6 options, respectively, to match the format you used for the key in CCDB.
  • You can try using an IP address instead of the name to point to the robot node. Using Graham example, try using the
ssh -i ~/.ssh/automation_key -vvv username@199.241.166.5 "ls -l"
instead, to force SSH to use the IPv4 addresses.
  • You can try to disable the IPv6 addressing for your system, to make sure that only IPv4 is used.
Currently, there should not be any negative impact on your system. However, Microsoft does not recommend this, and this should be your last resort method, if nothing else works.
How to disable IPv6 will depend on your system and the operating system.

Automation using Python and Paramiko

If you are using the Paramiko Python module to automate your workflow, this is how you can make it work with the robot nodes:

# ====================================================================================================
#! /usr/bin/env python3
# ====================================================================================================
import os
import paramiko
# ====================================================================================================
</div>

<div lang="en" dir="ltr" class="mw-content-ltr">
key = paramiko.Ed25519Key.from_private_key_file("/home/username/.ssh/cc_allowed")
</div>

<div lang="en" dir="ltr" class="mw-content-ltr">
user = "username"
host = "robot.graham.alliancecan.ca"
</div>

<div lang="en" dir="ltr" class="mw-content-ltr">
ssh = paramiko.SSHClient()
</div>

<div lang="en" dir="ltr" class="mw-content-ltr">
# If the host is not known, it is OK.
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
</div>

<div lang="en" dir="ltr" class="mw-content-ltr">
ssh.connect(hostname=host, username=user, pkey=key)
</div>

<div lang="en" dir="ltr" class="mw-content-ltr">
cmd = "ls -l"
stdin, stdout, stderr = ssh.exec_command(cmd)
</div>

<div lang="en" dir="ltr" class="mw-content-ltr">
print("".join(stdout.readlines()))
</div>

<div lang="en" dir="ltr" class="mw-content-ltr">
ssh.close()
# ====================================================================================================

This code connects to the robot node on Graham using an automation key specified in CCDB and executes the ls -l command to get the list of files. Then prints the list to the screen.

Note, that it is important to install paramiko with the

$ pip install paramiko[all]

command. This will make sure that the support for the Ed25519 key type will also be installed.