bro_hub 0.2.0 → 0.3.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 +4 -4
- data/CHANGELOG.md +21 -0
- data/README.md +175 -165
- data/app/controllers/bro_hub/apm/application_controller.rb +26 -0
- data/app/controllers/bro_hub/apm/deploys_controller.rb +7 -0
- data/app/controllers/bro_hub/apm/endpoints_controller.rb +8 -0
- data/app/controllers/bro_hub/apm/errors_controller.rb +15 -0
- data/app/controllers/bro_hub/apm/findings_controller.rb +8 -0
- data/app/controllers/bro_hub/apm/overview_controller.rb +7 -0
- data/app/controllers/bro_hub/apm/queries_controller.rb +11 -0
- data/app/controllers/bro_hub/apm/status_controller.rb +9 -0
- data/app/controllers/bro_hub/application_controller.rb +16 -0
- data/app/controllers/bro_hub/audit_events_controller.rb +2 -2
- data/app/controllers/bro_hub/cache/application_controller.rb +55 -0
- data/app/controllers/bro_hub/cache/audit_events_controller.rb +40 -0
- data/app/controllers/bro_hub/cache/flushes_controller.rb +34 -0
- data/app/controllers/bro_hub/cache/keys_controller.rb +41 -0
- data/app/controllers/bro_hub/cache/purges_controller.rb +40 -0
- data/app/controllers/bro_hub/env/application_controller.rb +27 -0
- data/app/controllers/bro_hub/env/audit_controller.rb +10 -0
- data/app/controllers/bro_hub/env/home_controller.rb +6 -0
- data/app/controllers/bro_hub/env/proposals_controller.rb +86 -0
- data/app/controllers/bro_hub/env/reviews_controller.rb +72 -0
- data/app/controllers/bro_hub/feature_controller.rb +21 -0
- data/app/controllers/bro_hub/safe/application_controller.rb +24 -0
- data/app/controllers/bro_hub/safe/flagged_commands_controller.rb +14 -0
- data/app/controllers/bro_hub/safe/reviews_controller.rb +39 -0
- data/app/controllers/bro_hub/safe/rules_controller.rb +18 -0
- data/app/controllers/bro_hub/safe/sessions_controller.rb +63 -0
- data/app/controllers/bro_hub/safe/unlocks_controller.rb +14 -0
- data/app/helpers/bro_hub/apm/application_helper.rb +104 -0
- data/app/helpers/bro_hub/application_helper.rb +5 -19
- data/app/helpers/bro_hub/cache/application_helper.rb +63 -0
- data/app/helpers/bro_hub/env/application_helper.rb +67 -0
- data/app/helpers/bro_hub/safe/application_helper.rb +59 -0
- data/app/jobs/bro_hub/safe/purge_job.rb +10 -0
- data/app/models/bro_hub/audit_event.rb +2 -6
- data/app/models/bro_hub/cache/audit_event.rb +15 -0
- data/app/models/bro_hub/env/audit_event.rb +72 -0
- data/app/models/bro_hub/safe/command.rb +22 -0
- data/app/models/bro_hub/safe/review.rb +22 -0
- data/app/models/bro_hub/safe/session.rb +57 -0
- data/app/models/bro_hub/safe/unlock.rb +15 -0
- data/app/views/bro_hub/apm/deploys/index.html.erb +21 -0
- data/app/views/bro_hub/apm/endpoints/index.html.erb +27 -0
- data/app/views/bro_hub/apm/errors/index.html.erb +22 -0
- data/app/views/bro_hub/apm/errors/not_found.html.erb +3 -0
- data/app/views/bro_hub/apm/errors/show.html.erb +43 -0
- data/app/views/bro_hub/apm/findings/index.html.erb +24 -0
- data/app/views/bro_hub/apm/overview/show.html.erb +59 -0
- data/app/views/bro_hub/apm/queries/index.html.erb +37 -0
- data/app/views/bro_hub/apm/shared/_page_styles.html.erb +15 -0
- data/app/views/bro_hub/apm/status/show.html.erb +47 -0
- data/app/views/bro_hub/audit_events/index.html.erb +1 -1
- data/app/views/bro_hub/cache/audit_events/index.html.erb +39 -0
- data/app/views/bro_hub/cache/audit_events/show.html.erb +48 -0
- data/app/views/bro_hub/cache/flushes/new.html.erb +19 -0
- data/app/views/bro_hub/cache/keys/index.html.erb +36 -0
- data/app/views/bro_hub/cache/keys/show.html.erb +57 -0
- data/app/views/bro_hub/cache/purges/new.html.erb +17 -0
- data/app/views/bro_hub/cache/purges/preview.html.erb +37 -0
- data/app/views/bro_hub/cache/shared/_store_tabs.html.erb +9 -0
- data/app/views/bro_hub/cache/shared/unsupported.html.erb +3 -0
- data/app/views/bro_hub/env/audit/index.html.erb +31 -0
- data/app/views/bro_hub/env/home/index.html.erb +25 -0
- data/app/views/bro_hub/env/proposals/_new_key_row.html.erb +7 -0
- data/app/views/bro_hub/env/proposals/new.html.erb +99 -0
- data/app/views/bro_hub/env/reviews/new.html.erb +29 -0
- data/app/views/bro_hub/env/reviews/show.html.erb +122 -0
- data/app/views/bro_hub/env/shared/_page_styles.html.erb +57 -0
- data/app/views/bro_hub/env/shared/_script.html.erb +114 -0
- data/app/views/bro_hub/env/shared/not_ready.html.erb +17 -0
- data/app/views/bro_hub/safe/flagged_commands/index.html.erb +22 -0
- data/app/views/bro_hub/safe/rules/show.html.erb +59 -0
- data/app/views/bro_hub/safe/sessions/index.html.erb +51 -0
- data/app/views/bro_hub/safe/sessions/show.html.erb +92 -0
- data/app/views/bro_hub/safe/unlocks/index.html.erb +25 -0
- data/app/views/bro_hub/shared/schema_missing.html.erb +25 -0
- data/app/views/layouts/bro_hub/application.html.erb +1 -1
- data/config/routes/apm.rb +10 -0
- data/config/routes/cache.rb +17 -0
- data/config/routes/env.rb +10 -0
- data/config/routes/safe.rb +10 -0
- data/config/routes.rb +9 -4
- data/db/migrate/20261129000000_create_bro_hub_tables.rb +85 -0
- data/lib/bro_hub/apm/api.rb +88 -0
- data/lib/bro_hub/apm/configuration.rb +58 -0
- data/lib/bro_hub/apm/local_status.rb +49 -0
- data/lib/bro_hub/apm.rb +28 -0
- data/lib/bro_hub/audit.rb +2 -9
- data/lib/bro_hub/cache/audit.rb +15 -0
- data/lib/bro_hub/cache/configuration.rb +105 -0
- data/lib/bro_hub/cache/errors.rb +26 -0
- data/lib/bro_hub/cache/redis_store.rb +220 -0
- data/lib/bro_hub/cache.rb +38 -0
- data/lib/bro_hub/configuration.rb +19 -4
- data/lib/bro_hub/controller_base.rb +3 -6
- data/lib/bro_hub/engine.rb +60 -1
- data/lib/bro_hub/env/audit.rb +17 -0
- data/lib/bro_hub/env/configuration.rb +72 -0
- data/lib/bro_hub/env/credentials.rb +135 -0
- data/lib/bro_hub/env/deployments.rb +45 -0
- data/lib/bro_hub/env/differ.rb +58 -0
- data/lib/bro_hub/env/editor.rb +104 -0
- data/lib/bro_hub/env/encrypted_blob.rb +18 -0
- data/lib/bro_hub/env/environment.rb +74 -0
- data/lib/bro_hub/env.rb +36 -0
- data/lib/bro_hub/features.rb +13 -0
- data/lib/bro_hub/layout.rb +2 -5
- data/lib/bro_hub/safe/cipher.rb +46 -0
- data/lib/bro_hub/safe/commands.rb +25 -0
- data/lib/bro_hub/safe/configuration.rb +191 -0
- data/lib/bro_hub/safe/encrypted_type.rb +16 -0
- data/lib/bro_hub/safe/errors.rb +48 -0
- data/lib/bro_hub/safe/executor.rb +142 -0
- data/lib/bro_hub/safe/guards/const_guard.rb +42 -0
- data/lib/bro_hub/safe/guards/credentials_guard.rb +31 -0
- data/lib/bro_hub/safe/guards/env_guard.rb +90 -0
- data/lib/bro_hub/safe/guards/file_guard.rb +28 -0
- data/lib/bro_hub/safe/guards/guarded.rb +51 -0
- data/lib/bro_hub/safe/guards/irb_context.rb +23 -0
- data/lib/bro_hub/safe/guards/method_guard.rb +32 -0
- data/lib/bro_hub/safe/guards/network_guard.rb +98 -0
- data/lib/bro_hub/safe/guards/sql_guard.rb +26 -0
- data/lib/bro_hub/safe/parsed_command.rb +66 -0
- data/lib/bro_hub/safe/recorder.rb +82 -0
- data/lib/bro_hub/safe/shield.rb +83 -0
- data/lib/bro_hub/safe/supervisor.rb +81 -0
- data/lib/bro_hub/safe/terminal.rb +55 -0
- data/lib/bro_hub/safe/trail.rb +24 -0
- data/lib/bro_hub/safe/validator.rb +74 -0
- data/lib/bro_hub/safe.rb +134 -0
- data/lib/bro_hub/schema.rb +36 -0
- data/lib/bro_hub/section.rb +7 -13
- data/lib/bro_hub/testing.rb +23 -6
- data/lib/bro_hub/version.rb +1 -1
- data/lib/bro_hub.rb +31 -6
- data/lib/generators/bro_hub/install/install_generator.rb +89 -71
- data/lib/generators/bro_hub/install/templates/bro_hub.rb.tt +115 -0
- data/lib/generators/bro_hub/uninstall/templates/drop_bro_hub_tables.rb.tt +16 -0
- data/lib/generators/bro_hub/uninstall/uninstall_generator.rb +57 -0
- data/lib/tasks/bro_hub.rake +9 -0
- metadata +148 -13
- data/db/migrate/20261116000000_create_bro_hub_audit_events.rb +0 -35
- data/lib/bro_hub/root_middleware.rb +0 -17
- data/lib/generators/bro_hub/install/templates/bro_hub.rb +0 -11
- data/lib/generators/bro_hub/installable.rb +0 -63
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
module BroHub::Cache
|
|
2
|
+
# BroHub.config.cache. Who is signed in, how they are labelled and whether SSL is required are BroHub.config's.
|
|
3
|
+
class Configuration
|
|
4
|
+
# --- Who may do what ------------------------------------------------------------------
|
|
5
|
+
|
|
6
|
+
# Permission checks, each ->(user) { true/false }. Everything is denied until you change them.
|
|
7
|
+
# can_view_keys - open CacheBro, search keys, see TTL and size
|
|
8
|
+
# can_view_values - reveal what a key holds (every reveal is audited)
|
|
9
|
+
# can_delete - delete a single key
|
|
10
|
+
# can_purge - delete every key matching a pattern (includes delete)
|
|
11
|
+
# can_flush - delete everything in a store's namespace (includes purge and delete)
|
|
12
|
+
# can_audit - read the audit log
|
|
13
|
+
#
|
|
14
|
+
# Higher permissions include the lower ones, so you don't have to repeat yourself.
|
|
15
|
+
# Revealing values and auditing are separate on purpose: values can hold personal data.
|
|
16
|
+
attr_accessor :can_view_keys, :can_view_values, :can_delete, :can_purge, :can_flush, :can_audit
|
|
17
|
+
|
|
18
|
+
PERMISSIONS = %i[ view_keys view_values delete purge flush audit ].freeze
|
|
19
|
+
|
|
20
|
+
# Permissions that also grant the key.
|
|
21
|
+
IMPLIED_BY = {
|
|
22
|
+
view_keys: %i[ view_values delete purge flush ],
|
|
23
|
+
view_values: [],
|
|
24
|
+
delete: %i[ purge flush ],
|
|
25
|
+
purge: %i[ flush ],
|
|
26
|
+
flush: [],
|
|
27
|
+
audit: []
|
|
28
|
+
}.freeze
|
|
29
|
+
|
|
30
|
+
# Actions that can require a reason.
|
|
31
|
+
ACTIONS = %i[ reveal delete purge flush ].freeze
|
|
32
|
+
|
|
33
|
+
# --- Which caches ---------------------------------------------------------------------
|
|
34
|
+
|
|
35
|
+
# { "Name" => -> { store } }. The name appears in the UI and the audit log. Stores that aren't a
|
|
36
|
+
# Rails :redis_cache_store are listed as unsupported.
|
|
37
|
+
attr_accessor :stores
|
|
38
|
+
|
|
39
|
+
# --- Limits ---------------------------------------------------------------------------
|
|
40
|
+
|
|
41
|
+
# SCAN COUNT hint per Redis call, and how many calls one page of search results may spend before it
|
|
42
|
+
# hands you a cursor to keep going. Redis scans the whole keyspace either way, in small steps.
|
|
43
|
+
attr_accessor :scan_count, :scan_budget
|
|
44
|
+
|
|
45
|
+
# A page of keys is "at least this many" (a SCAN step can return a few more).
|
|
46
|
+
attr_accessor :page_size
|
|
47
|
+
|
|
48
|
+
# A single purge deletes at most this many keys, then says so and stops. Run it again to continue.
|
|
49
|
+
attr_accessor :max_purge_keys
|
|
50
|
+
|
|
51
|
+
# Characters of a value shown after revealing it.
|
|
52
|
+
attr_accessor :value_preview_length
|
|
53
|
+
|
|
54
|
+
# How many affected keys the audit log keeps as an example for a purge or flush.
|
|
55
|
+
attr_accessor :audit_sample_size
|
|
56
|
+
|
|
57
|
+
# Which actions need a reason typed in (stored in the audit log): any of :reveal, :delete, :purge, :flush.
|
|
58
|
+
attr_accessor :reason_required_for
|
|
59
|
+
|
|
60
|
+
def initialize
|
|
61
|
+
@can_view_keys = ->(_user) { false }
|
|
62
|
+
@can_view_values = ->(_user) { false }
|
|
63
|
+
@can_delete = ->(_user) { false }
|
|
64
|
+
@can_purge = ->(_user) { false }
|
|
65
|
+
@can_flush = ->(_user) { false }
|
|
66
|
+
@can_audit = ->(_user) { false }
|
|
67
|
+
|
|
68
|
+
@stores = { "Rails.cache" => -> { Rails.cache } }
|
|
69
|
+
|
|
70
|
+
@scan_count = 500
|
|
71
|
+
@scan_budget = 200
|
|
72
|
+
@page_size = 100
|
|
73
|
+
@max_purge_keys = 100_000
|
|
74
|
+
@value_preview_length = 5_000
|
|
75
|
+
@audit_sample_size = 25
|
|
76
|
+
@reason_required_for = %i[ purge flush ]
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# Does the user hold this permission, directly or through a higher one?
|
|
80
|
+
def allows?(user, permission)
|
|
81
|
+
return false if user.nil?
|
|
82
|
+
raise ArgumentError, "Unknown permission #{permission.inspect}" unless PERMISSIONS.include?(permission)
|
|
83
|
+
|
|
84
|
+
[ permission, *IMPLIED_BY.fetch(permission) ].any? { |name| !!public_send(:"can_#{name}").call(user) }
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# May this person see the section at all: any key or audit access.
|
|
88
|
+
def allows_any?(user)
|
|
89
|
+
allows?(user, :view_keys) || allows?(user, :audit)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# Who may see this feature's rows in the hub's audit log.
|
|
93
|
+
def audit_visible?(user)
|
|
94
|
+
allows?(user, :audit)
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def reason_required?(action)
|
|
98
|
+
Array(reason_required_for).map(&:to_sym).include?(action.to_sym)
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def store_names
|
|
102
|
+
stores.keys.map(&:to_s)
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
module BroHub::Cache
|
|
2
|
+
class Error < StandardError; end
|
|
3
|
+
|
|
4
|
+
# config.stores has no store with that name.
|
|
5
|
+
class UnknownStore < Error; end
|
|
6
|
+
|
|
7
|
+
# The store isn't one CacheBro can manage (only Redis for now), or is set up in a way it can't handle.
|
|
8
|
+
class UnsupportedStore < Error; end
|
|
9
|
+
|
|
10
|
+
# Destructive actions need a reason (see config.reason_required_for).
|
|
11
|
+
class ReasonRequired < Error; end
|
|
12
|
+
|
|
13
|
+
# A pattern the action refuses (empty, or everything).
|
|
14
|
+
class InvalidPattern < Error; end
|
|
15
|
+
|
|
16
|
+
# A purge or flush failed part way (Redis went away, say). `matches` is what was deleted before that,
|
|
17
|
+
# so the audit log can still say so. The original error is `cause`.
|
|
18
|
+
class SweepFailed < Error
|
|
19
|
+
attr_reader :matches
|
|
20
|
+
|
|
21
|
+
def initialize(message, matches)
|
|
22
|
+
super(message)
|
|
23
|
+
@matches = matches
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
module BroHub::Cache
|
|
2
|
+
# Everything CacheBro does to one Rails :redis_cache_store.
|
|
3
|
+
#
|
|
4
|
+
# Keys are shown without the store's namespace, and every key the person types is put back under it, so
|
|
5
|
+
# nothing outside the namespace can be reached. Redis is only ever walked with SCAN (never KEYS), in small steps.
|
|
6
|
+
class RedisStore
|
|
7
|
+
Entry = Struct.new(:key, :ttl, :size, keyword_init: true)
|
|
8
|
+
|
|
9
|
+
# One page of search results. `cursor` is where to continue (nil when the whole keyspace was covered).
|
|
10
|
+
Listing = Struct.new(:entries, :cursor, keyword_init: true) do
|
|
11
|
+
def more? = !cursor.nil?
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# What a pattern matches (or, after a purge, what it deleted). `truncated`: there are more than the limit.
|
|
15
|
+
Matches = Struct.new(:count, :sample, :truncated, keyword_init: true)
|
|
16
|
+
|
|
17
|
+
# A revealed value: what it is, and as much of it as is safe to render. `error` when it couldn't be read.
|
|
18
|
+
Value = Struct.new(:klass, :preview, :truncated, :error, keyword_init: true)
|
|
19
|
+
|
|
20
|
+
REDIS_CACHE_STORE = "ActiveSupport::Cache::RedisCacheStore".freeze
|
|
21
|
+
|
|
22
|
+
# Looked up by name so nothing is loaded (and no redis gem is required) for apps on another store.
|
|
23
|
+
def self.supports?(cache)
|
|
24
|
+
cache.class.ancestors.any? { |klass| klass.name == REDIS_CACHE_STORE }
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
attr_reader :name
|
|
28
|
+
|
|
29
|
+
def initialize(name, cache)
|
|
30
|
+
@name = name.to_s
|
|
31
|
+
@cache = cache
|
|
32
|
+
unless self.class.supports?(cache)
|
|
33
|
+
raise UnsupportedStore, "#{@name} is a #{cache.class.name}. CacheBro manages Redis cache stores (:redis_cache_store) for now."
|
|
34
|
+
end
|
|
35
|
+
raise UnsupportedStore, "#{@name} spreads keys over several Redis servers. CacheBro needs a single one." if distributed?
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def namespace
|
|
39
|
+
value = @cache.options[:namespace]
|
|
40
|
+
value = value.call if value.respond_to?(:call)
|
|
41
|
+
value.to_s.presence
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def namespaced?
|
|
45
|
+
!namespace.nil?
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# --- Reading --------------------------------------------------------------------------
|
|
49
|
+
|
|
50
|
+
# Searches keys. Plain text matches anywhere in the key, `*` and `?` make it a pattern. One call walks part of
|
|
51
|
+
# the keyspace: pass the returned cursor back in to continue.
|
|
52
|
+
def list(query: nil, cursor: "0")
|
|
53
|
+
config = BroHub.config.cache
|
|
54
|
+
cursor = cursor.to_s
|
|
55
|
+
raws = []
|
|
56
|
+
calls = 0
|
|
57
|
+
|
|
58
|
+
entries = with_redis do |redis|
|
|
59
|
+
loop do
|
|
60
|
+
cursor, batch = redis.scan(cursor, match: search_match(query), count: config.scan_count)
|
|
61
|
+
raws.concat(batch)
|
|
62
|
+
calls += 1
|
|
63
|
+
break if cursor == "0" || raws.size >= config.page_size || calls >= config.scan_budget
|
|
64
|
+
end
|
|
65
|
+
describe(redis, raws)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
Listing.new(entries: entries, cursor: (cursor == "0" ? nil : cursor))
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# TTL and size of one key (nil when it doesn't exist).
|
|
72
|
+
def stat(key)
|
|
73
|
+
raw = prefix + key
|
|
74
|
+
ttl, size = with_redis { |redis| stats_for(redis, [ raw ]).first }
|
|
75
|
+
build_entry(key, ttl, size)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# What the key holds, for display. Never raises for a value that can't be read: that is shown instead.
|
|
79
|
+
def read(key)
|
|
80
|
+
value = @cache.read(key)
|
|
81
|
+
text = value.inspect
|
|
82
|
+
limit = BroHub.config.cache.value_preview_length
|
|
83
|
+
Value.new(klass: value.class.name, preview: text.first(limit), truncated: text.length > limit)
|
|
84
|
+
rescue StandardError => error
|
|
85
|
+
Value.new(klass: nil, preview: nil, truncated: false, error: "#{error.class}: #{error.message.first(300)}")
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# --- Deleting -------------------------------------------------------------------------
|
|
89
|
+
|
|
90
|
+
def delete(key)
|
|
91
|
+
with_redis { |redis| redis.unlink(prefix + key) }
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# How many keys the pattern matches, up to `limit`.
|
|
95
|
+
def preview(pattern)
|
|
96
|
+
sweep(purge_match(pattern), limit: BroHub.config.cache.max_purge_keys, delete: false)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# Deletes the keys matching the pattern, at most config.max_purge_keys of them.
|
|
100
|
+
def purge(pattern)
|
|
101
|
+
sweep(purge_match(pattern), limit: BroHub.config.cache.max_purge_keys, delete: true)
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# Deletes everything under the namespace. Refused without one: that would be the whole Redis database.
|
|
105
|
+
def flush
|
|
106
|
+
raise UnsupportedStore, "#{name} has no namespace, so flushing it would touch keys that aren't the cache's. Set `namespace:` on the cache store." unless namespaced?
|
|
107
|
+
|
|
108
|
+
sweep("#{glob_escape(prefix)}*", limit: nil, delete: true)
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
private
|
|
112
|
+
def with_redis(&)
|
|
113
|
+
@cache.redis.then(&)
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def distributed?
|
|
117
|
+
with_redis { |redis| redis.respond_to?(:nodes) }
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def prefix
|
|
121
|
+
namespaced? ? "#{namespace}:" : ""
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# Redis glob: * ? [ ] and \ are special. Everything but the wildcards is matched literally.
|
|
125
|
+
def glob_escape(text)
|
|
126
|
+
text.gsub(/[\\*?\[\]]/) { |char| "\\#{char}" }
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
# Only * and ? are wildcards for people. [ ] and \ stay literal, so a key like "a[1]" can be found.
|
|
130
|
+
def with_wildcards(text)
|
|
131
|
+
text.gsub(/[\\\[\]]/) { |char| "\\#{char}" }
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def pattern?(text)
|
|
135
|
+
text.match?(/[*?]/)
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def search_match(query)
|
|
139
|
+
text = query.to_s.strip
|
|
140
|
+
return "#{glob_escape(prefix)}*" if text.empty?
|
|
141
|
+
|
|
142
|
+
pattern?(text) ? "#{glob_escape(prefix)}#{with_wildcards(text)}" : "#{glob_escape(prefix)}*#{glob_escape(text)}*"
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# Purge patterns are used as typed (so "posts/*" is a prefix), never widened.
|
|
146
|
+
def purge_match(pattern)
|
|
147
|
+
text = pattern.to_s.strip
|
|
148
|
+
raise InvalidPattern, "Give a pattern, like posts/*" if text.empty?
|
|
149
|
+
raise InvalidPattern, "That matches everything. Use Flush for that." if text.match?(/\A\*+\z/)
|
|
150
|
+
|
|
151
|
+
"#{glob_escape(prefix)}#{with_wildcards(text)}"
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
# The key as people see it: valid text, without the namespace. Nil for keys that aren't text.
|
|
155
|
+
def display_key(raw)
|
|
156
|
+
key = raw.dup.force_encoding(Encoding::UTF_8)
|
|
157
|
+
key.delete_prefix(prefix) if key.valid_encoding?
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
def describe(redis, raws)
|
|
161
|
+
raws = raws.uniq.select { |raw| display_key(raw) }
|
|
162
|
+
return [] if raws.empty?
|
|
163
|
+
|
|
164
|
+
raws.zip(stats_for(redis, raws)).filter_map { |raw, (ttl, size)| build_entry(display_key(raw), ttl, size) }
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
def build_entry(key, pttl, size)
|
|
168
|
+
return if pttl.nil? || pttl == -2 # gone since it was found, or not a cache entry
|
|
169
|
+
|
|
170
|
+
Entry.new(key: key, ttl: (pttl / 1000.0 if pttl >= 0), size: size)
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
# [ [ pttl, bytes ], ... ] in one round trip. A key holding something other than a string (a shared Redis
|
|
174
|
+
# database) makes the pipeline fail, so fall back to asking one by one and leaving those out.
|
|
175
|
+
def stats_for(redis, raws)
|
|
176
|
+
redis.pipelined { |pipeline| raws.each { |raw| pipeline.pttl(raw); pipeline.strlen(raw) } }.each_slice(2).to_a
|
|
177
|
+
rescue ::Redis::CommandError
|
|
178
|
+
raws.map { |raw| redis.type(raw) == "string" ? [ redis.pttl(raw), redis.strlen(raw) ] : [ nil, nil ] }
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
# Walks the keyspace for `match`, optionally deleting what it finds, stopping at `limit` keys.
|
|
182
|
+
# A failure part way raises SweepFailed with what was done so far.
|
|
183
|
+
def sweep(match, limit:, delete:)
|
|
184
|
+
count = 0
|
|
185
|
+
sample = []
|
|
186
|
+
truncated = false
|
|
187
|
+
sample_size = BroHub.config.cache.audit_sample_size
|
|
188
|
+
|
|
189
|
+
catch(:done) do
|
|
190
|
+
with_redis do |redis|
|
|
191
|
+
cursor = "0"
|
|
192
|
+
loop do
|
|
193
|
+
cursor, batch = redis.scan(cursor, match: match, count: BroHub.config.cache.scan_count)
|
|
194
|
+
batch = batch.uniq
|
|
195
|
+
unless batch.empty?
|
|
196
|
+
room = limit ? limit - count : batch.size
|
|
197
|
+
if room <= 0
|
|
198
|
+
truncated = true
|
|
199
|
+
throw :done
|
|
200
|
+
end
|
|
201
|
+
taken = batch.first(room)
|
|
202
|
+
truncated = taken.size < batch.size
|
|
203
|
+
redis.unlink(*taken) if delete
|
|
204
|
+
count += taken.size
|
|
205
|
+
sample.concat(taken.filter_map { |raw| display_key(raw) }.first(sample_size - sample.size)) if sample.size < sample_size
|
|
206
|
+
throw :done if truncated
|
|
207
|
+
end
|
|
208
|
+
break if cursor == "0"
|
|
209
|
+
end
|
|
210
|
+
end
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
Matches.new(count: count, sample: sample, truncated: truncated)
|
|
214
|
+
rescue StandardError => error
|
|
215
|
+
raise error if error.is_a?(BroHub::Cache::Error)
|
|
216
|
+
|
|
217
|
+
raise SweepFailed.new("#{error.class}: #{error.message}", Matches.new(count: count, sample: sample, truncated: truncated))
|
|
218
|
+
end
|
|
219
|
+
end
|
|
220
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
require "bro_hub/cache/errors"
|
|
2
|
+
require "bro_hub/cache/configuration"
|
|
3
|
+
require "bro_hub/cache/redis_store"
|
|
4
|
+
require "bro_hub/cache/audit"
|
|
5
|
+
|
|
6
|
+
module BroHub
|
|
7
|
+
# The cache feature (was CacheBro): browse, inspect and purge a Rails Redis cache.
|
|
8
|
+
#
|
|
9
|
+
# * Search keys (TTL, size), reveal a value, delete a key, purge a pattern, flush a namespace.
|
|
10
|
+
# * Permissions are lambdas (BroHub.config.cache). Revealing values is its own permission.
|
|
11
|
+
# * Everything that reveals or deletes is recorded, append-only, with who, what, how many and (optionally) why.
|
|
12
|
+
#
|
|
13
|
+
# This is a convenience for people who already have a console, not a sandbox: it makes the same actions easier
|
|
14
|
+
# to do and impossible to do unrecorded through the UI.
|
|
15
|
+
module Cache
|
|
16
|
+
# The sidebar items of this feature's section, relative to <mount>/cache. Showing an item is navigation only:
|
|
17
|
+
# every controller authorizes each request.
|
|
18
|
+
SIDEBAR_ITEMS = [
|
|
19
|
+
{ label: "Keys", path: "/", match: %r{\A/(key(/|\z)|\z)}, visible_to: ->(user) { BroHub.config.cache.allows?(user, :view_keys) } },
|
|
20
|
+
{ label: "Purge", path: "/purge", match: %r{\A/purge}, visible_to: ->(user) { BroHub.config.cache.allows?(user, :purge) } },
|
|
21
|
+
{ label: "Flush", path: "/flush", visible_to: ->(user) { BroHub.config.cache.allows?(user, :flush) } },
|
|
22
|
+
{ label: "Audit log", path: "/audit", match: %r{\A/audit}, visible_to: ->(user) { BroHub.config.cache.allows?(user, :audit) } }
|
|
23
|
+
].freeze
|
|
24
|
+
|
|
25
|
+
class << self
|
|
26
|
+
# The store with that name (the first one when nil), wrapped for BroHub.
|
|
27
|
+
# Raises UnknownStore, or UnsupportedStore for a cache that isn't a Redis cache store.
|
|
28
|
+
def store(name = nil)
|
|
29
|
+
config = BroHub.config.cache
|
|
30
|
+
name = name.presence&.to_s || config.store_names.first
|
|
31
|
+
source = config.stores.find { |key, _| key.to_s == name }&.last
|
|
32
|
+
raise UnknownStore, "No cache store named #{name.inspect}" unless source
|
|
33
|
+
|
|
34
|
+
RedisStore.new(name, source.respond_to?(:call) ? source.call : source)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -10,10 +10,26 @@ module BroHub
|
|
|
10
10
|
# ->(user) { text shown at the top right }
|
|
11
11
|
attr_accessor :user_label
|
|
12
12
|
|
|
13
|
-
# Refuse
|
|
13
|
+
# Refuse every hub page, of every feature, over plain HTTP.
|
|
14
14
|
attr_accessor :require_ssl
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
# Which opt-in features are on: any of :safe, :env, :apm, :cache. Default none.
|
|
17
|
+
attr_reader :features
|
|
18
|
+
|
|
19
|
+
def features=(keys)
|
|
20
|
+
keys = Array(keys).map { |key| key.to_s.to_sym }
|
|
21
|
+
unknown = keys.find { |key| !FEATURES.key?(key) }
|
|
22
|
+
if unknown
|
|
23
|
+
raise ArgumentError, "Unknown BroHub feature #{unknown.inspect}. Choose from #{FEATURES.keys.map(&:inspect).join(", ")}."
|
|
24
|
+
end
|
|
25
|
+
@features = keys.uniq
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Each feature's own settings: BroHub.config.safe.can_audit = ->(user) { user.admin? }
|
|
29
|
+
def safe = @safe ||= BroHub::Safe::Configuration.new
|
|
30
|
+
def env = @env ||= BroHub::Env::Configuration.new
|
|
31
|
+
def apm = @apm ||= BroHub::Apm::Configuration.new
|
|
32
|
+
def cache = @cache ||= BroHub::Cache::Configuration.new
|
|
17
33
|
|
|
18
34
|
def initialize
|
|
19
35
|
@parent_controller = "::ApplicationController"
|
|
@@ -26,8 +42,7 @@ module BroHub
|
|
|
26
42
|
end
|
|
27
43
|
@user_label = ->(user) { user.try(:email) || user.try(:email_address) || user.try(:name) || user.to_s }
|
|
28
44
|
@require_ssl = defined?(Rails) && Rails.respond_to?(:env) ? Rails.env.production? : false
|
|
29
|
-
@
|
|
30
|
-
@brand_url = "https://www.deadbro.com"
|
|
45
|
+
@features = []
|
|
31
46
|
end
|
|
32
47
|
end
|
|
33
48
|
end
|
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
module BroHub
|
|
2
|
-
#
|
|
3
|
-
# what must be right for *any* host app, so a clash with the host's
|
|
4
|
-
#
|
|
5
|
-
# class SafeBro::ApplicationController < SafeBro.config.parent_controller.constantize
|
|
6
|
-
# include BroHub::ControllerBase
|
|
7
|
-
# bro_hub_section :safe_bro
|
|
2
|
+
# Included by BroHub::ApplicationController, which every feature's controllers inherit from (through
|
|
3
|
+
# BroHub::FeatureController). It holds what must be right for *any* host app, so a clash with the host's
|
|
4
|
+
# ApplicationController is fixed once, here.
|
|
8
5
|
module ControllerBase
|
|
9
6
|
extend ActiveSupport::Concern
|
|
10
7
|
|
data/lib/bro_hub/engine.rb
CHANGED
|
@@ -2,6 +2,65 @@ module BroHub
|
|
|
2
2
|
class Engine < ::Rails::Engine
|
|
3
3
|
isolate_namespace BroHub
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
class << self
|
|
6
|
+
# Fails fast at boot, with an instruction, when an enabled feature's gem is missing.
|
|
7
|
+
def check_dependencies!(features)
|
|
8
|
+
return unless features.include?(:apm)
|
|
9
|
+
|
|
10
|
+
begin
|
|
11
|
+
require_dead_bro
|
|
12
|
+
rescue LoadError => error
|
|
13
|
+
# Only the gem itself being absent is a missing dependency. A LoadError raised inside an installed
|
|
14
|
+
# dead_bro is a real bug and must not be disguised.
|
|
15
|
+
raise unless error.path == "dead_bro"
|
|
16
|
+
|
|
17
|
+
raise BroHub::MissingDependency,
|
|
18
|
+
%(BroHub's apm feature needs the dead_bro gem. Add gem "dead_bro" to your Gemfile, or remove :apm from config.features.)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# These reference ::DeadBro, so they load only once the gem is known to be there.
|
|
22
|
+
require "bro_hub/apm/api"
|
|
23
|
+
require "bro_hub/apm/local_status"
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def require_dead_bro
|
|
27
|
+
require "dead_bro"
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# After the host's config/initializers, so config.features is already set.
|
|
32
|
+
initializer "bro_hub.features", after: :load_config_initializers do
|
|
33
|
+
BroHub::Engine.check_dependencies!(BroHub.config.features)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Secret values and uploaded files must never end up in the host's logs. The names are specific to EnvBro's forms.
|
|
37
|
+
initializer "bro_hub.filter_parameters" do |app|
|
|
38
|
+
app.config.filter_parameters += %i[secret_value proposed_file base_file proposed_enc base_enc]
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# The reason people type for a cache action is never worth logging. After the host's initializers, so
|
|
42
|
+
# config.features is already set.
|
|
43
|
+
initializer "bro_hub.cache_filter_parameters", after: "bro_hub.features" do |app|
|
|
44
|
+
app.config.filter_parameters += %i[reason] if BroHub.feature?(:cache)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Credentials are read at boot, so this is when a merged proposal takes effect.
|
|
48
|
+
config.after_initialize do
|
|
49
|
+
BroHub::Env::Deployments.track if BroHub.feature?(:env)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# The safe feature: nothing is installed by merely loading the gem. Its protections start with the console.
|
|
53
|
+
console do
|
|
54
|
+
BroHub::Safe.supervisor.run_console if BroHub.feature?(:safe) && BroHub.config.safe.enabled?
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
runner do
|
|
58
|
+
BroHub::Safe.supervisor.run_runner if BroHub.feature?(:safe) && BroHub.config.safe.enabled? && BroHub.config.safe.audit_runner
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# One sidebar section per enabled feature.
|
|
62
|
+
initializer "bro_hub.sections", after: "bro_hub.features" do
|
|
63
|
+
BroHub.register_feature_sections!
|
|
64
|
+
end
|
|
6
65
|
end
|
|
7
66
|
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
module BroHub::Env
|
|
2
|
+
# Records who did what, through BroHub's audit table. Never records secret values, only key names.
|
|
3
|
+
module Audit
|
|
4
|
+
module_function
|
|
5
|
+
|
|
6
|
+
# columns: proposal_token and file_digest (stored as BroHub's reference and digest) and proposal (the row a
|
|
7
|
+
# deployment belongs to).
|
|
8
|
+
def record(event, user:, columns: {}, **details)
|
|
9
|
+
BroHub::Audit.record(
|
|
10
|
+
source: BroHub::Env::AuditEvent::SOURCE, model: BroHub::Env::AuditEvent, event: event,
|
|
11
|
+
user: user.is_a?(String) ? user : BroHub.config.user_label.call(user).to_s,
|
|
12
|
+
reference: columns[:proposal_token], digest: columns[:file_digest], parent: columns[:proposal],
|
|
13
|
+
**details
|
|
14
|
+
)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
module BroHub::Env
|
|
2
|
+
# BroHub.config.env. Who is signed in, how they are labelled and whether SSL is required are BroHub.config's.
|
|
3
|
+
class Configuration
|
|
4
|
+
# Permission checks, each ->(user) { true/false }.
|
|
5
|
+
# can_create - propose new keys
|
|
6
|
+
# can_update - propose new values for existing keys
|
|
7
|
+
# can_destroy - propose removing keys
|
|
8
|
+
# can_review - upload a proposed file and see which keys changed (values masked)
|
|
9
|
+
# can_reveal - reveal the actual values in a review diff (every reveal is audited)
|
|
10
|
+
#
|
|
11
|
+
# Any of them gets a user into the propose page (which only offers what they may do).
|
|
12
|
+
# Higher permissions include the lower ones, so you don't have to repeat yourself:
|
|
13
|
+
# review includes create, update and destroy; reveal includes everything.
|
|
14
|
+
attr_accessor :can_create, :can_update, :can_destroy, :can_review, :can_reveal
|
|
15
|
+
|
|
16
|
+
PERMISSIONS = %i[create update destroy review reveal].freeze
|
|
17
|
+
|
|
18
|
+
# Permissions that also grant the key.
|
|
19
|
+
IMPLIED_BY = {
|
|
20
|
+
create: %i[review reveal],
|
|
21
|
+
update: %i[review reveal],
|
|
22
|
+
destroy: %i[review reveal],
|
|
23
|
+
review: %i[reveal],
|
|
24
|
+
reveal: []
|
|
25
|
+
}.freeze
|
|
26
|
+
|
|
27
|
+
# Shorthand: grant create, update and destroy with one check.
|
|
28
|
+
def can_propose=(check)
|
|
29
|
+
@can_create = @can_update = @can_destroy = check
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# Top-level credentials key where EnvBro stores proposal metadata
|
|
33
|
+
# (base file digest, author, time). Set to nil to disable.
|
|
34
|
+
attr_accessor :metadata_key
|
|
35
|
+
|
|
36
|
+
attr_accessor :max_upload_bytes
|
|
37
|
+
|
|
38
|
+
# On boot, record when the running credentials file came from an EnvBro proposal.
|
|
39
|
+
attr_accessor :track_deployments
|
|
40
|
+
|
|
41
|
+
def initialize
|
|
42
|
+
@can_create = ->(_user) { false }
|
|
43
|
+
@can_update = ->(_user) { false }
|
|
44
|
+
@can_destroy = ->(_user) { false }
|
|
45
|
+
@can_review = ->(_user) { false }
|
|
46
|
+
@can_reveal = ->(_user) { false }
|
|
47
|
+
@metadata_key = "env_bro"
|
|
48
|
+
@max_upload_bytes = 1024 * 1024
|
|
49
|
+
@track_deployments = true
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# May this person see the section at all: any propose permission, or review.
|
|
53
|
+
def allows_any?(user)
|
|
54
|
+
allows?(user, :propose) || allows?(user, :review)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# Who may see this feature's rows in the hub's audit log.
|
|
58
|
+
def audit_visible?(user)
|
|
59
|
+
allows?(user, :review)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Does the user hold this permission, directly or through a higher one?
|
|
63
|
+
# :propose means any of create, update or destroy.
|
|
64
|
+
def allows?(user, permission)
|
|
65
|
+
return false if user.nil?
|
|
66
|
+
return %i[create update destroy].any? { |p| allows?(user, p) } if permission == :propose
|
|
67
|
+
raise ArgumentError, "Unknown permission #{permission.inspect}" unless PERMISSIONS.include?(permission)
|
|
68
|
+
|
|
69
|
+
[ permission, *IMPLIED_BY.fetch(permission) ].any? { |p| !!public_send(:"can_#{p}").call(user) }
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|