[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(
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,6 +188,21 @@ def action_put(
):
sshconf = ""
for group in _os.listdir(source_directory):
path_temp = _os.path.join(source_directory, group)
if (
not
(
_os.path.isdir(path_temp)
and
(group == ".")
and
(group == "..")
and
(group == ".git")
)
):
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)
@ -169,12 +244,13 @@ def main():
choices = [
"init",
"key-add",
"key-generate",
"key-remove",
"put",
],
default = "put",
metavar = "<action>",
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 = "<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()
## 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