From 6179e0fd0a739339ca2a8522c8dcbe16b3b1c59d Mon Sep 17 00:00:00 2001 From: Anil Belur Date: Wed, 25 May 2022 11:35:09 +1000 Subject: [PATCH] Feat: Add get-private-keys to lftools.jenkins Add ``get-private-keys`` to lftools.jenkins. The ``get-private-keys`` command retrives the private keys and passphrases stored in the credential store. This feature will add the command ``get-private-keys`` in order to print these values. Issue-ID: RELENG-4245 Change-Id: Iff0cab0b14d43bea540ca513ade08a5e330836e4 Signed-off-by: Anil Belur --- lftools/cli/jenkins/__init__.py | 31 ++++++++++++++++++++++ .../add-get-ssh-private-keys-33e8e5e7f40a7afd.yaml | 7 +++++ 2 files changed, 38 insertions(+) create mode 100644 releasenotes/notes/add-get-ssh-private-keys-33e8e5e7f40a7afd.yaml diff --git a/lftools/cli/jenkins/__init__.py b/lftools/cli/jenkins/__init__.py index 7b0ce340..1ab1a3e2 100644 --- a/lftools/cli/jenkins/__init__.py +++ b/lftools/cli/jenkins/__init__.py @@ -99,6 +99,36 @@ for (c in creds) { log.info(result) +@click.command() +@click.pass_context +def get_private_keys(ctx): + """Print all available SSH User Private Keys.""" + jenkins = ctx.obj["jenkins"] + groovy_script = """ +import com.cloudbees.plugins.credentials.* +import com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey + +println "Printing all SSH User Private keys ..." +def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials( + com.cloudbees.plugins.credentials.Credentials.class, + Jenkins.instance, + null, + null +); + +for (c in creds) { + if(c instanceof BasicSSHUserPrivateKey) { + println("SSH Private key ID: " + c.getId()) + println("SSH User name: " + c.getUsername()) + println("SSH Private key passphrase: " + c.getPassphrase()) + println("SSH Private key: " + c.getPrivateKey()) + } +} +""" + result = jenkins.server.run_script(groovy_script) + log.info(result) + + @click.command() @click.argument("groovy_file") @click.pass_context @@ -206,6 +236,7 @@ jenkins_cli.add_command(nodes) jenkins_cli.add_command(builds) jenkins_cli.add_command(get_credentials, name="get-credentials") jenkins_cli.add_command(get_secrets, name="get-secrets") +jenkins_cli.add_command(get_private_keys, name="get-private-keys") jenkins_cli.add_command(groovy) jenkins_cli.add_command(jobs) jenkins_cli.add_command(quiet_down, name="quiet-down") diff --git a/releasenotes/notes/add-get-ssh-private-keys-33e8e5e7f40a7afd.yaml b/releasenotes/notes/add-get-ssh-private-keys-33e8e5e7f40a7afd.yaml new file mode 100644 index 00000000..2640f701 --- /dev/null +++ b/releasenotes/notes/add-get-ssh-private-keys-33e8e5e7f40a7afd.yaml @@ -0,0 +1,7 @@ +--- +features: + - | + Add ``get-private-keys`` to lftools.jenkins. + ``get-private-keys`` retrives the private keys and passphrases stored in + the credential store. This feature will add the command ``get-private-keys`` + in order to print these as well. -- 2.16.6