Module: TCellAgent::DLP
- Defined in:
- lib/tcell_agent/rails/dlp.rb,
lib/tcell_agent/rails/dlp/process_request.rb
Class Method Summary collapse
- ._handle_dataexpsure_forms(request) ⇒ Object
- ._handle_dataexpsure_headers(request) ⇒ Object
- ._handler_dataexposure_cookies(request) ⇒ Object
- .for_params(request, &block) ⇒ Object
- .get_dlp_logger ⇒ Object
- .handle_request_dlp_parameters(request) ⇒ Object
- .instrument_find_by_sql(results) ⇒ Object
- .instrument_pluck(results, column_names, model) ⇒ Object
- .loop_params_hash(method, param_hash, &block) ⇒ Object
Class Method Details
._handle_dataexpsure_forms(request) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/tcell_agent/rails/dlp/process_request.rb', line 36 def self._handle_dataexpsure_forms(request) dataex_policy = TCellAgent.policy(TCellAgent::PolicyTypes::DATALOSS) tcell_context = request.env[TCellAgent::Instrumentation::TCELL_ID] return unless tcell_context && dataex_policy && dataex_policy.actions_for_form_parameter? for_params(request) do |_method, param_name, param_value| actions = dataex_policy.get_actions_for_form_parameter(param_name, tcell_context.route_id) if actions actions.each do |action| tcell_context.add_filter_for_request_parameter(param_value, action, param_name) end end end end |
._handle_dataexpsure_headers(request) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/tcell_agent/rails/dlp/process_request.rb', line 50 def self._handle_dataexpsure_headers(request) dataex_policy = TCellAgent.policy(TCellAgent::PolicyTypes::DATALOSS) tcell_context = request.env[TCellAgent::Instrumentation::TCELL_ID] return unless tcell_context && dataex_policy && dataex_policy.actions_for_headers? headers = request.env.select { |k, _v| k.start_with? 'HTTP_' } headers.each do |header_name, header_value| header_name = header_name.sub(/^HTTP_/, '').tr('_', '-') actions = dataex_policy.get_actions_for_header(header_name) next unless actions actions.each do |action| tcell_context.add_filter_for_header_value(header_value, action, header_name) end end end |
._handler_dataexposure_cookies(request) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/tcell_agent/rails/dlp/process_request.rb', line 65 def self.(request) dataex_policy = TCellAgent.policy(TCellAgent::PolicyTypes::DATALOSS) tcell_context = request.env[TCellAgent::Instrumentation::TCELL_ID] return unless tcell_context && dataex_policy && dataex_policy. request..each do |, | actions = dataex_policy.() next unless actions actions.each do |action| tcell_context.(, action, ) end end end |
.for_params(request, &block) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/tcell_agent/rails/dlp/process_request.rb', line 29 def self.for_params(request, &block) get_params = request.GET loop_params_hash('get', get_params, &block) if get_params post_params = request.POST loop_params_hash('post', post_params, &block) if post_params end |
.get_dlp_logger ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/tcell_agent/rails/dlp.rb', line 29 def self.get_dlp_logger unless defined?(@rails_dlp_logger) @rails_dlp_logger = TCellAgent::ModuleLogger.new( TCellAgent.logger, name ) end @rails_dlp_logger end |
.handle_request_dlp_parameters(request) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/tcell_agent/rails/dlp/process_request.rb', line 3 def self.handle_request_dlp_parameters(request) TCellAgent::Instrumentation.safe_block('Handling Dataexposure (request forms)') do _handle_dataexpsure_forms(request) end TCellAgent::Instrumentation.safe_block('Handling Dataexposure (request headers)') do _handle_dataexpsure_headers(request) end TCellAgent::Instrumentation.safe_block('Handling Dataexposure (request cookies)') do (request) end end |
.instrument_find_by_sql(results) ⇒ Object
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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/tcell_agent/rails/dlp.rb', line 142 def self.instrument_find_by_sql(results) return if results.empty? if TCellAgent.configuration.should_instrument? && TCellAgent.configuration.should_intercept_requests? dlp_policy = TCellAgent.policy(TCellAgent::PolicyTypes::DATALOSS) request_env = TCellAgent::Instrumentation::Rails::Middleware::ContextMiddleware::THREADS.fetch(Thread.current.object_id, {}) tcell_context = request_env[TCellAgent::Instrumentation::TCELL_ID] if tcell_context tcell_context.database_result_sizes.push(results.size) if dlp_policy && dlp_policy.enabled first_record = results.first database_name = first_record.class.connection_config.fetch(:database, '*').split('/').last model = first_record.class column_names = model.columns.map(&:name) table_name = model.table_name if dlp_policy.database_discovery_enabled TCellAgent.discover_database_fields( tcell_context.route_id, database_name, '*', table_name, column_names ) end if results.size > TCellAgent.configuration.max_data_ex_db_records_per_request get_dlp_logger.warn("Route (#{tcell_context.route_id}) retrieved too many records") end column_name_to_rules = column_names.each_with_object({}) do |column_name, memo| rules = dlp_policy.get_actions_for_table( database_name, '*', table_name, column_name, tcell_context.route_id ) memo[column_name] = rules if rules end return if column_name_to_rules.empty? results[0...TCellAgent.configuration.max_data_ex_db_records_per_request].each do |record| column_name_to_rules.each do |column_name, rules| next unless rules rules.each do |rule| tcell_context.add_response_db_filter( record[column_name.to_sym], rule, database_name, '*', table_name, column_name ) end end end end end end end |
.instrument_pluck(results, column_names, model) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/tcell_agent/rails/dlp.rb', line 39 def self.instrument_pluck(results, column_names, model) return if results.empty? if TCellAgent.configuration.should_instrument? && TCellAgent.configuration.should_intercept_requests? dlp_policy = TCellAgent.policy(TCellAgent::PolicyTypes::DATALOSS) request_env = TCellAgent::Instrumentation::Rails::Middleware::ContextMiddleware::THREADS.fetch(Thread.current.object_id, {}) tcell_context = request_env[TCellAgent::Instrumentation::TCELL_ID] if tcell_context tcell_context.database_result_sizes.push(results.size) if dlp_policy && dlp_policy.enabled database_name = model.connection_config.fetch( :database, '*' ).split('/').last table_name = model.table_name column_names = if column_names.size.zero? model.columns.map(&:name) else column_names.map(&:to_s) end if dlp_policy.database_discovery_enabled TCellAgent.discover_database_fields( tcell_context.route_id, database_name, '*', table_name, column_names ) end normalized_column_names = {} column_name_to_rules = column_names.each_with_object({}) do |namespaced_column_name, memo| namespace = nil column_name = namespaced_column_name if column_name =~ /\./ namespace, column_name = column_name.split(/\./) end normalized_column_names[namespaced_column_name] = column_name next unless column_name && (!namespace || namespace == table_name) rules = dlp_policy.get_actions_for_table( database_name, '*', table_name, column_name, tcell_context.route_id ) memo[namespaced_column_name] = rules if rules end if results.size > TCellAgent.configuration.max_data_ex_db_records_per_request get_dlp_logger.warn("Route (#{tcell_context.route_id}) retrieved too many records") end return if column_name_to_rules.empty? # column_names.size == 1 # results => [1, 2, 3, 4] # column_names.size > 1 # results => [[1, 'email'], [2, 'email']] if column_names.size == 1 results[0...TCellAgent.configuration.max_data_ex_db_records_per_request].each do |result| namespaced_column_name = column_names[0] rules = column_name_to_rules.fetch(namespaced_column_name, []) rules.each do |rule| tcell_context.add_response_db_filter( result, rule, database_name, '*', table_name, normalized_column_names[namespaced_column_name] ) end end else results[0...TCellAgent.configuration.max_data_ex_db_records_per_request].each do |result| result.each_with_index do |val, index| namespaced_column_name = column_names[index] rules = column_name_to_rules.fetch(namespaced_column_name, []) rules.each do |rule| tcell_context.add_response_db_filter( val, rule, database_name, '*', table_name, normalized_column_names[namespaced_column_name] ) end end end end end end end end |
.loop_params_hash(method, param_hash, &block) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/tcell_agent/rails/dlp/process_request.rb', line 17 def self.loop_params_hash(method, param_hash, &block) param_hash.each do |param_name, param_value| if param_value && param_value.is_a?(Hash) loop_params_hash(method, param_value, &block) elsif !param_value || !param_value.instance_of?(String) || param_value == '' next else block.call(method, param_name, param_value) end end end |