25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/autonoma/handler.rb', line 25
def self.handle_request(config, req)
if config.shared_secret == config.signing_secret
raise Errors.same_secrets
end
unless config.allow_production
env = ENV["RAILS_ENV"] || ENV["RACK_ENV"] || ENV["RUBY_ENV"] || ENV["ENV"]
raise Errors.production_blocked if env == "production"
end
signature = req.["x-signature"] || req.["X-Signature"] || ""
unless Hmac.verify_signature(req.body, signature, config.shared_secret)
raise Errors.invalid_signature
end
begin
body = JSON.parse(req.body)
rescue JSON::ParserError
raise Errors.invalid_body("invalid JSON")
end
action = body["action"]
raise Errors.invalid_body("missing action") unless action
case action
when "discover"
handle_discover(config)
when "up"
handle_up(config, body)
when "down"
handle_down(config, body)
else
raise Errors.unknown_action(action)
end
rescue AutonomaError => e
HandlerResponse.new(status: e.status, body: { "error" => e.message, "code" => e.code })
rescue StandardError => e
HandlerResponse.new(status: 500, body: { "error" => e.message, "code" => "INTERNAL_ERROR" })
end
|