From 2f19b82138efcaf195d076aecf74514a38ea16ed Mon Sep 17 00:00:00 2001 From: "Sean P. McDonald" Date: Tue, 12 Sep 2023 11:58:28 -0500 Subject: [PATCH] (New Feature) Add access list command Add a command to list the current logged in user's tokens --- lib/pupent/access.rb | 5 +++++ lib/pupent/cli.rb | 5 +++++ lib/pupent/pupent_option_parser.rb | 22 +++++++++++++++++++--- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/lib/pupent/access.rb b/lib/pupent/access.rb index 765071f196..56514e86cf 100644 --- a/lib/pupent/access.rb +++ b/lib/pupent/access.rb @@ -42,6 +42,11 @@ def show raise Bolt::Error.new(msg, 'bolt/no-token') end + def list(client) + response, = client.pe_get("/v1/users/current", headers: { 'X-Authentication' => show }) + JSON.pretty_generate(client.pe_get("/v1/users/#{response['id']}/tokens", headers: { 'X-Authentication' => show }).first) + end + def delete_token @logger.info("Deleting token...") File.delete(token_location) diff --git a/lib/pupent/cli.rb b/lib/pupent/cli.rb index de2bddd1cb..7dcc83a2bc 100644 --- a/lib/pupent/cli.rb +++ b/lib/pupent/cli.rb @@ -57,6 +57,11 @@ def execute @parsed_options[:token_file] ).login(new_client, @parsed_options[:lifetime]) 0 + when 'list' + $stdout.puts Access.new( + @parsed_options[:token_file] + ).list(new_client) + 0 when 'show' $stdout.puts Access.new(@parsed_options[:token_file]).show 0 diff --git a/lib/pupent/pupent_option_parser.rb b/lib/pupent/pupent_option_parser.rb index 2e85ad6674..b5d60c9ecc 100644 --- a/lib/pupent/pupent_option_parser.rb +++ b/lib/pupent/pupent_option_parser.rb @@ -130,6 +130,19 @@ def self.colorize(color, string) #{colorize(:cyan, 'Options')} HELP + ACCESS_LIST_HELP = <<~HELP + #{colorize(:cyan, 'Name')} + access list + + #{colorize(:cyan, 'Usage')} + pupent access list [options] + + #{colorize(:cyan, 'Description')} + Print the list of tokens in Puppet Enterprise for the user logged in with the local token + + #{colorize(:cyan, 'Options')} + HELP + ACCESS_SHOW_HELP = <<~HELP #{colorize(:cyan, 'Name')} access show @@ -233,13 +246,16 @@ def self.parse(argv) parser.banner = ACCESS_HELP access_globals(parser) case action - when "login" + when 'login' parser.banner = ACCESS_LOGIN_HELP access_login(parser) - when "show" + when 'list' + parser.banner = ACCESS_LIST_HELP + # No options to add + when 'show' parser.banner = ACCESS_SHOW_HELP # No options to add - when "delete-token-file" + when 'delete-token-file' parser.banner = ACCESS_DELETE_HELP # No options to add else