Class: CoreLibrary::LoggerHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/apimatic-core/utilities/logger_helper.rb

Overview

logger helper methods.

Constant Summary collapse

NON_SENSITIVE_HEADERS =
%w[
  accept accept-charset accept-encoding accept-language access-control-allow-origin
  cache-control connection content-encoding content-language content-length
  content-location content-md5 content-range content-type date etag expect
  expires from host if-match if-modified-since if-none-match if-range
  if-unmodified-since keep-alive last-modified location max-forwards pragma
  range referer retry-after server trailer transfer-encoding upgrade user-agent
  vary via warning x-forwarded-for x-requested-with x-powered-by
].map(&:downcase).freeze

Class Method Summary collapse

Class Method Details

.apply_masking_to_sensitive_headers(headers, mask_sensitive_headers, headers_to_unmask) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/apimatic-core/utilities/logger_helper.rb', line 64

def self.apply_masking_to_sensitive_headers(headers, mask_sensitive_headers, headers_to_unmask)
  return headers unless mask_sensitive_headers
  return headers if headers.nil?

  masked_headers = {}
  headers.each do |key, val|
    masked_headers[key] = mask_if_sensitive_header(key, val, headers_to_unmask)
  end

  masked_headers
end

.exclude_headers(headers, headers_to_exclude) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/apimatic-core/utilities/logger_helper.rb', line 52

def self.exclude_headers(headers, headers_to_exclude)
  excluded_headers = {}

  headers.each do |key, val|
    excluded_headers[key] = val unless headers_to_exclude.any? do |excluded_name|
      excluded_name.downcase == key.downcase
    end
  end

  excluded_headers
end

.extract_headers_to_log(http_logging_config, mask_sensitive_headers, headers) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/apimatic-core/utilities/logger_helper.rb', line 26

def self.extract_headers_to_log(http_logging_config, mask_sensitive_headers, headers)
  return headers if headers.nil?

  filtered_headers = if http_logging_config.headers_to_include.any?
                       include_headers(headers, http_logging_config.headers_to_include)
                     elsif http_logging_config.headers_to_exclude.any?
                       exclude_headers(headers, http_logging_config.headers_to_exclude)
                     else
                       headers
                     end

  apply_masking_to_sensitive_headers(filtered_headers, mask_sensitive_headers,
                                     http_logging_config.headers_to_unmask)
end

.get_content_length(headers) ⇒ Object



20
21
22
23
24
# File 'lib/apimatic-core/utilities/logger_helper.rb', line 20

def self.get_content_length(headers)
  return '' if headers.nil?

  headers.find { |key, _| key.downcase == CONTENT_LENGTH_HEADER }&.last || ''
end

.get_content_type(headers) ⇒ Object



14
15
16
17
18
# File 'lib/apimatic-core/utilities/logger_helper.rb', line 14

def self.get_content_type(headers)
  return '' if headers.nil?

  headers.find { |key, _| key.downcase == CONTENT_TYPE_HEADER }&.last || ''
end

.include_headers(headers, headers_to_include) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/apimatic-core/utilities/logger_helper.rb', line 41

def self.include_headers(headers, headers_to_include)
  included_headers = {}

  headers_to_include.each do |name|
    key = headers.keys.find { |header_key| header_key.downcase == name.downcase }
    included_headers[key] = headers[key] if headers[key]
  end

  included_headers
end

.mask_if_sensitive_header(name, value, headers_to_unmask) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/apimatic-core/utilities/logger_helper.rb', line 76

def self.mask_if_sensitive_header(name, value, headers_to_unmask)
  headers_to_unmask ||= []
  headers_to_unmask = headers_to_unmask.map(&:downcase)
  name_downcase = name.downcase

  NON_SENSITIVE_HEADERS.include?(name_downcase) || headers_to_unmask.include?(name_downcase) ?
    value : REDACTED
end