[mod] add action "key-generate"

This commit is contained in:
fenris 2026-05-04 21:57:41 +02:00
parent 714f9a58f2
commit af3636bcc1

View file

@ -32,6 +32,65 @@ def action_init(
def action_key_add( 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, source_directory,
group, group,
name name
@ -89,6 +148,7 @@ def action_key_add(
) )
) )
def action_key_remove( def action_key_remove(
source_directory, source_directory,
group, group,
@ -128,28 +188,43 @@ def action_put(
): ):
sshconf = "" sshconf = ""
for group in _os.listdir(source_directory): for group in _os.listdir(source_directory):
conf_path = _os.path.join(source_directory, group, "conf.json") path_temp = _os.path.join(source_directory, group)
conf_content = __helpers_misc.file_read(conf_path) if (
conf_data = _json.loads(conf_content) not
# conf (
if True: _os.path.isdir(path_temp)
sshconf = (sshconf + "\n" + __helpers_ssh.sshconf_encode(conf_data)) and
# public keys (group == ".")
if True: and
__helpers_misc.directory_create(target_directory) (group == "..")
for name in _os.listdir(_os.path.join(source_directory, group, "keys")): and
_shutil.copy( (group == ".git")
_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 ):
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) __helpers_misc.file_write(_os.path.join(target_directory, "config"), sshconf)
@ -169,12 +244,13 @@ def main():
choices = [ choices = [
"init", "init",
"key-add", "key-add",
"key-generate",
"key-remove", "key-remove",
"put", "put",
], ],
default = "put", default = "put",
metavar = "<action>", metavar = "<action>",
help = "options: init | key-add | key-remove | put", help = "options: init | key-add | key-generate | key-remove | put",
) )
argument_parser.add_argument( argument_parser.add_argument(
"-s", "-s",
@ -204,6 +280,18 @@ def main():
default = None, default = None,
metavar = "<name>", metavar = "<name>",
) )
argument_parser.add_argument(
"-k",
"--key-path",
type = str,
default = None,
metavar = "<key-path>",
)
argument_parser.add_argument(
"-r",
"--remove-private-key",
action = "store_true",
)
args = argument_parser.parse_args() args = argument_parser.parse_args()
## exec ## exec
@ -213,6 +301,16 @@ def main():
) )
elif (args.action == "key-add"): elif (args.action == "key-add"):
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.source_directory,
args.group, args.group,
args.name args.name