terrafying-components 1.4.3 → 1.4.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
 - data/lib/hash/merge_with_arrays.rb +7 -0
 - data/lib/terrafying/components.rb +10 -0
 - data/lib/terrafying/components/auditd.rb +158 -0
 - data/lib/terrafying/components/ca.rb +55 -0
 - data/lib/terrafying/components/dynamicset.rb +229 -0
 - data/lib/terrafying/components/endpoint.rb +96 -0
 - data/lib/terrafying/components/endpointservice.rb +63 -0
 - data/lib/terrafying/components/ignition.rb +117 -0
 - data/lib/terrafying/components/instance.rb +112 -0
 - data/lib/terrafying/components/instanceprofile.rb +81 -0
 - data/lib/terrafying/components/letsencrypt.rb +146 -0
 - data/lib/terrafying/components/loadbalancer.rb +159 -0
 - data/lib/terrafying/components/ports.rb +35 -0
 - data/lib/terrafying/components/selfsignedca.rb +171 -0
 - data/lib/terrafying/components/service.rb +148 -0
 - data/lib/terrafying/components/staticset.rb +153 -0
 - data/lib/terrafying/components/subnet.rb +105 -0
 - data/lib/terrafying/components/support/deregister-vpn +48 -0
 - data/lib/terrafying/components/support/register-vpn +46 -0
 - data/lib/terrafying/components/templates/ignition.yaml +115 -0
 - data/lib/terrafying/components/usable.rb +129 -0
 - data/lib/terrafying/components/version.rb +5 -0
 - data/lib/terrafying/components/vpc.rb +417 -0
 - data/lib/terrafying/components/vpn.rb +358 -0
 - data/lib/terrafying/components/zone.rb +144 -0
 - metadata +28 -31
 
| 
         @@ -0,0 +1,146 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
             
     | 
| 
      
 2 
     | 
    
         
            +
            require 'terrafying/components/ca'
         
     | 
| 
      
 3 
     | 
    
         
            +
            require 'terrafying/generator'
         
     | 
| 
      
 4 
     | 
    
         
            +
            require 'open-uri'
         
     | 
| 
      
 5 
     | 
    
         
            +
            module Terrafying
         
     | 
| 
      
 6 
     | 
    
         
            +
             
     | 
| 
      
 7 
     | 
    
         
            +
              module Components
         
     | 
| 
      
 8 
     | 
    
         
            +
             
     | 
| 
      
 9 
     | 
    
         
            +
                class LetsEncrypt < Terrafying::Context
         
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
      
 11 
     | 
    
         
            +
                  attr_reader :name, :source
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
                  PROVIDERS = {
         
     | 
| 
      
 14 
     | 
    
         
            +
                    staging: {
         
     | 
| 
      
 15 
     | 
    
         
            +
                      api_url: 'https://acme-staging.api.letsencrypt.org/directory',
         
     | 
| 
      
 16 
     | 
    
         
            +
                      ca_cert: 'https://letsencrypt.org/certs/fakeleintermediatex1.pem'
         
     | 
| 
      
 17 
     | 
    
         
            +
                    },
         
     | 
| 
      
 18 
     | 
    
         
            +
                    live:    {
         
     | 
| 
      
 19 
     | 
    
         
            +
                      api_url: 'https://acme-v01.api.letsencrypt.org/directory',
         
     | 
| 
      
 20 
     | 
    
         
            +
                      ca_cert: 'https://letsencrypt.org/certs/lets-encrypt-x3-cross-signed.pem.txt'
         
     | 
| 
      
 21 
     | 
    
         
            +
                    }
         
     | 
| 
      
 22 
     | 
    
         
            +
                  }.freeze
         
     | 
| 
      
 23 
     | 
    
         
            +
             
     | 
| 
      
 24 
     | 
    
         
            +
                  include CA
         
     | 
| 
      
 25 
     | 
    
         
            +
             
     | 
| 
      
 26 
     | 
    
         
            +
                  def self.create(name, bucket, options={})
         
     | 
| 
      
 27 
     | 
    
         
            +
                    LetsEncrypt.new.create name, bucket, options
         
     | 
| 
      
 28 
     | 
    
         
            +
                  end
         
     | 
| 
      
 29 
     | 
    
         
            +
             
     | 
| 
      
 30 
     | 
    
         
            +
                  def initialize()
         
     | 
| 
      
 31 
     | 
    
         
            +
                    super
         
     | 
| 
      
 32 
     | 
    
         
            +
                  end
         
     | 
| 
      
 33 
     | 
    
         
            +
             
     | 
| 
      
 34 
     | 
    
         
            +
                  def create(name, bucket, options={})
         
     | 
| 
      
 35 
     | 
    
         
            +
                    options = {
         
     | 
| 
      
 36 
     | 
    
         
            +
                      prefix: "",
         
     | 
| 
      
 37 
     | 
    
         
            +
                      provider: :staging,
         
     | 
| 
      
 38 
     | 
    
         
            +
                      email_address: "cloud@uswitch.com",
         
     | 
| 
      
 39 
     | 
    
         
            +
                      public_certificate: false,
         
     | 
| 
      
 40 
     | 
    
         
            +
                    }.merge(options)
         
     | 
| 
      
 41 
     | 
    
         
            +
             
     | 
| 
      
 42 
     | 
    
         
            +
                    @name = name
         
     | 
| 
      
 43 
     | 
    
         
            +
                    @bucket = bucket
         
     | 
| 
      
 44 
     | 
    
         
            +
                    @prefix = options[:prefix]
         
     | 
| 
      
 45 
     | 
    
         
            +
                    @provider = PROVIDERS[options[:provider].to_sym]
         
     | 
| 
      
 46 
     | 
    
         
            +
             
     | 
| 
      
 47 
     | 
    
         
            +
                    provider :acme, {}
         
     | 
| 
      
 48 
     | 
    
         
            +
                    provider :tls, {}
         
     | 
| 
      
 49 
     | 
    
         
            +
             
     | 
| 
      
 50 
     | 
    
         
            +
                    resource :tls_private_key, "#{@name}-account", {
         
     | 
| 
      
 51 
     | 
    
         
            +
                               algorithm: "ECDSA",
         
     | 
| 
      
 52 
     | 
    
         
            +
                               ecdsa_curve: "P384",
         
     | 
| 
      
 53 
     | 
    
         
            +
                             }
         
     | 
| 
      
 54 
     | 
    
         
            +
             
     | 
| 
      
 55 
     | 
    
         
            +
                    @account_key = output_of(:tls_private_key, "#{@name}-account", "private_key_pem")
         
     | 
| 
      
 56 
     | 
    
         
            +
             
     | 
| 
      
 57 
     | 
    
         
            +
                    @registration_url = resource :acme_registration, "#{@name}-reg", {
         
     | 
| 
      
 58 
     | 
    
         
            +
                                                   server_url: @provider[:server_url],
         
     | 
| 
      
 59 
     | 
    
         
            +
                                                   account_key_pem: @account_key,
         
     | 
| 
      
 60 
     | 
    
         
            +
                                                   email_address: options[:email_address],
         
     | 
| 
      
 61 
     | 
    
         
            +
                                                 }
         
     | 
| 
      
 62 
     | 
    
         
            +
             
     | 
| 
      
 63 
     | 
    
         
            +
                    resource :aws_s3_bucket_object, "#{@name}-account", {
         
     | 
| 
      
 64 
     | 
    
         
            +
                               bucket: @bucket,
         
     | 
| 
      
 65 
     | 
    
         
            +
                               key: File.join(@prefix, @name, "account.key"),
         
     | 
| 
      
 66 
     | 
    
         
            +
                               content: @account_key,
         
     | 
| 
      
 67 
     | 
    
         
            +
                             }
         
     | 
| 
      
 68 
     | 
    
         
            +
             
     | 
| 
      
 69 
     | 
    
         
            +
                    @ca_cert_acl = options[:public_certificate] ? 'public-read' : 'private'
         
     | 
| 
      
 70 
     | 
    
         
            +
             
     | 
| 
      
 71 
     | 
    
         
            +
                    open(@provider[:ca_cert], 'rb') do |cert|
         
     | 
| 
      
 72 
     | 
    
         
            +
                      resource :aws_s3_bucket_object, "#{@name}-cert", {
         
     | 
| 
      
 73 
     | 
    
         
            +
                                 bucket: @bucket,
         
     | 
| 
      
 74 
     | 
    
         
            +
                                 key: File.join(@prefix, @name, "ca.cert"),
         
     | 
| 
      
 75 
     | 
    
         
            +
                                 source: cert.read,
         
     | 
| 
      
 76 
     | 
    
         
            +
                                 acl: @ca_cert_acl
         
     | 
| 
      
 77 
     | 
    
         
            +
                               }
         
     | 
| 
      
 78 
     | 
    
         
            +
                    end
         
     | 
| 
      
 79 
     | 
    
         
            +
             
     | 
| 
      
 80 
     | 
    
         
            +
                    @source = File.join("s3://", @bucket, @prefix, @name, "ca.cert")
         
     | 
| 
      
 81 
     | 
    
         
            +
             
     | 
| 
      
 82 
     | 
    
         
            +
                    self
         
     | 
| 
      
 83 
     | 
    
         
            +
                  end
         
     | 
| 
      
 84 
     | 
    
         
            +
             
     | 
| 
      
 85 
     | 
    
         
            +
                  def create_keypair_in(ctx, name, options={})
         
     | 
| 
      
 86 
     | 
    
         
            +
                    options = {
         
     | 
| 
      
 87 
     | 
    
         
            +
                      common_name: name,
         
     | 
| 
      
 88 
     | 
    
         
            +
                      organization: "uSwitch Limited",
         
     | 
| 
      
 89 
     | 
    
         
            +
                      validity_in_hours: 24 * 365,
         
     | 
| 
      
 90 
     | 
    
         
            +
                      allowed_uses: [
         
     | 
| 
      
 91 
     | 
    
         
            +
                        "nonRepudiation",
         
     | 
| 
      
 92 
     | 
    
         
            +
                        "digitalSignature",
         
     | 
| 
      
 93 
     | 
    
         
            +
                        "keyEncipherment"
         
     | 
| 
      
 94 
     | 
    
         
            +
                      ],
         
     | 
| 
      
 95 
     | 
    
         
            +
                      dns_names: [],
         
     | 
| 
      
 96 
     | 
    
         
            +
                      ip_addresses: [],
         
     | 
| 
      
 97 
     | 
    
         
            +
                      min_days_remaining: 21,
         
     | 
| 
      
 98 
     | 
    
         
            +
                    }.merge(options)
         
     | 
| 
      
 99 
     | 
    
         
            +
             
     | 
| 
      
 100 
     | 
    
         
            +
                    key_ident = "#{@name}-#{tf_safe(name)}"
         
     | 
| 
      
 101 
     | 
    
         
            +
             
     | 
| 
      
 102 
     | 
    
         
            +
                    ctx.resource :tls_private_key, key_ident, {
         
     | 
| 
      
 103 
     | 
    
         
            +
                                   algorithm: "ECDSA",
         
     | 
| 
      
 104 
     | 
    
         
            +
                                   ecdsa_curve: "P384",
         
     | 
| 
      
 105 
     | 
    
         
            +
                                 }
         
     | 
| 
      
 106 
     | 
    
         
            +
             
     | 
| 
      
 107 
     | 
    
         
            +
                    ctx.resource :tls_cert_request, key_ident, {
         
     | 
| 
      
 108 
     | 
    
         
            +
                                   key_algorithm: "ECDSA",
         
     | 
| 
      
 109 
     | 
    
         
            +
                                   private_key_pem: output_of(:tls_private_key, key_ident, :private_key_pem),
         
     | 
| 
      
 110 
     | 
    
         
            +
                                   subject: {
         
     | 
| 
      
 111 
     | 
    
         
            +
                                     common_name: options[:common_name],
         
     | 
| 
      
 112 
     | 
    
         
            +
                                     organization: options[:organization],
         
     | 
| 
      
 113 
     | 
    
         
            +
                                   },
         
     | 
| 
      
 114 
     | 
    
         
            +
                                   dns_names: options[:dns_names],
         
     | 
| 
      
 115 
     | 
    
         
            +
                                   ip_addresses: options[:ip_addresses],
         
     | 
| 
      
 116 
     | 
    
         
            +
                                 }
         
     | 
| 
      
 117 
     | 
    
         
            +
             
     | 
| 
      
 118 
     | 
    
         
            +
                    ctx.resource :acme_certificate, key_ident, {
         
     | 
| 
      
 119 
     | 
    
         
            +
                                   server_url: @provider[:server_url],
         
     | 
| 
      
 120 
     | 
    
         
            +
                                   account_key_pem: @account_key,
         
     | 
| 
      
 121 
     | 
    
         
            +
                                   registration_url: @registration_url,
         
     | 
| 
      
 122 
     | 
    
         
            +
                                   min_days_remaining: options[:min_days_remaining],
         
     | 
| 
      
 123 
     | 
    
         
            +
                                   dns_challenge: {
         
     | 
| 
      
 124 
     | 
    
         
            +
                                     provider: "route53",
         
     | 
| 
      
 125 
     | 
    
         
            +
                                   },
         
     | 
| 
      
 126 
     | 
    
         
            +
                                   certificate_request_pem: output_of(:tls_cert_request, key_ident, :cert_request_pem),
         
     | 
| 
      
 127 
     | 
    
         
            +
                                 }
         
     | 
| 
      
 128 
     | 
    
         
            +
             
     | 
| 
      
 129 
     | 
    
         
            +
                    ctx.resource :aws_s3_bucket_object, "#{key_ident}-key", {
         
     | 
| 
      
 130 
     | 
    
         
            +
                                   bucket: @bucket,
         
     | 
| 
      
 131 
     | 
    
         
            +
                                   key: File.join(@prefix, @name, name, "key"),
         
     | 
| 
      
 132 
     | 
    
         
            +
                                   content: output_of(:tls_private_key, key_ident, :private_key_pem),
         
     | 
| 
      
 133 
     | 
    
         
            +
                                 }
         
     | 
| 
      
 134 
     | 
    
         
            +
             
     | 
| 
      
 135 
     | 
    
         
            +
                    ctx.resource :aws_s3_bucket_object, "#{key_ident}-cert", {
         
     | 
| 
      
 136 
     | 
    
         
            +
                                   bucket: @bucket,
         
     | 
| 
      
 137 
     | 
    
         
            +
                                   key: File.join(@prefix, @name, name, "cert"),
         
     | 
| 
      
 138 
     | 
    
         
            +
                                   content: output_of(:acme_certificate, key_ident, :certificate_pem),
         
     | 
| 
      
 139 
     | 
    
         
            +
                                 }
         
     | 
| 
      
 140 
     | 
    
         
            +
             
     | 
| 
      
 141 
     | 
    
         
            +
                    reference_keypair(ctx, name)
         
     | 
| 
      
 142 
     | 
    
         
            +
                  end
         
     | 
| 
      
 143 
     | 
    
         
            +
             
     | 
| 
      
 144 
     | 
    
         
            +
                end
         
     | 
| 
      
 145 
     | 
    
         
            +
              end
         
     | 
| 
      
 146 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,159 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
             
     | 
| 
      
 2 
     | 
    
         
            +
            require 'terrafying/components/usable'
         
     | 
| 
      
 3 
     | 
    
         
            +
            require 'terrafying/generator'
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
            require_relative './ports'
         
     | 
| 
      
 6 
     | 
    
         
            +
             
     | 
| 
      
 7 
     | 
    
         
            +
            module Terrafying
         
     | 
| 
      
 8 
     | 
    
         
            +
             
     | 
| 
      
 9 
     | 
    
         
            +
              module Components
         
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
      
 11 
     | 
    
         
            +
                class LoadBalancer < Terrafying::Context
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
                  attr_reader :id, :name, :type, :security_group, :ports, :target_groups, :alias_config
         
     | 
| 
      
 14 
     | 
    
         
            +
             
     | 
| 
      
 15 
     | 
    
         
            +
                  include Usable
         
     | 
| 
      
 16 
     | 
    
         
            +
             
     | 
| 
      
 17 
     | 
    
         
            +
                  def self.create_in(vpc, name, options={})
         
     | 
| 
      
 18 
     | 
    
         
            +
                    LoadBalancer.new.create_in vpc, name, options
         
     | 
| 
      
 19 
     | 
    
         
            +
                  end
         
     | 
| 
      
 20 
     | 
    
         
            +
             
     | 
| 
      
 21 
     | 
    
         
            +
                  def self.find_in(vpc, name)
         
     | 
| 
      
 22 
     | 
    
         
            +
                    LoadBalancer.new.find_in vpc, name
         
     | 
| 
      
 23 
     | 
    
         
            +
                  end
         
     | 
| 
      
 24 
     | 
    
         
            +
             
     | 
| 
      
 25 
     | 
    
         
            +
                  def initialize()
         
     | 
| 
      
 26 
     | 
    
         
            +
                    super
         
     | 
| 
      
 27 
     | 
    
         
            +
                  end
         
     | 
| 
      
 28 
     | 
    
         
            +
             
     | 
| 
      
 29 
     | 
    
         
            +
                  def find_in(vpc, name)
         
     | 
| 
      
 30 
     | 
    
         
            +
                    ident = "network-#{tf_safe(vpc.name)}-#{name}"
         
     | 
| 
      
 31 
     | 
    
         
            +
                    @type = "network"
         
     | 
| 
      
 32 
     | 
    
         
            +
             
     | 
| 
      
 33 
     | 
    
         
            +
                    begin
         
     | 
| 
      
 34 
     | 
    
         
            +
                      lb = aws.lb_by_name(ident)
         
     | 
| 
      
 35 
     | 
    
         
            +
                    rescue
         
     | 
| 
      
 36 
     | 
    
         
            +
                      ident = "application-#{tf_safe(vpc.name)}-#{name}"
         
     | 
| 
      
 37 
     | 
    
         
            +
                      @type = "application"
         
     | 
| 
      
 38 
     | 
    
         
            +
             
     | 
| 
      
 39 
     | 
    
         
            +
                      lb = aws.lb_by_name(ident)
         
     | 
| 
      
 40 
     | 
    
         
            +
             
     | 
| 
      
 41 
     | 
    
         
            +
                      @security_group = aws.security_group_by_tags({ loadbalancer_name: ident })
         
     | 
| 
      
 42 
     | 
    
         
            +
                    end
         
     | 
| 
      
 43 
     | 
    
         
            +
             
     | 
| 
      
 44 
     | 
    
         
            +
                    @id = lb.load_balancer_arn
         
     | 
| 
      
 45 
     | 
    
         
            +
                    @name = ident
         
     | 
| 
      
 46 
     | 
    
         
            +
             
     | 
| 
      
 47 
     | 
    
         
            +
                    target_groups = aws.target_groups_by_lb(@id)
         
     | 
| 
      
 48 
     | 
    
         
            +
             
     | 
| 
      
 49 
     | 
    
         
            +
                    @target_groups = target_groups.map(&:target_group_arn)
         
     | 
| 
      
 50 
     | 
    
         
            +
                    @ports = enrich_ports(target_groups.map(&:port).sort.uniq)
         
     | 
| 
      
 51 
     | 
    
         
            +
             
     | 
| 
      
 52 
     | 
    
         
            +
                    @alias_config = {
         
     | 
| 
      
 53 
     | 
    
         
            +
                      name: lb.dns_name,
         
     | 
| 
      
 54 
     | 
    
         
            +
                      zone_id: lb.canonical_hosted_zone_id,
         
     | 
| 
      
 55 
     | 
    
         
            +
                      evaluate_target_health: true,
         
     | 
| 
      
 56 
     | 
    
         
            +
                    }
         
     | 
| 
      
 57 
     | 
    
         
            +
             
     | 
| 
      
 58 
     | 
    
         
            +
                    self
         
     | 
| 
      
 59 
     | 
    
         
            +
                  end
         
     | 
| 
      
 60 
     | 
    
         
            +
             
     | 
| 
      
 61 
     | 
    
         
            +
                  def create_in(vpc, name, options={})
         
     | 
| 
      
 62 
     | 
    
         
            +
                    options = {
         
     | 
| 
      
 63 
     | 
    
         
            +
                      ports: [],
         
     | 
| 
      
 64 
     | 
    
         
            +
                      public: false,
         
     | 
| 
      
 65 
     | 
    
         
            +
                      subnets: vpc.subnets.fetch(:private, []),
         
     | 
| 
      
 66 
     | 
    
         
            +
                      tags: {},
         
     | 
| 
      
 67 
     | 
    
         
            +
                    }.merge(options)
         
     | 
| 
      
 68 
     | 
    
         
            +
             
     | 
| 
      
 69 
     | 
    
         
            +
                    @ports = enrich_ports(options[:ports])
         
     | 
| 
      
 70 
     | 
    
         
            +
             
     | 
| 
      
 71 
     | 
    
         
            +
                    l4_ports = @ports.select{ |p| is_l4_port(p) }
         
     | 
| 
      
 72 
     | 
    
         
            +
             
     | 
| 
      
 73 
     | 
    
         
            +
                    if l4_ports.count > 0 && l4_ports.count < @ports.count
         
     | 
| 
      
 74 
     | 
    
         
            +
                      raise 'Ports have to either be all layer 4 or 7'
         
     | 
| 
      
 75 
     | 
    
         
            +
                    end
         
     | 
| 
      
 76 
     | 
    
         
            +
             
     | 
| 
      
 77 
     | 
    
         
            +
                    @type = l4_ports.count == 0 ? "application" : "network"
         
     | 
| 
      
 78 
     | 
    
         
            +
             
     | 
| 
      
 79 
     | 
    
         
            +
                    ident = "#{type}-#{tf_safe(vpc.name)}-#{name}"
         
     | 
| 
      
 80 
     | 
    
         
            +
                    @name = ident
         
     | 
| 
      
 81 
     | 
    
         
            +
             
     | 
| 
      
 82 
     | 
    
         
            +
                    if @type == "application"
         
     | 
| 
      
 83 
     | 
    
         
            +
                      @security_group = resource :aws_security_group, ident, {
         
     | 
| 
      
 84 
     | 
    
         
            +
                                                   name: "loadbalancer-#{ident}",
         
     | 
| 
      
 85 
     | 
    
         
            +
                                                   description: "Describe the ingress and egress of the load balancer #{ident}",
         
     | 
| 
      
 86 
     | 
    
         
            +
                                                   tags: options[:tags].merge(
         
     | 
| 
      
 87 
     | 
    
         
            +
                                                     {
         
     | 
| 
      
 88 
     | 
    
         
            +
                                                       loadbalancer_name: ident,
         
     | 
| 
      
 89 
     | 
    
         
            +
                                                     }
         
     | 
| 
      
 90 
     | 
    
         
            +
                                                   ),
         
     | 
| 
      
 91 
     | 
    
         
            +
                                                   vpc_id: vpc.id,
         
     | 
| 
      
 92 
     | 
    
         
            +
                                                 }
         
     | 
| 
      
 93 
     | 
    
         
            +
             
     | 
| 
      
 94 
     | 
    
         
            +
                      path_mtu_setup!
         
     | 
| 
      
 95 
     | 
    
         
            +
                    end
         
     | 
| 
      
 96 
     | 
    
         
            +
             
     | 
| 
      
 97 
     | 
    
         
            +
                    @id = resource :aws_lb, ident, {
         
     | 
| 
      
 98 
     | 
    
         
            +
                                     name: ident,
         
     | 
| 
      
 99 
     | 
    
         
            +
                                     load_balancer_type: type,
         
     | 
| 
      
 100 
     | 
    
         
            +
                                     internal: !options[:public],
         
     | 
| 
      
 101 
     | 
    
         
            +
                                     subnets: options[:subnets].map(&:id),
         
     | 
| 
      
 102 
     | 
    
         
            +
                                     tags: options[:tags],
         
     | 
| 
      
 103 
     | 
    
         
            +
                                   }.merge(@type == "application" ? { security_groups: [@security_group] } : {})
         
     | 
| 
      
 104 
     | 
    
         
            +
             
     | 
| 
      
 105 
     | 
    
         
            +
                    @target_groups = []
         
     | 
| 
      
 106 
     | 
    
         
            +
             
     | 
| 
      
 107 
     | 
    
         
            +
                    @ports.each { |port|
         
     | 
| 
      
 108 
     | 
    
         
            +
                      port_ident = "#{ident}-#{port[:downstream_port]}"
         
     | 
| 
      
 109 
     | 
    
         
            +
             
     | 
| 
      
 110 
     | 
    
         
            +
                      target_group = resource :aws_lb_target_group, port_ident, {
         
     | 
| 
      
 111 
     | 
    
         
            +
                                                name: port_ident,
         
     | 
| 
      
 112 
     | 
    
         
            +
                                                port: port[:downstream_port],
         
     | 
| 
      
 113 
     | 
    
         
            +
                                                protocol: port[:type].upcase,
         
     | 
| 
      
 114 
     | 
    
         
            +
                                                vpc_id: vpc.id,
         
     | 
| 
      
 115 
     | 
    
         
            +
                                              }.merge(port.has_key?(:health_check) ? { health_check: port[:health_check] }: {})
         
     | 
| 
      
 116 
     | 
    
         
            +
             
     | 
| 
      
 117 
     | 
    
         
            +
                      ssl_options = {}
         
     | 
| 
      
 118 
     | 
    
         
            +
                      if port.has_key?(:ssl_certificate)
         
     | 
| 
      
 119 
     | 
    
         
            +
                        ssl_options = {
         
     | 
| 
      
 120 
     | 
    
         
            +
                          ssl_policy: "ELBSecurityPolicy-2015-05",
         
     | 
| 
      
 121 
     | 
    
         
            +
                          certificate_arn: port[:ssl_certificate],
         
     | 
| 
      
 122 
     | 
    
         
            +
                        }
         
     | 
| 
      
 123 
     | 
    
         
            +
                      end
         
     | 
| 
      
 124 
     | 
    
         
            +
             
     | 
| 
      
 125 
     | 
    
         
            +
                      resource :aws_lb_listener, port_ident, {
         
     | 
| 
      
 126 
     | 
    
         
            +
                                 load_balancer_arn: @id,
         
     | 
| 
      
 127 
     | 
    
         
            +
                                 port: port[:upstream_port],
         
     | 
| 
      
 128 
     | 
    
         
            +
                                 protocol: port[:type].upcase,
         
     | 
| 
      
 129 
     | 
    
         
            +
                                 default_action: {
         
     | 
| 
      
 130 
     | 
    
         
            +
                                   target_group_arn: target_group,
         
     | 
| 
      
 131 
     | 
    
         
            +
                                   type: "forward",
         
     | 
| 
      
 132 
     | 
    
         
            +
                                 },
         
     | 
| 
      
 133 
     | 
    
         
            +
                               }.merge(ssl_options)
         
     | 
| 
      
 134 
     | 
    
         
            +
             
     | 
| 
      
 135 
     | 
    
         
            +
                      @target_groups << target_group
         
     | 
| 
      
 136 
     | 
    
         
            +
                    }
         
     | 
| 
      
 137 
     | 
    
         
            +
             
     | 
| 
      
 138 
     | 
    
         
            +
                    @alias_config = {
         
     | 
| 
      
 139 
     | 
    
         
            +
                      name: output_of(:aws_lb, ident, :dns_name),
         
     | 
| 
      
 140 
     | 
    
         
            +
                      zone_id: output_of(:aws_lb, ident, :zone_id),
         
     | 
| 
      
 141 
     | 
    
         
            +
                      evaluate_target_health: true,
         
     | 
| 
      
 142 
     | 
    
         
            +
                    }
         
     | 
| 
      
 143 
     | 
    
         
            +
             
     | 
| 
      
 144 
     | 
    
         
            +
                    self
         
     | 
| 
      
 145 
     | 
    
         
            +
                  end
         
     | 
| 
      
 146 
     | 
    
         
            +
             
     | 
| 
      
 147 
     | 
    
         
            +
                  def attach(set)
         
     | 
| 
      
 148 
     | 
    
         
            +
                    if set.respond_to?(:attach_load_balancer)
         
     | 
| 
      
 149 
     | 
    
         
            +
                      set.attach_load_balancer(self)
         
     | 
| 
      
 150 
     | 
    
         
            +
                    else
         
     | 
| 
      
 151 
     | 
    
         
            +
                      raise "Dont' know how to attach object to LB"
         
     | 
| 
      
 152 
     | 
    
         
            +
                    end
         
     | 
| 
      
 153 
     | 
    
         
            +
                  end
         
     | 
| 
      
 154 
     | 
    
         
            +
             
     | 
| 
      
 155 
     | 
    
         
            +
                end
         
     | 
| 
      
 156 
     | 
    
         
            +
             
     | 
| 
      
 157 
     | 
    
         
            +
              end
         
     | 
| 
      
 158 
     | 
    
         
            +
             
     | 
| 
      
 159 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,35 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
             
     | 
| 
      
 2 
     | 
    
         
            +
            PORT_NAMES = {
         
     | 
| 
      
 3 
     | 
    
         
            +
              22 => "ssh",
         
     | 
| 
      
 4 
     | 
    
         
            +
              80 => "http",
         
     | 
| 
      
 5 
     | 
    
         
            +
              443 => "https",
         
     | 
| 
      
 6 
     | 
    
         
            +
              1194 => "openvpn",
         
     | 
| 
      
 7 
     | 
    
         
            +
            }
         
     | 
| 
      
 8 
     | 
    
         
            +
             
     | 
| 
      
 9 
     | 
    
         
            +
            def enrich_ports(ports)
         
     | 
| 
      
 10 
     | 
    
         
            +
              ports.map { |port|
         
     | 
| 
      
 11 
     | 
    
         
            +
                if port.is_a?(Numeric)
         
     | 
| 
      
 12 
     | 
    
         
            +
                  port = { upstream_port: port, downstream_port: port }
         
     | 
| 
      
 13 
     | 
    
         
            +
                end
         
     | 
| 
      
 14 
     | 
    
         
            +
             
     | 
| 
      
 15 
     | 
    
         
            +
                if port.has_key?(:number)
         
     | 
| 
      
 16 
     | 
    
         
            +
                  port[:upstream_port] = port[:number]
         
     | 
| 
      
 17 
     | 
    
         
            +
                  port[:downstream_port] = port[:number]
         
     | 
| 
      
 18 
     | 
    
         
            +
                end
         
     | 
| 
      
 19 
     | 
    
         
            +
             
     | 
| 
      
 20 
     | 
    
         
            +
                port = {
         
     | 
| 
      
 21 
     | 
    
         
            +
                  type: "tcp",
         
     | 
| 
      
 22 
     | 
    
         
            +
                  name: PORT_NAMES.fetch(port[:upstream_port], port[:upstream_port].to_s),
         
     | 
| 
      
 23 
     | 
    
         
            +
                }.merge(port)
         
     | 
| 
      
 24 
     | 
    
         
            +
             
     | 
| 
      
 25 
     | 
    
         
            +
                port
         
     | 
| 
      
 26 
     | 
    
         
            +
              }
         
     | 
| 
      
 27 
     | 
    
         
            +
            end
         
     | 
| 
      
 28 
     | 
    
         
            +
             
     | 
| 
      
 29 
     | 
    
         
            +
            def is_l4_port(port)
         
     | 
| 
      
 30 
     | 
    
         
            +
              port[:type] == "tcp" || port[:type] == "udp"
         
     | 
| 
      
 31 
     | 
    
         
            +
            end
         
     | 
| 
      
 32 
     | 
    
         
            +
             
     | 
| 
      
 33 
     | 
    
         
            +
            def is_l7_port(port)
         
     | 
| 
      
 34 
     | 
    
         
            +
              port[:type] == "http" || port[:type] == "https"
         
     | 
| 
      
 35 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,171 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
             
     | 
| 
      
 2 
     | 
    
         
            +
            require 'terrafying/components/ca'
         
     | 
| 
      
 3 
     | 
    
         
            +
            require 'terrafying/generator'
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
            module Terrafying
         
     | 
| 
      
 6 
     | 
    
         
            +
             
     | 
| 
      
 7 
     | 
    
         
            +
              module Components
         
     | 
| 
      
 8 
     | 
    
         
            +
             
     | 
| 
      
 9 
     | 
    
         
            +
                class SelfSignedCA < Terrafying::Context
         
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
      
 11 
     | 
    
         
            +
                  attr_reader :name, :source, :ca_key
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
                  include CA
         
     | 
| 
      
 14 
     | 
    
         
            +
             
     | 
| 
      
 15 
     | 
    
         
            +
                  def self.create(name, bucket, options={})
         
     | 
| 
      
 16 
     | 
    
         
            +
                    SelfSignedCA.new.create name, bucket, options
         
     | 
| 
      
 17 
     | 
    
         
            +
                  end
         
     | 
| 
      
 18 
     | 
    
         
            +
             
     | 
| 
      
 19 
     | 
    
         
            +
                  def initialize()
         
     | 
| 
      
 20 
     | 
    
         
            +
                    super
         
     | 
| 
      
 21 
     | 
    
         
            +
                  end
         
     | 
| 
      
 22 
     | 
    
         
            +
             
     | 
| 
      
 23 
     | 
    
         
            +
                  def create(name, bucket, options={})
         
     | 
| 
      
 24 
     | 
    
         
            +
                    options = {
         
     | 
| 
      
 25 
     | 
    
         
            +
                      prefix: "",
         
     | 
| 
      
 26 
     | 
    
         
            +
                      common_name: name,
         
     | 
| 
      
 27 
     | 
    
         
            +
                      organization: "uSwitch Limited",
         
     | 
| 
      
 28 
     | 
    
         
            +
                      public_certificate: false,
         
     | 
| 
      
 29 
     | 
    
         
            +
                    }.merge(options)
         
     | 
| 
      
 30 
     | 
    
         
            +
             
     | 
| 
      
 31 
     | 
    
         
            +
                    @name = name
         
     | 
| 
      
 32 
     | 
    
         
            +
                    @bucket = bucket
         
     | 
| 
      
 33 
     | 
    
         
            +
                    @prefix = options[:prefix]
         
     | 
| 
      
 34 
     | 
    
         
            +
             
     | 
| 
      
 35 
     | 
    
         
            +
                    @ident = "#{name}-ca"
         
     | 
| 
      
 36 
     | 
    
         
            +
             
     | 
| 
      
 37 
     | 
    
         
            +
                    provider :tls, {}
         
     | 
| 
      
 38 
     | 
    
         
            +
             
     | 
| 
      
 39 
     | 
    
         
            +
                    resource :tls_private_key, @ident, {
         
     | 
| 
      
 40 
     | 
    
         
            +
                               algorithm: "ECDSA",
         
     | 
| 
      
 41 
     | 
    
         
            +
                               ecdsa_curve: "P384",
         
     | 
| 
      
 42 
     | 
    
         
            +
                             }
         
     | 
| 
      
 43 
     | 
    
         
            +
             
     | 
| 
      
 44 
     | 
    
         
            +
                    resource :tls_self_signed_cert, @ident, {
         
     | 
| 
      
 45 
     | 
    
         
            +
                               key_algorithm: "ECDSA",
         
     | 
| 
      
 46 
     | 
    
         
            +
                               private_key_pem: output_of(:tls_private_key, @ident, :private_key_pem),
         
     | 
| 
      
 47 
     | 
    
         
            +
                               subject: {
         
     | 
| 
      
 48 
     | 
    
         
            +
                                 common_name: options[:common_name],
         
     | 
| 
      
 49 
     | 
    
         
            +
                                 organization: options[:organization],
         
     | 
| 
      
 50 
     | 
    
         
            +
                               },
         
     | 
| 
      
 51 
     | 
    
         
            +
                               is_ca_certificate: true,
         
     | 
| 
      
 52 
     | 
    
         
            +
                               validity_period_hours: 24 * 365,
         
     | 
| 
      
 53 
     | 
    
         
            +
                               allowed_uses: [
         
     | 
| 
      
 54 
     | 
    
         
            +
                                 "certSigning",
         
     | 
| 
      
 55 
     | 
    
         
            +
                                 "digitalSignature",
         
     | 
| 
      
 56 
     | 
    
         
            +
                               ],
         
     | 
| 
      
 57 
     | 
    
         
            +
                             }
         
     | 
| 
      
 58 
     | 
    
         
            +
             
     | 
| 
      
 59 
     | 
    
         
            +
                    @source = File.join("s3://", @bucket, @prefix, @name, "ca.cert")
         
     | 
| 
      
 60 
     | 
    
         
            +
             
     | 
| 
      
 61 
     | 
    
         
            +
                    @ca_key = output_of(:tls_private_key, @ident, :private_key_pem)
         
     | 
| 
      
 62 
     | 
    
         
            +
                    @ca_cert = output_of(:tls_self_signed_cert, @ident, :cert_pem)
         
     | 
| 
      
 63 
     | 
    
         
            +
             
     | 
| 
      
 64 
     | 
    
         
            +
                    if options[:public_certificate]
         
     | 
| 
      
 65 
     | 
    
         
            +
                      cert_acl = "public-read"
         
     | 
| 
      
 66 
     | 
    
         
            +
                    else
         
     | 
| 
      
 67 
     | 
    
         
            +
                      cert_acl = "private"
         
     | 
| 
      
 68 
     | 
    
         
            +
                    end
         
     | 
| 
      
 69 
     | 
    
         
            +
             
     | 
| 
      
 70 
     | 
    
         
            +
                    resource :aws_s3_bucket_object, "#{@name}-cert", {
         
     | 
| 
      
 71 
     | 
    
         
            +
                               bucket: @bucket,
         
     | 
| 
      
 72 
     | 
    
         
            +
                               key: File.join(@prefix, @name, "ca.cert"),
         
     | 
| 
      
 73 
     | 
    
         
            +
                               content: @ca_cert,
         
     | 
| 
      
 74 
     | 
    
         
            +
                               acl: cert_acl,
         
     | 
| 
      
 75 
     | 
    
         
            +
                             }
         
     | 
| 
      
 76 
     | 
    
         
            +
             
     | 
| 
      
 77 
     | 
    
         
            +
                    self
         
     | 
| 
      
 78 
     | 
    
         
            +
                  end
         
     | 
| 
      
 79 
     | 
    
         
            +
             
     | 
| 
      
 80 
     | 
    
         
            +
                  def keypair
         
     | 
| 
      
 81 
     | 
    
         
            +
                    resource :aws_s3_bucket_object, "#{@name}-key", {
         
     | 
| 
      
 82 
     | 
    
         
            +
                               bucket: @bucket,
         
     | 
| 
      
 83 
     | 
    
         
            +
                               key: File.join(@prefix, @name, "ca.key"),
         
     | 
| 
      
 84 
     | 
    
         
            +
                               content: @ca_key,
         
     | 
| 
      
 85 
     | 
    
         
            +
                             }
         
     | 
| 
      
 86 
     | 
    
         
            +
             
     | 
| 
      
 87 
     | 
    
         
            +
                    {
         
     | 
| 
      
 88 
     | 
    
         
            +
                      ca: self,
         
     | 
| 
      
 89 
     | 
    
         
            +
                      source: {
         
     | 
| 
      
 90 
     | 
    
         
            +
                        cert: File.join("s3://", @bucket, @prefix, @name, "ca.cert"),
         
     | 
| 
      
 91 
     | 
    
         
            +
                        key: File.join("s3://", @bucket, @prefix, @name, "ca.key"),
         
     | 
| 
      
 92 
     | 
    
         
            +
                      },
         
     | 
| 
      
 93 
     | 
    
         
            +
                      resources: [
         
     | 
| 
      
 94 
     | 
    
         
            +
                        "aws_s3_bucket_object.#{@name}-key",
         
     | 
| 
      
 95 
     | 
    
         
            +
                        "aws_s3_bucket_object.#{@name}-cert"
         
     | 
| 
      
 96 
     | 
    
         
            +
                      ],
         
     | 
| 
      
 97 
     | 
    
         
            +
                      iam_statement: {
         
     | 
| 
      
 98 
     | 
    
         
            +
                        Effect: "Allow",
         
     | 
| 
      
 99 
     | 
    
         
            +
                        Action: [
         
     | 
| 
      
 100 
     | 
    
         
            +
                          "s3:GetObjectAcl",
         
     | 
| 
      
 101 
     | 
    
         
            +
                          "s3:GetObject",
         
     | 
| 
      
 102 
     | 
    
         
            +
                        ],
         
     | 
| 
      
 103 
     | 
    
         
            +
                        Resource: [
         
     | 
| 
      
 104 
     | 
    
         
            +
                          "arn:aws:s3:::#{File.join(@bucket, @prefix, @name, "ca.cert")}",
         
     | 
| 
      
 105 
     | 
    
         
            +
                          "arn:aws:s3:::#{File.join(@bucket, @prefix, @name, "ca.key")}",
         
     | 
| 
      
 106 
     | 
    
         
            +
                        ]
         
     | 
| 
      
 107 
     | 
    
         
            +
                      }
         
     | 
| 
      
 108 
     | 
    
         
            +
                    }
         
     | 
| 
      
 109 
     | 
    
         
            +
                  end
         
     | 
| 
      
 110 
     | 
    
         
            +
             
     | 
| 
      
 111 
     | 
    
         
            +
                  def create_keypair_in(ctx, name, options={})
         
     | 
| 
      
 112 
     | 
    
         
            +
                    options = {
         
     | 
| 
      
 113 
     | 
    
         
            +
                      common_name: name,
         
     | 
| 
      
 114 
     | 
    
         
            +
                      organization: "uSwitch Limited",
         
     | 
| 
      
 115 
     | 
    
         
            +
                      validity_in_hours: 24 * 365,
         
     | 
| 
      
 116 
     | 
    
         
            +
                      allowed_uses: [
         
     | 
| 
      
 117 
     | 
    
         
            +
                        "nonRepudiation",
         
     | 
| 
      
 118 
     | 
    
         
            +
                        "digitalSignature",
         
     | 
| 
      
 119 
     | 
    
         
            +
                        "keyEncipherment"
         
     | 
| 
      
 120 
     | 
    
         
            +
                      ],
         
     | 
| 
      
 121 
     | 
    
         
            +
                      dns_names: [],
         
     | 
| 
      
 122 
     | 
    
         
            +
                      ip_addresses: [],
         
     | 
| 
      
 123 
     | 
    
         
            +
                    }.merge(options)
         
     | 
| 
      
 124 
     | 
    
         
            +
             
     | 
| 
      
 125 
     | 
    
         
            +
                    key_ident = "#{@name}-#{tf_safe(name)}"
         
     | 
| 
      
 126 
     | 
    
         
            +
             
     | 
| 
      
 127 
     | 
    
         
            +
                    ctx.resource :tls_private_key, key_ident, {
         
     | 
| 
      
 128 
     | 
    
         
            +
                                   algorithm: "ECDSA",
         
     | 
| 
      
 129 
     | 
    
         
            +
                                   ecdsa_curve: "P384",
         
     | 
| 
      
 130 
     | 
    
         
            +
                                 }
         
     | 
| 
      
 131 
     | 
    
         
            +
             
     | 
| 
      
 132 
     | 
    
         
            +
                    ctx.resource :tls_cert_request, key_ident, {
         
     | 
| 
      
 133 
     | 
    
         
            +
                                   key_algorithm: "ECDSA",
         
     | 
| 
      
 134 
     | 
    
         
            +
                                   private_key_pem: output_of(:tls_private_key, key_ident, :private_key_pem),
         
     | 
| 
      
 135 
     | 
    
         
            +
                                   subject: {
         
     | 
| 
      
 136 
     | 
    
         
            +
                                     common_name: options[:common_name],
         
     | 
| 
      
 137 
     | 
    
         
            +
                                     organization: options[:organization],
         
     | 
| 
      
 138 
     | 
    
         
            +
                                   },
         
     | 
| 
      
 139 
     | 
    
         
            +
                                   dns_names: options[:dns_names],
         
     | 
| 
      
 140 
     | 
    
         
            +
                                   ip_addresses: options[:ip_addresses],
         
     | 
| 
      
 141 
     | 
    
         
            +
                                 }
         
     | 
| 
      
 142 
     | 
    
         
            +
             
     | 
| 
      
 143 
     | 
    
         
            +
                    ctx.resource :tls_locally_signed_cert, key_ident, {
         
     | 
| 
      
 144 
     | 
    
         
            +
                                   cert_request_pem: output_of(:tls_cert_request, key_ident, :cert_request_pem),
         
     | 
| 
      
 145 
     | 
    
         
            +
                                   ca_key_algorithm: "ECDSA",
         
     | 
| 
      
 146 
     | 
    
         
            +
                                   ca_private_key_pem: @ca_key,
         
     | 
| 
      
 147 
     | 
    
         
            +
                                   ca_cert_pem: @ca_cert,
         
     | 
| 
      
 148 
     | 
    
         
            +
                                   validity_period_hours: options[:validity_in_hours],
         
     | 
| 
      
 149 
     | 
    
         
            +
                                   allowed_uses: options[:allowed_uses],
         
     | 
| 
      
 150 
     | 
    
         
            +
                                 }
         
     | 
| 
      
 151 
     | 
    
         
            +
             
     | 
| 
      
 152 
     | 
    
         
            +
                    ctx.resource :aws_s3_bucket_object, "#{key_ident}-key", {
         
     | 
| 
      
 153 
     | 
    
         
            +
                                   bucket: @bucket,
         
     | 
| 
      
 154 
     | 
    
         
            +
                                   key: File.join(@prefix, @name, name, "key"),
         
     | 
| 
      
 155 
     | 
    
         
            +
                                   content: output_of(:tls_private_key, key_ident, :private_key_pem),
         
     | 
| 
      
 156 
     | 
    
         
            +
                                 }
         
     | 
| 
      
 157 
     | 
    
         
            +
             
     | 
| 
      
 158 
     | 
    
         
            +
                    ctx.resource :aws_s3_bucket_object, "#{key_ident}-cert", {
         
     | 
| 
      
 159 
     | 
    
         
            +
                                   bucket: @bucket,
         
     | 
| 
      
 160 
     | 
    
         
            +
                                   key: File.join(@prefix, @name, name, "cert"),
         
     | 
| 
      
 161 
     | 
    
         
            +
                                   content: output_of(:tls_locally_signed_cert, key_ident, :cert_pem),
         
     | 
| 
      
 162 
     | 
    
         
            +
                                 }
         
     | 
| 
      
 163 
     | 
    
         
            +
             
     | 
| 
      
 164 
     | 
    
         
            +
                    reference_keypair(ctx, name)
         
     | 
| 
      
 165 
     | 
    
         
            +
                  end
         
     | 
| 
      
 166 
     | 
    
         
            +
             
     | 
| 
      
 167 
     | 
    
         
            +
                end
         
     | 
| 
      
 168 
     | 
    
         
            +
             
     | 
| 
      
 169 
     | 
    
         
            +
              end
         
     | 
| 
      
 170 
     | 
    
         
            +
             
     | 
| 
      
 171 
     | 
    
         
            +
            end
         
     |