diff --git a/source/main.py b/source/main.py index 2f57c6d..3446a43 100755 --- a/source/main.py +++ b/source/main.py @@ -32,6 +32,65 @@ def action_init( def action_key_add( + source_directory, + group, + name, + key_path, + options = None +): + options = ( + { + "remove_private_key": False, + } + | + (options or {}) + ) + directory_key = _os.path.join(source_directory, group, "keys") + path_key_private = key_path + path_key_public = ("%s.pub" % path_key_private) + __helpers_misc.directory_create(directory_key) + ## transfer private key to keepass database + if True: + keepass_db_path = _os.path.join(source_directory, "private_keys.kdbx") + keepass_authfile_path = _os.path.join(source_directory, "private_keys.keyx") + __helpers_keepass.action_mkdir( + keepass_db_path, + group, + { + "key_file": keepass_authfile_path, + } + ) + __helpers_keepass.action_add( + keepass_db_path, + group, + name, + { + "key_file": keepass_authfile_path, + } + ) + __helpers_keepass.action_attachment_import( + keepass_db_path, + group, + name, + 'ssh private key', + path_key_private, + { + "key_file": keepass_authfile_path, + } + ) + ## remove private key file + if (options["remove_private_key"]): + __helpers_misc.shell_exec( + __helpers_misc.string_coin( + "rm --force {{path}}", + { + "path": path_key_private, + } + ) + ) + + +def action_key_generate( source_directory, group, name @@ -89,6 +148,7 @@ def action_key_add( ) ) + def action_key_remove( source_directory, group, @@ -128,28 +188,43 @@ def action_put( ): sshconf = "" for group in _os.listdir(source_directory): - conf_path = _os.path.join(source_directory, group, "conf.json") - conf_content = __helpers_misc.file_read(conf_path) - conf_data = _json.loads(conf_content) - # conf - if True: - sshconf = (sshconf + "\n" + __helpers_ssh.sshconf_encode(conf_data)) - # public keys - if True: - __helpers_misc.directory_create(target_directory) - for name in _os.listdir(_os.path.join(source_directory, group, "keys")): - _shutil.copy( - _os.path.join(source_directory, group, "keys", name), - _os.path.join(target_directory, "%s%s" % (conf_data["settings"]["prefix"], name, )) - ) - # private keys - if True: - keepass_db_path = _os.path.join(source_directory, "private_keys.kdbx") - _shutil.copy( - keepass_db_path, - _os.path.join(target_directory, "private_keys.kdbx") + path_temp = _os.path.join(source_directory, group) + if ( + not + ( + _os.path.isdir(path_temp) + and + (group == ".") + and + (group == "..") + and + (group == ".git") ) - ## todo: keyfile + ): + pass + else: + conf_path = _os.path.join(source_directory, group, "conf.json") + conf_content = __helpers_misc.file_read(conf_path) + conf_data = _json.loads(conf_content) + # conf + if True: + sshconf = (sshconf + "\n" + __helpers_ssh.sshconf_encode(conf_data)) + # public keys + if True: + __helpers_misc.directory_create(target_directory) + for name in _os.listdir(_os.path.join(source_directory, group, "keys")): + _shutil.copy( + _os.path.join(source_directory, group, "keys", name), + _os.path.join(target_directory, "%s%s" % (conf_data["settings"]["prefix"], name, )) + ) + # private keys + if True: + keepass_db_path = _os.path.join(source_directory, "private_keys.kdbx") + _shutil.copy( + keepass_db_path, + _os.path.join(target_directory, "private_keys.kdbx") + ) + ## todo: keyfile __helpers_misc.file_write(_os.path.join(target_directory, "config"), sshconf) @@ -169,12 +244,13 @@ def main(): choices = [ "init", "key-add", + "key-generate", "key-remove", "put", ], default = "put", metavar = "", - help = "options: init | key-add | key-remove | put", + help = "options: init | key-add | key-generate | key-remove | put", ) argument_parser.add_argument( "-s", @@ -204,6 +280,18 @@ def main(): default = None, metavar = "", ) + argument_parser.add_argument( + "-k", + "--key-path", + type = str, + default = None, + metavar = "", + ) + argument_parser.add_argument( + "-r", + "--remove-private-key", + action = "store_true", + ) args = argument_parser.parse_args() ## exec @@ -213,6 +301,16 @@ def main(): ) elif (args.action == "key-add"): action_key_add( + args.source_directory, + args.group, + args.name, + args.key_path, + { + "remove_private_key": args.remove_private_key, + } + ) + elif (args.action == "key-generate"): + action_key_generate( args.source_directory, args.group, args.name