Skip to content

Commit 32149aa

Browse files
committed
Fixes #39117 - Support generating registration command via REST API in isolated networks managed by external capsules
1 parent 1981e78 commit 32149aa

2 files changed

Lines changed: 81 additions & 0 deletions

File tree

app/controllers/api/v2/registration_commands_controller.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ module V2
33
class RegistrationCommandsController < V2::BaseController
44
include Api::Version2
55
include Foreman::Controller::RegistrationCommands
6+
include Foreman::Controller::SmartProxyAuth
7+
8+
add_smart_proxy_filters :create, :features => ['Registration', 'Templates']
69

710
before_action :find_smart_proxy, if: -> { registration_params['smart_proxy_id'] }
811
api :POST, "/registration_commands", N_("Generate global registration command")

test/controllers/api/v2/registration_commands_controller_test.rb

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,82 @@ class Api::V2::RegistrationCommandsControllerTest < ActionController::TestCase
9393
assert_includes response, "--header 'Authorization: Bearer"
9494
end
9595
end
96+
97+
describe 'Smart Proxy authentication' do
98+
test 'smart proxy with Registration and Templates features can generate registration command' do
99+
Setting[:restrict_registered_smart_proxies] = true
100+
101+
features = [FactoryBot.create(:feature, name: 'Registration'), FactoryBot.create(:feature, name: 'Templates')]
102+
proxy = FactoryBot.create(:smart_proxy, features: features, url: 'https://proxy.example.com')
103+
104+
@request.env['HTTPS'] = 'on'
105+
@request.env['SSL_CLIENT_S_DN'] = 'CN=proxy.example.com'
106+
@request.env['SSL_CLIENT_VERIFY'] = 'SUCCESS'
107+
108+
post :create
109+
assert_response :success
110+
assert_equal proxy, @controller.detected_proxy
111+
end
112+
113+
test 'smart proxy without Registration feature cannot generate registration command' do
114+
Setting[:restrict_registered_smart_proxies] = true
115+
reset_api_credentials
116+
User.current = nil
117+
118+
features = [FactoryBot.create(:feature, name: 'Templates')]
119+
FactoryBot.create(:smart_proxy, features: features, url: 'https://proxy.example.com')
120+
121+
@request.env['HTTPS'] = 'on'
122+
@request.env['SSL_CLIENT_S_DN'] = 'CN=proxy.example.com'
123+
@request.env['SSL_CLIENT_VERIFY'] = 'SUCCESS'
124+
125+
post :create
126+
assert_response :forbidden
127+
end
128+
129+
test 'smart proxy without Templates feature cannot generate registration command' do
130+
Setting[:restrict_registered_smart_proxies] = true
131+
reset_api_credentials
132+
User.current = nil
133+
134+
features = [FactoryBot.create(:feature, name: 'Registration')]
135+
FactoryBot.create(:smart_proxy, features: features, url: 'https://proxy.example.com')
136+
137+
@request.env['HTTPS'] = 'on'
138+
@request.env['SSL_CLIENT_S_DN'] = 'CN=proxy.example.com'
139+
@request.env['SSL_CLIENT_VERIFY'] = 'SUCCESS'
140+
141+
post :create
142+
assert_response :forbidden
143+
end
144+
145+
test 'unregistered smart proxy cannot generate registration command' do
146+
Setting[:restrict_registered_smart_proxies] = true
147+
reset_api_credentials
148+
User.current = nil
149+
150+
@request.env['HTTPS'] = 'on'
151+
@request.env['SSL_CLIENT_S_DN'] = 'CN=unknown.example.com'
152+
@request.env['SSL_CLIENT_VERIFY'] = 'SUCCESS'
153+
154+
post :create
155+
assert_response :forbidden
156+
end
157+
158+
test 'smart proxy with unverified SSL cert cannot generate registration command' do
159+
Setting[:restrict_registered_smart_proxies] = true
160+
reset_api_credentials
161+
User.current = nil
162+
163+
features = [FactoryBot.create(:feature, name: 'Registration'), FactoryBot.create(:feature, name: 'Templates')]
164+
FactoryBot.create(:smart_proxy, features: features, url: 'https://proxy.example.com')
165+
166+
@request.env['HTTPS'] = 'on'
167+
@request.env['SSL_CLIENT_S_DN'] = 'CN=proxy.example.com'
168+
@request.env['SSL_CLIENT_VERIFY'] = 'FAILED'
169+
170+
post :create
171+
assert_response :forbidden
172+
end
173+
end
96174
end

0 commit comments

Comments
 (0)