Module: Contrast::Config::YamlFile
- Defined in:
- lib/contrast/config/yaml_file.rb
Overview
Helper module with methods to find active configuration file used by the agent or create it.\ This file is used before the agent start so methods added here must not be dependent on Agent instrumentation.
Constant Summary collapse
- CONFIG_FILE_NAME =
'contrast_security'
- CONTRAST_ENV_MARKER =
'CONTRAST__'
- EXT =
rubocop:disable Security/Object/Freeze
{ yml: 'yml', yaml: 'yaml' }.freeze
- POSSIBLE_TARGET_PATHS =
%w[ ./ ./config/ /etc/contrast/ruby/ /etc/contrast/ /etc/ ].freeze
- HEADER =
rubocop:disable Security/Object/Freeze
"# +-------------------------------------------------------------------------+\n" \ "# This Contrast Security configuration is Auto-generated by rake task.\n" \ "# To List all available rake task use 'rake -T'. \n" \ "#\n" \ "# Please enter valid api information, for the Ruby Agent to be able to\n" \ "# connect to Contrast UI. You can validate your config file by running: \n" \ "# 'bundle exec rake contrast:config:validate' \n" \ "#\n" \ "# To find your organization keys please follow this documentation:\n" \ "# https://docs.contrastsecurity.com/en/find-the-agent-keys.html\n" \ "# +-------------------------------------------------------------------------+\n"
- FOOTER =
"\n# For more information visit the full Ruby agent configuration guide:\n" \ '# https://docs.contrastsecurity.com/en/ruby-configuration.html'
- COMMENT =
"#agent:\n" \ "# logger:\n" \ "# level: WARN\n" \ "# path: contrast_agent.log\n" \ "#server:\n" \ "# name: Server name\n" \ "#application:\n" \ "# name: Application name\n" \ "# code: Application name\n" \ "# group: Application group\n" \ "# session_metadata: Application metadata used for creation of Session ID\n" \ "# version: Application version\n"
- DEFAULT_CONFIG =
{ 'api' => { 'url' => 'https://app.contrastsecurity.com', 'api_key' => 'contrast_user', 'service_key' => 'demo', 'user_name' => 'demo' } }.freeze
Class Method Summary collapse
-
.create ⇒ Object
Create new config file to the default destination.
-
.created? ⇒ Boolean
Checks all accessible by the agent path for active configuration created.
- .env_config_set? ⇒ Boolean
- .execution_directory ⇒ Object
- .file_name ⇒ Object
-
.find! ⇒ String
Finds active configuration used.
- .target_path ⇒ Object
Class Method Details
.create ⇒ Object
Create new config file to the default destination.
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 |
# File 'lib/contrast/config/yaml_file.rb', line 92 def create # rubocop:disable Rails/Output return puts("\u{02C3} Contrast configuration set by ENV variables.") if env_config_set? puts("\u{1F48E} Generating: Contrast Configuration file.") if Contrast::Config::YamlFile.created? puts("\u{2705} Configuration file already exists: #{ Contrast::Config::YamlFile.find! }") else File.open(Contrast::Config::YamlFile.target_path, 'w') do |file| file.write(HEADER) file.write(YAML.dump(DEFAULT_CONFIG).gsub('---', ' ')) file.write(COMMENT) file.write(FOOTER) file.close end puts("\u{2728} Created! path #{ Contrast::Config::YamlFile.target_path }\n") puts("\nOpen the file and enter your Contrast Security api keys or set them via environment variables.\n") puts('Visit our documentation for more details: ' \ 'https://docs.contrastsecurity.com/en/ruby-configuration.html') end rescue StandardError => e puts("\u{2757} WARNING configuration could not be created due to error: '#{ e }'. " \ "Try created in manually by running 'bundle exec rake contrast:config:create'.") # rubocop:enable Rails/Output end |
.created? ⇒ Boolean
Checks all accessible by the agent path for active configuration created.
67 68 69 70 71 72 |
# File 'lib/contrast/config/yaml_file.rb', line 67 def created? return true if File.exist?(Contrast::Config::YamlFile.target_path) return true unless find!.empty? false end |
.env_config_set? ⇒ Boolean
131 132 133 |
# File 'lib/contrast/config/yaml_file.rb', line 131 def env_config_set? ENV.keys&.select { |config| config.include?(CONTRAST_ENV_MARKER) }&.any? end |
.execution_directory ⇒ Object
123 124 125 |
# File 'lib/contrast/config/yaml_file.rb', line 123 def execution_directory Dir.pwd end |
.file_name ⇒ Object
127 128 129 |
# File 'lib/contrast/config/yaml_file.rb', line 127 def file_name ENV['CONTRAST_YAML_FILE_TEST_CREATE_CONFIG_FILE_NAME_VALUE'] || CONFIG_FILE_NAME end |
.find! ⇒ String
Finds active configuration used.
77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/contrast/config/yaml_file.rb', line 77 def find! found = '' paths = POSSIBLE_TARGET_PATHS.dup paths << ENV['CONTRAST_CONFIG_PATH'] if ENV['CONTRAST_CONFIG_PATH'] paths.each do |path| EXT.each_value do |extension| effective_config = "#{ path }#{ file_name }.#{ extension }" found = effective_config if File.exist?(effective_config) end end found end |