Class: IPAddr

Inherits:
Object
  • Object
show all
Defined in:
lib/azure/storage/core/utility.rb

Overview

Code validate private/public IP acceptable ranges.

Constant Summary collapse

PRIVATE_RANGES =
[
  IPAddr.new('10.0.0.0/8'),
  IPAddr.new('172.16.0.0/12'),
  IPAddr.new('192.168.0.0/16')
]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.address_prefix(ip, cidr) ⇒ Object


235
236
237
# File 'lib/azure/storage/core/utility.rb', line 235

def address_prefix(ip, cidr)
  ip + '/' + cidr.to_s
end

.valid?(ip) ⇒ Boolean


239
240
241
# File 'lib/azure/storage/core/utility.rb', line 239

def valid?(ip)
  (IPAddr.new(ip) rescue nil).nil?
end

.validate_address_space(ip) ⇒ Object


227
228
229
230
231
232
233
# File 'lib/azure/storage/core/utility.rb', line 227

def validate_address_space(ip)
  if ip.split('/').size != 2
    raise "Cidr is invalid for IP #{ip}."
  elsif valid?(ip)
    raise "Address space '#{ip}' is invalid."
  end
end

.validate_ip_and_prefix(ip, cidr) ⇒ Object


217
218
219
220
221
222
223
224
225
# File 'lib/azure/storage/core/utility.rb', line 217

def validate_ip_and_prefix(ip, cidr)
  if cidr.to_s.empty?
    raise "Cidr is missing for IP '#{ip}'."
  elsif valid?(ip)
    raise "Ip address '#{ip}' is invalid."
  elsif !IPAddr.new(ip).private?
    raise "Ip Address #{ip} must be private."
  end
end

Instance Method Details

#private?Boolean


204
205
206
207
208
209
210
# File 'lib/azure/storage/core/utility.rb', line 204

def private?
  return false unless self.ipv4?
  PRIVATE_RANGES.each do |ipr|
    return true if ipr.include?(self)
  end
  false
end

#public?Boolean


212
213
214
# File 'lib/azure/storage/core/utility.rb', line 212

def public?
  !private?
end