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>
This commit is contained in:
Benjamin Porter 2015-09-21 14:03:47 -06:00
parent 012e499bbe
commit 2bf50a2368
1 changed files with 27 additions and 0 deletions

View File

@ -15,11 +15,32 @@ class CliTools
end
def self.remove_from_whitelist(ip)
if self.is_ip_address(ip)
self.remove_ip_from_whitelist(ip)
else
self.remove_note_from_whitelist(ip)
end
end
def self.remove_ip_from_whitelist(ip)
HTTParty.get(
"#{api_url}maintainAccess?api_key=#{api_key}&op=remove&addr=#{ip}"
)
end
def self.remove_note_from_whitelist(note)
ips = self.whitelisted_ips
if ips["ab_rsp"] && ips["ab_rsp"]["access"]
ips["ab_rsp"]["access"].each do |entry|
return remove_ip_from_whitelist(entry["addr"]) if entry["note"] == note
end
puts "There were no whitelisted IP addresses with a note matching '#{note}'"
else
puts "Error retrieving list of whitelisted IP addresses: #{ips.to_json}"
end
end
def self.whitelisted?(ip)
ips = whitelisted_ips
ips["ab_rsp"] && ips["ab_rsp"]["access"].any?{ |i| i["addr"] == ip }
@ -103,6 +124,12 @@ class CliTools
AcademicBenchmark.config["api_url"]
end
private
def self.is_ip_address(ip)
# this simple and brief regex matches IP addresses strictly
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}
end
end # class CliTools
end # module AcademicBenchmark