File tree Expand file tree Collapse file tree 4 files changed +66
-1
lines changed
app/models/metasploit_data_models/search/visitor
spec/app/models/metasploit_data_models/search/visitor Expand file tree Collapse file tree 4 files changed +66
-1
lines changed Original file line number Diff line number Diff line change 4343 --health-retries 5
4444
4545 strategy :
46- fail-fast : true
46+ fail-fast : false
4747 matrix :
4848 ruby :
4949 - ' 2.7'
Original file line number Diff line number Diff line change @@ -3,6 +3,8 @@ source "https://rubygems.org"
33# Specify your gem's dependencies in metasploit_data_models.gemspec
44gemspec
55
6+ gem 'metasploit-model' , git : 'https://github.com/cdelafuente-r7/metasploit-model' , branch : 'feat/model/search/operation/jsonb'
7+
68group :development do
79 #gem 'metasploit-erd'
810 # embed ERDs on index, namespace Module and Class<ApplicationRecord> pages
Original file line number Diff line number Diff line change @@ -49,6 +49,25 @@ class MetasploitDataModels::Search::Visitor::Where
4949 attribute . matches ( match_value )
5050 end
5151
52+ visit 'Metasploit::Model::Search::Operation::Jsonb' do |operation |
53+ attribute = attribute_visitor . visit operation . operator
54+
55+ begin
56+ operation_hash = JSON . parse ( operation . value )
57+ left = Arel ::Nodes ::InfixOperation . new (
58+ '->>' ,
59+ Arel ::Nodes . build_quoted ( attribute ) ,
60+ Arel ::Nodes . build_quoted ( operation_hash . keys [ 0 ] )
61+ )
62+ right = "%#{ operation_hash . values [ 0 ] } %"
63+ rescue JSON ::ParserError
64+ left = Arel ::Nodes ::NamedFunction . new ( "cast" , [ attribute . as ( 'text' ) ] )
65+ right = "%#{ operation . value } %"
66+ end
67+
68+ left . matches ( right )
69+ end
70+
5271 visit 'MetasploitDataModels::IPAddress::CIDR' do |cidr |
5372 cast_to_inet "#{ cidr . address } /#{ cidr . prefix_length } "
5473 end
Original file line number Diff line number Diff line change 183183 end
184184 end
185185 end
186+
187+ context 'with MetasploitDataModels::Search::Operation::Jsonb' do
188+ let ( :klass ) do
189+ klass = Class . new ( ApplicationRecord )
190+ Object . const_set ( 'Jsonb' , klass )
191+ klass
192+ end
193+
194+ let ( :node ) do
195+ Metasploit ::Model ::Search ::Operation ::Jsonb . new (
196+ :operator => operator ,
197+ :value => value
198+ )
199+ end
200+
201+ let ( :operator ) do
202+ Metasploit ::Model ::Search ::Operator ::Attribute . new (
203+ :klass => klass ,
204+ :attribute => :metadata
205+ )
206+ end
207+
208+ let ( :value ) do
209+ 'adcs_ca:myCA'
210+ end
211+
212+ it 'should visit operation.operator with attribute_visitor' do
213+ expect ( visitor . attribute_visitor ) . to receive ( :visit ) . with ( operator ) . and_call_original
214+
215+ visit
216+ end
217+
218+ it 'should return the expected Arel::Nodes::Matches' do
219+ attribute = Arel ::Attributes ::Attribute . new (
220+ Arel ::Table . new ( :metasploit_credential_privates ) ,
221+ 'metadata'
222+ )
223+ allow ( visitor . attribute_visitor ) . to receive ( :visit ) . with ( operator ) . and_return ( attribute )
224+
225+ results = visit
226+ expect ( results ) . to be_a ( Arel ::Nodes ::Matches )
227+ expect ( results . to_sql ) . to eq ( "\" metasploit_credential_privates\" .\" metadata\" ->> 'adcs_ca' ILIKE '%myCA%'" )
228+ end
229+ end
186230 end
187231end
You can’t perform that action at this time.
0 commit comments