-
Notifications
You must be signed in to change notification settings - Fork 124
/
application_controller.rb
350 lines (298 loc) · 9.31 KB
/
application_controller.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# frozen_string_literal: true
class ApplicationController < ActionController::API
include Authenticates
include ::ActionView::Layouts
class Unauthorized < RuntimeError
attr_reader :return_message_in_response
def initialize(message = nil, return_message_with_response = false)
super(message)
@return_message_in_response = return_message_with_response
end
end
class GatewayTimeout < RuntimeError
end
class BadGateway < RuntimeError
end
class BadRequest < RuntimeError
end
class InternalServerError < RuntimeError
end
class ServiceUnavailable < RuntimeError
end
class Forbidden < Exceptions::Forbidden
def message
'Forbidden'
end
end
class RecordNotFound < Exceptions::RecordNotFound
end
class RecordExists < Exceptions::RecordExists
end
class UnprocessableEntity < RuntimeError
end
rescue_from Exceptions::RecordNotFound, with: :record_not_found
rescue_from Errors::Conjur::MissingSecretValue, with: :render_secret_not_found
rescue_from Exceptions::RecordExists, with: :record_exists
rescue_from Exceptions::Forbidden, with: :forbidden
rescue_from BadRequest, with: :bad_request
rescue_from Unauthorized, with: :unauthorized
rescue_from InternalServerError, with: :internal_server_error
rescue_from ServiceUnavailable, with: :service_unavailable
rescue_from GatewayTimeout, with: :gateway_timeout
rescue_from BadGateway, with: :bad_gateway
rescue_from Exceptions::NotImplemented, with: :not_implemented
rescue_from Sequel::ValidationFailed, with: :validation_failed
rescue_from Sequel::NoMatchingRow, with: :no_matching_row
rescue_from Sequel::ForeignKeyConstraintViolation, with: :foreign_key_constraint_violation
rescue_from Conjur::PolicyParser::Invalid, with: :policy_invalid
rescue_from Exceptions::InvalidPolicyObject, with: :policy_invalid
rescue_from ArgumentError, with: :argument_error
rescue_from ActionController::ParameterMissing, with: :argument_error
rescue_from UnprocessableEntity, with: :unprocessable_entity
rescue_from Errors::Conjur::BadSecretEncoding, with: :bad_secret_encoding
rescue_from Errors::Authentication::RoleNotApplicableForKeyRotation, with: :method_not_allowed
rescue_from Errors::Authorization::AccessToResourceIsForbiddenForRole, with: :forbidden
rescue_from Errors::Conjur::RequestedResourceNotFound, with: :resource_not_found
rescue_from Errors::Authorization::InsufficientResourcePrivileges, with: :forbidden
around_action :run_with_transaction
# sets the default content type header on incoming requests that match the
# path_match regex
def self.set_default_content_type_for_path(path_match, content_type)
::Rack::DefaultContentType.content_type_by_path[path_match] = content_type
end
private
# Wrap the request in a transaction.
def run_with_transaction(&block)
Sequel::Model.db.transaction(&block)
end
def resource_not_found e
logger.debug("#{e}\n#{e.backtrace.join("\n")}")
render_resource_not_not_found(e)
end
def render_resource_not_not_found e
logger.debug("#{e}\n#{e.backtrace.join("\n")}")
render(json: {
error: {
code: "not_found",
message: e.message
}
}, status: :not_found)
end
def record_not_found e
logger.debug("#{e}\n#{e.backtrace.join("\n")}")
render_record_not_found(e)
end
def no_matching_row e
logger.debug("#{e}\n#{e.backtrace.join("\n")}")
target = e.dataset.model.table_name.to_s.underscore rescue nil
render(json: {
error: {
code: "not_found",
target: target,
message: e.message
}.compact
}, status: :not_found)
end
def foreign_key_constraint_violation e
logger.debug("#{e}\n#{e.backtrace.join("\n")}")
# check if this is a violation of role_memberships_member_id_fkey
# or role_memberships_role_id_fkey
# sample exceptions:
# PG::ForeignKeyViolation: ERROR: insert or update on table "role_memberships" violates foreign key constraint "role_memberships_member_id_fkey"
# DETAIL: Key (member_id)=(cucumber:group:security-admin) is not present in table "roles".
# or
# PG::ForeignKeyViolation: ERROR: insert or update on table "role_memberships" violates foreign key constraint "role_memberships_role_id_fkey"
# DETAIL: Key (role_id)=(cucumber:group:developers) is not present in table "roles".
if e.message.index(/role_memberships_member_id_fkey/) ||
e.message.index(/role_memberships_role_id_fkey/)
key_string = ''
e.message.split(" ").map do |text|
if text["(member_id)"] || text["(role_id)"]
key_string = text
break
end
end
# the member ID is inside the second set of parentheses of the key_string
key_index = key_string.index(/\(/, 1) + 1
key = key_string[key_index, key_string.length - key_index - 1]
exc = Exceptions::RecordNotFound.new(key, message: "Role #{key} does not exist")
render_record_not_found(exc)
else
# if this isn't a case we're handling yet, let the exception proceed
raise e
end
end
def validation_failed e
logger.debug("#{e}\n#{e.backtrace.join("\n")}")
message = e.errors.map do |field, messages|
messages.map do |message|
[field, message].join(' ')
end
end.flatten.join(',')
details = e.errors.map do |field, messages|
messages.map do |message|
{
code: error_code_of_exception_class(e.class),
target: field,
message: message
}
end
end.flatten
render(json: {
error: {
code: error_code_of_exception_class(e.class),
message: message,
details: details
}
}, status: :unprocessable_entity)
end
def policy_invalid e
logger.debug("#{e}\n#{e.backtrace.join("\n")}")
error = { code: "policy_invalid", message: e.message }
if e.instance_of?(Conjur::PolicyParser::Invalid)
error[:innererror] = {
code: "policy_invalid",
filename: e.filename,
line: e.mark.line,
column: e.mark.column
}
end
render(json: { error: error }, status: :unprocessable_entity)
end
def argument_error e
logger.debug("#{e}\n#{e.backtrace.join("\n")}")
render(json: {
error: {
code: error_code_of_exception_class(e.class),
message: e.message
}
}, status: :unprocessable_entity)
end
def record_exists e
logger.debug("#{e}\n#{e.backtrace.join("\n")}")
render(json: {
error: {
code: "conflict",
message: e.message,
target: e.kind,
details: {
code: "conflict",
target: "id",
message: e.id
}
}
}, status: :conflict)
end
def forbidden e
logger.debug("#{e}\n#{e.backtrace.join("\n")}")
head(:forbidden)
end
def method_not_allowed e
logger.debug("#{e}\n#{e.backtrace.join("\n")}")
render(json: {
error: {
code: :method_not_allowed,
message: e.message
}
}, status: :method_not_allowed)
end
def conflict e
logger.debug("#{e}\n#{e.backtrace.join("\n")}")
render(json: {
error: {
code: :conflict,
message: e.message
}
}, status: :conflict)
end
def bad_request e
logger.debug("#{e}\n#{e.backtrace.join("\n")}")
head(:bad_request)
end
def unprocessable_entity e
logger.debug("#{e}\n#{e.backtrace.join("\n")}")
render(json: {
error: {
code: :unprocessable_entity,
message: e.message
}
}, status: :unprocessable_entity)
end
def bad_secret_encoding e
logger.debug("#{e}\n#{e.backtrace.join("\n")}")
render(json: {
error: {
code: :not_acceptable,
message: e.message
}
}, status: :not_acceptable)
end
def unauthorized e
logger.debug("#{e}\n#{e.backtrace.join("\n")}")
if e.return_message_in_response
render(json: {
error: {
code: :unauthorized,
message: e.message
}
}, status: :unauthorized)
else
head(:unauthorized)
end
end
def internal_server_error e
logger.debug("#{e}\n#{e.backtrace.join("\n")}")
head(:internal_server_error)
end
def service_unavailable e
logger.debug("#{e}\n#{e.backtrace.join("\n")}")
head(:service_unavailable)
end
def gateway_timeout e
logger.debug("#{e}\n#{e.backtrace.join("\n")}")
head(:gateway_timeout)
end
def bad_gateway e
logger.debug("#{e}\n#{e.backtrace.join("\n")}")
head(:bad_gateway)
end
def not_implemented e
logger.debug("#{e}\n#{e.backtrace.join("\n")}")
render(json: {
error: {
code: "not_implemented",
message: e.message
}
}, status: :not_implemented)
end
# Gets the value of the :account parameter.
def account
@account ||= params[:account]
end
def render_secret_not_found e
logger.debug("#{e}\n#{e.backtrace.join("\n")}")
render(json: {
error: {
code: "not_found",
message: e.message
}
}, status: :not_found)
end
def render_record_not_found e
render(json: {
error: {
code: "not_found",
message: e.message,
target: e.kind,
details: {
code: "not_found",
target: "id",
message: e.id
}
}
}, status: :not_found)
end
def error_code_of_exception_class cls
cls.to_s.underscore.split('/')[-1]
end
end