-
-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
56 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,67 @@ | ||
require "../../network" | ||
require "ipaddress" | ||
require "crest" | ||
|
||
class Configuration::Settings::Networks | ||
getter errors : Array(String) | ||
getter networks : Array(String) | ||
getter network_type : String | ||
|
||
def initialize(@errors, @networks) | ||
def initialize(@errors, @networks, @network_type) | ||
end | ||
|
||
def validate(network_type : String) | ||
def validate | ||
if networks | ||
if networks.empty? | ||
errors << "#{network_type} allowed networks are required" | ||
else | ||
networks.each do |network| | ||
Network.new(network, network_type).validate.each do |error| | ||
errors << error | ||
end | ||
end | ||
validate_networks | ||
validate_current_ip_must_be_included_in_at_least_one_network | ||
end | ||
else | ||
errors << "#{network_type} allowed networks are required" | ||
end | ||
end | ||
|
||
private def validate_networks | ||
networks.each do |cidr| | ||
begin | ||
IPAddress.new(cidr).network? | ||
rescue ArgumentError | ||
errors << "#{network_type} allowed network #{cidr} is not a valid network in CIDR notation" | ||
end | ||
end | ||
end | ||
|
||
private def validate_current_ip_must_be_included_in_at_least_one_network | ||
current_ip = IPAddress.new("127.0.0.1") | ||
|
||
begin | ||
current_ip = IPAddress.new(Crest.get("http://whatismyip.akamai.com").body) | ||
rescue ex : Crest::RequestFailed | ||
errors << "Unable to determine your current IP (necessary to validate allowed networks for SSH and API)" | ||
return | ||
end | ||
|
||
included = false | ||
|
||
networks.each do |cidr| | ||
begin | ||
network = IPAddress.new(cidr).network | ||
|
||
if network.includes? current_ip | ||
included = true | ||
end | ||
rescue ex: ArgumentError | ||
if ex.message =~ /Invalid netmask/ | ||
errors << "#{network_type} allowed network #{cidr} has an invalid netmark" | ||
else | ||
errors << "#{network_type} allowed network #{cidr} is not a valid network in CIDR notation" | ||
end | ||
end | ||
end | ||
|
||
unless included | ||
errors << "Your current IP #{current_ip} must belong to at least one of the #{network_type} allowed networks" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.