Skip to content
This repository was archived by the owner on Feb 11, 2025. It is now read-only.

Commit 88b538e

Browse files
author
andy
committed
feature with config to automatically get IANA bootstrap files
1 parent 0dff771 commit 88b538e

File tree

4 files changed

+118
-3
lines changed

4 files changed

+118
-3
lines changed

lib/nicinfo/config.rb

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,14 @@ def copy_bsfiles
121121
end
122122

123123
def set_bsfiles_last_update_time t = Time.now
124-
f = File.open(File.join( @app_data, NicInfo::BSFILE_LAST_CHECK_FILENAME ), "w" )
124+
f = File.open(bsfiles_last_update_filename, "w" )
125125
f.write t.strftime( "%Y-%m-%d %H:%M:%S")
126126
f.close
127127
end
128128

129129
def get_bsfiles_last_update_time
130130
retval = nil
131-
fname = File.join( @app_data, NicInfo::BSFILE_LAST_CHECK_FILENAME )
131+
fname = bsfiles_last_update_filename
132132
if File.exists?( fname )
133133
f = File.open( fname, "r" )
134134
data = f.read
@@ -138,6 +138,29 @@ def get_bsfiles_last_update_time
138138
return retval
139139
end
140140

141+
def bsfiles_last_update_filename
142+
File.join(@rdap_bootstrap_dir, NicInfo::BSFILE_LAST_CHECK_FILENAME)
143+
end
144+
145+
def check_bsfiles_age?
146+
retval = false
147+
if @config[ NicInfo::BOOTSTRAP ][ NicInfo::CHECK_BSFILES_AGE ]
148+
t = get_bsfiles_last_update_time
149+
if t != nil
150+
configed_age = @config[ NicInfo::BOOTSTRAP ][ NicInfo::BSFILES_AGE ]
151+
age = t + configed_age
152+
retval = true if age < Time.now
153+
else
154+
retval = true
155+
end
156+
end
157+
return retval
158+
end
159+
160+
def update_bsfiles? aged
161+
return aged && @config[ NicInfo::BOOTSTRAP ][ NicInfo::UPDATE_BSFILES ]
162+
end
163+
141164
def save name, data
142165
data_file = File.open( File.join( @app_data, name ), "w" )
143166
data_file.write data
@@ -294,6 +317,20 @@ def self.formulate_config_file_name data_dir
294317
295318
ns_root_url: https://rdap.arin.net/registry
296319
320+
# Check for updates of the IANA RDAP bootstrap files
321+
# If true, check for IANA bootstrap files once they have aged past bsfiles_age
322+
# Values are true or false
323+
check_bsfiles_age: true
324+
325+
# The age at which IANA RDAP bootstrap files are to be checked if check_bsfiles is true
326+
# This value is in seconds
327+
bsfiles_age: 604800
328+
329+
# If the age of the IANA RDAP bootstrap files are considered old according to bsfiles_age
330+
# and check_bsfiles is true, then this value will cause a fetch of new files if true.
331+
# Values are true or false
332+
update_bsfiles: true
333+
297334
search:
298335
299336
# Substring matching
@@ -307,7 +344,7 @@ def self.formulate_config_file_name data_dir
307344
308345
config:
309346
# This should not be altered.
310-
version: 3
347+
version: 4
311348
312349
YAML_CONFIG
313350

lib/nicinfo/constants.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ module NicInfo
7575
IP_ROOT_URL = "ip_root_url"
7676
AS_ROOT_URL = "as_root_url"
7777
DOMAIN_ROOT_URL = "domain_root_url"
78+
CHECK_BSFILES_AGE = "check_bsfiles_age"
79+
BSFILES_AGE = "bsfiles_age"
80+
UPDATE_BSFILES = "update_bsfiles"
7881
SEARCH = "search"
7982
SUBSTRING = "substring"
8083
CONFIG = "config"

lib/nicinfo/nicinfo_main.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,15 @@ def run
391391

392392
if @config.options.get_iana_files
393393
get_iana_files
394+
else
395+
check_bsfiles_age = @config.check_bsfiles_age?
396+
update_bsfiles = @config.update_bsfiles?( check_bsfiles_age )
397+
if update_bsfiles
398+
@config.logger.mesg( "IANA RDAP bootstrap files are old and need to be updated." )
399+
get_iana_files
400+
elsif check_bsfiles_age
401+
@config.logger.mesg( "IANA RDAP bootstrap files are old. Update them with --iana option" )
402+
end
394403
end
395404

396405
if @config.options.demo
@@ -508,6 +517,7 @@ def get_iana_files
508517
get_file_via_http("http://data.iana.org/rdap/ipv4.json", File.join(@config.rdap_bootstrap_dir, "ipv4.json"), 0)
509518
get_file_via_http("http://data.iana.org/rdap/ipv6.json", File.join(@config.rdap_bootstrap_dir, "ipv6.json"), 0)
510519
get_file_via_http("http://data.iana.org/rdap/dns.json", File.join(@config.rdap_bootstrap_dir, "dns.json"), 0)
520+
@config.set_bsfiles_last_update_time
511521
end
512522

513523
def do_rdap_query

spec/config_spec.rb

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,74 @@
114114
c.logger.message_level = "NONE"
115115
c.setup_workspace
116116

117+
expect( File.exists?( c.bsfiles_last_update_filename ) ).to be_falsey
117118
t2 = c.get_bsfiles_last_update_time
118119
expect( t2 ).to be_nil
119120

120121
end
121122

123+
it 'should update bsfiles based on aged and standard config' do
124+
dir = File.join( @work_dir, "test_update_bsfiles_based_on_age_standard" )
125+
c = NicInfo::Config.new( dir )
126+
c.logger.message_level = "NONE"
127+
c.setup_workspace
128+
129+
expect( c.update_bsfiles?( true ) ).to be_truthy
130+
expect( c.update_bsfiles?( false ) ).to be_falsey
131+
end
132+
133+
it 'should update bsfiles based on aged and no update config' do
134+
dir = File.join( @work_dir, "test_update_bsfiles_based_on_age_no_update" )
135+
c = NicInfo::Config.new( dir )
136+
c.logger.message_level = "NONE"
137+
c.setup_workspace
138+
139+
c.config[ NicInfo::BOOTSTRAP ][ NicInfo::UPDATE_BSFILES ] = false
140+
expect( c.update_bsfiles?( true ) ).to be_falsey
141+
expect( c.update_bsfiles?( false ) ).to be_falsey
142+
end
143+
144+
it 'should update bsfiles on new install' do
145+
dir = File.join( @work_dir, "test_update_bsfiles_on_new_install" )
146+
147+
c = NicInfo::Config.new( dir )
148+
c.logger.message_level = "NONE"
149+
c.setup_workspace
150+
151+
expect( c.update_bsfiles?( c.check_bsfiles_age? ) ).to be_truthy
152+
153+
end
154+
155+
it 'should not update bsfiles if not aged' do
156+
dir = File.join( @work_dir, "test_not_update_bsfiles_not_aged" )
157+
158+
c = NicInfo::Config.new( dir )
159+
c.logger.message_level = "NONE"
160+
c.setup_workspace
161+
162+
t1 = Time.now.round - 1
163+
c.set_bsfiles_last_update_time t1
164+
c.config[ NicInfo::BOOTSTRAP ][ NicInfo::BSFILES_AGE ] = 500
165+
166+
check_bsfiles_age = c.check_bsfiles_age?
167+
expect( check_bsfiles_age ).to be_falsey
168+
expect( c.update_bsfiles?( check_bsfiles_age ) ).to be_falsey
169+
end
170+
171+
it 'should update bsfiles if aged' do
172+
dir = File.join( @work_dir, "test_update_bsfiles_fi_aged" )
173+
174+
c = NicInfo::Config.new( dir )
175+
c.logger.message_level = "NONE"
176+
c.setup_workspace
177+
178+
t1 = Time.now.round - 500
179+
c.set_bsfiles_last_update_time t1
180+
c.config[ NicInfo::BOOTSTRAP ][ NicInfo::BSFILES_AGE ] = 1
181+
182+
check_bsfiles_age = c.check_bsfiles_age?
183+
expect( check_bsfiles_age ).to be_truthy
184+
expect( c.update_bsfiles?( c.check_bsfiles_age? ) ).to be_truthy
185+
end
186+
122187
end

0 commit comments

Comments
 (0)