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

Updating to match new version of source page
(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>
38,760

edits