Commit 9c3acd8a authored by Antoine Catton's avatar Antoine Catton

Clean up ssh-key generation.

parent 22127d38
...@@ -28,6 +28,7 @@ import json ...@@ -28,6 +28,7 @@ import json
import hashlib import hashlib
import os import os
import subprocess import subprocess
import re
from slapos.recipe.librecipe import GenericBaseRecipe from slapos.recipe.librecipe import GenericBaseRecipe
from slapos.recipe.librecipe.inotify import subfiles from slapos.recipe.librecipe.inotify import subfiles
...@@ -51,6 +52,8 @@ def sshkeys_authority(args): ...@@ -51,6 +52,8 @@ def sshkeys_authority(args):
break break
if not os.path.exists(private_key): if not os.path.exists(private_key):
if os.path.exists(public_key):
os.unlink(public_key)
keygen_cmd = [keygen_binary, '-t', key_type, '-f', private_key, keygen_cmd = [keygen_binary, '-t', key_type, '-f', private_key,
'-s', size] '-s', size]
# If the keygeneration return an non-zero status, it means there's a # If the keygeneration return an non-zero status, it means there's a
...@@ -72,9 +75,15 @@ def sshkeys_authority(args): ...@@ -72,9 +75,15 @@ def sshkeys_authority(args):
if keygen.wait() != 0: if keygen.wait() != 0:
raise subprocess.CalledProcessError("%r returned a non-zero status" % \ raise subprocess.CalledProcessError("%r returned a non-zero status" % \
' '.join(keygen_cmd)) ' '.join(keygen_cmd))
# Line : "Public key portion is :" public_key_value = ''
keygen.stdout.readline() for line in keygen.stdout:
public_key_value = keygen.stdout.readline().strip() # Perl programming !
# Don't worry, just regex to detect the ssh public key line
matchresult = re.match(r'ssh-.*?=+', line)
if matchresult:
public_key_value = matchresult.group(0)
break
with open(public_key, 'w') as public_key_file: with open(public_key, 'w') as public_key_file:
public_key_file.write(public_key_value) public_key_file.write(public_key_value)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment