Skip to content

Commit

Permalink
auth log: remove protocol associations (#1664)
Browse files Browse the repository at this point in the history
* auth log: remove protocol associations
  • Loading branch information
dmitry-sinina authored Dec 20, 2024
1 parent 1176e79 commit 07b3cc7
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 36 deletions.
30 changes: 22 additions & 8 deletions app/admin/cdr/auth_logs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
[:gateway_name, proc { |row| row.gateway.try(:name) }],
[:node_name, proc { |row| row.node.try(:name) }],
[:pop_name, proc { |row| row.pop.try(:name) }],
[:transport_protocol, proc { |row| row.transport_protocol.try(:name) }],
:transport_protocol_name,
:transport_remote_ip,
:transport_remote_port,
:transport_local_ip,
:transport_local_port,
[:origination_protocol, proc { |row| row.origination_protocol.try(:name) }],
:origination_protocol_name,
:origination_ip,
:origination_port,
:username,
Expand Down Expand Up @@ -52,7 +52,7 @@

controller do
def scoped_collection
super.preload(:gateway, :pop, :node, :transport_protocol, :origination_protocol)
super.preload(:gateway, :pop, :node)
end
end

Expand All @@ -67,11 +67,11 @@ def scoped_collection
column :internal_reason

column :originator do |c|
"#{c.origination_protocol.try(:display_name)}://#{c.origination_ip}:#{c.origination_port}"
"#{c.origination_protocol_name}://#{c.origination_ip}:#{c.origination_port}"
end

column :remote_socket do |c|
"#{c.transport_protocol.try(:display_name)}://#{c.transport_remote_ip}:#{c.transport_remote_port}"
"#{c.transport_protocol_name}://#{c.transport_remote_ip}:#{c.transport_remote_port}"
end

column :local_socket do |c|
Expand Down Expand Up @@ -113,9 +113,23 @@ def scoped_collection
filter :pop
filter :node
filter :username
filter :origination_ip
filter :transport_remote_ip
filter :transport_local_ip
filter :origination_proto_id_eq, label: 'Origination protocol', as: :select, collection: Cdr::AuthLog::TRANSPORT_PROTOCOLS.invert
filter :origination_ip_covers,
as: :string,
input_html: { class: 'search_filter_string' },
label: 'Origination IP'

filter :transport_proto_id_eq, label: 'Transport protocol', as: :select, collection: Cdr::AuthLog::TRANSPORT_PROTOCOLS.invert
filter :transport_remote_ip_covers,
as: :string,
input_html: { class: 'search_filter_string' },
label: 'Transport remote IP'

filter :transport_local_ip_covers,
as: :string,
input_html: { class: 'search_filter_string' },
label: 'Transport local IP'

filter :ruri
filter :from_uri
filter :to_uri
Expand Down
54 changes: 52 additions & 2 deletions app/models/cdr/auth_log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,66 @@ class Cdr::AuthLog < Cdr::Base
self.pg_partition_depth_past = 3
self.pg_partition_depth_future = 3

TRANSPORT_PROTOCOL_UDP = 1
TRANSPORT_PROTOCOL_TCP = 2
TRANSPORT_PROTOCOL_TLS = 3
TRANSPORT_PROTOCOLS = {
TRANSPORT_PROTOCOL_UDP => 'UDP',
TRANSPORT_PROTOCOL_TCP => 'TCP',
TRANSPORT_PROTOCOL_TLS => 'TLS'
}.freeze

belongs_to :gateway, class_name: 'Gateway', foreign_key: :gateway_id, optional: true
belongs_to :node, class_name: 'Node', foreign_key: :node_id, optional: true
belongs_to :pop, class_name: 'Pop', foreign_key: :pop_id, optional: true
belongs_to :origination_protocol, class_name: 'Equipment::TransportProtocol', foreign_key: :origination_proto_id, optional: true
belongs_to :transport_protocol, class_name: 'Equipment::TransportProtocol', foreign_key: :transport_proto_id, optional: true

scope :successful, -> { where success: true }
scope :failed, -> { where success: false }

scope :transport_local_ip_covers, lambda { |ip|
begin
IPAddr.new(ip)
rescue StandardError
return none
end
where('transport_local_ip<<=?::inet', ip)
}

scope :transport_remote_ip_covers, lambda { |ip|
begin
IPAddr.new(ip)
rescue StandardError
return none
end
where('transport_remote_ip<<=?::inet', ip)
}

scope :origination_ip_covers, lambda { |ip|
begin
IPAddr.new(ip)
rescue StandardError
return none
end
where('origination_ip<<=?::inet', ip)
}

def self.ransackable_scopes(_auth_object = nil)
%i[
origination_ip_covers
transport_local_ip_covers
transport_remote_ip_covers
]
end

def display_name
id.to_s
end

def transport_protocol_name
TRANSPORT_PROTOCOLS[transport_proto_id]
end

def origination_protocol_name
TRANSPORT_PROTOCOLS[origination_proto_id]
end
end
6 changes: 2 additions & 4 deletions app/resources/api/rest/admin/auth_log_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ def self.default_sort
:code,
:reason,
:internal_reason,
:origination_ip, :origination_port, :origination_proto_id, :transport_proto_id,
:transport_remote_ip, :transport_remote_port,
:origination_proto_id, :origination_ip, :origination_port,
:transport_proto_id, :transport_remote_ip, :transport_remote_port,
:transport_local_ip, :transport_local_port,
:username, :realm,
:request_method,
Expand All @@ -29,8 +29,6 @@ def self.default_sort
has_one :gateway, class_name: 'Gateway', always_include_linkage_data: true
has_one :pop, class_name: 'Pop', always_include_linkage_data: true
has_one :node, class_name: 'Node', always_include_linkage_data: true
has_one :origination_protocol, class_name: 'TransportProtocol', foreign_key: :origination_proto_id
has_one :transport_protocol, class_name: 'TransportProtocol', foreign_key: :transport_proto_id

filter :request_time_gteq, apply: lambda { |records, values, _options|
records.where('request_time >= ?', values[0])
Expand Down
14 changes: 1 addition & 13 deletions spec/controllers/api/rest/admin/auth_logs_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
}
end
let(:includes) do
%w[pop gateway node origination-protocol transport-protocol]
%w[pop gateway node]
end

it 'http status should eq 200' do
Expand Down Expand Up @@ -205,18 +205,6 @@
'type' => 'nodes',
'id' => auth_log.node.id.to_s
}
),
'origination-protocol' => hash_including(
'data' => {
'type' => 'transport-protocols',
'id' => auth_log.origination_protocol.id.to_s
}
),
'transport-protocol' => hash_including(
'data' => {
'type' => 'transport-protocols',
'id' => auth_log.transport_protocol.id.to_s
}
)
)
)
Expand Down
11 changes: 4 additions & 7 deletions spec/factories/cdr/auth_logs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
#
FactoryBot.define do
factory :auth_log, class: 'Cdr::AuthLog' do
request_time { 1.minute.ago }
code { 200 }
request_time { 1.minute.ago }
code { 200 }
reason { 'OK' }
internal_reason { 'Response matched' }
origination_ip { '1.1.1.1' }
Expand All @@ -61,11 +61,8 @@
call_id { '2b8a45f5730c1b3459a00b9c322a79da' }
success { true }

transport_protocol { Equipment::TransportProtocol.take }
origination_protocol { Equipment::TransportProtocol.take }

# association :transport_protocol, factory: :transport_protocol
# association :origination_protocol, factory: :transport_protocol
transport_proto_id { Cdr::AuthLog::TRANSPORT_PROTOCOL_TLS }
origination_proto_id { Cdr::AuthLog::TRANSPORT_PROTOCOL_UDP }

association :gateway, factory: :gateway
association :pop, factory: :pop
Expand Down
4 changes: 2 additions & 2 deletions spec/features/cdr/auth_logs/export_auth_logs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
['Pop name', item.pop.try(:name)],
['Node name', item.node.try(:name)],
['Gateway name', item.gateway.try(:name)],
['Transport protocol', item.transport_protocol.try(:name)],
['Transport protocol name', item.transport_protocol_name],
['Transport remote ip', item.transport_remote_ip.to_s],
['Transport remote port', item.transport_remote_port.to_s],
['Transport local ip', item.transport_local_ip.to_s],
['Transport local port', item.transport_local_port.to_s],
['Origination protocol', item.origination_protocol.try(:name)],
['Origination protocol name', item.origination_protocol_name],
['Origination ip', item.origination_ip.to_s],
['Origination port', item.origination_port.to_s],
['Username', item.username.to_s],
Expand Down

0 comments on commit 07b3cc7

Please sign in to comment.