Skip to content

Commit

Permalink
Merge branch 'master' into depfu/update/webmock-3.24.0
Browse files Browse the repository at this point in the history
  • Loading branch information
digitaltom authored Nov 5, 2024
2 parents 8a13dce + 1500238 commit 3ef17f0
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 80 deletions.
2 changes: 1 addition & 1 deletion PACKAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Note: Look below for direction on publishing to registry.
```
* Alternatively, if an OBS working copy is already checked out, update the working copy by running `osc up`
2. Run `make dist` in your RMT working directory to build a tarball.
3. Copy the files from the `package/obs` directory to the OBS working directory.
3. Copy the files from the `package/obs` directory to the OBS working directory `systemsmanagement:SCC:RMT/rmt-server`.
4. Examine the changes by running `osc status` and `osc diff`.
5. Stage the changes by running `osc addremove`.
6. Build the package with osc:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,12 @@

context 'when verification provider returns false' do
before do
expect(InstanceVerification::Providers::Example).to receive(:new)
.with(be_a(ActiveSupport::Logger), be_a(ActionDispatch::Request), payload, instance_data).and_return(plugin_double)
expect(plugin_double).to receive(:instance_valid?).and_return(false)
stub_request(:post, scc_activate_url)
.to_return(
status: 200,
body: { error: 'Unexpected instance verification error has occurred' }.to_json,
headers: {}
)
post url, params: payload, headers: headers
end

Expand All @@ -86,9 +89,13 @@

context 'when verification provider raises an unhandled exception' do
before do
expect(InstanceVerification::Providers::Example).to receive(:new)
.with(be_a(ActiveSupport::Logger), be_a(ActionDispatch::Request), payload, instance_data).and_return(plugin_double)
expect(plugin_double).to receive(:instance_valid?).and_raise('Custom plugin error')
stub_request(:post, scc_activate_url)
.to_return(
status: 422,
body: { error: 'Unexpected instance verification error has occurred' }.to_json,
headers: {}
)

post url, params: payload, headers: headers
end

Expand Down Expand Up @@ -623,7 +630,7 @@
.with({ headers: scc_headers, body: payload.merge({ byos_mode: 'byos' }) })
.and_return(
status: 401,
body: { error: 'error_message' }.to_json,
body: 'Migration target not allowed on this instance type',
headers: {}
)
request
Expand Down
124 changes: 54 additions & 70 deletions engines/scc_proxy/lib/scc_proxy/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,23 @@ def announce_system_scc(auth, params)
JSON.parse(response.body)
end

def scc_activate_product(product, auth, params, mode)
def scc_activate_product(system, product, auth, params, mode)
uri = URI.parse(SYSTEM_PRODUCTS_URL)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
scc_request = prepare_scc_request(uri.path, product, auth, params, mode)
http.request(scc_request)
response = http.request(scc_request)
unless response.code_type == Net::HTTPCreated
error = JSON.parse(response.body)
Rails.logger.info "Could not activate #{product.product_string}, error: #{error['error']} #{response.code}"
error['error'] = SccProxy.parse_error(error['error']) if error['error'].include? 'json'
# if trying to activate first product on a hybrid system
# it means the system was "just" announced on this call
# if product activation failed, system should get de-register from SCC
SccProxy.deregister_system_scc(auth, system) if system.payg?

raise ActionController::TranslatedError.new(error['error'])
end
end

def deactivate_product_scc(auth, product, params, logger)
Expand All @@ -168,12 +179,19 @@ def deactivate_product_scc(auth, product, params, logger)
response
end

def deregister_system_scc(auth, system_token)
def deregister_system_scc(auth, system)
uri = URI.parse(DEREGISTER_SYSTEM_URL)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
scc_request = Net::HTTP::Delete.new(uri.path, headers(auth, system_token))
http.request(scc_request)
scc_request = Net::HTTP::Delete.new(uri.path, headers(auth, system.system_token))
response = http.request(scc_request)
unless response.code_type == Net::HTTPNoContent
error = JSON.parse(response.body)
Rails.logger.info "Could not de-activate system #{system.login}, error: #{error['error']} #{response.code}"
error['error'] = SccProxy.parse_error(error['error'], params[:token], params[:email]) if error['error'].include? 'json'
raise ActionController::TranslatedError.new(error['error'])
end
Rails.logger.info 'System successfully deregistered from SCC'
end

def parse_error(error_message, token = nil, email = nil)
Expand Down Expand Up @@ -346,46 +364,16 @@ def has_no_regcode?(auth_header)

protected

# rubocop:disable Metrics/PerceivedComplexity
# rubocop:disable Metrics/CyclomaticComplexity
def scc_activate_product
logger.info "Activating product #{@product.product_string} to SCC"
auth = nil
auth = request.headers['HTTP_AUTHORIZATION'] if request.headers.include?('HTTP_AUTHORIZATION')
mode = nil
if @system.byos?
mode = 'byos'
elsif !@product.free? && @product.extension? && params[:token].present?
mode = 'hybrid'
# the extensions must be the same version and arch
# than base product
base_prod = @system.products.find_by(product_type: :base)
if @system.payg? && base_prod.present?
raise 'Incompatible extension product' unless @product.arch == base_prod.arch && @product.version == base_prod.version

update_params_system_info mode
announce_auth = "Token token=#{params[:token]}"

response = SccProxy.announce_system_scc(announce_auth, params)
end
end

mode = find_mode
unless mode.nil?
# if system is byos or hybrid and there is a token
# make a request to SCC
response = SccProxy.scc_activate_product(@product, auth, params, mode)
unless response.code_type == Net::HTTPCreated
error = JSON.parse(response.body)
logger.info "Could not activate #{@product.product_string}, error: #{error['error']} #{response.code}"
error['error'] = SccProxy.parse_error(error['error']) if error['error'].include? 'json'
if @system.payg?
# if trying to activate first product on a hybrid system
# it means the system was "just" announced on this call
# if product activation failed, system should get de-register from SCC
deregister_hybrid(request.headers['HTTP_AUTHORIZATION'])
end
raise ActionController::TranslatedError.new(error['error'])
end
logger.info "Activating product #{@product.product_string} to SCC"
logger.info 'No token provided' if params[:token].blank?
SccProxy.scc_activate_product(
@system, @product, request.headers['HTTP_AUTHORIZATION'], params, mode
)
# if the system is PAYG and the registration code is valid for the extension,
# then the system is hybrid
# update the system to HYBRID mode if HYBRID MODE and system not HYBRID already
Expand All @@ -394,20 +382,31 @@ def scc_activate_product
logger.info "Product #{@product.product_string} successfully activated with SCC"
InstanceVerification.update_cache(request.remote_ip, @system.login, @product.id)
end
logger.info 'No token provided' if params[:token].blank?
end
# rubocop:enable Metrics/CyclomaticComplexity
# rubocop:enable Metrics/PerceivedComplexity

def deregister_hybrid(auth)
response = SccProxy.deregister_system_scc(auth, @system.system_token)
unless response.code_type == Net::HTTPNoContent
error = JSON.parse(response.body)
logger.info "Could not de-activate system #{@system.login}, error: #{error['error']} #{response.code}"
error['error'] = SccProxy.parse_error(error['error'], params[:token], params[:email]) if error['error'].include? 'json'
raise ActionController::TranslatedError.new(error['error'])

def find_mode
if @system.byos?
'byos'
elsif !@product.free? && @product.extension? && params[:token].present?
announce_base_product_hybrid 'hybrid'
'hybrid'
end
end

def announce_base_product_hybrid(mode)
# in order for SCC to be able activate the extension (i.e. LTSS)
# the system must be announced to SCC first
base_prod = @system.products.find_by(product_type: :base)
# the extensions must be the same version and arch
# than base product
if @system.payg? && base_prod.present?
raise 'Incompatible extension product' unless @product.arch == base_prod.arch && @product.version == base_prod.version

update_params_system_info mode
SccProxy.announce_system_scc(
"Token token=#{params[:token]}", params
)
end
logger.info 'System successfully deregistered from SCC'
end

def scc_upgrade
Expand Down Expand Up @@ -466,14 +465,7 @@ def make_system_payg(auth)
# switch it back to payg
# drop the just de-activated activation from the list to avoid another call to SCC
# and check if there is any product
response = SccProxy.deregister_system_scc(auth, @system.system_token)
unless response.code_type == Net::HTTPNoContent
error = JSON.parse(response.body)
logger.info "Could not de-activate system #{@system.login}, error: #{error['error']} #{response.code}"
error['error'] = SccProxy.parse_error(error['error'], params[:token], params[:email]) if error['error'].include? 'json'
raise ActionController::TranslatedError.new(error['error'])
end
logger.info 'System successfully deregistered from SCC'
SccProxy.deregister_system_scc(auth, @system)
@system.payg!
end
end
Expand All @@ -486,15 +478,7 @@ def make_system_payg(auth)
def scc_deregistration
if @system.byos? || @system.hybrid?
# byos and hybrid systems should get de-register from SCC
auth = request.headers['HTTP_AUTHORIZATION']
response = SccProxy.deregister_system_scc(auth, @system.system_token)
unless response.code_type == Net::HTTPNoContent
error = JSON.parse(response.body)
logger.info "Could not de-activate system #{@system.login}, error: #{error['error']} #{response.code}"
error['error'] = SccProxy.parse_error(error['error'], params[:token], params[:email]) if error['error'].include? 'json'
raise ActionController::TranslatedError.new(error['error'])
end
logger.info 'System successfully deregistered from SCC'
SccProxy.deregister_system_scc(request.headers['HTTP_AUTHORIZATION'], @system)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,10 @@
end

context 'when de-register system from SCC fails' do
let(:error_message) { "Could not activate #{product.product_string}, error: No product found on SCC for: foo bar x86_64 json api 401" }

before do
allow(Rails.logger).to receive(:info)
stub_request(:delete, scc_systems_url)
.to_return(
status: 401,
Expand All @@ -483,11 +486,11 @@
)
stub_request(:post, scc_register_system_url)
.to_return(status: 201, body: { ok: 'OK' }.to_json, headers: {})

post url, params: payload, headers: headers
end

it 'renders an error with exception details' do
expect(Rails.logger).to receive(:info).with(error_message)
post url, params: payload, headers: headers
data = JSON.parse(response.body)
expect(data['error']).to eq('Could not de-register system')
end
Expand Down
8 changes: 8 additions & 0 deletions package/obs/rmt-server.spec
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ chrpath -d %{buildroot}%{lib_dir}/vendor/bundle/ruby/*/extensions/*/*/mysql2-*/m
%dir %{_sysconfdir}/slp.reg.d
%config(noreplace) %attr(0640, %{rmt_user}, root) %{_sysconfdir}/rmt.conf
%config(noreplace) %{_sysconfdir}/slp.reg.d/rmt-server.reg

%{_mandir}/man8/rmt-cli.8%{?ext_man}
%{_bindir}/rmt-cli
%{_bindir}/rmt-data-import
Expand All @@ -289,6 +290,11 @@ chrpath -d %{buildroot}%{lib_dir}/vendor/bundle/ruby/*/extensions/*/*/mysql2-*/m
%{_unitdir}/rmt-server-systems-scc-sync.timer
%{_unitdir}/rmt-uptime-cleanup.service
%{_unitdir}/rmt-uptime-cleanup.timer
%config(noreplace) %{_unitdir}/rmt-server-mirror.timer
%config(noreplace) %{_unitdir}/rmt-server-sync.timer
%config(noreplace) %{_unitdir}/rmt-server-systems-scc-sync.timer
%config(noreplace) %{_unitdir}/rmt-uptime-cleanup.timer

%dir %{_datadir}/bash-completion/
%dir %{_datadir}/bash-completion/completions/
%{_datadir}/bash-completion/completions/rmt-cli
Expand Down Expand Up @@ -323,6 +329,8 @@ chrpath -d %{buildroot}%{lib_dir}/vendor/bundle/ruby/*/extensions/*/*/mysql2-*/m
%{_unitdir}/rmt-server-regsharing.timer
%{_unitdir}/rmt-server-trim-cache.service
%{_unitdir}/rmt-server-trim-cache.timer
%config(noreplace) %{_unitdir}/rmt-server-regsharing.timer
%config(noreplace) %{_unitdir}/rmt-server-trim-cache.timer

%pre
getent group %{rmt_group} >/dev/null || %{_sbindir}/groupadd -r %{rmt_group}
Expand Down

0 comments on commit 3ef17f0

Please sign in to comment.