irrc 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/bin/irrc DELETED
@@ -1,5 +0,0 @@
1
- #!/usr/bin/env ruby -Ilib
2
-
3
- require 'irrc/cli'
4
-
5
- Irrc::Cli::Client.new(ARGV).start
@@ -1 +0,0 @@
1
- require 'irrc/cli/client'
@@ -1,108 +0,0 @@
1
- require 'optparse'
2
-
3
- require 'irrc'
4
- require 'irrc/cli/yaml_printer'
5
-
6
- module Irrc
7
- module Cli
8
- class Client
9
- def initialize(args)
10
- @args = args
11
- @options = Struct.new(:host, :source, :protocol, :threads, :debug).new(nil, [], [], 1, nil)
12
- end
13
-
14
- def start
15
- OptionParser.new(&method(:options)).parse!(@args)
16
-
17
- verify_arguments
18
- set_default_arguments
19
-
20
- Irrc::Cli::YamlPrinter.print perform
21
-
22
- rescue
23
- $stderr.print "#{$!.class}: " unless $!.instance_of?(RuntimeError)
24
- $stderr.puts $!.message
25
-
26
- exit 1
27
- end
28
-
29
-
30
- private
31
-
32
- def options(opts)
33
- opts.banner = <<-EOS
34
- Usage: #{opts.program_name} [options] [objects ...]
35
-
36
- Description:
37
- Better IRR client to resolve as-set, route-set or aut-num object into prefixes.
38
-
39
- If no [-4|-6|--ipv4|--ipv6] option given, it tries both of ipv4 and ipv6.
40
-
41
- Options:
42
- EOS
43
-
44
- opts.on '-h HOST', 'Specify FQDN of IRR / Whois server to send queries.',
45
- 'IRR / Whois name is also acceptable. This switch is mandatory.' do |host|
46
- @options.host = host
47
- end
48
-
49
- opts.on '-s SOURCE', '--source', 'Specify an authoritative IRR / Whois source name.',
50
- 'Multiply this option for multiple SOURCE.',
51
- "eg) #{opts.program_name} -s jpirr -s radb AS-JPNIC" do |source|
52
- @options.source |= [source]
53
- end
54
-
55
- opts.on '-4', '--ipv4', 'Resolve IPv4 prefixes.' do
56
- @options.protocol |= [:ipv4]
57
- end
58
-
59
- opts.on '-6', '--ipv6', 'Resolve IPv6 prefixes.' do
60
- @options.protocol |= [:ipv6]
61
- end
62
-
63
- opts.on '-t NUMBER', '--threads', 'Number of threads to resolve prefixes per IRR / Whois server.' do |threads|
64
- @options.threads = threads
65
- end
66
-
67
- opts.on '-d', '--debug', 'Print raw queries, answers and additional informations.' do
68
- @options.debug = true
69
- end
70
- end
71
-
72
- def verify_arguments
73
- if @args.empty?
74
- $stderr.puts <<-EOS
75
- Missing Argument: objects required.
76
-
77
- Use --help for usage.
78
- EOS
79
- exit 1
80
- end
81
-
82
- unless @options.host
83
- $stderr.puts <<-EOS
84
- Missing Argument: -h option is required.
85
-
86
- Use --help for usage.
87
- EOS
88
- exit 1
89
- end
90
- end
91
-
92
- def set_default_arguments
93
- @options.protocol = [:ipv4, :ipv6] if @options.protocol.empty?
94
- end
95
-
96
- def perform
97
- client = if @options.debug
98
- Irrc::Client.new(@options.threads) {|c| c.logger = Logger.new(STDERR) }
99
- else
100
- Irrc::Client.new(@options.threads)
101
- end
102
-
103
- client.query(@options.host, @args, source: @options.source, protocol: @options.protocol)
104
- client.perform
105
- end
106
- end
107
- end
108
- end
@@ -1,13 +0,0 @@
1
- require 'yaml'
2
-
3
- module Irrc
4
- module Cli
5
- class YamlPrinter
6
- class << self
7
- def print(hash)
8
- puts hash.to_yaml
9
- end
10
- end
11
- end
12
- end
13
- end
@@ -1,193 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe 'IRR as-set resolution' do
4
- include_context 'irr queries'
5
-
6
- context 'When FQDN specified for IRR server' do
7
- subject { send_query(irr_fqdn, 'AS-JPNIC') }
8
-
9
- it 'returns ipv4 and ipv6 prefixes by default' do
10
- expect(subject['AS-JPNIC'][:ipv4]['AS2515']).to include '192.41.192.0/24', '202.12.30.0/24',
11
- '211.120.240.0/21', '211.120.248.0/24'
12
- expect(subject['AS-JPNIC'][:ipv6]['AS2515']).to include '2001:dc2::/32', '2001:0fa0::/32'
13
- end
14
- end
15
-
16
- context 'When a name specified for IRR server' do
17
- subject { send_query(irr, as_set) }
18
-
19
- it 'returns the same result as if the FQDN specified' do
20
- expect(subject).to eq send_query(irr_fqdn, as_set)
21
- end
22
- end
23
-
24
- it 'gently closes connections to IRR server' do
25
- expect_any_instance_of(Irrc::Irrd::Client).to receive(:close)
26
- send_query(irr_fqdn, as_set)
27
- end
28
-
29
- describe 'Fintering by Authoritative IRR server' do
30
- subject { send_query(:jpirr, 'AS-JPNIC', source: :apnic) }
31
-
32
- it 'returns nothing' do
33
- expect(subject).to eq({})
34
- end
35
- end
36
-
37
- context 'When as-set resolution is done but something wrong while further processes' do
38
- subject { send_query(irr, as_set) }
39
- before do
40
- allow_any_instance_of(Irrc::Irrd::Client).to receive(:resolve_prefixes_from_aut_nums){ raise }
41
- end
42
-
43
- it 'ignores a halfway result' do
44
- expect(subject).to eq({})
45
- end
46
- end
47
-
48
- context 'When only ipv4 is specified for protocol' do
49
- subject { send_query(irr, as_set, protocol: :ipv4) }
50
-
51
- it 'returns nothing about ipv6' do
52
- expect(subject[as_set][:ipv6]).to be_nil
53
- end
54
- end
55
-
56
- context 'When nil specified for protocol' do
57
- subject { send_query(irr, as_set, protocol: nil) }
58
-
59
- it 'returns nothing about the as-set' do
60
- expect(subject[as_set]).to eq({})
61
- end
62
- end
63
-
64
- context 'When blank protocol specified' do
65
- subject { send_query(irr, as_set, protocol: []) }
66
-
67
- it 'returns nothing about the as-set' do
68
- expect(subject[as_set]).to eq({})
69
- end
70
- end
71
-
72
- context 'When an invalid protocol specified' do
73
- subject { send_query(irr, as_set, protocol: :invalid) }
74
-
75
- it 'reports an ArgumentError' do
76
- expect { subject }.to raise_error ArgumentError
77
- end
78
- end
79
-
80
- context 'When a non-mirrored server specified for authoritative IRR server' do
81
- subject { send_query(irr, as_set, source: :outsider) }
82
-
83
- it 'returns nothing' do
84
- expect(subject).to eq({})
85
- end
86
- end
87
-
88
- context 'When nil specified for authoritative IRR server' do
89
- subject { send_query(irr, as_set, source: nil) }
90
-
91
- it 'returns a result without any filter of authoritative IRR server' do
92
- expect(subject).to eq send_query(irr, as_set)
93
- end
94
- end
95
-
96
- context 'When blank authoritative IRR server specified' do
97
- subject { send_query(irr, as_set, source: []) }
98
-
99
- it 'returns a result without any filter of authoritative IRR server' do
100
- expect(subject).to eq send_query(irr, as_set)
101
- end
102
- end
103
-
104
- context 'When non-existent IRR object specified' do
105
- subject { send_query(irr, 'AS-NON-EXISTENT') }
106
-
107
- it 'ignores the IRR error' do
108
- expect(subject).to eq({})
109
- end
110
- end
111
-
112
- context 'When invalid IRR server name specified' do
113
- subject { send_query(:invalid, as_set) }
114
-
115
- it 'reports an error' do
116
- expect { subject }.to raise_error
117
- end
118
- end
119
-
120
- context 'When non-resolvable IRR server fqdn specified' do
121
- subject { send_query('non-resolvable.localdomain', as_set) }
122
-
123
- it 'reports an error' do
124
- expect { subject }.to raise_error
125
- end
126
- end
127
-
128
- context 'When unreachable IRR server specified' do
129
- subject { send_query('192.0.2.1', as_set) }
130
-
131
- it 'reports an error' do
132
- expect { subject }.to raise_error
133
- end
134
- end
135
-
136
- context 'When specifing an IRR server out of service' do
137
- subject { send_query('127.0.0.1', as_set) }
138
-
139
- it 'reports an error' do
140
- expect { subject }.to raise_error
141
- end
142
- end
143
-
144
- describe 'NOTE: These may fail due to Whois database changes, not code. Check Whois database if fails.' do
145
- describe 'route-set' do
146
- subject { send_query(irr, 'RS-RC-26462') }
147
-
148
- it 'returns the same result as Whois database' do
149
- expect(subject['RS-RC-26462']).to eq(
150
- {:ipv4=>{nil=>["137.238.0.0/16"]}, :ipv6=>{nil=>[]}}
151
- )
152
- end
153
- end
154
-
155
- describe 'nested as-set' do
156
- subject { send_query(irr, 'AS-PDOXUPLINKS', source: :apnic) }
157
-
158
- it 'returns the same result as Whois database' do
159
- expect(subject['AS-PDOXUPLINKS']).to eq(
160
- {:ipv4=>
161
- {"AS703"=>[],
162
- "AS1221"=>["203.92.26.0/24", "155.143.128.0/17"],
163
- "AS2764"=>[],
164
- "AS7474"=>[],
165
- "AS7657"=>[],
166
- "AS4565"=>[],
167
- "AS5650"=>[],
168
- "AS6461"=>[]},
169
- :ipv6=>
170
- {"AS703"=>[],
171
- "AS1221"=>[],
172
- "AS2764"=>[],
173
- "AS7474"=>[],
174
- "AS7657"=>[],
175
- "AS4565"=>[],
176
- "AS5650"=>[],
177
- "AS6461"=>[]}}
178
- )
179
- end
180
- end
181
-
182
- describe 'nested route-set' do
183
- subject { send_query(irr, 'RS-RR-COUDERSPORT') }
184
-
185
- it 'returns the same result as Whois database' do
186
- expect(subject['RS-RR-COUDERSPORT']).to eq(
187
- {:ipv4=>{nil=>["107.14.160.0/20", "71.74.32.0/20", "75.180.128.0/19"]},
188
- :ipv6=>{nil=>[]}}
189
- )
190
- end
191
- end
192
- end
193
- end
@@ -1,32 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe 'IRR Invalid object resolution' do
4
- include_context 'irr queries'
5
-
6
- describe 'Try as-jpnic with JPIRR' do
7
- context 'When invalid object specified' do
8
- subject { send_query(irr, 'INVALID') }
9
-
10
- it "doesn't report an error" do
11
- expect(subject['INVALID']).to eq({})
12
- end
13
- end
14
-
15
- context 'When a blank String given for IRR object to resolve' do
16
- subject { send_query(irr, '') }
17
-
18
- it "doesn't report an error" do
19
- expect(subject['']).to eq({})
20
- end
21
- end
22
-
23
- context 'When nil given for IRR object to resolve' do
24
- subject { send_query(irr, nil) }
25
-
26
- it 'does nothing even reporting the error' do
27
- expect_any_instance_of(Irrc::Irrd::Client).not_to receive(:connect)
28
- expect(subject).to eq({})
29
- end
30
- end
31
- end
32
- end
@@ -1,193 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe 'Whois as-set resolution' do
4
- include_context 'whois queries'
5
-
6
- context 'When FQDN specified for Whois server' do
7
- subject { send_query(whois_fqdn, 'AS-JPNIC') }
8
-
9
- it 'returns ipv4 and ipv6 prefixes by default' do
10
- expect(subject['AS-JPNIC'][:ipv4]['AS2515']).to include '192.41.192.0/24', '202.12.30.0/24',
11
- '211.120.240.0/21', '211.120.248.0/24'
12
- expect(subject['AS-JPNIC'][:ipv6]['AS2515']).to include '2001:dc2::/32', '2001:0fa0::/32'
13
- end
14
- end
15
-
16
- context 'When a name specified for Whois server' do
17
- subject { send_query(whois, as_set) }
18
-
19
- it 'returns the same result as if the FQDN specified' do
20
- expect(subject).to eq send_query(whois_fqdn, as_set)
21
- end
22
- end
23
-
24
- it 'gently closes connections to Whois server' do
25
- expect_any_instance_of(Irrc::Whoisd::Client).to receive(:close)
26
- send_query(whois_fqdn, as_set)
27
- end
28
-
29
- describe 'Fintering by Authoritative Whois server' do
30
- subject { send_query(:jpirr, 'AS-JPNIC', source: :apnic) }
31
-
32
- it 'returns nothing' do
33
- expect(subject).to eq({})
34
- end
35
- end
36
-
37
- context 'When as-set resolution is done but something wrong while further processes' do
38
- subject { send_query(whois, as_set) }
39
- before do
40
- allow_any_instance_of(Irrc::Whoisd::Client).to receive(:resolve_prefixes_from_aut_nums){ raise }
41
- end
42
-
43
- it 'ignores a halfway result' do
44
- expect(subject).to eq({})
45
- end
46
- end
47
-
48
- context 'When only ipv4 is specified for protocol' do
49
- subject { send_query(whois, as_set, protocol: :ipv4) }
50
-
51
- it 'returns nothing about ipv6' do
52
- expect(subject[as_set][:ipv6]).to be_nil
53
- end
54
- end
55
-
56
- context 'When nil specified for protocol' do
57
- subject { send_query(whois, as_set, protocol: nil) }
58
-
59
- it 'returns nothing about the as-set' do
60
- expect(subject[as_set]).to eq({})
61
- end
62
- end
63
-
64
- context 'When blank protocol specified' do
65
- subject { send_query(whois, as_set, protocol: []) }
66
-
67
- it 'returns nothing about the as-set' do
68
- expect(subject[as_set]).to eq({})
69
- end
70
- end
71
-
72
- context 'When an invalid protocol specified' do
73
- subject { send_query(whois, as_set, protocol: :invalid) }
74
-
75
- it 'reports an ArgumentError' do
76
- expect { subject }.to raise_error ArgumentError
77
- end
78
- end
79
-
80
- context 'When a non-mirrored server specified for authoritative Whois server' do
81
- subject { send_query(whois, as_set, source: :outsider) }
82
-
83
- it 'returns nothing' do
84
- expect(subject).to eq({})
85
- end
86
- end
87
-
88
- context 'When nil specified for authoritative Whois server' do
89
- subject { send_query(whois, as_set, source: nil) }
90
-
91
- it 'returns a result without any filter of authoritative Whois server' do
92
- expect(subject).to eq send_query(whois, as_set)
93
- end
94
- end
95
-
96
- context 'When blank authoritative Whois server specified' do
97
- subject { send_query(whois, as_set, source: []) }
98
-
99
- it 'returns a result without any filter of authoritative Whois server' do
100
- expect(subject).to eq send_query(whois, as_set)
101
- end
102
- end
103
-
104
- context 'When non-existent Whois object specified' do
105
- subject { send_query(whois, 'AS-NON-EXISTENT') }
106
-
107
- it 'ignores the Whois error' do
108
- expect(subject).to eq({})
109
- end
110
- end
111
-
112
- context 'When invalid Whois server name specified' do
113
- subject { send_query(:invalid, as_set) }
114
-
115
- it 'reports an error' do
116
- expect { subject }.to raise_error
117
- end
118
- end
119
-
120
- context 'When non-resolvable Whois server fqdn specified' do
121
- subject { send_query('non-resolvable.localdomain', as_set) }
122
-
123
- it 'reports an error' do
124
- expect { subject }.to raise_error
125
- end
126
- end
127
-
128
- context 'When unreachable Whois server specified' do
129
- subject { send_query('192.0.2.1', as_set) }
130
-
131
- it 'reports an error' do
132
- expect { subject }.to raise_error
133
- end
134
- end
135
-
136
- context 'When specifing an Whois server out of service' do
137
- subject { send_query('127.0.0.1', as_set) }
138
-
139
- it 'reports an error' do
140
- expect { subject }.to raise_error
141
- end
142
- end
143
-
144
- describe 'NOTE: These may fail due to Whois database changes, not code. Check Whois database if fails.' do
145
- describe 'route-set' do
146
- subject { send_query(whois, 'RS-RC-26462') }
147
-
148
- it 'returns the same result as Whois database' do
149
- expect(subject['RS-RC-26462']).to eq(
150
- {:ipv4=>{nil=>["137.238.0.0/16"]}, :ipv6=>{nil=>["2620:0:5080::/48"]}}
151
- )
152
- end
153
- end
154
-
155
- describe 'nested as-set' do
156
- subject { send_query(whois, 'AS-PDOXUPLINKS', source: :apnic) }
157
-
158
- it 'returns the same result as Whois database' do
159
- expect(subject['AS-PDOXUPLINKS']).to eq(
160
- {:ipv4=>
161
- {"AS703"=>[],
162
- "AS1221"=>["155.143.128.0/17", "203.92.26.0/24"],
163
- "AS2764"=>[],
164
- "AS7474"=>[],
165
- "AS7657"=>[],
166
- "AS4565"=>[],
167
- "AS5650"=>[],
168
- "AS6461"=>[]},
169
- :ipv6=>
170
- {"AS703"=>[],
171
- "AS1221"=>[],
172
- "AS2764"=>[],
173
- "AS7474"=>[],
174
- "AS7657"=>[],
175
- "AS4565"=>[],
176
- "AS5650"=>[],
177
- "AS6461"=>[]}}
178
- )
179
- end
180
- end
181
-
182
- describe 'nested route-set' do
183
- subject { send_query(whois, 'RS-RR-COUDERSPORT') }
184
-
185
- it 'returns the same result as Whois database' do
186
- expect(subject['RS-RR-COUDERSPORT']).to eq(
187
- {:ipv4=>{nil=>["71.74.32.0/20", "75.180.128.0/19", "107.14.160.0/20"]},
188
- :ipv6=>{nil=>[]}}
189
- )
190
- end
191
- end
192
- end
193
- end