class SSLocal::PumaRebinder

Takes a Puma configuration/dsl object, and - if there are any non-SSL TCP bindings and if the expected certificate files exist - then change those bindings to use SSL with the certificate files.

If there's an unexpected binding string, then return to the originally supplied bindings.

Constants

UnexpectedBindFormat

Attributes

binds[R]
dsl[R]

Public Class Methods

call(dsl) click to toggle source
# File lib/sslocal/puma_rebinder.rb, line 15
def self.call(dsl)
  new(dsl).call
end
new(dsl) click to toggle source
# File lib/sslocal/puma_rebinder.rb, line 19
def initialize(dsl)
  @dsl = dsl
  @binds = dsl.get(:binds)
end

Public Instance Methods

call() click to toggle source
# File lib/sslocal/puma_rebinder.rb, line 24
def call
  return if binds.nil?
  return unless state.enabled?

  dsl.clear_binds!
  rebind
rescue UnexpectedBindFormat => error
  puts "SSLocal: ssl not enabled due to #{error.message}"
  reset
end

Private Instance Methods

environment() click to toggle source
# File lib/sslocal/puma_rebinder.rb, line 39
def environment
  @environment ||= dsl.get(:environment)
end
host_and_port(bind) click to toggle source
# File lib/sslocal/puma_rebinder.rb, line 43
def host_and_port(bind)
  matches = bind.scan(%r{\Atcp://(.*):(\d+)\z}).first
  if matches.nil? || matches.length != 2
    raise UnexpectedBindFormat, "unexpected bind format #{bind}"
  end

  matches.first.empty? ? ["0.0.0.0", matches.last] : matches
end
rebind() click to toggle source
# File lib/sslocal/puma_rebinder.rb, line 52
def rebind
  binds.each do |bind|
    if bind.start_with?("tcp://")
      dsl.ssl_bind(
        *host_and_port(bind),
        :key => state.key_path, :cert => state.cert_path
      )
    else
      dsl.bind(bind)
    end
  end
end
reset() click to toggle source
# File lib/sslocal/puma_rebinder.rb, line 65
def reset
  dsl.clear_binds!
  binds.each { |bind| dsl.bind(bind) }
end
state() click to toggle source
# File lib/sslocal/puma_rebinder.rb, line 70
def state
  @state ||= ::SSLocal::State.new(environment)
end