-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.rb
322 lines (273 loc) · 8.03 KB
/
tasks.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
require 'sidekiq'
require 'themis/finals/checker/result'
require 'time_difference'
require 'net/http'
require 'json'
require './utils'
require 'raven/base'
require 'jwt'
require 'openssl'
config_module_name = ENV['THEMIS_FINALS_CHECKER_MODULE'] || ::File.join(
::Dir.pwd,
'checker.rb'
)
require config_module_name
$logger = get_logger
config_redis = {
url: "redis://#{ENV['REDIS_HOST']}:#{ENV['REDIS_PORT']}/#{ENV['REDIS_DB']}"
}
unless ::ENV.fetch('REDIS_PASSWORD', nil).nil?
config_redis[:password] = ENV['REDIS_PASSWORD']
end
::Sidekiq.default_worker_options = { 'retry' => 0 }
::Sidekiq.configure_server do |config|
config.redis = config_redis
config.on(:startup) do
$logger.info "Starting queue process, instance #{ENV['QUEUE_INSTANCE']}"
end
config.on(:quiet) do
$logger.info 'Got USR1, stopping further job processing...'
end
config.on(:shutdown) do
$logger.info 'Got TERM, shutting down process...'
end
end
::Sidekiq.configure_client do |config|
config.redis = config_redis
end
$raven_enabled = !ENV['SENTRY_DSN'].nil?
if $raven_enabled
::Raven.configure do |config|
config.dsn = ENV['SENTRY_DSN']
config.ssl_verification = false
config.logger = $logger
config.async = lambda { |event|
::Thread.new { ::Raven.send_event(event) }
}
end
end
def decode_capsule(capsule)
wrap_prefix = ::ENV['THEMIS_FINALS_FLAG_WRAP_PREFIX']
wrap_suffix = ::ENV['THEMIS_FINALS_FLAG_WRAP_SUFFIX']
encoded_payload = capsule.slice(
wrap_prefix.length,
capsule.length - wrap_prefix.length - wrap_suffix.length
)
key = ::OpenSSL::PKey.read(::ENV['THEMIS_FINALS_FLAG_SIGN_KEY_PUBLIC'].gsub('\n', "\n"))
alg = 'none'
if key.class == ::OpenSSL::PKey::RSA
alg = 'RS256'
elsif key.class == ::OpenSSL::PKey::EC
alg = 'ES256'
end
payload = ::JWT.decode(encoded_payload, key, true, { algorithm: alg })
payload[0]['flag']
end
class Push
include ::Sidekiq::Worker
def internal_push(endpoint, capsule, label, metadata)
result = ::Themis::Finals::Checker::Result::INTERNAL_ERROR
updated_label = label
begin
result, updated_label, message = push(endpoint, capsule, label, metadata)
rescue Interrupt
raise
rescue Exception => e
if $raven_enabled
::Raven.capture_exception e
end
$logger.error e.message
e.backtrace.each { |line| $logger.error line }
end
return result, updated_label, message
end
def perform(job_data)
params = job_data['params']
metadata = job_data['metadata']
timestamp_created = ::DateTime.iso8601 metadata['timestamp']
timestamp_delivered = ::DateTime.now
flag = decode_capsule(params['capsule'])
status, updated_label, message = internal_push(
params['endpoint'],
params['capsule'],
params['label'],
metadata
)
timestamp_processed = ::DateTime.now
job_result = {
status: status,
flag: flag,
label: updated_label,
message: message
}
delivery_time = ::TimeDifference.between(
timestamp_created,
timestamp_delivered
).in_seconds
processing_time = ::TimeDifference.between(
timestamp_delivered,
timestamp_processed
).in_seconds
log_message = \
'PUSH flag `%s` /%d to `%s`@`%s` (%s) - status %s, label `%s` '\
'[delivery %.2fs, processing %.2fs]' % [
flag,
metadata['round'],
metadata['service_name'],
metadata['team_name'],
params['endpoint'],
::Themis::Finals::Checker::Result.key(status),
job_result[:label],
delivery_time,
processing_time
]
if $raven_enabled
short_log_message = \
'PUSH `%s...` /%d to `%s` - status %s' % [
flag[0..7],
metadata['round'],
metadata['team_name'],
::Themis::Finals::Checker::Result.key(status)
]
raven_data = {
level: 'info',
tags: {
tf_operation: 'push',
tf_status: ::Themis::Finals::Checker::Result.key(status).to_s,
tf_team: metadata['team_name'],
tf_service: metadata['service_name'],
tf_round: metadata['round']
},
extra: {
endpoint: params['endpoint'],
capsule: params['capsule'],
flag: flag,
label: job_result[:label],
message: job_result[:message],
delivery_time: delivery_time,
processing_time: processing_time
}
}
::Raven.capture_message short_log_message, raven_data
end
$logger.info log_message
uri = URI(job_data['report_url'])
req = ::Net::HTTP::Post.new(uri)
req.body = job_result.to_json
req.content_type = 'application/json'
req.basic_auth(
::ENV['THEMIS_FINALS_AUTH_MASTER_USERNAME'],
::ENV['THEMIS_FINALS_AUTH_MASTER_PASSWORD']
)
res = ::Net::HTTP.start(uri.hostname, uri.port) do |http|
http.request(req)
end
unless res.is_a?(::Net::HTTPSuccess)
$logger.error res.code
$logger.error res.message
end
end
end
class Pull
include ::Sidekiq::Worker
def internal_pull(endpoint, capsule, label, metadata)
result = ::Themis::Finals::Checker::Result::INTERNAL_ERROR
begin
result, message = pull(endpoint, capsule, label, metadata)
rescue Interrupt
raise
rescue Exception => e
if $raven_enabled
::Raven.capture_exception e
end
$logger.error e.message
e.backtrace.each { |line| $logger.error line }
end
return result, message
end
def perform(job_data)
params = job_data['params']
metadata = job_data['metadata']
timestamp_created = ::DateTime.iso8601 metadata['timestamp']
timestamp_delivered = ::DateTime.now
flag = decode_capsule(params['capsule'])
status, message = internal_pull(
params['endpoint'],
params['capsule'],
params['label'],
metadata
)
timestamp_processed = ::DateTime.now
job_result = {
request_id: params['request_id'],
status: status,
message: message
}
delivery_time = ::TimeDifference.between(
timestamp_created,
timestamp_delivered
).in_seconds
processing_time = ::TimeDifference.between(
timestamp_delivered,
timestamp_processed
).in_seconds
log_message = \
'PULL flag `%s` /%d from `%s`@`%s` (%s) with label `%s` - status %s '\
'[delivery %.2fs, processing %.2fs]' % [
flag,
metadata['round'],
metadata['service_name'],
metadata['team_name'],
params['endpoint'],
params['label'],
::Themis::Finals::Checker::Result.key(status),
delivery_time,
processing_time
]
if $raven_enabled
short_log_message = \
'PULL `%s...` /%d from `%s` - status %s' % [
flag[0..7],
metadata['round'],
metadata['team_name'],
::Themis::Finals::Checker::Result.key(status)
]
raven_data = {
level: 'info',
tags: {
tf_operation: 'pull',
tf_status: ::Themis::Finals::Checker::Result.key(status).to_s,
tf_team: metadata['team_name'],
tf_service: metadata['service_name'],
tf_round: metadata['round']
},
extra: {
endpoint: params['endpoint'],
capsule: params['capsule'],
flag: flag,
label: params['label'],
message: job_result[:message],
delivery_time: delivery_time,
processing_time: processing_time
}
}
::Raven.capture_message short_log_message, raven_data
end
$logger.info log_message
uri = URI(job_data['report_url'])
req = ::Net::HTTP::Post.new(uri)
req.body = job_result.to_json
req.content_type = 'application/json'
req.basic_auth(
::ENV['THEMIS_FINALS_AUTH_MASTER_USERNAME'],
::ENV['THEMIS_FINALS_AUTH_MASTER_PASSWORD']
)
res = ::Net::HTTP.start(uri.hostname, uri.port) do |http|
http.request(req)
end
unless res.is_a?(::Net::HTTPSuccess)
$logger.error res.code
$logger.error res.message
end
end
end