Skip to content

Commit 2bf50a2

Browse files
committed
Academic Benchmarks CLI: add ability to remove IPs by note
Referencing the IP addresses by note is super convenient when managing this interface. Fixes CNVS-23385 Test Plan: * Be careful when testing. You are affecting the same * system that is used in production From a rails console: 1. Whitelist an IP address with a note of your choice. Make sure you invent a new note and don't reuse one that already exists. AcademicBenchmarks::CliTools.whitelist_ip("3.3.3.3", "cool-note") 2. Now remove the IP address from the whitelist with: AcademicBenchmarks::CliTools.remove_from_whitelist("<note-name") Change-Id: I7cb1c1aac4d527fe2608c33bbc73b006abd6df69 Reviewed-on: https://gerrit.instructure.com/63703 Tested-by: Jenkins Reviewed-by: Ryan Taylor <rtaylor@instructure.com> QA-Review: Michael Hargiss <mhargiss@instructure.com> Product-Review: Benjamin Porter <bporter@instructure.com>
1 parent 012e499 commit 2bf50a2

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

  • gems/plugins/academic_benchmark/lib/academic_benchmark

gems/plugins/academic_benchmark/lib/academic_benchmark/cli_tools.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,32 @@ def self.whitelist_ip(ip, note)
1515
end
1616

1717
def self.remove_from_whitelist(ip)
18+
if self.is_ip_address(ip)
19+
self.remove_ip_from_whitelist(ip)
20+
else
21+
self.remove_note_from_whitelist(ip)
22+
end
23+
end
24+
25+
def self.remove_ip_from_whitelist(ip)
1826
HTTParty.get(
1927
"#{api_url}maintainAccess?api_key=#{api_key}&op=remove&addr=#{ip}"
2028
)
2129
end
2230

31+
def self.remove_note_from_whitelist(note)
32+
ips = self.whitelisted_ips
33+
34+
if ips["ab_rsp"] && ips["ab_rsp"]["access"]
35+
ips["ab_rsp"]["access"].each do |entry|
36+
return remove_ip_from_whitelist(entry["addr"]) if entry["note"] == note
37+
end
38+
puts "There were no whitelisted IP addresses with a note matching '#{note}'"
39+
else
40+
puts "Error retrieving list of whitelisted IP addresses: #{ips.to_json}"
41+
end
42+
end
43+
2344
def self.whitelisted?(ip)
2445
ips = whitelisted_ips
2546
ips["ab_rsp"] && ips["ab_rsp"]["access"].any?{ |i| i["addr"] == ip }
@@ -103,6 +124,12 @@ def self.api_url
103124
AcademicBenchmark.config["api_url"]
104125
end
105126

127+
private
128+
def self.is_ip_address(ip)
129+
# this simple and brief regex matches IP addresses strictly
130+
ip =~ %r{\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b}
131+
end
132+
106133
end # class CliTools
107134

108135
end # module AcademicBenchmark

0 commit comments

Comments
 (0)