Class: Aptible::CLI::Helpers::Vhost::OptionSetBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/aptible/cli/helpers/vhost/option_set_builder.rb

Constant Summary collapse

FLAGS =
%i(
  environment
  app
  database
  create
  tls
  ports
  port
  alb
).freeze
SSL_PROTOCOL_VALUES =
[
  'TLSv1 TLSv1.1 TLSv1.2',
  'TLSv1 TLSv1.1 TLSv1.2 PFS',
  'TLSv1.1 TLSv1.2',
  'TLSv1.1 TLSv1.2 PFS',
  'TLSv1.2',
  'TLSv1.2 PFS',
  'TLSv1.2 PFS TLSv1.3',
  'TLSv1.3'
].freeze
ALB_PROTOCOL_VALUES =
SSL_PROTOCOL_VALUES
ELB_PROTOCOL_VALUES =
SSL_PROTOCOL_VALUES.reject { |v| v.include?(' PFS') }.freeze
SSL_PROTOCOL_ALB_DESC =
(
  'Specify the allowed SSL protocols. Valid options: ' +
  ALB_PROTOCOL_VALUES.map { |v| "\"#{v}\"" }.join(', ') +
  '. PFS options require an HTTPS (ALB) endpoint. ' \
  'Use "default" to reset to the platform default'
).freeze
SSL_PROTOCOL_ELB_DESC =
(
  'Specify the allowed SSL protocols. Valid options: ' +
  ELB_PROTOCOL_VALUES.map { |v| "\"#{v}\"" }.join(', ') +
  '. Use "default" to reset to the platform default'
).freeze

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ OptionSetBuilder

Returns a new instance of OptionSetBuilder.



46
47
48
49
# File 'lib/aptible/cli/helpers/vhost/option_set_builder.rb', line 46

def initialize(&block)
  FLAGS.each { |f| instance_variable_set("@#{f}", false) }
  instance_exec(&block) if block
end

Instance Method Details

#declare_options(thor) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/aptible/cli/helpers/vhost/option_set_builder.rb', line 51

def declare_options(thor)
  thor.instance_exec(self) do |builder|
    option :environment, aliases: '--env'

    if builder.database?
      option :database
    elsif builder.app?
      app_options

      if builder.create?
        option(
          :default_domain,
          type: :boolean,
          desc: 'Enable Default Domain on this Endpoint'
        )

      end

      if builder.ports?
        option(
          :ports,
          type: :array,
          desc: 'A list of ports to expose on this Endpoint'
        )
      end

      if builder.port?
        option(
          :port,
          type: :numeric,
          desc: 'A port to expose on this Endpoint'
        )
      end

      option(
        :idle_timeout,
        type: :string,
        desc: 'Timeout (seconds) to enforce idle timeouts while ' \
              'sending and receiving responses. Use "default" to ' \
              'reset to the platform default'
      )

      if builder.alb?
        option(
          :load_balancing_algorithm_type,
          type: :string,
          desc: 'The load balancing algorithm for this Endpoint. ' \
                'Valid options are round_robin, ' \
                'least_outstanding_requests, and ' \
                'weighted_random'
        )

        option(
          :shared,
          type: :boolean,
          desc: "Share this Endpoint's load balancer with other " \
                'Endpoints'
        )

        option(
          :force_ssl,
          type: :boolean,
          desc: 'Redirect all HTTP requests to HTTPS, and ' \
                'enable the Strict-Transport-Security header (HSTS)'
        )

        option(
          :maintenance_page_url,
          type: :string,
          desc: 'The URL of a maintenance page to cache and serve ' \
                'when requests time out, or your app is unhealthy. ' \
                'Use "default" to reset to the platform default'
        )

        option(
          :release_healthcheck_timeout,
          type: :string,
          desc: 'Timeout (seconds) to wait for your app to ' \
                'respond to a release health check. Use "default" ' \
                'to reset to the platform default'
        )

        option(
          :show_elb_healthchecks,
          type: :boolean,
          desc: 'Show all runtime health check requets in the ' \
                "endpoint's logs"
        )

        option(
          :ssl_protocols_override,
          type: :string,
          desc: SSL_PROTOCOL_ALB_DESC
        )

        option(
          :strict_health_checks,
          type: :boolean,
          desc: 'Require containers to respond to health checks ' \
                'with a 200 OK HTTP response.'
        )

      end
    end

    if builder.create?
      option(
        :internal,
        type: :boolean,
        desc: 'Restrict this Endpoint to internal traffic'
      )
    end

    option(
      :ip_whitelist,
      type: :array,
      desc: 'A list of IPv4 sources (addresses or CIDRs) to ' \
            'which to restrict traffic to this Endpoint'
    )

    unless builder.create?
      # Yes, it has to be a dash...
      # See: https://github.com/erikhuda/thor/pull/551
      option(
        :'no-ip_whitelist',
        type: :boolean,
        desc: 'Disable IP Whitelist'
      )
    end

    if builder.tls?
      option(
        :certificate_file,
        type: :string,
        desc: 'A file containing a certificate to use on this ' \
              'Endpoint'
      )
      option(
        :private_key_file,
        type: :string,
        desc: 'A file containing a private key to use on this ' \
              'Endpoint'
      )

      option(
        :managed_tls,
        type: :boolean,
        desc: 'Enable Managed TLS on this Endpoint'
      )

      option(
        :managed_tls_domain,
        desc: 'A domain to use for Managed TLS'
      )

      option(
        :certificate_fingerprint,
        type: :string,
        desc: 'The fingerprint of an existing Certificate to use ' \
              'on this Endpoint'
      )

      unless builder.alb?
        option(
          :ssl_protocols_override,
          type: :string,
          desc: SSL_PROTOCOL_ELB_DESC
        )

        option(
          :ssl_ciphers_override,
          type: :string,
          desc: 'Specify the allowed SSL ciphers. ' \
                'Use "default" to reset to the platform default'
        )

        option(
          :disable_weak_cipher_suites,
          type: :boolean,
          desc: 'Block the SSLv3 protocol and RC4 ciphers'
        )
      end
    end
  end
end

#prepare(account, options) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/aptible/cli/helpers/vhost/option_set_builder.rb', line 237

def prepare(, options)
  options = options.dup # We're going to delete keys here
  verify_option_conflicts(options)

  params = {}
  settings = {}

  params[:ip_whitelist] = options.delete(:ip_whitelist) do
    create? ? [] : nil
  end

  if options.delete(:'no-ip_whitelist') { false }
    params[:ip_whitelist] = []
  end

  params[:container_port] = options.delete(:port) if port?

  if ports?
    raw_ports = options.delete(:ports) do
      create? ? [] : nil
    end

    if raw_ports
      params[:container_ports] = raw_ports.map do |p|
        begin
          Integer(p)
        rescue ArgumentError
          m = "Invalid port: #{p}"
          raise Thor::Error, m
        end
      end
    end
  end

  if app?
    params[:internal] = options.delete(:internal) do
      create? ? false : nil
    end

    params[:default] = options.delete(:default_domain) do
      create? ? false : nil
    end

    options.delete(:app)
  elsif database?
    params[:internal] = options.delete(:internal) do
      create? ? false : nil
    end

    options.delete(:database)
  else
    params[:internal] = false
  end

  process_tls(, options, params) if tls?

  if alb?
    lba_type = options.delete(:load_balancing_algorithm_type)
    if lba_type
      valid_types = %w(round_robin least_outstanding_requests
                       weighted_random)
      unless valid_types.include?(lba_type)
        e = "Invalid load balancing algorithm type: #{lba_type}. " \
            "Valid options are: #{valid_types.join(', ')}"
        raise Thor::Error, e
      end
      params[:load_balancing_algorithm_type] = lba_type
    end

    params[:shared] = options.delete(:shared)
  end

  if (proto = options[:ssl_protocols_override]) &&
     proto != 'default'
    unless ALB_PROTOCOL_VALUES.include?(proto)
      raise Thor::Error,
            "Invalid --ssl-protocols-override: \"#{proto}\". " \
            "Valid options are: #{ALB_PROTOCOL_VALUES.join(', ')}"
    end

    if !ELB_PROTOCOL_VALUES.include?(proto) && !alb?
      raise Thor::Error,
            "Invalid --ssl-protocols-override: \"#{proto}\". " \
            'PFS options are only valid for an HTTPS (ALB) endpoint. ' \
            "Valid options are: #{ELB_PROTOCOL_VALUES.join(', ')}"
    end
  end

  vhost_settings = %i(
    idle_timeout
    maintenance_page_url
    release_healthcheck_timeout
    ssl_protocols_override
    ssl_ciphers_override
  )

  vhost_settings.each do |key|
    val = options.delete(key)
    next if val.nil?

    settings[key.to_s.upcase] = case val
                                when 'default'
                                  ''
                                else
                                  val
                                end
  end

  boolean_vhost_settings = %i(
    force_ssl
    show_elb_healthchecks
    strict_health_checks
    disable_weak_cipher_suites
  )

  boolean_vhost_settings.each do |key|
    value = options.delete(key)
    next if value.nil?

    settings[key.to_s.upcase] = value.to_s
  end

  options.delete(:environment)

  # NOTE: This is here to ensure that specs don't test for options
  # that are not declared. This is not expected to happen when using
  # this.
  raise "Unexpected options: #{options}" if options.any?

  [params.delete_if { |_, v| v.nil? }, settings]
end