Class: Jfrog::Saas::Log::SchemaValidator

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/jfrog/saas/log/collector.rb

Instance Method Summary collapse

Instance Method Details

#jpd_whitelisted(jpd_url) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/jfrog/saas/log/collector.rb', line 127

def jpd_whitelisted(jpd_url)
  whitelisted_domains_yaml = YAML.load_file(File.open(File.join(File.dirname(__FILE__), 'whitelisted_domains.yaml')))
  MessageUtils.instance.put_message(MessageUtils::WHITELIST_FILE_DETAIL, { "param1": File.join(File.dirname(__FILE__), 'whitelisted_domains.yaml').to_s,
                                                                                   "#{MessageUtils::LOG_LEVEL}": CommonUtils::LOG_INFO,
                                                                                   "#{MessageUtils::SOLUTION}": MessageUtils::SOLUTION_OVERRIDE_INIT })
  whitelisted_domains = whitelisted_domains_yaml['whitelist']['domains']
  uri = Addressable::URI.parse(jpd_url)
  if whitelisted_domains.include? uri.domain
    true
  else
    false
  end
end

#validate(config_file) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/jfrog/saas/log/collector.rb', line 141

def validate(config_file)
  config_valid = true
  config_file_yaml = YAML.load_file(config_file)
  config_in_json = config_file_yaml.to_json
  config_schema_file = File.open((File.join(File.dirname(__FILE__), 'config.template.schema.json')))
  MessageUtils.instance.put_message(MessageUtils::VALIDATION_SCHEMA_FILE_DETAIL, { "param1": (File.join(File.dirname(__FILE__), 'config.template.schema.json')).to_s,
                                                                                          "#{MessageUtils::LOG_LEVEL}": CommonUtils::LOG_INFO,
                                                                                          "#{MessageUtils::SOLUTION}": MessageUtils::SOLUTION_OVERRIDE_INIT })

  config_schema = JSON.parse(config_schema_file.read)
  if !config_schema.nil? && !config_in_json.nil?
    begin
      JSON::Validator.validate!(config_schema, config_in_json)
      jpd_url = (config_file_yaml['connection']['jpd_url']).to_s.strip
      if jpd_whitelisted(jpd_url)
        config_valid = true
      else
        MessageUtils.instance.put_message(MessageUtils::CONFIG_FILE_VALIDATION_FAILED_DETAILS, { "param1": "#{config_file.to_s} contains JPD URL which is not permitted",
                                                                                                 "#{MessageUtils::LOG_LEVEL}": CommonUtils::LOG_ERROR,
                                                                                                 "#{MessageUtils::SOLUTION}": MessageUtils::SOLUTION_OVERRIDE_TERMINATE })
        config_valid = false
      end
    rescue JSON::Schema::ValidationError
      MessageUtils.instance.put_message(MessageUtils::CONFIG_FILE_VALIDATION_FAILED_DETAILS, { "param1": $ERROR_INFO.message,
                                                                                               "#{MessageUtils::LOG_LEVEL}": CommonUtils::LOG_ERROR,
                                                                                               "#{MessageUtils::SOLUTION}": MessageUtils::SOLUTION_OVERRIDE_TERMINATE })
    end
  end
  config_valid
end