Skip to content

Commit 69632d9

Browse files
committed
release: authorize webhook request using webhook secret token
GitHub: GH-43 In this PR, we set up the authorization flow for webhook requests. At the following PRs, we will implement the logic of deoployments.
1 parent fd8bf4c commit 69632d9

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

ansible/files/home/deployer/webhook/lib/deployer/app.rb

+30-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# Copyright (C) 2024 Horimoto Yasuhiro <[email protected]>
12
# Copyright (C) 2024 Takuya Kodama <[email protected]>
23
#
34
# This program is free software: you can redistribute it and/or modify
@@ -13,10 +14,38 @@
1314
# You should have received a copy of the GNU General Public License
1415
# along with this program. If not, see <https://www.gnu.org/licenses/>.
1516

17+
require "openssl"
18+
require_relative "response"
19+
1620
module Deployer
1721
class App
1822
def call(env)
19-
[200, {}, ["Hello deployer"]]
23+
request = Rack::Request.new(env)
24+
response = Response.new
25+
process(request, response) or response.finish
26+
end
27+
28+
private
29+
30+
def process(request, response)
31+
unless request.post?
32+
response.set(:method_not_allowed, "must POST")
33+
return nil
34+
end
35+
36+
unless verify_signature(request)
37+
response.set(:unauthorized, "Authorization failed")
38+
return nil
39+
end
40+
41+
response.finish
42+
end
43+
44+
def verify_signature(request)
45+
signature = 'sha256=' + OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'),
46+
ENV['SECRET_TOKEN'],
47+
request.body.read)
48+
Rack::Utils.secure_compare(signature, request.env['HTTP_X_HUB_SIGNATURE_256'])
2049
end
2150
end
2251
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright (C) 2010-2019 Sutou Kouhei <[email protected]>
2+
# Copyright (C) 2015 Kenji Okimoto <[email protected]>
3+
#
4+
# This program is free software: you can redistribute it and/or modify
5+
# it under the terms of the GNU General Public License as published by
6+
# the Free Software Foundation, either version 3 of the License, or
7+
# (at your option) any later version.
8+
#
9+
# This program is distributed in the hope that it will be useful,
10+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
# GNU General Public License for more details.
13+
#
14+
# You should have received a copy of the GNU General Public License
15+
# along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
17+
require "rack/response"
18+
19+
module Deployer
20+
class Response < Rack::Response
21+
def set(status_keyword, message)
22+
self.status = Rack::Utils.status_code(status_keyword)
23+
self["Content-Type"] = "text/plain"
24+
write(message)
25+
end
26+
end
27+
end

0 commit comments

Comments
 (0)