Skip to content

Commit

Permalink
(New Feature) Add access list command
Browse files Browse the repository at this point in the history
Add a command to list the current logged in user's tokens
  • Loading branch information
mcdonaldseanp committed Sep 12, 2023
1 parent fdc3fd2 commit 2f19b82
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
5 changes: 5 additions & 0 deletions lib/pupent/access.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions lib/pupent/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 19 additions & 3 deletions lib/pupent/pupent_option_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2f19b82

Please sign in to comment.