From 9d61520841d6ed796d5e3941740d5800cfde4b54 Mon Sep 17 00:00:00 2001 From: Thanh Ha Date: Sat, 8 Sep 2018 13:53:14 -0400 Subject: [PATCH] Add jenkins token cmd to retrieve the API token Change-Id: I43397d25978aaf752db33003044e7cb8d4884705 Signed-off-by: Thanh Ha --- lftools/cli/jenkins/__init__.py | 4 ++ lftools/cli/jenkins/token.py | 44 +++++++++++++++++ lftools/jenkins/token.py | 55 ++++++++++++++++++++++ .../notes/jenkins-token-cmd-8e5cdce9175f69a1.yaml | 13 +++++ 4 files changed, 116 insertions(+) create mode 100644 lftools/cli/jenkins/token.py create mode 100644 lftools/jenkins/token.py create mode 100644 releasenotes/notes/jenkins-token-cmd-8e5cdce9175f69a1.yaml diff --git a/lftools/cli/jenkins/__init__.py b/lftools/cli/jenkins/__init__.py index 384d5043..de6f5559 100644 --- a/lftools/cli/jenkins/__init__.py +++ b/lftools/cli/jenkins/__init__.py @@ -23,6 +23,7 @@ from lftools.cli.jenkins.builds import builds from lftools.cli.jenkins.jobs import jobs from lftools.cli.jenkins.nodes import nodes from lftools.cli.jenkins.plugins import plugins_init +from lftools.cli.jenkins.token import token from lftools.jenkins import JJB_INI log = logging.getLogger(__name__) @@ -56,6 +57,8 @@ def jenkins_cli(ctx, server, user, password): username=user, password=password) + ctx.obj['jenkins_url'] = server + @click.command() @click.pass_context @@ -190,3 +193,4 @@ jenkins_cli.add_command(groovy) jenkins_cli.add_command(jobs) jenkins_cli.add_command(quiet_down, name='quiet-down') jenkins_cli.add_command(remove_offline_nodes, name='remove-offline-nodes') +jenkins_cli.add_command(token) diff --git a/lftools/cli/jenkins/token.py b/lftools/cli/jenkins/token.py new file mode 100644 index 00000000..c1c4ba1d --- /dev/null +++ b/lftools/cli/jenkins/token.py @@ -0,0 +1,44 @@ +# SPDX-License-Identifier: EPL-1.0 +############################################################################## +# Copyright (c) 2018 The Linux Foundation and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Eclipse Public License v1.0 +# which accompanies this distribution, and is available at +# http://www.eclipse.org/legal/epl-v10.html +############################################################################## +"""Jenkins token commands.""" + +__author__ = 'Thanh Ha' + +import logging + +import click + +from lftools.jenkins.token import get_token + +log = logging.getLogger(__name__) + + +@click.group() +@click.pass_context +def token(ctx): + """Get API token.""" + + +@click.command() +@click.pass_context +def change(ctx): + """Generate a new API token.""" + log.info(get_token(ctx.obj['jenkins_url'], change=True)) + + +@click.command(name='print') +@click.pass_context +def print_token(ctx): + """Print current API token.""" + log.info(get_token(ctx.obj['jenkins_url'])) + + +token.add_command(change) +token.add_command(print_token) diff --git a/lftools/jenkins/token.py b/lftools/jenkins/token.py new file mode 100644 index 00000000..b6128ed4 --- /dev/null +++ b/lftools/jenkins/token.py @@ -0,0 +1,55 @@ +# SPDX-License-Identifier: EPL-1.0 +############################################################################## +# Copyright (c) 2018 The Linux Foundation and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Eclipse Public License v1.0 +# which accompanies this distribution, and is available at +# http://www.eclipse.org/legal/epl-v10.html +############################################################################## +"""Jenkins token functions.""" + +__author__ = 'Thanh Ha' + +import logging + +import jenkins + +from lftools import config + +log = logging.getLogger(__name__) + + +def get_token(url, change=False): + """Get API token. + + This function uses the global username / password for Jenkins from + lftools.ini as the user asking for the token may not already know the + api token. + """ + username = config.get_setting('global', 'username') + password = config.get_setting('global', 'password') + + if change: + log.debug('Resetting Jenkins API token on {}'.format(url)) + else: + log.debug('Fetching Jenkins API token from {}'.format(url)) + + server = jenkins.Jenkins( + url, + username=username, + password=password) + + get_token = """ +import jenkins.security.* +User u = User.get("{}") +ApiTokenProperty t = u.getProperty(ApiTokenProperty.class) +if ({}) {{ + t.changeApiToken() +}} +def token = t.getApiToken() +println "$token" +""".format(username, str(change).lower()) + + token = server.run_script(get_token) + return token diff --git a/releasenotes/notes/jenkins-token-cmd-8e5cdce9175f69a1.yaml b/releasenotes/notes/jenkins-token-cmd-8e5cdce9175f69a1.yaml new file mode 100644 index 00000000..e1d094a9 --- /dev/null +++ b/releasenotes/notes/jenkins-token-cmd-8e5cdce9175f69a1.yaml @@ -0,0 +1,13 @@ +--- +features: + - | + Add new cmd to fetch Jenkins token from user account. An optional + ``--change`` parameter can be passed to have Jenkins change the API token. + + Usage: lftools jenkins token [OPTIONS] + + Get API token. + + Options: + --change Generate a new API token. + --help Show this message and exit. -- 2.16.6