sonar-client 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/lib/sonar/cli/cli.rb +29 -21
- data/lib/sonar/version.rb +1 -1
- data/spec/sonar/cli_spec.rb +30 -6
- data/spec/spec_helper.rb +13 -2
- metadata +2 -3
- data/Gemfile.lock +0 -94
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7d8b3759eb0d676fdbcfff3fac747291e8371b0d
|
4
|
+
data.tar.gz: 8f28065237179666a832d1cf3e6f3f3f4a3dc91e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c34e45704359cdd73976d7982e71e24e9791a7af1ada099411f6d40e8325c020de6ab10adf2c570d54c275d3f172d9f4f3ab2076202ef6babd1a2a83cace7fab
|
7
|
+
data.tar.gz: a913134e524d16c906ca1d92370737ddc4d92e3fc1d71a2d9229e6e090fed248c9d2c9fbda1c8777da6ef3e1ceb14080379bafa63353dbe88afc488cf179d9f2
|
data/.gitignore
CHANGED
data/lib/sonar/cli/cli.rb
CHANGED
@@ -8,7 +8,7 @@ module Sonar
|
|
8
8
|
class CLI < Thor
|
9
9
|
class_option 'profile', aliases: '-P', type: :string, default: File.join(File.expand_path('~'), Sonar::RCFile::FILENAME),
|
10
10
|
desc: 'Path to Sonar RC file', banner: 'FILE'
|
11
|
-
class_option 'format', type: :string, desc: 'Flat JSON or pretty printed [flat/pretty]'
|
11
|
+
class_option 'format', type: :string, desc: 'Flat JSON, JSON lines, or pretty printed [flat/lines/pretty]'
|
12
12
|
|
13
13
|
def initialize(*)
|
14
14
|
@config = Sonar::RCFile.instance.load_file
|
@@ -32,8 +32,14 @@ module Sonar
|
|
32
32
|
@query = {}
|
33
33
|
@query[type.to_sym] = term
|
34
34
|
@query[:limit] = options['record_limit']
|
35
|
-
@client.search(@query)
|
36
|
-
|
35
|
+
resp = @client.search(@query)
|
36
|
+
|
37
|
+
if resp.is_a?(Sonar::Request::RequestIterator)
|
38
|
+
resp.each do |data|
|
39
|
+
print_json(cleanup_data(data), options['format'])
|
40
|
+
end
|
41
|
+
else
|
42
|
+
print_json(cleanup_data(resp), options['format'])
|
37
43
|
end
|
38
44
|
end
|
39
45
|
|
@@ -54,6 +60,12 @@ module Sonar
|
|
54
60
|
case format
|
55
61
|
when 'pretty'
|
56
62
|
ap(json)
|
63
|
+
when 'lines'
|
64
|
+
if json.has_key?('collection')
|
65
|
+
json['collection'].each { |l| puts l.to_json }
|
66
|
+
else
|
67
|
+
puts 'Could not parse the response into lines.'
|
68
|
+
end
|
57
69
|
else
|
58
70
|
# TODO: use a faster JSON generator?
|
59
71
|
puts(json.to_json)
|
@@ -62,31 +74,27 @@ module Sonar
|
|
62
74
|
|
63
75
|
# Clean up whitespace and parse JSON values in responses
|
64
76
|
def cleanup_data(data)
|
65
|
-
|
66
|
-
|
67
|
-
return data
|
68
|
-
end
|
69
|
-
|
70
|
-
ncoll = []
|
71
|
-
|
72
|
-
data[1].each do |item|
|
73
|
-
nitem = {}
|
77
|
+
return data unless data.is_a?(Hash) && data.has_key?('collection')
|
78
|
+
data['collection'].each do |item|
|
74
79
|
item.each_pair do |k,v|
|
75
|
-
|
76
80
|
# Purge whitespace within values
|
77
|
-
|
78
|
-
nkey = k.strip
|
81
|
+
v.is_a?(::String) ? v.strip! : v
|
79
82
|
|
80
83
|
# Parse JSON values
|
81
|
-
if
|
82
|
-
|
84
|
+
if v.is_a?(Array)
|
85
|
+
v.map! do |e|
|
86
|
+
e = safe_parse_json(e)
|
87
|
+
end
|
88
|
+
else
|
89
|
+
v = safe_parse_json(v)
|
83
90
|
end
|
84
|
-
|
85
|
-
nitem[nkey] = nval
|
86
91
|
end
|
87
|
-
ncoll << nitem
|
88
92
|
end
|
89
|
-
|
93
|
+
data
|
94
|
+
end
|
95
|
+
|
96
|
+
def safe_parse_json(s)
|
97
|
+
JSON.parse(s) rescue s
|
90
98
|
end
|
91
99
|
|
92
100
|
# Merge Thor options with those stored in sonar.rc file
|
data/lib/sonar/version.rb
CHANGED
data/spec/sonar/cli_spec.rb
CHANGED
@@ -7,15 +7,39 @@ describe Sonar::CLI do
|
|
7
7
|
Sonar::RCFile.instance.path = "#{fixtures_path}/sonar.rc"
|
8
8
|
end
|
9
9
|
it "should return the profile" do
|
10
|
-
output =
|
10
|
+
output = run_command('profile')
|
11
11
|
expect(output).to match(/email@asdfasdfasfd.com/)
|
12
12
|
end
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
13
|
+
|
14
|
+
context 'client that returns an rdns resp' do
|
15
|
+
before do
|
16
|
+
allow_any_instance_of(Sonar::Client).to receive(:search).and_return(
|
17
|
+
{ 'collection' => [{ 'address' => '192.168.1.1 ' }], 'more' => 'false' }
|
18
|
+
)
|
19
|
+
end
|
20
|
+
it 'strips whitespace from values' do
|
21
|
+
output = run_command('search rdns 8.8.8.8')
|
22
|
+
expect(output).to eq('{"collection":[{"address":"192.168.1.1"}],"more":"false"}')
|
23
|
+
end
|
24
|
+
it 'can return lines format' do
|
25
|
+
output = run_command('search --format lines rdns 8.8.8.8')
|
26
|
+
expect(output).to eq('{"address":"192.168.1.1"}')
|
27
|
+
end
|
17
28
|
end
|
18
|
-
|
29
|
+
context 'client that returns sslcert reply with nested json' do
|
30
|
+
before do
|
31
|
+
allow_any_instance_of(Sonar::Client).to receive(:search).and_return(
|
32
|
+
Sonar::Client.new.search(sslcert: '152a0a633aaf13f02c428ac1a3e672e895512bfd')
|
33
|
+
)
|
34
|
+
end
|
35
|
+
it 'parses the nested values in an array' do
|
36
|
+
output = run_command('search sslcert 152a0a633aaf13f02c428ac1a3e672e895512bfd')
|
37
|
+
expect(JSON.parse(output)['collection'].first['details'].first['subject']['ST']).to eq('California')
|
38
|
+
end
|
19
39
|
end
|
20
40
|
end
|
41
|
+
|
42
|
+
def run_command(args)
|
43
|
+
capture(:stdout) { Sonar::CLI.start(args.split) }.strip
|
44
|
+
end
|
21
45
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -28,8 +28,6 @@ end
|
|
28
28
|
RSpec.configure do |c|
|
29
29
|
c.include APIMatchers::RSpecMatchers
|
30
30
|
|
31
|
-
c.treat_symbols_as_metadata_keys_with_true_values = true
|
32
|
-
|
33
31
|
#
|
34
32
|
# Add gem specific configuration for easy access
|
35
33
|
#
|
@@ -50,6 +48,19 @@ RSpec.configure do |c|
|
|
50
48
|
end
|
51
49
|
end
|
52
50
|
|
51
|
+
def capture(stream)
|
52
|
+
begin
|
53
|
+
stream = stream.to_s
|
54
|
+
eval "$#{stream} = StringIO.new"
|
55
|
+
yield
|
56
|
+
result = eval("$#{stream}").string
|
57
|
+
ensure
|
58
|
+
eval("$#{stream} = #{stream.upcase}")
|
59
|
+
end
|
60
|
+
|
61
|
+
result
|
62
|
+
end
|
63
|
+
|
53
64
|
def fixtures_path
|
54
65
|
File.expand_path('../fixtures', __FILE__)
|
55
66
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sonar-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Paul Deardorff & HD Moore
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-04-
|
11
|
+
date: 2015-04-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday_middleware
|
@@ -233,7 +233,6 @@ files:
|
|
233
233
|
- .rspec
|
234
234
|
- .rubocop.yml
|
235
235
|
- Gemfile
|
236
|
-
- Gemfile.lock
|
237
236
|
- LICENSE
|
238
237
|
- README.md
|
239
238
|
- Rakefile
|
data/Gemfile.lock
DELETED
@@ -1,94 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: .
|
3
|
-
specs:
|
4
|
-
sonar-client (0.0.3)
|
5
|
-
awesome_print
|
6
|
-
faraday_middleware (~> 0.9.0)
|
7
|
-
hashie (~> 2.0.3)
|
8
|
-
multi_json
|
9
|
-
thor
|
10
|
-
|
11
|
-
GEM
|
12
|
-
remote: https://rubygems.org/
|
13
|
-
specs:
|
14
|
-
activesupport (4.2.0)
|
15
|
-
i18n (~> 0.7)
|
16
|
-
json (~> 1.7, >= 1.7.7)
|
17
|
-
minitest (~> 5.1)
|
18
|
-
thread_safe (~> 0.3, >= 0.3.4)
|
19
|
-
tzinfo (~> 1.1)
|
20
|
-
addressable (2.3.6)
|
21
|
-
api_matchers (0.5.1)
|
22
|
-
activesupport (>= 3.2.5)
|
23
|
-
nokogiri (>= 1.5.2)
|
24
|
-
rspec (>= 2.14)
|
25
|
-
awesome_print (1.6.1)
|
26
|
-
crack (0.4.2)
|
27
|
-
safe_yaml (~> 1.0.0)
|
28
|
-
diff-lcs (1.2.5)
|
29
|
-
docile (1.1.5)
|
30
|
-
faraday (0.9.1)
|
31
|
-
multipart-post (>= 1.2, < 3)
|
32
|
-
faraday_middleware (0.9.1)
|
33
|
-
faraday (>= 0.7.4, < 0.10)
|
34
|
-
hashie (2.0.5)
|
35
|
-
i18n (0.7.0)
|
36
|
-
json (1.8.2)
|
37
|
-
mini_portile (0.6.2)
|
38
|
-
minitest (5.5.1)
|
39
|
-
multi_json (1.10.1)
|
40
|
-
multipart-post (2.0.0)
|
41
|
-
nokogiri (1.6.5)
|
42
|
-
mini_portile (~> 0.6.0)
|
43
|
-
rake (10.4.2)
|
44
|
-
rspec (3.1.0)
|
45
|
-
rspec-core (~> 3.1.0)
|
46
|
-
rspec-expectations (~> 3.1.0)
|
47
|
-
rspec-mocks (~> 3.1.0)
|
48
|
-
rspec-core (3.1.7)
|
49
|
-
rspec-support (~> 3.1.0)
|
50
|
-
rspec-expectations (3.1.2)
|
51
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
52
|
-
rspec-support (~> 3.1.0)
|
53
|
-
rspec-mocks (3.1.3)
|
54
|
-
rspec-support (~> 3.1.0)
|
55
|
-
rspec-support (3.1.2)
|
56
|
-
safe_yaml (1.0.4)
|
57
|
-
shoulda (3.5.0)
|
58
|
-
shoulda-context (~> 1.0, >= 1.0.1)
|
59
|
-
shoulda-matchers (>= 1.4.1, < 3.0)
|
60
|
-
shoulda-context (1.2.1)
|
61
|
-
shoulda-matchers (2.7.0)
|
62
|
-
activesupport (>= 3.0.0)
|
63
|
-
simplecov (0.9.1)
|
64
|
-
docile (~> 1.1.0)
|
65
|
-
multi_json (~> 1.0)
|
66
|
-
simplecov-html (~> 0.8.0)
|
67
|
-
simplecov-html (0.8.0)
|
68
|
-
simplecov-rcov (0.2.3)
|
69
|
-
simplecov (>= 0.4.1)
|
70
|
-
thor (0.19.1)
|
71
|
-
thread_safe (0.3.4)
|
72
|
-
tzinfo (1.2.2)
|
73
|
-
thread_safe (~> 0.1)
|
74
|
-
vcr (2.8.0)
|
75
|
-
webmock (1.8.11)
|
76
|
-
addressable (>= 2.2.7)
|
77
|
-
crack (>= 0.1.7)
|
78
|
-
yard (0.8.7.6)
|
79
|
-
|
80
|
-
PLATFORMS
|
81
|
-
ruby
|
82
|
-
|
83
|
-
DEPENDENCIES
|
84
|
-
api_matchers
|
85
|
-
bundler
|
86
|
-
rake
|
87
|
-
rspec
|
88
|
-
shoulda
|
89
|
-
simplecov
|
90
|
-
simplecov-rcov
|
91
|
-
sonar-client!
|
92
|
-
vcr (~> 2.8.0)
|
93
|
-
webmock (~> 1.8.0)
|
94
|
-
yard
|