rails_surrogate_key_logging 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 51ed41b166599e90b95c595ecbd87fb3812dbeca4087dec61f4a6d4efc03793f
4
- data.tar.gz: 5389b994867bac68b6edaeaf90e2af777af74634bfea2d505378bb8f5ec55432
3
+ metadata.gz: 2e6e90f9c083df089f9b7cee7d45bddb3e27cc8b6f33ca926810a5006c374f40
4
+ data.tar.gz: 942657d9c5b766de9b879071a79e1bd7a5928c9d371a7a015174499875a52238
5
5
  SHA512:
6
- metadata.gz: 799c109ed078447639c5c06f2f6f20ba9d27d5fa1674b61cfc9f8ac96f0d033fe9c01b8472f03676e2852752ae42ca108b2b5b3f13093f22d03d5ae222b1eb4f
7
- data.tar.gz: 2c06278e7a1744f49bd90e88682b82d69b40f502a5af20b69fb0bc1e264110996806a7a340cdd2ab63c5e1cbbbb9a329ff1483273ff8d46fc2c75cc1c6e4b502
6
+ metadata.gz: ec95153a37880c2f9421595e32c840bf0ac292785f83f2cd59f0358767cc673e53918dbde9e7999511282ccd5db8e0c787082d0b4de5915e2d8d0deb8552ebbe
7
+ data.tar.gz: 7620c957e9a36efef906e4bfa8a7a68a467fff0201d2149966b33cae0da6e2cd6e67c753eff3df4dd8e2d8ccee939fb2fea5bfb3e119e44449b7b9259f41a682
data/README.md CHANGED
@@ -1 +1,134 @@
1
1
  # rails-surrogate-key-logging
2
+
3
+ This gem enhances and uses Rails' built-in `ParameterFilter` to add "Surrogate Key" logging.
4
+
5
+ ## Installation
6
+
7
+ - Add `gem :rails_surrogate_key_logging` to your Gemfile.
8
+ - Run `bin/bundle install`
9
+ - Add `include SurrogateKeyLogging::ActionController::Params` to your `ApplicationController`
10
+ - Add `include SurrogateKeyLogging::ActiveRecord::Attributes` to your `ApplicationRecord`
11
+
12
+
13
+
14
+ ## Configuration
15
+
16
+ In a new application initializer (`config/initializers/surrogate_key_logging.rb`) or in your `config/environments/*.rb`, use the following block:
17
+
18
+ ```ruby
19
+ SurrogateKeyLogging.configure do |config|
20
+ config.key = value
21
+ end
22
+ ```
23
+
24
+ ### Config
25
+
26
+ Key | Type | Default | Description
27
+ ---|---|---|---
28
+ `enabled` | Boolean | `Rails.env.production?` | Whether surrogate logging is injected into Rails.
29
+ `debug` | Boolean | `false` | Whether to log a statement showing that a surrogate replacement happened and what the mapping from surrogate to value, and logs from the key store (Such as queries made by ActiveRecord to it's Surrogate model).
30
+ `key_prefix` | String | `''` | This string will be prepended to generated surrogates. Can make it easier to identify a surrogate in logs.
31
+ `key_for` | Proc \| Lambda \| `responds_to?(:call)` | `-> (value) { "#{config.key_prefix}#{SecureRandom.uuid}" }` | The method used to generate a surrogate for a given value. While the `value` is supplied to the method, it is generally considered insecure for the surrogate to be derivable from it's value.
32
+ `cache` | Boolean | `true` | Should the key mananger maintain an in-memory cache of value -> surrogate map that have been used. When in a server context, this cache will last for the lifetime of a single request. The cache can also be manually busted at any time by calling `SurrogateKeyLogging.reset!`.
33
+ `cache_key_for` | Proc \| Lambda \| `responds_to?(:call)` | `-> (value) { value }` | The method used to create the keys used in the cache. Typically this should be left to the default unless you expect to make many surrogates for very large values.
34
+ `key_ttl` | Integer | `90.days` | Used by `bin/rails skl:clear:stale` to delete old surrogates.
35
+ `key_store` | Symbol | None | The key store to use. See [Key Stores](#key-stores).
36
+
37
+
38
+
39
+ ## Key Stores
40
+
41
+ Key Store | Config Value
42
+ ---|---
43
+ [ActiveRecord](#active-record) | `:active_record`
44
+
45
+ ### Active Record
46
+
47
+ This will use a `SurrogateKeyLogging::Surrogate` model to manage surrogates. This will require adding `surrogate_key_logging_#{Rails.env}` to your application's `config/database.yml` See [Example](#example-database-yml). After configuring your `config/database.yml` you will need to run `bin/rails skl:key_store:active_record:db:create` and `bin/rails skl:key_store:active_record:db:migrate`.
48
+
49
+ #### Example database.yml
50
+ ```yml
51
+ default: &default
52
+ adapter: mysql2
53
+ username: <%= Rails.application.credentials.database[:username] %>
54
+ password: <%= Rails.application.credentials.database[:password] %>
55
+ host: 127.0.0.1
56
+ port: 3306
57
+ database: myapp_<%= Rails.env %>
58
+ prepared_statements: true
59
+
60
+ surrogate_key_logging_default: &surrogate_key_logging_default
61
+ <<: *default
62
+ database: surrogate_keys_<%= Rails.env %>
63
+
64
+
65
+
66
+ development:
67
+ <<: *default
68
+
69
+ test:
70
+ <<: *default
71
+
72
+ production:
73
+ <<: *default
74
+
75
+
76
+
77
+ surrogate_key_logging_development:
78
+ <<: *surrogate_key_logging_default
79
+
80
+ surrogate_key_logging_test:
81
+ <<: *surrogate_key_logging_default
82
+
83
+ surrogate_key_logging_production:
84
+ <<: *surrogate_key_logging_default
85
+ ```
86
+
87
+
88
+
89
+ ## Usage
90
+
91
+ ### Controllers
92
+
93
+ In any controller including `SurrogateKeyLogging::ActionController::Params` you may use the `surrogate_params(*params, action: '*')` method. This method may be used multiple times. Pass the `action` argument to limit those `params` to only that `action` or omit it to apply those `params` to ALL actions in that controller.
94
+
95
+ #### Params format
96
+ Param | Examples | Output
97
+ ---|---|---
98
+ `:foo` | `{ foo: 'bar1', another: {foo: 'baz1'}, foobar: 'barbaz' }` | `{foo: SURROGATE, another: { foo: SURROGATE }, foobar: 'barbaz' }`
99
+ `'another.foo'` | `{ foo: 'bar1', another: { foo: 'baz1' }, foobar: { another: { foo: 'barbaz' } } }` | `{ foo: 'bar1', another: { foo: SURROGATE }, foobar: { another: { foo: SURROGATE } } }`
100
+ `'another[foo]'` | `{ foo: 'bar1', another: { foo: 'baz1' }, foobar: { another: { foo: 'barbaz' } } }` | `{ foo: 'bar1', another: { foo: SURROGATE }, foobar: { another: { foo: 'barbaz' } } }`
101
+
102
+ #### Example Controller
103
+ ```ruby
104
+ class WidgetsController < ApplicationController
105
+ surrogate_params :name
106
+ surrogate_params :owner, action: :search
107
+
108
+ def name
109
+ ...
110
+ end
111
+
112
+ def search
113
+ ...
114
+ end
115
+ end
116
+ ```
117
+
118
+ In this example the `name` parameter will be surrogated in all requests to this controller, and the `owner` parameter will surrogated only in requests to the `search` action.
119
+
120
+
121
+
122
+ ### Models
123
+
124
+ In any controller including `SurrogateKeyLogging::ActiveRecord::Attributes` you may use `surrogate_parent_names(*names)` and `surrogate_attributes(*attrs)`. All permutations of parent names to attributes will be used to create filters. By default `surrogate_parent_names` is initialized with the singular and plural names of the model.
125
+
126
+ #### Example Model
127
+ ```ruby
128
+ class Widget < ApplicationRecord
129
+ surrogate_parent_names :things
130
+ surrogate_attributes :name, :owner
131
+ end
132
+ ```
133
+
134
+ In this example, the following filters will be used to look for attributes to be surrogated: `widget.name`, `widget[name]`, `[widget][name]`, `widgets.name`, `widgets[name]`, `[widgets][name]`, `things.name`, `things[name]`, `[things][name]`
@@ -18,14 +18,14 @@ module SurrogateKeyLogging
18
18
  end
19
19
 
20
20
  rake_tasks do
21
- load 'tasks/surrogate_key_logging.rake'
22
- load 'tasks/key_store/active_record.rake'
21
+ # load 'tasks/surrogate_key_logging.rake'
22
+ # load 'tasks/key_store/active_record.rake'
23
23
  end
24
24
 
25
25
  initializer 'surrogate_key_logging.config' do |app|
26
26
  SurrogateKeyLogging.configure do |config|
27
27
  config.enabled = Rails.env.production? unless config.key?(:enabled)
28
- config.debug = !Rails.env.production? unless config.key?(:debug)
28
+ config.debug = false unless config.key?(:debug)
29
29
  config.key_prefix = '' unless config.key?(:key_prefix)
30
30
  config.key_for ||= -> (value) { "#{config.key_prefix}#{SecureRandom.uuid}" }
31
31
  config.cache = true unless config.key?(:cache)
@@ -15,6 +15,7 @@ module SurrogateKeyLogging
15
15
  end
16
16
 
17
17
  def get(value)
18
+ return if value.blank?
18
19
  if should_cache
19
20
  get_cached(value)
20
21
  else
@@ -36,7 +37,7 @@ module SurrogateKeyLogging
36
37
 
37
38
  def call(_key, value, _parents = [], _original_params = nil)
38
39
  surrogate = get(value)
39
- # Rails.logger.tagged('SurrogateKeyLogging') { Rails.logger.info "Surrogate: `#{surrogate}`, value: `#{value}`" } if SurrogateKeyLogging.config.debug
40
+ Rails.logger.tagged('SurrogateKeyLogging') { Rails.logger.info "Surrogate: `#{surrogate}`, value: `#{value}`" } if SurrogateKeyLogging.config.debug
40
41
  surrogate
41
42
  end
42
43
 
@@ -4,7 +4,7 @@ module SurrogateKeyLogging
4
4
 
5
5
  module Version
6
6
  MAJOR = 0
7
- MINOR = 2
7
+ MINOR = 3
8
8
  PATCH = 0
9
9
 
10
10
  end
@@ -64,6 +64,10 @@ module SurrogateKeyLogging
64
64
  parameter_filter.filter params
65
65
  end
66
66
 
67
+ def surrogate_for(value)
68
+ key_manager.get(value)
69
+ end
70
+
67
71
  def add_param_to_filter(attr, *parents)
68
72
  if parents.empty?
69
73
  surrogate_attributes attr.to_s
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_surrogate_key_logging
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Taylor Yelverton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-01-24 00:00:00.000000000 Z
11
+ date: 2023-02-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack