cache_bro 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6287878eefcb94a718dfd1a6d1540355185e989bdc5bc21686933c5a09331f45
4
- data.tar.gz: d9ab530ca70ac53107bd557da234b55337824a6523a2596e51f7fd4fc7512e9d
3
+ metadata.gz: 4819270ff06afe2e6fcab2a2ab501d7c4c3e518cf96800fc130327555e466f91
4
+ data.tar.gz: c796ed6be81fae535dcd14a187344df483dd96768d40b14fb06779af8fce29de
5
5
  SHA512:
6
- metadata.gz: fac35f3a49ba16a2b2e87afe7c222356ee3904ff5dad45a89a5352913d96644ce23e486ff37c1d15f67f53d31fa4a68f349066ce9f8a2ffe1dc13d5c828b1e8f
7
- data.tar.gz: 60fd4404a3f9e3e1d0494f14550cbd3961e0b71fd56d291fcc013be8873b936e72cc8248f4dcc39dea7b88405714e5018740cf3223ea532d1cc454c7f99da62c
6
+ metadata.gz: 20639e0f1deda7181dd780da262feb1df435dc40e15db470587a8134fdff48b03c450421a661a578eb32bd4fc0bc826ca5c69a078658d9330440982e8a1b48fc
7
+ data.tar.gz: 2970cff41d5166738d4ee632e4d6bbf090a9d8771654a553b26832176c49bab8c06e08d80a5b537904557b292eaa38c4e544ce3530cd966922dc7fa039d013d1
data/README.md CHANGED
@@ -28,7 +28,7 @@ bin/rails generate cache_bro:install
28
28
  bin/rails db:migrate
29
29
  ```
30
30
 
31
- The installer sets up BroHub if it isn't yet, adds `config/initializers/cache_bro.rb` and copies the migration.
31
+ The installer sets up BroHub if it isn't yet, adds `config/initializers/cache_bro.rb`. The audit table is BroHub's shared one, its migration is copied by the same installer.
32
32
  CacheBro then lives at `<hub>/cache_bro`.
33
33
 
34
34
  Give your cache store a **namespace**:
@@ -59,11 +59,18 @@ Higher includes lower: `flush` includes `purge` and `delete`, `purge` includes `
59
59
  `view_keys`. `view_values` and `audit` stand alone on purpose: values can hold personal data, and the people who
60
60
  clean the cache shouldn't automatically read it.
61
61
 
62
+ ## Upgrading from 0.1.x
63
+
64
+ CacheBro 0.2 needs **bro_hub 0.2** and writes to BroHub's shared audit table instead of `cache_bro_audit_events`. Nothing is
65
+ copied over: `bundle update cache_bro`, `bin/rails g bro_hub:install`, `bin/rails db:migrate`, then drop the old table
66
+ (`drop_table :cache_bro_audit_events` in a migration). CacheBro no longer ships migrations of its own.
67
+
62
68
  ## Audit
63
69
 
64
- Every reveal, delete, purge and flush is recorded in `cache_bro_audit_events`: who, what (the key or the pattern,
65
- never the value), how many keys, which store and environment, when, and the reason if one was given. Entries can't be
66
- changed or deleted through the model.
70
+ Every reveal, delete, purge and flush is recorded in BroHub's shared audit table (`bro_hub_audit_events`, with
71
+ `CacheBro::AuditEvent` as CacheBro's slice of it): who, what (the key or the pattern, never the value), how many keys,
72
+ which store and environment, when, and the reason if one was given. Entries can't be changed or deleted through the
73
+ model. They also show up in BroHub's combined audit log for people with `can_audit`.
67
74
 
68
75
  - Purges and flushes also keep a sample of the keys deleted.
69
76
  - A reveal is recorded **before** the value is shown. If the audit log can't be written, nothing is revealed.
@@ -22,7 +22,7 @@ module CacheBro
22
22
  scope = AuditEvent.all
23
23
  scope = scope.where(event: filter_params[:event]) if AuditEvent::EVENTS.include?(filter_params[:event])
24
24
  scope = scope.where(user_label: filter_params[:user]) if filter_params[:user].present?
25
- scope = scope.where(store: filter_params[:store]) if filter_params[:store].present?
25
+ scope = scope.where(context: filter_params[:store]) if filter_params[:store].present?
26
26
  scope = scope.where("target LIKE ?", "%#{AuditEvent.sanitize_sql_like(filter_params[:q])}%") if filter_params[:q].present?
27
27
  scope
28
28
  end
@@ -1,25 +1,15 @@
1
1
  module CacheBro
2
2
  # Who revealed, deleted or purged what, and when. Holds keys and patterns, never values.
3
- class AuditEvent < ApplicationRecord
3
+ # The rows live in BroHub's one audit table; this is CacheBro's slice of it.
4
+ class AuditEvent < BroHub::AuditEvent
5
+ SOURCE = "cache_bro".freeze
4
6
  EVENTS = %w[ value_viewed key_deleted keys_purged store_flushed ].freeze
5
7
 
6
- validates :event, inclusion: { in: EVENTS }
7
- validates :environment, :store, presence: true
8
-
9
- scope :recent, -> { order(created_at: :desc, id: :desc) }
8
+ default_scope { where(source: SOURCE) }
10
9
 
11
- # Audit entries are append-only.
12
- def readonly?
13
- persisted?
14
- end
15
-
16
- # Keys the action touched, for a purge or flush (a sample, see config.audit_sample_size).
17
- def sample
18
- Array(details["sample"])
19
- end
10
+ validates :event, inclusion: { in: EVENTS }
20
11
 
21
- def truncated?
22
- details["truncated"] == true
23
- end
12
+ # The cache store the action was on ("Rails.cache").
13
+ alias_attribute :store, :context
24
14
  end
25
15
  end
@@ -1,25 +1,15 @@
1
1
  module CacheBro
2
- # Records who did what in CacheBro::AuditEvent. Keys and patterns are recorded, never values.
2
+ # Records who did what, through BroHub's audit table. Keys and patterns are recorded, never values.
3
3
  module Audit
4
4
  module_function
5
5
 
6
6
  # target: the key, or the pattern. count: how many keys the action touched.
7
7
  def record(event, user:, store:, target: nil, count: nil, reason: nil, **details)
8
- attributes = {
9
- event: event.to_s,
10
- user_label: user.is_a?(String) ? user : CacheBro.config.user_label.call(user).to_s,
11
- environment: Rails.env.to_s,
12
- store: store.to_s,
13
- target: target,
14
- count: count,
15
- reason: reason.presence,
16
- details: details.deep_stringify_keys
17
- }
18
- CacheBro::AuditEvent.create!(**attributes)
19
- rescue StandardError => error
20
- # The action already happened (or, for a reveal, is about to be refused): at least leave a trace in the log.
21
- Rails.logger&.error("CacheBro: could not write the audit log (#{error.class}: #{error.message}). Entry: #{attributes.inspect}")
22
- raise
8
+ BroHub::Audit.record(
9
+ source: CacheBro::AuditEvent::SOURCE, model: CacheBro::AuditEvent, event: event,
10
+ user: user.is_a?(String) ? user : CacheBro.config.user_label.call(user).to_s,
11
+ context: store, target: target, count: count, reason: reason, **details
12
+ )
23
13
  end
24
14
  end
25
15
  end
@@ -11,8 +11,9 @@ module CacheBro
11
11
  # engine under <hub>/cache_bro. Showing a section is navigation only: every controller authorizes each request.
12
12
  initializer "cache_bro.bro_hub" do
13
13
  BroHub.register :cache_bro,
14
- name: "CacheBro", label: "CacheBro · cache", accent: :blue, order: 30,
14
+ name: "CacheBro", label: "CacheBro · cache", accent: :blue, order: 40,
15
15
  engine: "CacheBro::Engine",
16
+ audit_visible_to: ->(user) { CacheBro.config.allows?(user, :audit) },
16
17
  visible_to: ->(user) { CacheBro.config.allows?(user, :view_keys) || CacheBro.config.allows?(user, :audit) },
17
18
  items: [
18
19
  { label: "Keys", path: "/", match: %r{\A/(key(/|\z)|\z)}, visible_to: ->(user) { CacheBro.config.allows?(user, :view_keys) } },
@@ -1,3 +1,3 @@
1
1
  module CacheBro
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -10,16 +10,12 @@ module CacheBro
10
10
 
11
11
  source_root File.expand_path("templates", __dir__)
12
12
 
13
- desc "Sets up the BroHub dashboard (CacheBro lives in it), adds the CacheBro initializer and copies the migration."
13
+ desc "Sets up the BroHub dashboard (CacheBro lives in it), adds the CacheBro initializer. The audit table comes with BroHub."
14
14
 
15
15
  def copy_initializer
16
16
  copy_config_once "cache_bro.rb", "config/initializers/cache_bro.rb"
17
17
  end
18
18
 
19
- def copy_migrations
20
- rails_command "cache_bro:install:migrations", inline: true
21
- end
22
-
23
19
  def next_steps
24
20
  say <<~TEXT
25
21
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cache_bro
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ruby Dev SRL
@@ -29,14 +29,14 @@ dependencies:
29
29
  requirements:
30
30
  - - ">="
31
31
  - !ruby/object:Gem::Version
32
- version: 0.1.2
32
+ version: 0.2.0
33
33
  type: :runtime
34
34
  prerelease: false
35
35
  version_requirements: !ruby/object:Gem::Requirement
36
36
  requirements:
37
37
  - - ">="
38
38
  - !ruby/object:Gem::Version
39
- version: 0.1.2
39
+ version: 0.2.0
40
40
  description: 'A mountable Rails engine for the BroHub dashboard: search cache keys,
41
41
  see TTL and size, reveal a value, delete a key or purge a pattern. Permissions are
42
42
  plain lambdas, destructive actions can require a reason, and everything lands in
@@ -56,7 +56,6 @@ files:
56
56
  - app/controllers/cache_bro/keys_controller.rb
57
57
  - app/controllers/cache_bro/purges_controller.rb
58
58
  - app/helpers/cache_bro/application_helper.rb
59
- - app/models/cache_bro/application_record.rb
60
59
  - app/models/cache_bro/audit_event.rb
61
60
  - app/views/cache_bro/audit_events/index.html.erb
62
61
  - app/views/cache_bro/audit_events/show.html.erb
@@ -69,7 +68,6 @@ files:
69
68
  - app/views/cache_bro/shared/forbidden.html.erb
70
69
  - app/views/cache_bro/shared/unsupported.html.erb
71
70
  - config/routes.rb
72
- - db/migrate/20261115130000_create_cache_bro_audit_events.rb
73
71
  - lib/cache_bro.rb
74
72
  - lib/cache_bro/audit.rb
75
73
  - lib/cache_bro/configuration.rb
@@ -1,6 +0,0 @@
1
- module CacheBro
2
- class ApplicationRecord < ActiveRecord::Base
3
- self.abstract_class = true
4
- self.table_name_prefix = "cache_bro_"
5
- end
6
- end
@@ -1,25 +0,0 @@
1
- class CreateCacheBroAuditEvents < ActiveRecord::Migration[7.1]
2
- def change
3
- create_table :cache_bro_audit_events do |t|
4
- t.string :event, null: false
5
- t.string :user_label
6
- t.string :environment, null: false
7
- t.string :store, null: false
8
-
9
- # The key (reveal, delete) or the pattern (purge). Empty for a flush.
10
- t.text :target
11
- # How many keys the action touched.
12
- t.integer :count
13
- t.text :reason
14
-
15
- # No column default: MySQL rejects defaults on JSON columns. Audit.record always sets it.
16
- t.json :details, null: false
17
-
18
- t.datetime :created_at, null: false
19
- end
20
-
21
- add_index :cache_bro_audit_events, :created_at
22
- add_index :cache_bro_audit_events, [ :event, :created_at ]
23
- add_index :cache_bro_audit_events, [ :user_label, :created_at ]
24
- end
25
- end