Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch scc request timeout exception #63

Merged
merged 4 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,35 @@ There different ways to run the module:
* `DISPLAY= rake run` — forces to run in ncurses mode;
* `Y2DIR=src/ /usr/sbin/yast2 --ncurses rmt` — same as above.

#### Docker Setup

To run the module within a Docker container:

1. Select a proper Docker container image for YaST from https://registry.opensuse.org, according to the branch, e.g.:

* On branch `master`, use `yast/head/containers_tumbleweed/yast-ruby`.
* On branch `SLE-15-SP6`, use `yast/sle-15/sp6/containers/yast-ruby`.

2. Run the Docker container with access to the localhost network with the chosen distribution and version:

```shell
docker run --network host -v "$(pwd):/usr/src/app" -w "/usr/src/app" -it registry.opensuse.org/yast/sle-15/sp6/containers/yast-ruby sh
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much Luis! This is awesome!

```

3. On the container, install the `rmt-server` package:

```shell
zypper --non-interactive install rmt-server
```

4. Run the YaST RMT module with `rake run` or through the other ways previously described.

### Running tests

It is possible to run the specs in a Docker container:

```
docker build -t yast-rmt-image .
docker run -it yast-rmt-image rspec
```shell
docker run -v "$(pwd):/usr/src/app" -w "/usr/src/app" -it registry.opensuse.org/yast/sle-15/sp6/containers/yast-ruby rake test:unit
```

### Resources
Expand Down
9 changes: 9 additions & 0 deletions package/yast2-rmt.changes
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
-------------------------------------------------------------------
Thu Feb 8 16:10:29 UTC 2024 - Luis Caparroz <[email protected]>

- Adds UI dialog to allow the user to retry the SCC credential validation when
the request times out (bsc#1218084).
- Adds HTTP User-Agent to requests to the SCC API and changes the enpoint for
credential validation.
- Version 1.3.5

-------------------------------------------------------------------
Thu Jun 9 10:47:13 UTC 2022 - Dominique Leuenberger <[email protected]>

Expand Down
2 changes: 1 addition & 1 deletion package/yast2-rmt.spec
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@


Name: yast2-rmt
Version: 1.3.4
Version: 1.3.5
Release: 0

BuildRoot: %{_tmppath}/%{name}-%{version}-build
Expand Down
71 changes: 59 additions & 12 deletions spec/rmt/wizard_scc_page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,26 +127,73 @@
expect(Yast::UI).to receive(:CloseDialog)

expect_any_instance_of(Net::HTTP::Get).to receive(:basic_auth).with(config['scc']['username'], config['scc']['password'])

expect(Net::HTTP).to receive(:start).and_return(response_double)
expect(response_double).to receive(:code).and_return(response_code)
end

let(:response_double) { instance_double(Net::HTTPResponse) }
context 'when the request completes without errors' do
before do
expect(Net::HTTP).to receive(:start).and_return(response_double)
expect(response_double).to receive(:code).and_return(response_code)
end

let(:response_double) { instance_double(Net::HTTPResponse) }

context 'when HTTP response code is 200' do
let(:response_code) { '200' }
context 'when HTTP response code is 200' do
let(:response_code) { '200' }

it 'returns true' do
expect(scc_page.scc_credentials_valid?).to be(true)
it 'returns true' do
expect(scc_page.scc_credentials_valid?).to be(true)
end
end

context 'when HTTP response code is not 200' do
let(:response_code) { '401' }

it 'returns false' do
expect(scc_page.scc_credentials_valid?).to be(false)
end
end
end

context 'when HTTP response code is not 200' do
let(:response_code) { '401' }
context 'when SCC times out and the user chooses not to try again' do
before do
expect(Yast::Popup).to receive(:ErrorAnyQuestion).and_return(false)
expect(Net::HTTP).to receive(:start).and_raise(Net::ReadTimeout)
end

context 'and the user chooses to not try again' do
it 'returns false' do
expect(scc_page.scc_credentials_valid?).to be(false)
end
end
end

context 'when SCC times out and the user chooses to try again' do
before do
expect(Yast::Popup).to receive(:ErrorAnyQuestion).and_return(true)
expect(Net::HTTP).to receive(:start).and_raise(Net::ReadTimeout)
expect(Net::HTTP).to receive(:start).and_return(response_double)
end

let(:response_double) { instance_double(Net::HTTPResponse) }

context 'when SCC responds quickly and the HTTP response code is 200' do
before do
expect(response_double).to receive(:code).and_return(200)
end

it 'returns true' do
expect(scc_page.scc_credentials_valid?).to be(true)
end
end

context 'when SCC responds quickly and the HTTP response code is not 200' do
before do
expect(response_double).to receive(:code).and_return(401)
end

it 'returns false' do
expect(scc_page.scc_credentials_valid?).to be(false)
it 'returns false' do
expect(scc_page.scc_credentials_valid?).to be(false)
end
end
end
end
Expand Down
23 changes: 19 additions & 4 deletions src/lib/rmt/wizard_scc_page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ module RMT; end
class RMT::WizardSCCPage < Yast::Client
include ::UI::EventDispatcher

YAST_RMT_USER_AGENT = 'yast2-rmt'.freeze
Copy link
Member

@lslezak lslezak Feb 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about adding some version number to the agent string? Standard web browsers do that as well.

Non-working pseudocode:

YAST_RMT_USER_AGENT = "yast2-rmt/#{RMT::VERSION}".freeze

In theory this could allow to handle some corner cases or bugs in SMT on the server size. Or if the SCC changes the API it could fallback to old API for old versions...

Ideally this should not be needed but it would be nice to have this possibility as the last resort...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created the constant module RMT::VERSION, added it to the user agent, and created a unit test to ensure it always matches the package version. Please check commit 89e919c.


def initialize(config)
textdomain 'rmt'
@config = config
Expand Down Expand Up @@ -126,14 +128,27 @@ def scc_credentials_valid?
)
)

uri = URI('https://scc.suse.com/connect/organizations/systems')
uri = URI('https://scc.suse.com/connect/organizations/orders')
req = Net::HTTP::Get.new(uri)
req.basic_auth(@config['scc']['username'], @config['scc']['password'])

res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) { |http| http.request(req) }
req['User-Agent'] = YAST_RMT_USER_AGENT

valid_credentials = nil
while valid_credentials.nil?
begin
res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) { |http| http.request(req) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a minor suggestion:

Suggested change
res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) { |http| http.request(req) }
res = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.is_a?(URI::HTTPS)) { |http| http.request(req) }

This is more robust as it will still work after you change the URL above to plain HTTP.

Copy link
Collaborator Author

@lcaparroz lcaparroz Feb 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a nice enhancement, but since the URL is hard-coded, it will not be very helpful.

I wonder if it would be valuable for test and development purposes to have a way to configure the SCC URL (e.g.: env var), then this change would be very welcome. I had to manually change both the URL and the use_ssl flag whenever I tested it locally.

valid_credentials = (res.code.to_i == 200)
rescue Net::ReadTimeout
break valid_credentials = false unless Popup.ErrorAnyQuestion(
_('Request Timeout'),
_("The request to SCC timed out.\n\nWould you like to try again?"),
_('Retry'), _('Cancel'), :focus_yes
)
end
end

UI.CloseDialog

res.code.to_i == 200
valid_credentials
end
end
Loading