Skip to content

Commit

Permalink
feat(APIv2): RHINENG-11779 expose os_versions on systems/SGs
Browse files Browse the repository at this point in the history
  • Loading branch information
skateman committed Aug 7, 2024
1 parent a2ef278 commit 873f1f7
Show file tree
Hide file tree
Showing 12 changed files with 4,915 additions and 4,450 deletions.
5 changes: 5 additions & 0 deletions app/controllers/v2/security_guides_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ def rule_tree
permission_for_action :rule_tree, Rbac::COMPLIANCE_VIEWER
permitted_params_for_action :rule_tree, id: ID_TYPE.required

def os_versions
render json: security_guides.os_versions, status: :ok
end
permission_for_action :os_versions, Rbac::COMPLIANCE_VIEWER

private

def security_guides
Expand Down
5 changes: 5 additions & 0 deletions app/controllers/v2/systems_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ def destroy
permission_for_action :destroy, Rbac::POLICY_WRITE
permitted_params_for_action :destroy, { id: ID_TYPE }

def os_versions
render json: systems.os_versions, status: :ok
end
permission_for_action :os_versions, Rbac::SYSTEM_READ

private

def systems
Expand Down
4 changes: 4 additions & 0 deletions app/models/v2/security_guide.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class SecurityGuide < ApplicationRecord
sortable_by :version, version_to_array(arel_table[:version])
sortable_by :os_major_version

def self.os_versions
reselect(:os_major_version).distinct.reorder(:os_major_version).map(&:os_major_version)
end

# Builds the hierarchical structure of groups and rules
def rule_tree
cached_rules = rules.order(:precedence).select(:id, :rule_group_id).group_by(&:rule_group_id)
Expand Down
10 changes: 10 additions & 0 deletions app/models/v2/system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ class System < ApplicationRecord
OS_MAJOR_VERSION = AN::InfixOperation.new('->', OS_VERSION, AN::Quoted.new('major')).as('os_major_version')
OWNER_ID = AN::InfixOperation.new('->>', arel_table[:system_profile], AN::Quoted.new('owner_id'))

OS_VERSIONS = AN::NamedFunction.new(
'CONCAT', [V2::System::OS_MAJOR_VERSION.left, AN::Quoted.new('.'), V2::System::OS_MINOR_VERSION.left]
).as('os_version')

# rubocop:disable Metrics/MethodLength
def self.first_group_name(table = arel_table)
AN::NamedFunction.new(
Expand Down Expand Up @@ -148,5 +152,11 @@ def self.arel_inventory_groups(groups, key, table)
)
)
end

def self.os_versions
distinct.reorder(OS_MAJOR_VERSION.left, OS_MINOR_VERSION.left)
.reselect(OS_VERSIONS, OS_MAJOR_VERSION, OS_MINOR_VERSION)
.map(&:os_version)
end
end
end
4 changes: 4 additions & 0 deletions app/policies/v2/security_guide_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ def rule_tree?
true
end

def os_versions?
true
end

# All users should see all security guides currently
class Scope < V2::ApplicationPolicy::Scope
end
Expand Down
4 changes: 4 additions & 0 deletions app/policies/v2/system_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ def destroy?
match_account? && match_group?
end

def os_versions?
true
end

private

def match_account?
Expand Down
16 changes: 14 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ def draw_routes(prefix)
resources :security_guides, only: %i[index show] do
get :supported_profiles, action: :index, controller: :supported_profiles, on: :collection
get :rule_tree, on: :member
get :os_versions, on: :collection

resources :value_definitions, only: %i[index show], parents: %i[security_guide]
resources :rules, only: %i[index show], parents: %i[security_guide]
resources :rule_groups, only: %i[index show], parents: %i[security_guide]

resources :profiles, only: %i[index show], parents: %i[security_guide] do
resources :rules, only: %i[index show], parents: %i[security_guide profiles]
end
Expand All @@ -41,18 +43,28 @@ def draw_routes(prefix)
resources :rules, only: %i[index create update destroy], parents: %i[policies tailorings]
get :tailoring_file, on: :member, defaults: { format: 'xml' }, constraints: { format: /json|xml/ }
end
resources :systems, only: %i[index create update destroy], parents: %i[policies]

resources :systems, only: %i[index create update destroy], parents: %i[policies] do
get :os_versions, on: :collection
end
end

resources :systems, only: %i[index show] do
resources :policies, only: %i[index], parents: %i[systems]
resources :reports, only: %i[index], parents: %i[systems]

get :os_versions, on: :collection
end

resources :reports, only: %i[index show destroy] do
resources :systems, only: %i[index show], parents: %i[reports]
resources :systems, only: %i[index show], parents: %i[reports] do
get :os_versions, on: :collection
end

resources :test_results, only: %i[index show], parents: %i[report] do
resources :rule_results, only: %i[index], parents: %i[report test_result]
end

get :stats, on: :member
end
end
Expand Down
21 changes: 21 additions & 0 deletions spec/integration/v2/security_guides_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,27 @@
end
end

path '/security_guides/os_versions' do
before { FactoryBot.create_list(:v2_security_guide, 25) }

get 'Request the list of available OS versions' do
v2_auth_header
tags 'Content'
description 'This feature is exclusively used by the frontend'
operationId 'SecurityGuidesOS'
content_types
deprecated true

response '200', 'Lists available OS versions' do
schema(type: :array, items: { type: 'integer' })

after { |e| autogenerate_examples(e, 'List of available OS versions') }

run_test!
end
end
end

path '/security_guides/{security_guide_id}' do
let(:item) { FactoryBot.create(:v2_security_guide) }

Expand Down
104 changes: 104 additions & 0 deletions spec/integration/v2/systems_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,27 @@
end
end

path '/systems/os_versions' do
before { FactoryBot.create_list(:system, 25, account: user.account) }

get 'Request the list of available OS versions' do
v2_auth_header
tags 'Systems'
description 'This feature is exclusively used by the frontend'
operationId 'SystemsOS'
content_types
deprecated true

response '200', 'Lists available OS versions' do
schema(type: :array, items: { type: 'string' })

after { |e| autogenerate_examples(e, 'List of available OS versions') }

run_test!
end
end
end

path '/systems/{system_id}' do
let(:item) { FactoryBot.create(:system, account: user.account) }

Expand Down Expand Up @@ -212,6 +233,47 @@
end
end

path '/policies/{policy_id}/systems/os_versions' do
before do
FactoryBot.create_list(
:system,
25,
policy_id: policy_id,
os_major_version: 8,
os_minor_version: 0,
account: user.account
)
end

let(:policy_id) do
FactoryBot.create(
:v2_policy,
account: user.account,
os_major_version: 8,
supports_minors: [0]
).id
end

get 'Request the list of available OS versions' do
v2_auth_header
tags 'Systems'
description 'This feature is exclusively used by the frontend'
operationId 'PolicySystemsOS'
content_types
deprecated true

parameter name: :policy_id, in: :path, type: :string, required: true

response '200', 'Lists available OS versions' do
schema(type: :array, items: { type: 'string' })

after { |e| autogenerate_examples(e, 'List of available OS versions') }

run_test!
end
end
end

path '/policies/{policy_id}/systems/{system_id}' do
let(:policy_id) do
FactoryBot.create(
Expand Down Expand Up @@ -375,6 +437,48 @@
end
end

path '/reports/{report_id}/systems/os_versions' do
before do
FactoryBot.create_list(
:system,
25,
policy_id: report_id,
os_major_version: 8,
os_minor_version: 0,
account: user.account
)
end

let(:report_id) do
FactoryBot.create(
:v2_report,
account: user.account,
os_major_version: 8,
supports_minors: [0],
assigned_system_count: 0
).id
end

get 'Request the list of available OS versions' do
v2_auth_header
tags 'Systems'
description 'This feature is exclusively used by the frontend'
operationId 'ReportSystemsOS'
content_types
deprecated true

parameter name: :report_id, in: :path, type: :string, required: true

response '200', 'Lists available OS versions' do
schema(type: :array, items: { type: 'string' })

after { |e| autogenerate_examples(e, 'List of available OS versions') }

run_test!
end
end
end

path '/reports/{report_id}/systems/{system_id}' do
let(:report_id) do
FactoryBot.create(
Expand Down
20 changes: 18 additions & 2 deletions spec/models/v2/security_guide_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
describe V2::SecurityGuide do
subject { FactoryBot.create(:v2_security_guide, os_major_version: os_version) }

describe 'os_major_version' do
describe '#os_major_version' do
context 'single digit os_major_version' do
let(:os_version) { 7 }

Expand All @@ -23,7 +23,7 @@
end
end

describe 'rule_tree' do
describe '#rule_tree' do
let(:os_version) { 8 }

let(:g1) { FactoryBot.create(:v2_rule_group, security_guide: subject, precedence: 1) }
Expand Down Expand Up @@ -99,4 +99,20 @@
expect(subject.rule_tree).to eq(result)
end
end

describe '.os_versions' do
let(:versions) { [7, 8, 9] }

before do
versions.each do |version|
FactoryBot.create_list(:v2_security_guide, (1..10).to_a.sample, os_major_version: version)
end
end

subject { described_class.all }

it 'returns a unique and sorted set of all versions' do
expect(subject.os_versions).to eq(versions)
end
end
end
22 changes: 22 additions & 0 deletions spec/models/v2/system_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

require 'rails_helper'

describe V2::System do
describe '.os_versions' do
let(:versions) { ['7.1', '7.2', '7.3', '7.4', '7.5', '8.2', '8.10', '9.0', '9.1'] }

before do
versions.each do |version|
major, minor = version.split('.')
FactoryBot.create_list(:system, (1..10).to_a.sample, os_major_version: major, os_minor_version: minor)
end
end

subject { described_class.all }

it 'returns a unique and sorted set of all versions' do
expect(subject.os_versions.to_set { |version| version.delete('"') }).to eq(versions.to_set)
end
end
end
Loading

0 comments on commit 873f1f7

Please sign in to comment.