-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.rb
74 lines (65 loc) · 1.61 KB
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
65
66
67
68
69
70
71
72
73
74
require 'openssl'
require 'securerandom'
require 'sinatra'
require_relative 'deploy'
deployer = Deploy.new
def verify_key(tempfile)
key = OpenSSL::PKey.read tempfile.read
Dir['keys/*'].any? do |filename|
pubkey = OpenSSL::PKey.read File.read(filename)
test_data = SecureRandom.alphanumeric(128)
digest = OpenSSL::Digest::SHA256.new
pubkey.verify(digest, key.sign(digest, test_data), test_data)
end
end
set :public_folder, __dir__ + '/static'
get '/' do
@messages = []
erb :index
end
get '/output' do
@messages = []
erb :output
end
post '/deploy' do
unless params[:key].is_a?(Hash) && !params[:key][:filename].nil? && !params[:key][:tempfile].nil?
halt 400
end
begin
verified = verify_key params[:key][:tempfile]
if verified
status, message = deployer.trigger
@status = status
@messages = [message]
erb :index
else
@status = false
@messages = ['Unrecognized key provided.']
erb :index
end
rescue => ex
@status = false
@messages = [ex.message]
erb :index
end
end
post '/output' do
unless params[:key].is_a?(Hash) && !params[:key][:filename].nil? && !params[:key][:tempfile].nil?
halt 400
end
begin
verified = verify_key params[:key][:tempfile]
if verified
@status = true
@output = File.read('/var/apps/qpixel/deploy_output.log')
erb :output
else
@status = false
@messages = ['Unrecognized key provided.']
erb :output
end
rescue => ex
@status = false
@messages = [ex.message]
end
end