diff --git a/.gitignore b/.gitignore index d3ad3d80b..38bd858dd 100644 --- a/.gitignore +++ b/.gitignore @@ -31,6 +31,7 @@ coverage/ config/secrets.yml.key config/secrets.yml.enc config/rmt.local.yml +config/system_uuid .env .tags* diff --git a/lib/suse/connect/api.rb b/lib/suse/connect/api.rb index ed0ce3597..32192b2cb 100644 --- a/lib/suse/connect/api.rb +++ b/lib/suse/connect/api.rb @@ -7,6 +7,9 @@ module Connect class Api class InvalidCredentialsError < StandardError; end + SYSTEM_UUID_LOCATION = 'config/system_uuid'.freeze + ROOT_PATH = Pathname.new(File.dirname(__FILE__)).join('../', '../', '../').freeze + CONNECT_API_URL = 'https://scc.suse.com/connect'.freeze def initialize(username, password) @username = username @@ -14,23 +17,23 @@ def initialize(username, password) end def list_orders - make_paginated_request(:get, 'https://scc.suse.com/connect/organizations/orders') + make_paginated_request(:get, "#{CONNECT_API_URL}/organizations/orders") end def list_products - make_paginated_request(:get, 'https://scc.suse.com/connect/organizations/products') + make_paginated_request(:get, "#{CONNECT_API_URL}/organizations/products") end def list_products_unscoped - make_paginated_request(:get, 'https://scc.suse.com/connect/organizations/products/unscoped') + make_paginated_request(:get, "#{CONNECT_API_URL}/organizations/products/unscoped") end def list_repositories - make_paginated_request(:get, 'https://scc.suse.com/connect/organizations/repositories') + make_paginated_request(:get, "#{CONNECT_API_URL}/organizations/repositories") end def list_subscriptions - make_paginated_request(:get, 'https://scc.suse.com/connect/organizations/subscriptions') + make_paginated_request(:get, "#{CONNECT_API_URL}/organizations/subscriptions") end protected @@ -47,7 +50,7 @@ def make_request(method, url, options) options[:userpwd] = "#{@username}:#{@password}" unless options[:userpwd] options[:method] = method options[:accept_encoding] = 'gzip, deflate' - options[:headers] = { 'RMT' => @username } + options[:headers] = { 'RMT' => system_uuid } response = RMT::HttpRequest.new(url, options).run raise InvalidCredentialsError if (response.code == 401) @@ -80,6 +83,22 @@ def make_paginated_request(method, url, options = {}) @entities end + private + + def system_uuid + @system_uuid ||= fetch_system_uuid + end + + def fetch_system_uuid + uuid_file_location = ROOT_PATH.join(SYSTEM_UUID_LOCATION) + if File.exist?(uuid_file_location) + File.read(uuid_file_location) + else + uuid = SecureRandom.uuid + File.write(uuid_file_location, uuid) + uuid + end + end end end end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 175bb499a..b17e0b5f6 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -26,6 +26,8 @@ # If you are not using ActiveRecord, you can remove this line. ActiveRecord::Migration.maintain_test_schema! +File.delete(Rails.root.join('config', 'system_uuid')) if File.exist?(Rails.root.join('config', 'system_uuid')) + RSpec.configure do |config| # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures config.fixture_path = "#{::Rails.root}/spec/fixtures" diff --git a/spec/suse/connect/api_spec.rb b/spec/suse/connect/api_spec.rb index 28dce7522..0b553a0a9 100644 --- a/spec/suse/connect/api_spec.rb +++ b/spec/suse/connect/api_spec.rb @@ -78,7 +78,7 @@ { 'Authorization' => 'Basic ' + Base64.encode64("#{username}:#{password}").strip, 'User-Agent' => "RMT/#{RMT::VERSION}", - 'RMT' => username + 'RMT' => api_client.send(:system_uuid) } end let(:response_data) { { foo: 'bar' } } @@ -87,41 +87,48 @@ subject { api_client.send(:make_single_request, 'GET', 'http://example.org/api_method') } it { is_expected.to eq(response_data) } + it { expect(File.read(Rails.root.join('config', 'system_uuid'))).to eq(api_client.send(:system_uuid)) } end describe '#make_paginated_request' do subject { api_client.send(:make_paginated_request, 'GET', 'http://example.org/api_method') } it { is_expected.to eq([response_data, response_data]) } + it { expect(File.read(Rails.root.join('config', 'system_uuid'))).to eq(api_client.send(:system_uuid)) } end describe '#list_orders' do subject { api_client.list_orders } it { is_expected.to eq([ { endpoint: 'organizations/orders' } ]) } + it { expect(File.read(Rails.root.join('config', 'system_uuid'))).to eq(api_client.send(:system_uuid)) } end describe '#list_products' do subject { api_client.list_products } it { is_expected.to eq([ { endpoint: 'organizations/products' } ]) } + it { expect(File.read(Rails.root.join('config', 'system_uuid'))).to eq(api_client.send(:system_uuid)) } end describe '#list_products_unscoped' do subject { api_client.list_products_unscoped } it { is_expected.to eq([ { endpoint: 'organizations/products/unscoped' } ]) } + it { expect(File.read(Rails.root.join('config', 'system_uuid'))).to eq(api_client.send(:system_uuid)) } end describe '#list_repositories' do subject { api_client.list_repositories } it { is_expected.to eq([ { endpoint: 'organizations/repositories' } ]) } + it { expect(File.read(Rails.root.join('config', 'system_uuid'))).to eq(api_client.send(:system_uuid)) } end describe '#list_subscriptions' do subject { api_client.list_subscriptions } it { is_expected.to eq([ { endpoint: 'organizations/subscriptions' } ]) } + it { expect(File.read(Rails.root.join('config', 'system_uuid'))).to eq(api_client.send(:system_uuid)) } end end