Skip to content

Commit 4117a83

Browse files
committed
Fixes #39118 - Support generating registration command via REST API in isolated networks managed by external capsules
1 parent cbfe296 commit 4117a83

5 files changed

Lines changed: 94 additions & 0 deletions

File tree

.rubocop_todo.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ Naming/VariableNumber:
218218
- 'test/plugins/plugin_initializer_test.rb'
219219
- 'test/puppetca_http_api/puppetca_http_impl_test.rb'
220220
- 'test/registration/registration_api_test.rb'
221+
- 'test/registration/registration_commands_api_test.rb'
221222
- 'test/sinatra/default_not_found_page_test.rb'
222223
- 'test/templates/templates_unattended_api_test.rb'
223224
- 'test/util_test.rb'
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
require 'registration/registration_api'
2+
require 'registration/registration_commands_api'
23

34
map '/register' do
45
run Proxy::Registration::Api
56
end
7+
8+
map '/registration_commands' do
9+
run Proxy::Registration::CommandsApi
10+
end

modules/registration/proxy_request.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
module Proxy::Registration
44
class ProxyRequest < ::Proxy::HttpRequest::ForemanRequest
5+
def registration_command(request)
6+
proxy_req = request_factory.create_post '/api/registration_commands',
7+
request.body.read,
8+
headers(request)
9+
10+
send_request(proxy_req)
11+
end
12+
513
def global_register(request)
614
proxy_req = request_factory.create_get '/register',
715
request_params(request),
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require 'registration/proxy_request'
2+
3+
class Proxy::Registration::CommandsApi < ::Sinatra::Base
4+
post '/' do
5+
content_type :json
6+
response = Proxy::Registration::ProxyRequest.new.registration_command(request)
7+
handle_response(response)
8+
rescue StandardError => e
9+
logger.exception "Error when proxying registration command", e
10+
render_error('Failed to generate registration command')
11+
end
12+
13+
private
14+
15+
def handle_response(response)
16+
if response.code.start_with? '2'
17+
response.body
18+
else
19+
render_error(response.body, code: response.code)
20+
end
21+
end
22+
23+
def render_error(message, code: 500)
24+
status code
25+
message
26+
end
27+
end
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
require 'test_helper'
2+
require 'registration/registration_commands_api'
3+
4+
class RegistrationCommandsApiTest < Test::Unit::TestCase
5+
include Rack::Test::Methods
6+
7+
def app
8+
Proxy::Registration::CommandsApi.new
9+
end
10+
11+
def setup
12+
@foreman_url = 'http://foreman.example.com'
13+
Proxy::SETTINGS.stubs(:foreman_url).returns(@foreman_url)
14+
end
15+
16+
def test_registration_command
17+
expected_body = '{"registration_command":"curl ... | bash"}'
18+
stub_request(:post, "#{@foreman_url}/api/registration_commands")
19+
.to_return(body: expected_body, headers: { 'Content-Type' => 'application/json' })
20+
21+
post '/'
22+
assert last_response.ok?
23+
assert_equal expected_body, last_response.body
24+
end
25+
26+
def test_registration_command_with_json_body
27+
request_body = '{"registration_command":{"organization_id":1,"hostgroup_id":2}}'
28+
expected_body = '{"registration_command":"curl ... | bash"}'
29+
stub_request(:post, "#{@foreman_url}/api/registration_commands")
30+
.with(body: request_body)
31+
.to_return(body: expected_body, headers: { 'Content-Type' => 'application/json' })
32+
33+
post '/', request_body, { 'CONTENT_TYPE' => 'application/json' }
34+
assert last_response.ok?
35+
assert_equal expected_body, last_response.body
36+
end
37+
38+
def test_registration_command_401
39+
stub_request(:post, "#{@foreman_url}/api/registration_commands")
40+
.to_return(body: 'Unauthorized', status: 401)
41+
42+
post '/'
43+
assert last_response.unauthorized?
44+
end
45+
46+
def test_registration_command_500
47+
Rack::NullLogger.any_instance.stubs(:exception)
48+
stub_request(:post, "#{@foreman_url}/api/registration_commands").to_timeout
49+
50+
post '/'
51+
assert last_response.server_error?
52+
end
53+
end

0 commit comments

Comments
 (0)