Skip to content

Commit

Permalink
Fixes #36972 - Make hammer accept unlimited as JWT life time
Browse files Browse the repository at this point in the history
  • Loading branch information
girijaasoni committed Dec 21, 2023
1 parent 70b2144 commit ade3227
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
3 changes: 1 addition & 2 deletions app/controllers/api/v2/registration_commands_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ class RegistrationCommandsController < V2::BaseController
include Foreman::Controller::RegistrationCommands

before_action :find_smart_proxy, if: -> { registration_params['smart_proxy_id'] }

api :POST, "/registration_commands", N_("Generate global registration command")
param :registration_command, Hash, required: false, action_aware: true do
param :organization_id, :number, desc: N_("ID of the Organization to register the host in")
Expand All @@ -15,7 +14,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, :number, desc: N_("Expiration of the authorization token (in hours), 0 means 'unlimited'.")
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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ def insecure
registration_params['insecure'] ? '--insecure' : ''
end

def is_token_valid(min_value, max_value)
registration_params['jwt_expiration'].to_i.between?(min_value, max_value)
end

def unlimited
registration_params['jwt_expiration'].to_i == 0 || registration_params['jwt_expiration'] == 'unlimited'
end

def registration_url(proxy = nil)
return global_registration_url unless proxy

Expand All @@ -33,7 +41,13 @@ 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'
min_value = 0
max_value = 999999
if is_token_valid(min_value, max_value)
jwt_args[:expiration] = registration_params['jwt_expiration'].to_i.hours.to_i unless unlimited
else
raise ::Foreman::Exception.new(N_("Invalid value %s for jwt_expiration. The value must be between %s to %s. 0 means 'unlimited'."), registration_params['jwt_expiration'], min_value, max_value)
end
else
jwt_args[:expiration] = 4.hours.to_i
end
Expand Down

0 comments on commit ade3227

Please sign in to comment.