From e9ed76f170a92b86a3eb5ba0a95fca02189f3259 Mon Sep 17 00:00:00 2001 From: Lopaka Delp Date: Mon, 27 Oct 2014 20:29:45 -0700 Subject: [PATCH 1/3] Determine if an IP address is within a private block before setting server:public_ip_* or server:private_ip_* machine tag. --- README.md | 2 +- libraries/rightscale_tag_helper.rb | 28 ++++++++++++++++++++++++++++ recipes/default.rb | 4 ++-- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ab7c3b1..183af24 100644 --- a/README.md +++ b/README.md @@ -387,7 +387,7 @@ type. To use them in a recipe add the following: ```ruby class Chef::Recipe - include RightScale::RightScaleTag + include Rightscale::RightscaleTag end ``` diff --git a/libraries/rightscale_tag_helper.rb b/libraries/rightscale_tag_helper.rb index 68e50be..3f329cd 100644 --- a/libraries/rightscale_tag_helper.rb +++ b/libraries/rightscale_tag_helper.rb @@ -453,6 +453,34 @@ def group_servers_by_application_name(servers) Rightscale::RightscaleTag.group_servers_by_application_name(servers) end + # Determines if an IP address is a public IPv4 address based on RFC 1918. + # + # @param ipaddress [String] the IPv4 address to check + # + # @return [Boolean] whether the IP is a public IPv4 address or not + # + def self.is_public_ip?(ipaddress) + require 'ipaddr' + + ['10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16'].each do |private_block| + IPAddr.new(private_block) + return false if IPAddr.new(private_block).include?(ipaddress) + end + true + end + + # Determines if an IP address is a public IPv4 address based on RFC 1918. + # + # @param ipaddress [String] the IPv4 address to check + # + # @return [Boolean] whether the IP is a public IPv4 address or not + # + # @see .is_public_ip? + # + def is_public_ip?(ipaddress) + Rightscale::RightscaleTag.is_public_ip?(ipaddress) + end + private # Adds required tags to the options for Chef::MachineTagHelper#tag_search that are needed for the various diff --git a/recipes/default.rb b/recipes/default.rb index ae3bbc6..e32ab3a 100644 --- a/recipes/default.rb +++ b/recipes/default.rb @@ -29,13 +29,13 @@ if node['cloud'] if node['cloud']['public_ips'] - node['cloud']['public_ips'].reject { |ip| ip.nil? || ip.empty? }.each_with_index do |public_ip, index| + node['cloud']['public_ips'].reject { |ip| ip.nil? || ip.empty? || !Rightscale::RightscaleTag.is_public_ip?(ip) }.each_with_index do |public_ip, index| machine_tag "server:public_ip_#{index}=#{public_ip}" end end if node['cloud']['private_ips'] - node['cloud']['private_ips'].reject { |ip| ip.nil? || ip.empty? }.each_with_index do |private_ip, index| + node['cloud']['private_ips'].reject { |ip| ip.nil? || ip.empty? || Rightscale::RightscaleTag.is_public_ip?(ip) }.each_with_index do |private_ip, index| machine_tag "server:private_ip_#{index}=#{private_ip}" end end From 6d669aa216ae2bfb98b099266f10f53321d0c35d Mon Sep 17 00:00:00 2001 From: Lopaka Delp Date: Mon, 27 Oct 2014 20:35:14 -0700 Subject: [PATCH 2/3] Bump version to 1.0.4. --- CHANGELOG.md | 5 +++++ metadata.rb | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ed7e027..df18409 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,11 @@ rightscale_tag Cookbook CHANGELOG This file is used to list changes made in each version of the rightscale_tag cookbook. +v1.0.4 +------ + +- Check IP addresses if they are private IPs before setting server:public_ip_# and server:private_ip_#. + v1.0.3 ------ diff --git a/metadata.rb b/metadata.rb index 8b41f93..58d3c35 100644 --- a/metadata.rb +++ b/metadata.rb @@ -4,7 +4,7 @@ license 'Apache 2.0' description 'Provides LWRPs and helper methods for building 3-tier applications using machine tags in RightScale' long_description IO.read(File.join(File.dirname(__FILE__), 'README.md')) -version '1.0.3' +version '1.0.4' depends 'machine_tag', '~> 1.0.3' depends 'marker', '~> 1.0.0' From 6b60e1f24ed2396bd750a06255f1ca27e27ecbc5 Mon Sep 17 00:00:00 2001 From: Lopaka Delp Date: Tue, 28 Oct 2014 13:49:33 -0700 Subject: [PATCH 3/3] Use ipaddress gem to determine if IP address is private. --- libraries/rightscale_tag_helper.rb | 28 ---------------------------- recipes/default.rb | 4 ++-- 2 files changed, 2 insertions(+), 30 deletions(-) diff --git a/libraries/rightscale_tag_helper.rb b/libraries/rightscale_tag_helper.rb index 3f329cd..68e50be 100644 --- a/libraries/rightscale_tag_helper.rb +++ b/libraries/rightscale_tag_helper.rb @@ -453,34 +453,6 @@ def group_servers_by_application_name(servers) Rightscale::RightscaleTag.group_servers_by_application_name(servers) end - # Determines if an IP address is a public IPv4 address based on RFC 1918. - # - # @param ipaddress [String] the IPv4 address to check - # - # @return [Boolean] whether the IP is a public IPv4 address or not - # - def self.is_public_ip?(ipaddress) - require 'ipaddr' - - ['10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16'].each do |private_block| - IPAddr.new(private_block) - return false if IPAddr.new(private_block).include?(ipaddress) - end - true - end - - # Determines if an IP address is a public IPv4 address based on RFC 1918. - # - # @param ipaddress [String] the IPv4 address to check - # - # @return [Boolean] whether the IP is a public IPv4 address or not - # - # @see .is_public_ip? - # - def is_public_ip?(ipaddress) - Rightscale::RightscaleTag.is_public_ip?(ipaddress) - end - private # Adds required tags to the options for Chef::MachineTagHelper#tag_search that are needed for the various diff --git a/recipes/default.rb b/recipes/default.rb index e32ab3a..3544f5a 100644 --- a/recipes/default.rb +++ b/recipes/default.rb @@ -29,13 +29,13 @@ if node['cloud'] if node['cloud']['public_ips'] - node['cloud']['public_ips'].reject { |ip| ip.nil? || ip.empty? || !Rightscale::RightscaleTag.is_public_ip?(ip) }.each_with_index do |public_ip, index| + node['cloud']['public_ips'].reject { |ip| ip.nil? || ip.empty? || IPAddress(ip).private? }.each_with_index do |public_ip, index| machine_tag "server:public_ip_#{index}=#{public_ip}" end end if node['cloud']['private_ips'] - node['cloud']['private_ips'].reject { |ip| ip.nil? || ip.empty? || Rightscale::RightscaleTag.is_public_ip?(ip) }.each_with_index do |private_ip, index| + node['cloud']['private_ips'].reject { |ip| ip.nil? || ip.empty? || !IPAddress(ip).private? }.each_with_index do |private_ip, index| machine_tag "server:private_ip_#{index}=#{private_ip}" end end