From f2d2d087cdeeeefb6fcfd709df0e0ae3328c6394 Mon Sep 17 00:00:00 2001 From: Girija Soni Date: Mon, 11 Dec 2023 13:37:40 +0530 Subject: [PATCH] Fixes #36972 - Make hammer accept unlimited as JWT life time --- app/controllers/api/v2/registration_commands_controller.rb | 2 +- .../concerns/foreman/controller/registration_commands.rb | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/app/controllers/api/v2/registration_commands_controller.rb b/app/controllers/api/v2/registration_commands_controller.rb index 8c8bb6984a9e..a6cfa2871035 100644 --- a/app/controllers/api/v2/registration_commands_controller.rb +++ b/app/controllers/api/v2/registration_commands_controller.rb @@ -15,7 +15,7 @@ class RegistrationCommandsController < V2::BaseController param :smart_proxy_id, :number, desc: N_("ID of the Smart Proxy. This Proxy must have enabled both the 'Templates' and 'Registration' features") param :setup_insights, :bool, desc: N_("Set 'host_registration_insights' parameter for the host. If it is set to true, insights client will be installed and registered on Red Hat family operating systems") param :setup_remote_execution, :bool, desc: N_("Set 'host_registration_remote_execution' parameter for the host. If it is set to true, SSH keys will be installed on the host") - param :jwt_expiration, :number, desc: N_("Expiration of the authorization token (in hours)") + param :jwt_expiration, String, desc: N_("Expiration of the authorization token (in hours)") param :insecure, :bool, desc: N_("Enable insecure argument for the initial curl") param :packages, String, desc: N_("Packages to install on the host when registered. Can be set by `host_packages` parameter, example: `pkg1 pkg2`") param :update_packages, :bool, desc: N_("Update all packages on the host") diff --git a/app/controllers/concerns/foreman/controller/registration_commands.rb b/app/controllers/concerns/foreman/controller/registration_commands.rb index 6db3b94a31ab..a23ecb09f514 100644 --- a/app/controllers/concerns/foreman/controller/registration_commands.rb +++ b/app/controllers/concerns/foreman/controller/registration_commands.rb @@ -33,11 +33,14 @@ def command_headers } if registration_params['jwt_expiration'].present? - jwt_args[:expiration] = registration_params['jwt_expiration'].to_i.hours.to_i if registration_params['jwt_expiration'] != 'unlimited' + if /^-?\d+\.?\d*$/.match?(registration_params['jwt_expiration'].to_s) + jwt_args[:expiration] = registration_params['jwt_expiration'].to_i.hours.to_i + elsif registration_params['jwt_expiration'] != 'unlimited' + raise ArgumentError, _("Error: Option --jwt-expiration: Numeric value is required.") + end else jwt_args[:expiration] = 4.hours.to_i end - "-H 'Authorization: Bearer #{User.current.jwt_token!(**jwt_args)}'" end