Partial setsshkey implementation

fixes2
Vadim vtroshchinskiy 2024-11-06 14:34:55 +01:00
parent c1a958bf9c
commit e4d954ef4e
1 changed files with 80 additions and 2 deletions

View File

@ -13,12 +13,13 @@ import grp
from termcolor import cprint
import git
import libarchive
#from libarchive.entry import FileType
import urllib.request
import pathlib
import socket
import time
import requests
import tempfile
#FORGEJO_VERSION="8.0.3"
FORGEJO_VERSION="9.0.0"
@ -303,7 +304,7 @@ class OpengnsysGitInstaller:
public_key = None
with libarchive.file_reader(client_initrd_path) as initrd:
for file in initrd:
#self.__logger.debug("Archivo: %s", file)
self.__logger.debug("Archivo: %s", file)
if file.pathname in self.key_paths_dict:
data = bytearray()
@ -318,6 +319,73 @@ class OpengnsysGitInstaller:
return public_key
def set_ssh_key(self, client_num = None):
INITRD = "oginitrd.img"
tftp_dir = os.path.join(self.base_path, "tftpboot")
if client_num is None:
self.__logger.info("Will modify default client")
client_num = self.oglive.get_default()
ogclient = self.oglive.get_clients()[client_num]
client_initrd_path = os.path.join(tftp_dir, ogclient, INITRD)
client_initrd_path_new = client_initrd_path + ".new"
self.__logger.info("initrd path is %s", client_initrd_path)
temp_dir = tempfile.TemporaryDirectory()
client_key = os.path.join(temp_dir.name, "id_ed25519")
self.__logger.debug("Writing new SSH key into %s", client_key)
subprocess.run(["/usr/bin/ssh-keygen", "-t", "ed25519", "-N", "", "-f", client_key], check=True)
keydata_priv = None
with open(client_key, "rb") as client_file:
keydata_priv = client_file.read()
keydata_pub = None
with open(client_key + ".pub", "rb") as client_file:
keydata_pub = client_file.read()
self.__logger.debug("Writing new initrd into %s", client_initrd_path_new)
with libarchive.file_reader(client_initrd_path) as orig_initrd:
#self.__logger.debug("Original initrd was format %s", orig_initrd.format_name)
with libarchive.file_writer(client_initrd_path_new, "cpio") as new_initrd:
for file in orig_initrd:
self.__logger.debug("File: %s, type %i", file, file.filetype)
if file.isreg():
data = bytearray()
for block in file.get_blocks():
data = data + block
self.__logger.debug("Adding pathname %s, len %i", file.pathname, len(data))
new_initrd.add_file_from_memory(file.pathname, len(data), bytes(data), permission = file.mode, mtime=file.mtime, ctime=file.ctime)
elif file.isdir():
file.modi
self.__logger.debug("Pathname %s is a directory", file.pathname)
elif file.islnk():
self.__logger.debug("Pathname %s is a symlink", file.pathname)
None
else:
self.__logger.error("Unhandled file type %s", str(file.filetype))
new_initrd.add_file_from_memory("scripts/ssl/id_ed25519.pub", len(keydata_pub), keydata_pub)
new_initrd.add_file_from_memory("scripts/ssl/id_ed25519", len(keydata_priv), keydata_priv)
new_initrd.add_file_from_memory()
def install(self):
"""Instalar
@ -653,6 +721,10 @@ if __name__ == '__main__':
parser.add_argument('--ignoresshkey', action='store_true', help="Ignorar clave de SSH")
parser.add_argument('--usesshkey', type=str, help="Usar clave SSH especificada")
parser.add_argument('--test-createuser', action='store_true')
parser.add_argument('--extract-ssh-key', action='store_true', help="Extract SSH key from oglive")
parser.add_argument('--set-ssh-key', action='store_true', help="Configure SSH key in oglive")
parser.add_argument('--oglive', type=int, metavar='NUM', help = "Do SSH key manipulation on this oglive")
args = parser.parse_args()
@ -670,6 +742,12 @@ if __name__ == '__main__':
installer.add_forgejo_repo("linux")
elif args.test_createuser:
installer.set_ssh_user_group("oggit2", "oggit2")
elif args.extract_ssh_key:
key = installer._extract_ssh_key()
print(f"Key: {key}")
elif args.set_ssh_key:
installer.set_ssh_key()
else:
installer.install()
installer.install_forgejo()