From 2e41f4db44eabd1a3e005f6759f24fe2d4443815 Mon Sep 17 00:00:00 2001 From: Girija Soni Date: Mon, 6 Jan 2025 23:15:07 +0530 Subject: [PATCH] Fixes #38124 - Invalidate tokens for specific user/users --- lib/hammer_cli_foreman.rb | 177 ++++++++---------- lib/hammer_cli_foreman/registration_tokens.rb | 24 +++ lib/hammer_cli_foreman/user.rb | 82 ++++---- 3 files changed, 133 insertions(+), 150 deletions(-) create mode 100644 lib/hammer_cli_foreman/registration_tokens.rb diff --git a/lib/hammer_cli_foreman.rb b/lib/hammer_cli_foreman.rb index ef28fe132..073ad71ba 100644 --- a/lib/hammer_cli_foreman.rb +++ b/lib/hammer_cli_foreman.rb @@ -3,7 +3,6 @@ require 'apipie_bindings' module HammerCLIForeman - def self.exception_handler_class HammerCLIForeman::ExceptionHandler end @@ -32,144 +31,114 @@ def self.exception_handler_class require 'hammer_cli_foreman/common_parameter' require 'hammer_cli_foreman/defaults' - HammerCLI::MainCommand.lazy_subcommand('auth', _("Foreman connection login/logout"), - 'HammerCLIForeman::Auth', 'hammer_cli_foreman/auth' - ) + HammerCLI::MainCommand.lazy_subcommand('auth', _('Foreman connection login/logout'), + 'HammerCLIForeman::Auth', 'hammer_cli_foreman/auth') - HammerCLI::MainCommand.lazy_subcommand('architecture', _("Manipulate architectures"), - 'HammerCLIForeman::Architecture', 'hammer_cli_foreman/architecture' - ) + HammerCLI::MainCommand.lazy_subcommand('architecture', _('Manipulate architectures'), + 'HammerCLIForeman::Architecture', 'hammer_cli_foreman/architecture') - HammerCLI::MainCommand.lazy_subcommand('auth-source', _("Manipulate auth sources"), - 'HammerCLIForeman::AuthSource', 'hammer_cli_foreman/auth_source' - ) + HammerCLI::MainCommand.lazy_subcommand('auth-source', _('Manipulate auth sources'), + 'HammerCLIForeman::AuthSource', 'hammer_cli_foreman/auth_source') - HammerCLI::MainCommand.lazy_subcommand('audit', _("Search audit trails."), - 'HammerCLIForeman::Audit', 'hammer_cli_foreman/audit' - ) + HammerCLI::MainCommand.lazy_subcommand('audit', _('Search audit trails.'), + 'HammerCLIForeman::Audit', 'hammer_cli_foreman/audit') - HammerCLI::MainCommand.lazy_subcommand('compute-profile', _("Manipulate compute profiles"), - 'HammerCLIForeman::ComputeProfile', 'hammer_cli_foreman/compute_profile' - ) + HammerCLI::MainCommand.lazy_subcommand('compute-profile', _('Manipulate compute profiles'), + 'HammerCLIForeman::ComputeProfile', 'hammer_cli_foreman/compute_profile') - HammerCLI::MainCommand.lazy_subcommand('compute-resource', _("Manipulate compute resources"), - 'HammerCLIForeman::ComputeResource', 'hammer_cli_foreman/compute_resource' - ) + HammerCLI::MainCommand.lazy_subcommand('compute-resource', _('Manipulate compute resources'), + 'HammerCLIForeman::ComputeResource', 'hammer_cli_foreman/compute_resource') - HammerCLI::MainCommand.lazy_subcommand('domain', _("Manipulate domains"), - 'HammerCLIForeman::Domain', 'hammer_cli_foreman/domain' - ) + HammerCLI::MainCommand.lazy_subcommand('domain', _('Manipulate domains'), + 'HammerCLIForeman::Domain', 'hammer_cli_foreman/domain') - HammerCLI::MainCommand.lazy_subcommand('fact', _("Search facts"), - 'HammerCLIForeman::Fact', 'hammer_cli_foreman/fact' - ) + HammerCLI::MainCommand.lazy_subcommand('fact', _('Search facts'), + 'HammerCLIForeman::Fact', 'hammer_cli_foreman/fact') - HammerCLI::MainCommand.lazy_subcommand('filter', _("Manage permission filters"), - 'HammerCLIForeman::Filter', 'hammer_cli_foreman/filter' - ) + HammerCLI::MainCommand.lazy_subcommand('filter', _('Manage permission filters'), + 'HammerCLIForeman::Filter', 'hammer_cli_foreman/filter') - HammerCLI::MainCommand.lazy_subcommand('host', _("Manipulate hosts"), - 'HammerCLIForeman::Host', 'hammer_cli_foreman/host' - ) + HammerCLI::MainCommand.lazy_subcommand('host', _('Manipulate hosts'), + 'HammerCLIForeman::Host', 'hammer_cli_foreman/host') - HammerCLI::MainCommand.lazy_subcommand('hostgroup', _("Manipulate hostgroups"), - 'HammerCLIForeman::Hostgroup', 'hammer_cli_foreman/hostgroup' - ) + HammerCLI::MainCommand.lazy_subcommand('hostgroup', _('Manipulate hostgroups'), + 'HammerCLIForeman::Hostgroup', 'hammer_cli_foreman/hostgroup') - HammerCLI::MainCommand.lazy_subcommand('http-proxy', _("Manipulate http proxies"), - 'HammerCLIForeman::HttpProxy', 'hammer_cli_foreman/http_proxy' - ) + HammerCLI::MainCommand.lazy_subcommand('http-proxy', _('Manipulate http proxies'), + 'HammerCLIForeman::HttpProxy', 'hammer_cli_foreman/http_proxy') - HammerCLI::MainCommand.lazy_subcommand('location', _("Manipulate locations"), - 'HammerCLIForeman::Location', 'hammer_cli_foreman/location' - ) + HammerCLI::MainCommand.lazy_subcommand('location', _('Manipulate locations'), + 'HammerCLIForeman::Location', 'hammer_cli_foreman/location') - HammerCLI::MainCommand.lazy_subcommand('medium', _("Manipulate installation media"), - 'HammerCLIForeman::Medium', 'hammer_cli_foreman/media' - ) + HammerCLI::MainCommand.lazy_subcommand('medium', _('Manipulate installation media'), + 'HammerCLIForeman::Medium', 'hammer_cli_foreman/media') - HammerCLI::MainCommand.lazy_subcommand('model', _("Manipulate hardware models"), - 'HammerCLIForeman::Model', 'hammer_cli_foreman/model' - ) + HammerCLI::MainCommand.lazy_subcommand('model', _('Manipulate hardware models'), + 'HammerCLIForeman::Model', 'hammer_cli_foreman/model') - HammerCLI::MainCommand.lazy_subcommand('os', _("Manipulate operating system"), - 'HammerCLIForeman::OperatingSystem', 'hammer_cli_foreman/operating_system' - ) + HammerCLI::MainCommand.lazy_subcommand('os', _('Manipulate operating system'), + 'HammerCLIForeman::OperatingSystem', 'hammer_cli_foreman/operating_system') - HammerCLI::MainCommand.lazy_subcommand('organization', _("Manipulate organizations"), - 'HammerCLIForeman::Organization', 'hammer_cli_foreman/organization' - ) + HammerCLI::MainCommand.lazy_subcommand('organization', _('Manipulate organizations'), + 'HammerCLIForeman::Organization', 'hammer_cli_foreman/organization') - HammerCLI::MainCommand.lazy_subcommand('partition-table', _("Manipulate partition tables"), - 'HammerCLIForeman::PartitionTable', 'hammer_cli_foreman/partition_table' - ) + HammerCLI::MainCommand.lazy_subcommand('partition-table', _('Manipulate partition tables'), + 'HammerCLIForeman::PartitionTable', 'hammer_cli_foreman/partition_table') - HammerCLI::MainCommand.lazy_subcommand('report', _("Browse and read reports"), - 'HammerCLIForeman::ConfigReport', 'hammer_cli_foreman/config_report', - :warning => _('%{report} command is deprecated and will be removed in one of the future versions. Please use %{config_report} command instead.') % {:report => 'report', :config_report => 'config-report'} - ) + HammerCLI::MainCommand.lazy_subcommand('report', _('Browse and read reports'), + 'HammerCLIForeman::ConfigReport', 'hammer_cli_foreman/config_report', + warning: format(_('%{report} command is deprecated and will be removed in one of the future versions. Please use %{config_report} command instead.'), report: 'report', config_report: 'config-report')) - HammerCLI::MainCommand.lazy_subcommand('report-template', _("Manipulate report templates"), - 'HammerCLIForeman::ReportTemplate', 'hammer_cli_foreman/report_template', - ) + HammerCLI::MainCommand.lazy_subcommand('report-template', _('Manipulate report templates'), + 'HammerCLIForeman::ReportTemplate', 'hammer_cli_foreman/report_template') - HammerCLI::MainCommand.lazy_subcommand('config-report', _("Browse and read reports"), - 'HammerCLIForeman::ConfigReport', 'hammer_cli_foreman/config_report' - ) + HammerCLI::MainCommand.lazy_subcommand('config-report', _('Browse and read reports'), + 'HammerCLIForeman::ConfigReport', 'hammer_cli_foreman/config_report') - HammerCLI::MainCommand.lazy_subcommand('role', _("Manage user roles"), - 'HammerCLIForeman::Role', 'hammer_cli_foreman/role' - ) + HammerCLI::MainCommand.lazy_subcommand('role', _('Manage user roles'), + 'HammerCLIForeman::Role', 'hammer_cli_foreman/role') - HammerCLI::MainCommand.lazy_subcommand('proxy', _("Manipulate smart proxies"), - 'HammerCLIForeman::SmartProxy', 'hammer_cli_foreman/smart_proxy' - ) + HammerCLI::MainCommand.lazy_subcommand('proxy', _('Manipulate smart proxies'), + 'HammerCLIForeman::SmartProxy', 'hammer_cli_foreman/smart_proxy') - HammerCLI::MainCommand.lazy_subcommand('realm', _("Manipulate realms"), - 'HammerCLIForeman::Realm', 'hammer_cli_foreman/realm' - ) + HammerCLI::MainCommand.lazy_subcommand('realm', _('Manipulate realms'), + 'HammerCLIForeman::Realm', 'hammer_cli_foreman/realm') - HammerCLI::MainCommand.lazy_subcommand('settings', _("Change server settings"), - 'HammerCLIForeman::Settings', 'hammer_cli_foreman/settings' - ) + HammerCLI::MainCommand.lazy_subcommand('settings', _('Change server settings'), + 'HammerCLIForeman::Settings', 'hammer_cli_foreman/settings') - HammerCLI::MainCommand.lazy_subcommand('subnet', _("Manipulate subnets"), - 'HammerCLIForeman::Subnet', 'hammer_cli_foreman/subnet' - ) + HammerCLI::MainCommand.lazy_subcommand('subnet', _('Manipulate subnets'), + 'HammerCLIForeman::Subnet', 'hammer_cli_foreman/subnet') - HammerCLI::MainCommand.lazy_subcommand('template', _("Manipulate provisioning templates"), - 'HammerCLIForeman::Template', 'hammer_cli_foreman/template' - ) + HammerCLI::MainCommand.lazy_subcommand('template', _('Manipulate provisioning templates'), + 'HammerCLIForeman::Template', 'hammer_cli_foreman/template') - HammerCLI::MainCommand.lazy_subcommand('user', _("Manipulate users"), - 'HammerCLIForeman::User', 'hammer_cli_foreman/user' - ) + HammerCLI::MainCommand.lazy_subcommand('user', _('Manipulate users'), + 'HammerCLIForeman::User', 'hammer_cli_foreman/user') - HammerCLI::MainCommand.lazy_subcommand('user-group', _("Manage user groups"), - 'HammerCLIForeman::Usergroup', 'hammer_cli_foreman/usergroup' - ) + HammerCLI::MainCommand.lazy_subcommand('user-group', _('Manage user groups'), + 'HammerCLIForeman::Usergroup', 'hammer_cli_foreman/usergroup') HammerCLI::MainCommand.lazy_subcommand('ping', _("Get the status of the server and/or it's subcomponents"), - 'HammerCLIForeman::PingCommand', 'hammer_cli_foreman/ping' - ) + 'HammerCLIForeman::PingCommand', 'hammer_cli_foreman/ping') HammerCLI::MainCommand.lazy_subcommand('status', _("Get the complete status of the server and/or it's subcomponents"), - 'HammerCLIForeman::StatusCommand', 'hammer_cli_foreman/status' - ) + 'HammerCLIForeman::StatusCommand', 'hammer_cli_foreman/status') + + HammerCLI::MainCommand.lazy_subcommand('mail-notification', _('Manage mail notifications'), + 'HammerCLIForeman::MailNotification', 'hammer_cli_foreman/mail_notification') - HammerCLI::MainCommand.lazy_subcommand('mail-notification', _("Manage mail notifications"), - 'HammerCLIForeman::MailNotification', 'hammer_cli_foreman/mail_notification' - ) + HammerCLI::MainCommand.lazy_subcommand('bookmark', _('Manage bookmarks'), + 'HammerCLIForeman::Bookmark', 'hammer_cli_foreman/bookmark') - HammerCLI::MainCommand.lazy_subcommand('bookmark', _("Manage bookmarks"), - 'HammerCLIForeman::Bookmark', 'hammer_cli_foreman/bookmark') + HammerCLI::MainCommand.lazy_subcommand('host-registration', _('Host Registration'), + 'HammerCLIForeman::Registration', 'hammer_cli_foreman/registration') - HammerCLI::MainCommand.lazy_subcommand('host-registration', _("Host Registration"), - 'HammerCLIForeman::Registration', 'hammer_cli_foreman/registration') - rescue => e - handler = HammerCLIForeman::ExceptionHandler.new(:context => {}, :adapter => :base) + HammerCLI::MainCommand.lazy_subcommand('registration-tokens', _('Manipulate registration tokens'), + 'HammerCLIForeman::RegistrationTokens', 'hammer_cli_foreman/registration_tokens') + rescue StandardError => e + handler = HammerCLIForeman::ExceptionHandler.new(context: {}, adapter: :base) handler.handle_exception(e) - raise HammerCLI::ModuleLoadingError.new(e) + raise HammerCLI::ModuleLoadingError, e end - end diff --git a/lib/hammer_cli_foreman/registration_tokens.rb b/lib/hammer_cli_foreman/registration_tokens.rb new file mode 100644 index 000000000..39d8da204 --- /dev/null +++ b/lib/hammer_cli_foreman/registration_tokens.rb @@ -0,0 +1,24 @@ +module HammerCLIForeman + class RegistrationTokens < HammerCLIForeman::Command + resource :registration_tokens + command_name 'registration-tokens' + desc _('Manage registration tokens') + + class InvalidateMultipleCommand < HammerCLIForeman::SingleResourceCommand + action :invalidate_jwt_tokens + command_name 'invalidate_multiple' + success_message _("Successfully invalidated registration tokens\n") + build_options + end + + class InvalidateCommand < HammerCLIForeman::SingleResourceCommand + action :invalidate_jwt + command_name 'invalidate' + success_message _("Successfully invalidated registration tokens\n") + + build_options + end + + autoload_subcommands + end +end diff --git a/lib/hammer_cli_foreman/user.rb b/lib/hammer_cli_foreman/user.rb index 01ab8f71e..eb7ad2e0a 100644 --- a/lib/hammer_cli_foreman/user.rb +++ b/lib/hammer_cli_foreman/user.rb @@ -1,41 +1,36 @@ module HammerCLIForeman - class User < HammerCLIForeman::Command - resource :users class ListCommand < HammerCLIForeman::ListCommand - output do - field :id, _("Id") - field :login, _("Login") - field :full_name, _("Name") - field :mail, _("Email") - field :admin, _("Admin"), Fields::Boolean - field :disabled, _("Disabled"), Fields::Boolean - field :last_login_on, _("Last login"), Fields::Date - field nil, _("Authorized by"), Fields::SingleReference, :key => :auth_source + field :id, _('Id') + field :login, _('Login') + field :full_name, _('Name') + field :mail, _('Email') + field :admin, _('Admin'), Fields::Boolean + field :disabled, _('Disabled'), Fields::Boolean + field :last_login_on, _('Last login'), Fields::Date + field nil, _('Authorized by'), Fields::SingleReference, key: :auth_source end def extend_data(user) - user["full_name"] = [user["firstname"], user["lastname"]].join(' ') + user['full_name'] = [user['firstname'], user['lastname']].join(' ') user end build_options end - class InfoCommand < HammerCLIForeman::InfoCommand - output ListCommand.output_definition do - field :mail_enabled, _("Email enabled"), Fields::Boolean - field :effective_admin, _("Effective admin"), Fields::Boolean - field :locale, _("Locale") - field :timezone, _("Timezone") - field :description, _("Description") - custom_field Fields::Reference, :label => _("Default organization"), :path => [:default_organization] - custom_field Fields::Reference, :label => _("Default location"), :path => [:default_location] + field :mail_enabled, _('Email enabled'), Fields::Boolean + field :effective_admin, _('Effective admin'), Fields::Boolean + field :locale, _('Locale') + field :timezone, _('Timezone') + field :description, _('Description') + custom_field Fields::Reference, label: _('Default organization'), path: [:default_organization] + custom_field Fields::Reference, label: _('Default location'), path: [:default_location] HammerCLIForeman::References.roles(self) HammerCLIForeman::References.usergroups(self) HammerCLIForeman::References.taxonomies(self) @@ -43,10 +38,10 @@ class InfoCommand < HammerCLIForeman::InfoCommand end def extend_data(user) - user["locale"] ||= _('default') if user.has_key?("locale") - user["timezone"] ||= _('default') if user.has_key?("timezone") - user["full_name"] = [user["firstname"], user["lastname"]].join(' ') - user["cached_usergroups"] = user.fetch('cached_usergroups', []) - user["usergroups"] + user['locale'] ||= _('default') if user.has_key?('locale') + user['timezone'] ||= _('default') if user.has_key?('timezone') + user['full_name'] = [user['firstname'], user['lastname']].join(' ') + user['cached_usergroups'] = user.fetch('cached_usergroups', []) - user['usergroups'] user end @@ -54,46 +49,41 @@ def extend_data(user) end class CreateCommand < HammerCLIForeman::CreateCommand - success_message _("User [%{login}] created.") - failure_message _("Could not create the user") + success_message _('User [%{login}] created.') + failure_message _('Could not create the user') build_options extend_with(HammerCLIForeman::CommandExtensions::User.new) end - class UpdateCommand < HammerCLIForeman::UpdateCommand - success_message _("User [%{login}] updated.") - failure_message _("Could not update the user") + success_message _('User [%{login}] updated.') + failure_message _('Could not update the user') build_options extend_with(HammerCLIForeman::CommandExtensions::User.new) end - class DeleteCommand < HammerCLIForeman::DeleteCommand - success_message _("User [%{login}] deleted.") - failure_message _("Could not delete the user") + success_message _('User [%{login}] deleted.') + failure_message _('Could not delete the user') build_options end HammerCLIForeman::AssociatingCommands::Role.extend_command(self) - lazy_subcommand('ssh-keys', _("Managing User SSH Keys."), - 'HammerCLIForeman::SSHKeys', 'hammer_cli_foreman/ssh_keys' - ) - lazy_subcommand('access-token', _("Managing personal access tokens"), - 'HammerCLIForeman::PersonalAccessToken', 'hammer_cli_foreman/personal_access_token' - ) - lazy_subcommand('mail-notification', _("Managing personal mail notifications"), - 'HammerCLIForeman::UserMailNotification', 'hammer_cli_foreman/user_mail_notification' - ) - lazy_subcommand('table-preference', _("Managing table preferences"), - 'HammerCLIForeman::TablePreference', 'hammer_cli_foreman/table_preference' - ) + lazy_subcommand('ssh-keys', _('Managing User SSH Keys.'), + 'HammerCLIForeman::SSHKeys', 'hammer_cli_foreman/ssh_keys') + lazy_subcommand('access-token', _('Managing personal access tokens'), + 'HammerCLIForeman::PersonalAccessToken', 'hammer_cli_foreman/personal_access_token') + lazy_subcommand('mail-notification', _('Managing personal mail notifications'), + 'HammerCLIForeman::UserMailNotification', 'hammer_cli_foreman/user_mail_notification') + lazy_subcommand('table-preference', _('Managing table preferences'), + 'HammerCLIForeman::TablePreference', 'hammer_cli_foreman/table_preference') + lazy_subcommand('registration-tokens', _('Managing registration tokens'), + 'HammerCLIForeman::RegistrationTokens', 'hammer_cli_foreman/registration_tokens') autoload_subcommands end - end