Skip to content

Commit

Permalink
Refactor IP validation logic in AllowedNetworks class
Browse files Browse the repository at this point in the history
  • Loading branch information
vitobotta committed Feb 6, 2025
1 parent 3e0770a commit cbcaff6
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions src/configuration/networking_components/allowed_networks.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,15 @@ class Configuration::NetworkingComponents::AllowedNetworks
validate_networks(errors, api, "API")
end

private def current_ip
@current_ip ||= begin
Crest.get("https://ipinfo.io/ip").body
rescue Crest::RequestFailed
private def validate_current_ip_must_be_included_in_at_least_one_network(errors, networks, network_type)
current_ip = IPAddress.new("127.0.0.1")

begin
current_ip = IPAddress.new(Crest.get("https://ipinfo.io/ip").body)
rescue ex : Crest::RequestFailed
errors << "Unable to determine your current IP (necessary to validate allowed networks for SSH and API)"
nil
return
end
end

private def validate_current_ip_must_be_included_in_at_least_one_network(errors, networks, network_type)
return if current_ip.nil?

included = false

Expand All @@ -40,7 +38,7 @@ class Configuration::NetworkingComponents::AllowedNetworks
begin
network = IPAddress.new(cidr).network

included = true = network.includes?(current_ip)
included = network.includes?(current_ip)
rescue ex: ArgumentError
if ex.message =~ /Invalid netmask/
errors << "#{network_type} allowed network #{cidr} has an invalid netmask"
Expand Down

0 comments on commit cbcaff6

Please sign in to comment.