From 70a8fd3e93efa392a9d1753e2355070ec7c2868d Mon Sep 17 00:00:00 2001 From: Marc Beninca Date: Fri, 31 May 2024 00:29:12 +0200 Subject: [PATCH] ssh/wip --- bootstrap.sh | 21 --------------------- cd/__init__.py | 25 ++++++++++++++++++++++++- cd/__main__.py | 1 + readme.md | 2 ++ 4 files changed, 27 insertions(+), 22 deletions(-) delete mode 100755 bootstrap.sh diff --git a/bootstrap.sh b/bootstrap.sh deleted file mode 100755 index 598653d..0000000 --- a/bootstrap.sh +++ /dev/null @@ -1,21 +0,0 @@ -#! /usr/bin/env bash - -KEY_ALGORITHM='ed25519' -KEY_DIRECTORY='.ssh' -KEY_PATH="${KEY_DIRECTORY}/id_${KEY_ALGORITHM}" -KEY_PUBLIC='AAAAC3NzaC1lZDI1NTE5AAAAIPHCNcgHlQoiNTXfnUZYvHz9OZwYsmBCrSSV7a7Zche5' -SERVER="$(basename ${GITHUB_SERVER_URL})" - -# ssh -cd \ -|| exit -mkdir "${KEY_DIRECTORY}" \ -|| exit -echo "${CD}" > "${KEY_PATH}" \ -|| exit -unset CD -chmod '400' "${KEY_PATH}" \ -|| exit -echo "${SERVER} ssh-${KEY_ALGORITHM} ${KEY_PUBLIC}" \ -> "${KEY_DIRECTORY}/known_hosts" \ -|| exit diff --git a/cd/__init__.py b/cd/__init__.py index 8f2bf6e..ac39077 100644 --- a/cd/__init__.py +++ b/cd/__init__.py @@ -2,6 +2,7 @@ import os from cd.project import Project from cd.projects import Projects +from rwx import fs from rwx import ps COMMANDS_PREFIX = 'cd-' @@ -50,7 +51,7 @@ def cd_clone_branch(): def cd_list_environment(): - for variable, value in sorted(os.environ.items()): + for variable, value in sorted(projects.environment.items()): print(variable, '=', value) @@ -64,3 +65,25 @@ def install_commands(path): ]: print(command) os.symlink(path, os.path.join(user, f'{COMMANDS_PREFIX}{command}')) + + +def set_ssh(): + ssh_hosts = projects.environment.get('CD_SSH_HOSTS', None) + ssh_key = projects.environment.get('CD', None) + ssh_type = projects.environment.get('CD_SSH_TYPE', 'ed25519') + # + home = os.path.expanduser('~') + # + ssh = os.path.join(home, '.ssh') + os.makedirs(ssh, exist_ok=True) + os.chmod(ssh, 0o700) + # + key = os.path.join(ssh, f'id_{ssh_type}') + if ssh_key: + fs.write(key, ssh_key) + os.chmod(key, 0o400) + # + known = os.path.join(ssh, 'known_hosts') + if ssh_hosts: + fs.write(known, ssh_hosts) + os.chmod(known, 0o400) diff --git a/cd/__main__.py b/cd/__main__.py index c93b684..ba1ca17 100755 --- a/cd/__main__.py +++ b/cd/__main__.py @@ -10,6 +10,7 @@ if __name__ == '__main__': command, *arguments = sys.argv command = os.path.basename(command) if command == '__main__.py': + cd.set_ssh() cd.install_commands(__file__) else: command = command.replace('-', '_') diff --git a/readme.md b/readme.md index 556a91f..2492ead 100644 --- a/readme.md +++ b/readme.md @@ -41,6 +41,8 @@ from various CA, CI, OCI / OS. |:--------------|:------------------------| | CD_CA_n | Numbered CA certificate | | CD_DNS | Space separated servers | +| CD_SSH_HOSTS | domain.tld ssh-type pub | +| CD_SSH_TYPE | SSH private key type | | CD_URL_ALMA | Alma repository URL | | CD_URL_ALPINE | Alpine repository URL | | CD_URL_ARCH | Arch repository URL |