Module: Contrast::Config::Validate

Included in:
Contrast::Config
Defined in:
lib/contrast/config/validate.rb

Overview

Helper module with methods to validate the existing yaml file.

Constant Summary collapse

SKIP_LOG =

rubocop:disable Rails/Output

%w[service_key api_key].cs__freeze
REQUIRED =
%i[url api_key service_key user_name].cs__freeze
PASS =
" \u{2705}  "
FAIL =
" \u{274C}  "
REQUIRED_HEADERS =
%i[server_type server_name server_path app_language app_name].cs__freeze

Instance Method Summary collapse

Instance Method Details

#decode_header(hash, key) ⇒ Object



131
132
133
134
135
136
# File 'lib/contrast/config/validate.rb', line 131

def decode_header hash, key
  value = hash[key]
  decoded_header = Base64.strict_decode64(value)
  puts("  #{ key }::#{ key == :app_language ? value : decoded_header }")
  value
end

#init_reporterObject



91
92
93
# File 'lib/contrast/config/validate.rb', line 91

def init_reporter
  Contrast::Agent::Reporter.new
end

#mask_keys(hash, key) ⇒ Object



124
125
126
127
128
129
# File 'lib/contrast/config/validate.rb', line 124

def mask_keys hash, key
  value = hash[key]
  redacted_value = Contrast::Configuration::REDACTED if SKIP_LOG.include?(key.to_s)
  puts("  #{ key }::#{ redacted_value || value }") unless value.is_a?(Contrast::Config::BaseConfiguration)
  value
end

#test_connection(reporter) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/contrast/config/validate.rb', line 109

def test_connection reporter
  puts("  Connection failed #{ FAIL }") unless reporter
  connection = reporter.client.send(:reporting_connection)
  abort("Failed to Initialize Connection please check error logs for details #{ FAIL } ") unless connection
  abort('Failed to Start Client please check error logs for details') unless reporter.client.startup
  last_response = reporter.client.response_handler.last_response_code
  if last_response.include?('40')
    puts("  Last response code: #{ last_response  } #{ FAIL }")
    abort("Failed to Initialize Connection please check error logs for details #{ FAIL } ")
  elsif connection
    puts("  Last response code: #{ last_response  } #{ PASS }")
    puts("  Connection successful #{ PASS }") if connection
  end
end

#validate_audit(config) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/contrast/config/validate.rb', line 82

def validate_audit config
  puts("  Request Audit Enabled: #{ config.enable }")
  return unless config.enable

  config.cs__class::CONFIG_VALUES.each do |value_name|
    puts("  #{ value_name }::#{ config.send(value_name.to_sym) }")
  end
end

#validate_cert(config) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/contrast/config/validate.rb', line 67

def validate_cert config
  puts("  Certification Enabled: #{ config.enable }")
  return unless config.enable

  mark = config.ca_file.nil? ? FAIL : PASS
  puts("  CA File: #{ config.ca_file } #{ mark }")
  abort('CA file path not provided') unless config.ca_file
  mark = config.cert_file.nil? ? FAIL : PASS
  puts("  Cert File: #{ config.cert_file } #{ mark }")
  abort('Cert file path not provided') unless config.cert_file
  mark = config.key_file.nil? ? FAIL : PASS
  puts("  Key File: #{ config.key_file } #{ mark }")
  abort('Key file path not provided') unless config.key_file
end

#validate_configObject



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
# File 'lib/contrast/config/validate.rb', line 32

def validate_config
  config = Contrast::Configuration.new
  abort("Unable to Build Config #{ FAIL }") unless config
  missing = []

  api_hash = config.api.to_contrast_hash

  api_hash.each_key do |key|
    value = mask_keys(api_hash, key)
    if value.is_a?(Contrast::Config::ApiProxyConfiguration)
      Contrast::Config.validate_proxy(value)
    elsif value.is_a?(Contrast::Config::CertificationConfiguration)
      Contrast::Config.validate_cert(value)
      next
    elsif value.is_a?(Contrast::Config::RequestAuditConfiguration)
      Contrast::Config.validate_audit(value)
      next
    elsif value.nil? && REQUIRED.include?(key.to_sym)
      missing << key
    end
  end
  return if missing.empty?

  abort("Validation failed: Missing required API configuration values: #{ missing.join(', ') } #{ FAIL }")
end

#validate_fileObject



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/contrast/config/validate.rb', line 20

def validate_file
  puts("\u{1F9EA} Validating Agent Configuration...\n")
  Contrast::Config.validate_config
  puts('done...')
  puts("Validating Contrast Reporter Headers...\n")
  reporter = Contrast::Config.validate_headers
  puts('done...')
  puts("Testing Reporter Client Connection...\n")
  Contrast::Config.test_connection(reporter) if reporter
  puts('Validation complete')
end

#validate_headersObject



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/contrast/config/validate.rb', line 95

def validate_headers
  missing = []
  reporter = init_reporter
  reporter_headers = reporter.client.headers.to_contrast_hash
  REQUIRED_HEADERS.each do |key|
    value = decode_header(reporter_headers, key)
    missing << key if value.nil?
  end
  unless missing.empty?
    abort("Validation failed: Missing required header values: #{ missing.join(', ') } #{ FAIL }")
  end
  reporter
end

#validate_proxy(config) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/contrast/config/validate.rb', line 58

def validate_proxy config
  puts("  Proxy Enabled: #{ config.enable }")
  return unless config.enable

  mark = config.url.nil? ? FAIL : PASS
  puts("  Proxy URL: #{ config.url } #{ mark }")
  abort('Proxy Enabled but no Proxy URL given') unless config.url
end