Class: Chook::Server

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/chook/server.rb,
lib/chook/server/routes/home.rb,
lib/chook/server/routes/handle_webhook_event.rb

Overview

see server.rb

Constant Summary collapse

DEFAULT_SERVER_ENGINE =
:webrick
DEFAULT_PORT =
8000

Class Method Summary collapse

Class Method Details

.chook_configureObject

Sinatra Settings


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
# File 'lib/chook/server.rb', line 69

def self.chook_configure
  configure do
    set :environment, :production
    enable :logging, :lock
    set :bind, '0.0.0.0'
    set :server, @server_engine
    set :port, @server_port

    if Chook::CONFIG.use_ssl
      case @server_engine.to_sym
      when :webrick
        require 'webrick/https'
        key = Chook::CONFIG.ssl_private_key_path.read
        cert = Chook::CONFIG.ssl_cert_path.read
        cert_name = Chook::CONFIG.ssl_cert_name
        set :SSLEnable, true
        set :SSLVerifyClient, OpenSSL::SSL::VERIFY_NONE
        set :SSLPrivateKey, OpenSSL::PKey::RSA.new(key, ssl_key_password)
        set :SSLCertificate, OpenSSL::X509::Certificate.new(cert)
        set :SSLCertName, [['CN', cert_name]]
      when :thin
        true
      end # case
    end # if ssl
  end # configure
end

.run!Object


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/chook/server.rb', line 42

def self.run!
  # trap HUPs to reload handlers
  Signal.trap('HUP') do
    Chook::HandledEvent::Handlers.load_handlers reload: true
  end
  Chook::HandledEvent::Handlers.load_handlers
  chook_configure
  case @server_engine.to_sym
  when :webrick
    super
  when :thin
    if Chook::CONFIG.use_ssl
      super do |server|
        server.ssl = true
        server.ssl_options = {
          cert_chain_file: Chook::CONFIG.ssl_cert_path.to_s,
          private_key_file: Chook::CONFIG.ssl_private_key_path.to_s,
          verify_peer: false
        }
      end # super do
    else
      super
    end # if use ssl
  end # case
end

.ssl_key_passwordObject

chook_configure


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/chook/server.rb', line 96

def self.ssl_key_password
  path = Chook::CONFIG.ssl_private_key_pw_path
  raise 'No config setting for "ssl_private_key_pw_path"' unless path
  file = Pathname.new path

  # if the path ends with a pipe, its a command that will
  # return the desired password, so remove the pipe,
  # execute it, and return stdout from it.
  if path.end_with? '|'
    raise 'ssl_private_key_pw_path: #{path} is not an executable file.' unless file.executable?
    return `#{path.chomp '|'}`.chomp
  end

  raise 'ssl_private_key_pw_path: #{path} is not a readable file.' unless file.readable?
  stat = file.stat
  raise "Password file for '#{pw}' has insecure permissions, must be 0600." unless ('%o' % stat.mode).end_with? '0600'

  # chomping an empty string removes all trailing \n's and \r\n's
  file.read.chomp('')
end