Skip to content

Commit

Permalink
Ignore services if they're not available in that region
Browse files Browse the repository at this point in the history
  • Loading branch information
arkadiyt committed Oct 27, 2018
1 parent ee0fab1 commit d1fdc85
Show file tree
Hide file tree
Showing 11 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Expand Up @@ -25,5 +25,8 @@ Metrics/LineLength:
Metrics/MethodLength:
Enabled: false

Naming/PredicateName:
Enabled: false

Style/Documentation:
Enabled: false
1 change: 1 addition & 0 deletions lib/aws_public_ips/checks/apigateway.rb
Expand Up @@ -8,6 +8,7 @@ module Checks
module Apigateway
def self.run
client = ::Aws::APIGateway::Client.new
return [] unless ::AwsPublicIps::Utils.has_service?(client)

# TODO(arkadiy) https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-private-integration.html

Expand Down
1 change: 1 addition & 0 deletions lib/aws_public_ips/checks/cloudfront.rb
Expand Up @@ -8,6 +8,7 @@ module Checks
module Cloudfront
def self.run
client = ::Aws::CloudFront::Client.new
return [] unless ::AwsPublicIps::Utils.has_service?(client)

# Cloudfront distrubtions are always public, they don't have a concept of VPC
# No "coming up" problem here like with RDS/Redshift
Expand Down
2 changes: 2 additions & 0 deletions lib/aws_public_ips/checks/ec2.rb
@@ -1,12 +1,14 @@
# frozen_string_literal: true

require 'aws-sdk-ec2'
require 'aws_public_ips/utils'

module AwsPublicIps
module Checks
module Ec2
def self.run
client = ::Aws::EC2::Client.new
return [] unless ::AwsPublicIps::Utils.has_service?(client)

# Iterate over all EC2 instances. This will include those from EC2, ECS, EKS, Fargate, Batch,
# Beanstalk, and NAT Instances
Expand Down
1 change: 1 addition & 0 deletions lib/aws_public_ips/checks/elasticsearch.rb
Expand Up @@ -8,6 +8,7 @@ module Checks
module Elasticsearch
def self.run
client = ::Aws::ElasticsearchService::Client.new
return [] unless ::AwsPublicIps::Utils.has_service?(client)

# ElasticSearch instances can be launched into classic into VPCs. Classic instances are public and have a
# `domain_status.endpoint` hostname, and VPC instances have a `domain_status.endpoints['vpc']` hostname.
Expand Down
1 change: 1 addition & 0 deletions lib/aws_public_ips/checks/elb.rb
Expand Up @@ -8,6 +8,7 @@ module Checks
module Elb
def self.run
client = ::Aws::ElasticLoadBalancing::Client.new
return [] unless ::AwsPublicIps::Utils.has_service?(client)

# EC2-Classic load balancers are only returned by the 'elasticloadbalancing' API, and
# EC2-VPC ALBs/NLBs are only returned by the 'elasticloadbalancingv2' API
Expand Down
1 change: 1 addition & 0 deletions lib/aws_public_ips/checks/elbv2.rb
Expand Up @@ -8,6 +8,7 @@ module Checks
module Elbv2
def self.run
client = ::Aws::ElasticLoadBalancingV2::Client.new
return [] unless ::AwsPublicIps::Utils.has_service?(client)

# EC2-Classic load balancers are only returned by the 'elasticloadbalancing' API, and
# EC2-VPC ALBs/NLBs are only returned by the 'elasticloadbalancingv2' API
Expand Down
1 change: 1 addition & 0 deletions lib/aws_public_ips/checks/lightsail.rb
Expand Up @@ -8,6 +8,7 @@ module Checks
module Lightsail
def self.run
client = ::Aws::Lightsail::Client.new
return [] unless ::AwsPublicIps::Utils.has_service?(client)

# Lightsail instances are always exposed directly, and can also be put behind a load balancer

Expand Down
1 change: 1 addition & 0 deletions lib/aws_public_ips/checks/rds.rb
Expand Up @@ -8,6 +8,7 @@ module Checks
module Rds
def self.run
client = ::Aws::RDS::Client.new
return [] unless ::AwsPublicIps::Utils.has_service?(client)

# RDS instances can be launched into VPCs or into Classic mode.
# In classic mode they are always public.
Expand Down
1 change: 1 addition & 0 deletions lib/aws_public_ips/checks/redshift.rb
Expand Up @@ -8,6 +8,7 @@ module Checks
module Redshift
def self.run
client = ::Aws::Redshift::Client.new
return [] unless ::AwsPublicIps::Utils.has_service?(client)

# Redshift clusters can be launched into VPCs or into Classic mode.
# In classic mode they are always public.
Expand Down
11 changes: 11 additions & 0 deletions lib/aws_public_ips/utils.rb
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'resolv'
require 'aws-partitions'

module AwsPublicIps
module Utils
Expand All @@ -19,5 +20,15 @@ def self.resolve_hostname(hostname)
resource.address.to_s.downcase
end
end

def self.has_service?(client)
region_partition = ::Aws::Partitions.partitions.find do |partition|
partition.regions.map(&:name).include?(client.config.region)
end
service_name = client.class.to_s.split('::')[-2]

aws_service = region_partition.services.find { |service| service.name == service_name }
!aws_service.regionalized? || aws_service.regions.include?(client.config.region)
end
end
end

0 comments on commit d1fdc85

Please sign in to comment.