Module: GetStreamRuby::LogRedaction
- Defined in:
- lib/getstream_ruby/log_redaction.rb
Overview
Redaction helpers for the SDK's structured log events. Shallow by design.
Constant Summary collapse
- REDACTED =
'<redacted>'- QUERY_PARAMS =
%w[api_key api_secret token].freeze
- BODY_KEYS =
%w[api_secret token password].freeze
- MESSAGE_SECRET_PATTERN =
Matches
key=valuefor the secret query params wherever they appear in a free-form string (e.g. a transport error message that embeds the request URL), value runs up to the next&, whitespace, or end of string. /\b(#{QUERY_PARAMS.join('|')})=[^&\s]*/i.freeze
Class Method Summary collapse
- .redact_json_body(body) ⇒ Object
-
.redact_message(string) ⇒ Object
Redacts secret query-parameter values wherever they appear in a free string (e.g.
error.messagefrom a transport exception, which may embed the full request URL). - .redact_query(params) ⇒ Object
Class Method Details
.redact_json_body(body) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/getstream_ruby/log_redaction.rb', line 26 def redact_json_body(body) return body if body.nil? || body.empty? data = JSON.parse(body) return body unless data.is_a?(Hash) changed = false BODY_KEYS.each do |key| if data.key?(key) data[key] = REDACTED changed = true end end changed ? JSON.generate(data) : body rescue JSON::ParserError body end |
.redact_message(string) ⇒ Object
Redacts secret query-parameter values wherever they appear in a free
string (e.g. error.message from a transport exception, which may embed
the full request URL). Case-insensitive on the key; the value is
replaced regardless of its own case.
50 51 52 53 54 |
# File 'lib/getstream_ruby/log_redaction.rb', line 50 def (string) return string if string.nil? || string.empty? string.gsub(MESSAGE_SECRET_PATTERN) { "#{Regexp.last_match(1)}=#{REDACTED}" } end |
.redact_query(params) ⇒ Object
20 21 22 23 24 |
# File 'lib/getstream_ruby/log_redaction.rb', line 20 def redact_query(params) return params if params.nil? || params.empty? params.to_h { |k, v| [k, QUERY_PARAMS.include?(k.to_s.downcase) ? REDACTED : v] } end |