minato_logger 0.2.10 → 0.2.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 25ff3fcf2b7ed82f3c39ee694c52d9ad81cf4086984441b082a0d46bb286ee5a
|
|
4
|
+
data.tar.gz: 855bb34c1b70f7ec4fcf2bc737ed72a46eb895383ff0d7c421467e16297c449c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f7133120d86d171ab7cfef89a1aceb3f49766b4f138ac3d78786d734e2e4d91dbea77582105a5db24ffe33565e40619d7b15030ae80dc828f57a29df13cf0065
|
|
7
|
+
data.tar.gz: 790fe184c0a52b9fd8e6c6ea6ae84bb63a68eaab082f8fab64e170b037177365c23199689fb38782239a4df93930ef4682d3e35c7fb136d064658cf695f29890
|
|
@@ -5,6 +5,12 @@ require 'json'
|
|
|
5
5
|
module MinatoLogger
|
|
6
6
|
module Middlewares
|
|
7
7
|
class RequestResponseLogger
|
|
8
|
+
SENSITIVE_HEADERS = %w[
|
|
9
|
+
AUTHORIZATION PROXY_AUTHORIZATION X_CSRF_TOKEN
|
|
10
|
+
HTTP_AUTHORIZATION HTTP_PROXY_AUTHORIZATION
|
|
11
|
+
HTTP_X_CSRF_TOKEN COOKIE HTTP_COOKIE
|
|
12
|
+
].freeze
|
|
13
|
+
|
|
8
14
|
def initialize(app)
|
|
9
15
|
@app = app
|
|
10
16
|
end
|
|
@@ -20,21 +26,25 @@ module MinatoLogger
|
|
|
20
26
|
private
|
|
21
27
|
|
|
22
28
|
def execute_with_logging(request, env)
|
|
23
|
-
|
|
29
|
+
current_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
30
|
+
data = { body: '', status: 500, start_time: current_time, headers: {} }
|
|
24
31
|
|
|
25
32
|
begin
|
|
26
|
-
data[:status], headers, response = @app.call(env)
|
|
33
|
+
data[:status], data[:headers], response = @app.call(env)
|
|
27
34
|
data[:body] = extract_body_safely(response || [])
|
|
28
35
|
|
|
29
|
-
[data[:status], headers, [data[:body]]]
|
|
36
|
+
[data[:status], data[:headers], [data[:body]]]
|
|
30
37
|
ensure
|
|
31
|
-
|
|
38
|
+
duration = ((current_time - data[:start_time]) * 1000).round(2)
|
|
39
|
+
log_response(request, env, data.merge({ duration: duration }))
|
|
32
40
|
end
|
|
33
41
|
end
|
|
34
42
|
|
|
35
43
|
def log_request(request, env)
|
|
36
44
|
log(default_log_data(request, env).merge({
|
|
37
45
|
type: 'REQUEST',
|
|
46
|
+
headers: extract_request_headers(request),
|
|
47
|
+
body: extract_request_body(request),
|
|
38
48
|
message: "REQUEST: #{request.method} #{request.fullpath}"
|
|
39
49
|
}))
|
|
40
50
|
end
|
|
@@ -56,6 +66,7 @@ module MinatoLogger
|
|
|
56
66
|
default_log_data(request, env).merge({
|
|
57
67
|
type: 'RESPONSE', duration: data[:duration], status: data[:status],
|
|
58
68
|
response: safe_parse_json(data[:body]),
|
|
69
|
+
headers: filter_sensitive_headers(data[:headers]),
|
|
59
70
|
message: "RESPONSE: #{request.method} #{request.fullpath} - " \
|
|
60
71
|
"#{data[:status]} (#{data[:duration]}ms)"
|
|
61
72
|
})
|
|
@@ -88,12 +99,36 @@ module MinatoLogger
|
|
|
88
99
|
body.to_s
|
|
89
100
|
end
|
|
90
101
|
|
|
91
|
-
def
|
|
92
|
-
|
|
102
|
+
def extract_request_body(request)
|
|
103
|
+
return nil unless request.body
|
|
104
|
+
|
|
105
|
+
body = request.body.read
|
|
106
|
+
request.body.rewind
|
|
107
|
+
safe_parse_json(body)
|
|
108
|
+
rescue StandardError => e
|
|
109
|
+
"Error reading request body: #{e}"
|
|
93
110
|
end
|
|
94
111
|
|
|
95
|
-
def
|
|
96
|
-
|
|
112
|
+
def extract_request_headers(request)
|
|
113
|
+
headers = {}
|
|
114
|
+
request.headers.each do |k, v|
|
|
115
|
+
next unless k.start_with?('HTTP_') || %w[CONTENT_TYPE CONTENT_LENGTH].include?(k)
|
|
116
|
+
|
|
117
|
+
normalized_key = k.start_with?('HTTP_') ? k[5..] : k
|
|
118
|
+
|
|
119
|
+
headers[normalized_key] = v
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
filter_sensitive_headers(headers)
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def filter_sensitive_headers(headers)
|
|
126
|
+
return {} unless headers.is_a?(Hash)
|
|
127
|
+
|
|
128
|
+
headers.reject do |k, _|
|
|
129
|
+
key = k.to_s.upcase.tr('-', '_')
|
|
130
|
+
SENSITIVE_HEADERS.any? { |sensitive| key.include?(sensitive) }
|
|
131
|
+
end
|
|
97
132
|
end
|
|
98
133
|
end
|
|
99
134
|
end
|