bro_hub 0.1.2 → 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.
Files changed (149) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +21 -0
  3. data/README.md +204 -87
  4. data/app/controllers/bro_hub/apm/application_controller.rb +26 -0
  5. data/app/controllers/bro_hub/apm/deploys_controller.rb +7 -0
  6. data/app/controllers/bro_hub/apm/endpoints_controller.rb +8 -0
  7. data/app/controllers/bro_hub/apm/errors_controller.rb +15 -0
  8. data/app/controllers/bro_hub/apm/findings_controller.rb +8 -0
  9. data/app/controllers/bro_hub/apm/overview_controller.rb +7 -0
  10. data/app/controllers/bro_hub/apm/queries_controller.rb +11 -0
  11. data/app/controllers/bro_hub/apm/status_controller.rb +9 -0
  12. data/app/controllers/bro_hub/application_controller.rb +16 -0
  13. data/app/controllers/bro_hub/audit_events_controller.rb +56 -0
  14. data/app/controllers/bro_hub/cache/application_controller.rb +55 -0
  15. data/app/controllers/bro_hub/cache/audit_events_controller.rb +40 -0
  16. data/app/controllers/bro_hub/cache/flushes_controller.rb +34 -0
  17. data/app/controllers/bro_hub/cache/keys_controller.rb +41 -0
  18. data/app/controllers/bro_hub/cache/purges_controller.rb +40 -0
  19. data/app/controllers/bro_hub/env/application_controller.rb +27 -0
  20. data/app/controllers/bro_hub/env/audit_controller.rb +10 -0
  21. data/app/controllers/bro_hub/env/home_controller.rb +6 -0
  22. data/app/controllers/bro_hub/env/proposals_controller.rb +86 -0
  23. data/app/controllers/bro_hub/env/reviews_controller.rb +72 -0
  24. data/app/controllers/bro_hub/feature_controller.rb +21 -0
  25. data/app/controllers/bro_hub/safe/application_controller.rb +24 -0
  26. data/app/controllers/bro_hub/safe/flagged_commands_controller.rb +14 -0
  27. data/app/controllers/bro_hub/safe/reviews_controller.rb +39 -0
  28. data/app/controllers/bro_hub/safe/rules_controller.rb +18 -0
  29. data/app/controllers/bro_hub/safe/sessions_controller.rb +63 -0
  30. data/app/controllers/bro_hub/safe/unlocks_controller.rb +14 -0
  31. data/app/helpers/bro_hub/apm/application_helper.rb +104 -0
  32. data/app/helpers/bro_hub/application_helper.rb +28 -18
  33. data/app/helpers/bro_hub/cache/application_helper.rb +63 -0
  34. data/app/helpers/bro_hub/env/application_helper.rb +67 -0
  35. data/app/helpers/bro_hub/safe/application_helper.rb +59 -0
  36. data/app/jobs/bro_hub/safe/purge_job.rb +10 -0
  37. data/app/models/bro_hub/application_record.rb +5 -0
  38. data/app/models/bro_hub/audit_event.rb +40 -0
  39. data/app/models/bro_hub/cache/audit_event.rb +15 -0
  40. data/app/models/bro_hub/env/audit_event.rb +72 -0
  41. data/app/models/bro_hub/safe/command.rb +22 -0
  42. data/app/models/bro_hub/safe/review.rb +22 -0
  43. data/app/models/bro_hub/safe/session.rb +57 -0
  44. data/app/models/bro_hub/safe/unlock.rb +15 -0
  45. data/app/views/bro_hub/apm/deploys/index.html.erb +21 -0
  46. data/app/views/bro_hub/apm/endpoints/index.html.erb +27 -0
  47. data/app/views/bro_hub/apm/errors/index.html.erb +22 -0
  48. data/app/views/bro_hub/apm/errors/not_found.html.erb +3 -0
  49. data/app/views/bro_hub/apm/errors/show.html.erb +43 -0
  50. data/app/views/bro_hub/apm/findings/index.html.erb +24 -0
  51. data/app/views/bro_hub/apm/overview/show.html.erb +59 -0
  52. data/app/views/bro_hub/apm/queries/index.html.erb +37 -0
  53. data/app/views/bro_hub/apm/shared/_page_styles.html.erb +15 -0
  54. data/app/views/bro_hub/apm/status/show.html.erb +47 -0
  55. data/app/views/bro_hub/audit_events/index.html.erb +38 -0
  56. data/app/views/bro_hub/audit_events/show.html.erb +54 -0
  57. data/app/views/bro_hub/cache/audit_events/index.html.erb +39 -0
  58. data/app/views/bro_hub/cache/audit_events/show.html.erb +48 -0
  59. data/app/views/bro_hub/cache/flushes/new.html.erb +19 -0
  60. data/app/views/bro_hub/cache/keys/index.html.erb +36 -0
  61. data/app/views/bro_hub/cache/keys/show.html.erb +57 -0
  62. data/app/views/bro_hub/cache/purges/new.html.erb +17 -0
  63. data/app/views/bro_hub/cache/purges/preview.html.erb +37 -0
  64. data/app/views/bro_hub/cache/shared/_store_tabs.html.erb +9 -0
  65. data/app/views/bro_hub/cache/shared/unsupported.html.erb +3 -0
  66. data/app/views/bro_hub/env/audit/index.html.erb +31 -0
  67. data/app/views/bro_hub/env/home/index.html.erb +25 -0
  68. data/app/views/bro_hub/env/proposals/_new_key_row.html.erb +7 -0
  69. data/app/views/bro_hub/env/proposals/new.html.erb +99 -0
  70. data/app/views/bro_hub/env/reviews/new.html.erb +29 -0
  71. data/app/views/bro_hub/env/reviews/show.html.erb +122 -0
  72. data/app/views/bro_hub/env/shared/_page_styles.html.erb +57 -0
  73. data/app/views/bro_hub/env/shared/_script.html.erb +114 -0
  74. data/app/views/bro_hub/env/shared/not_ready.html.erb +17 -0
  75. data/app/views/bro_hub/safe/flagged_commands/index.html.erb +22 -0
  76. data/app/views/bro_hub/safe/rules/show.html.erb +59 -0
  77. data/app/views/bro_hub/safe/sessions/index.html.erb +51 -0
  78. data/app/views/bro_hub/safe/sessions/show.html.erb +92 -0
  79. data/app/views/bro_hub/safe/unlocks/index.html.erb +25 -0
  80. data/app/views/bro_hub/shared/schema_missing.html.erb +25 -0
  81. data/app/views/layouts/bro_hub/application.html.erb +11 -2
  82. data/config/routes/apm.rb +10 -0
  83. data/config/routes/cache.rb +17 -0
  84. data/config/routes/env.rb +10 -0
  85. data/config/routes/safe.rb +10 -0
  86. data/config/routes.rb +12 -3
  87. data/db/migrate/20261129000000_create_bro_hub_tables.rb +85 -0
  88. data/lib/bro_hub/apm/api.rb +88 -0
  89. data/lib/bro_hub/apm/configuration.rb +58 -0
  90. data/lib/bro_hub/apm/local_status.rb +49 -0
  91. data/lib/bro_hub/apm.rb +28 -0
  92. data/lib/bro_hub/audit.rb +41 -0
  93. data/lib/bro_hub/cache/audit.rb +15 -0
  94. data/lib/bro_hub/cache/configuration.rb +105 -0
  95. data/lib/bro_hub/cache/errors.rb +26 -0
  96. data/lib/bro_hub/cache/redis_store.rb +220 -0
  97. data/lib/bro_hub/cache.rb +38 -0
  98. data/lib/bro_hub/configuration.rb +19 -4
  99. data/lib/bro_hub/controller_base.rb +3 -6
  100. data/lib/bro_hub/engine.rb +60 -1
  101. data/lib/bro_hub/env/audit.rb +17 -0
  102. data/lib/bro_hub/env/configuration.rb +72 -0
  103. data/lib/bro_hub/env/credentials.rb +135 -0
  104. data/lib/bro_hub/env/deployments.rb +45 -0
  105. data/lib/bro_hub/env/differ.rb +58 -0
  106. data/lib/bro_hub/env/editor.rb +104 -0
  107. data/lib/bro_hub/env/encrypted_blob.rb +18 -0
  108. data/lib/bro_hub/env/environment.rb +74 -0
  109. data/lib/bro_hub/env.rb +36 -0
  110. data/lib/bro_hub/features.rb +13 -0
  111. data/lib/bro_hub/layout.rb +2 -5
  112. data/lib/bro_hub/registry.rb +5 -0
  113. data/lib/bro_hub/safe/cipher.rb +46 -0
  114. data/lib/bro_hub/safe/commands.rb +25 -0
  115. data/lib/bro_hub/safe/configuration.rb +191 -0
  116. data/lib/bro_hub/safe/encrypted_type.rb +16 -0
  117. data/lib/bro_hub/safe/errors.rb +48 -0
  118. data/lib/bro_hub/safe/executor.rb +142 -0
  119. data/lib/bro_hub/safe/guards/const_guard.rb +42 -0
  120. data/lib/bro_hub/safe/guards/credentials_guard.rb +31 -0
  121. data/lib/bro_hub/safe/guards/env_guard.rb +90 -0
  122. data/lib/bro_hub/safe/guards/file_guard.rb +28 -0
  123. data/lib/bro_hub/safe/guards/guarded.rb +51 -0
  124. data/lib/bro_hub/safe/guards/irb_context.rb +23 -0
  125. data/lib/bro_hub/safe/guards/method_guard.rb +32 -0
  126. data/lib/bro_hub/safe/guards/network_guard.rb +98 -0
  127. data/lib/bro_hub/safe/guards/sql_guard.rb +26 -0
  128. data/lib/bro_hub/safe/parsed_command.rb +66 -0
  129. data/lib/bro_hub/safe/recorder.rb +82 -0
  130. data/lib/bro_hub/safe/shield.rb +83 -0
  131. data/lib/bro_hub/safe/supervisor.rb +81 -0
  132. data/lib/bro_hub/safe/terminal.rb +55 -0
  133. data/lib/bro_hub/safe/trail.rb +24 -0
  134. data/lib/bro_hub/safe/validator.rb +74 -0
  135. data/lib/bro_hub/safe.rb +134 -0
  136. data/lib/bro_hub/schema.rb +36 -0
  137. data/lib/bro_hub/section.rb +14 -10
  138. data/lib/bro_hub/testing.rb +62 -0
  139. data/lib/bro_hub/version.rb +1 -1
  140. data/lib/bro_hub.rb +32 -6
  141. data/lib/generators/bro_hub/install/install_generator.rb +93 -69
  142. data/lib/generators/bro_hub/install/templates/bro_hub.rb.tt +115 -0
  143. data/lib/generators/bro_hub/uninstall/templates/drop_bro_hub_tables.rb.tt +16 -0
  144. data/lib/generators/bro_hub/uninstall/uninstall_generator.rb +57 -0
  145. data/lib/tasks/bro_hub.rake +9 -0
  146. metadata +155 -10
  147. data/lib/bro_hub/root_middleware.rb +0 -17
  148. data/lib/generators/bro_hub/install/templates/bro_hub.rb +0 -11
  149. data/lib/generators/bro_hub/installable.rb +0 -63
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b1fbd1cddc86bdf0bc203daff3dbe7eab88f49d7046c2f7444b215b8a24ddee1
4
- data.tar.gz: 80a631c65cf8f57fca41e6c67b059a978aef763aa9fcdc1d386634b0bae43de7
3
+ metadata.gz: 2147a478c6231177b6f68351407584a74dabd22e2547a358dd56d1c55b5239c7
4
+ data.tar.gz: 4824766699023e1004daf5e292ac2caa5f9c01dea14cdc43a7c39edc09d7db24
5
5
  SHA512:
6
- metadata.gz: 5fc326ddbfcb13bbb77adae1928de8a1cbbdff6ab454c1b8896ea81a3d8c0d08ff3ecea53ed75af1e47f5eecb0c9cbf8a2691066d6ffe66a76560234af9ac022
7
- data.tar.gz: 3cc2551c4d6cdf13fce248f5b18991bdfd9c1c54cc39bfa4983e815898ddeb322da844880e469a760e094d47974357b11a9968ff8a99c2835f0db6c8945f18f2
6
+ metadata.gz: ff92615ecba858ea9f002558d9d741427b7e1666e261ab102e10552614a75c1086897fb3ea5469ea5e2adf3514693e124ce094f691012a16bb288f3e6a94d6d2
7
+ data.tar.gz: ea5bd9b35a0b464f93101a14c50281ea51b9fe92b8404dd438370f9dd3dc3a2e5c631830de14ce0bc80278b3ae6dd13258ef990cb8f3e84164cb7907d20d5d8d
data/CHANGELOG.md ADDED
@@ -0,0 +1,21 @@
1
+ # Changelog
2
+
3
+ ## 0.3.0
4
+
5
+ The first release to use. Earlier versions are superseded and should be ignored.
6
+
7
+ BroHub is one Rails engine with four opt-in features, switched on with `config.features`:
8
+
9
+ - `:safe` (SafeBro): audited, guarded production console with a review UI.
10
+ - `:env` (EnvBro): propose and review changes to encrypted credentials without holding the key.
11
+ - `:cache` (CacheBro): browse, inspect, delete, purge and flush the Rails Redis cache.
12
+ - `:apm` (DeadBro): DeadBro stats inside the dashboard. Needs the separate `dead_bro` gem.
13
+
14
+ Highlights:
15
+
16
+ - `bin/rails g bro_hub:install [--features=env,safe] [--mount-path=/bro_hub] [--migrate]` writes the initializer, mounts the engine, copies the one migration and prints next steps. Running it again is safe.
17
+ - `bin/rails g bro_hub:uninstall` generates a migration that drops every BroHub table, and removes the initializer and the mount.
18
+ - One migration creates every table. When a table or column is missing, every hub page renders a 503 telling you to run the installer and `db:migrate`.
19
+ - A feature that is off has no routes, no sidebar section, no console patches, and does nothing at boot.
20
+ - One combined audit log at `/<mount>/audit`, with key names, patterns, counts and reasons only, never secret values, cache values or command text.
21
+ - Requires Ruby 3.3+, Rails 7.1+ and IRB 1.13+.
data/README.md CHANGED
@@ -1,147 +1,264 @@
1
1
  # BroHub
2
2
 
3
- One dashboard shell for the Bro gems ([EnvBro](https://github.com/rubydevro/env_bro), [SafeBro](https://github.com/rubydevro/safe_bro), and whatever comes next).
3
+ One Rails engine, one mount point, one sidebar and one audit log for four tools you can turn on one by one: EnvBro, SafeBro, CacheBro and DeadBro.
4
4
 
5
- - **One mount point.** `/bro_hub/safe_bro`, `/bro_hub/env_bro`, ... BroHub mounts the gems' engines itself.
6
- - **One look.** Top bar, sidebar, breadcrumb bar and page styles are shared, so every gem feels the same.
7
- - **One sidebar.** Each gem registers its pages and shows up as a section. A section only appears for people allowed to use it.
8
- - **One place for "who is signed in".** Set it once for all gems.
5
+ | Feature | Key | URL | What it does |
6
+ | --- | --- | --- | --- |
7
+ | **SafeBro** · audit | `:safe` | `/<mount>/safe` | Records every production console session (who, why, every command, encrypted at rest), locks sensitive data until `unlock!`, checks commands before they run, and gives reviewers a UI to approve or flag sessions. |
8
+ | **EnvBro** · config | `:env` | `/<mount>/env` | Propose changes to Rails encrypted credentials without holding the key. A reviewer uploads the proposed file and sees which keys changed, values masked unless revealed. |
9
+ | **DeadBro** · performance | `:apm` | `/<mount>/apm` | Your DeadBro stats (endpoints, errors, SQL, findings, deploys, status) inside the dashboard. Needs the separate `dead_bro` gem. |
10
+ | **CacheBro** · cache | `:cache` | `/<mount>/cache` | Browse, inspect, delete and purge your Rails Redis cache, every action audited. |
9
11
 
10
- It is a shell, not a security boundary: hiding a section is navigation. Every Bro gem still authorizes its own pages.
12
+ The combined audit log of every feature is at `/<mount>/audit`. A feature that is off has no routes, no sidebar section, no console patches, and does nothing at boot.
11
13
 
12
- Rails 7.1+, Ruby 3.2+. No database, no JavaScript, no asset pipeline.
14
+ BroHub is a shell, not a security boundary: hiding a section is navigation. Every page checks its own permission.
15
+
16
+ Rails 7.1+, Ruby 3.3+, IRB 1.13+. Never stores secret values, cache values or console command text in the audit log: key names, patterns, counts and reasons only.
17
+
18
+ > **Read [What this is not](#what-this-is-not).** SafeBro is a deterrent and an audit trail. It is not a sandbox.
13
19
 
14
20
  ## Install
15
21
 
16
22
  ```ruby
23
+ # Gemfile
17
24
  gem "bro_hub"
18
25
  ```
19
26
 
20
- Minimum versions: **env_bro 0.2.0** and **safe_bro 0.1.0**. Older EnvBro (0.1.x) has no BroHub support: the installer skips
21
- it with a warning and leaves its old mount alone, so `bundle update env_bro` first.
22
-
23
27
  ```sh
24
- bin/rails generate bro_hub:install
28
+ bundle install
29
+ bin/rails g bro_hub:install [--features=env,safe] [--mount-path=/bro_hub] [--migrate]
25
30
  ```
26
31
 
27
- This sets up the whole dashboard in one go:
32
+ The generator writes `config/initializers/bro_hub.rb`, mounts the engine (`mount BroHub::Engine => "/bro_hub"`), copies the migration and prints next steps. Without `--features` it asks for each feature in a terminal; otherwise it turns on env, safe and cache, plus apm when the `dead_bro` gem is installed. Without `--migrate` it asks whether to run `bin/rails db:migrate` (in a terminal) or leaves it to you. It never overwrites an existing initializer or mount, so running it again is safe, and is how an upgrade picks up new migrations.
33
+
34
+ Then say who may do what. **Everything is denied until you say so** (see [Configuration](#configuration)). If the tables are missing, every hub page renders a 503 telling you to run `bin/rails g bro_hub:install` and `bin/rails db:migrate`.
28
35
 
29
- 1. adds `config/initializers/bro_hub.rb`,
30
- 2. mounts BroHub (it asks where, default `/bro_hub`; or `--mount-path=/ops/bro`):
31
- ```ruby
32
- mount BroHub::Engine => "/bro_hub"
33
- ```
34
- 3. offers to comment out old direct mounts such as `mount SafeBro::Engine`, since BroHub now mounts those gems itself
35
- (`--remove-direct-mounts` says yes without asking; without a terminal the lines are left alone with a warning),
36
- 4. runs the installer of every Bro gem in your Gemfile (SafeBro's initializer and migrations, EnvBro's, ...).
36
+ ### Which version
37
37
 
38
- Existing config is never overwritten: if `config/initializers/bro_hub.rb` (or `env_bro.rb` / `safe_bro.rb`) is there already,
39
- it is skipped, and `--skip` / `--force` / `--pretend` are passed on to every gem's installer. In scripts and agents, pass
40
- `--skip` and `--mount-path=...` so nothing waits on a prompt.
38
+ Use **0.3.0 or newer**. Earlier releases are superseded and should be ignored; nothing from them is read or migrated.
41
39
 
42
- You can also run a gem's own installer on its own: `bin/rails generate safe_bro:install` sets up BroHub first, then SafeBro.
43
- Running both, in any order, mounts BroHub once.
40
+ ### Turning a feature on later
44
41
 
45
- Who is signed in: the default calls `current_user` on your controller, and falls back to `Current.user` (the Rails 8
46
- authentication generator has only that). The top-right label tries `email`, `email_address` (Rails 8) then `name`.
42
+ Add it to `config.features` in the initializer and set its permissions:
47
43
 
48
44
  ```ruby
49
- # config/initializers/bro_hub.rb
50
45
  BroHub.configure do |config|
51
- config.current_user = ->(controller) { controller.current_user } # default: calls current_user if present
52
- config.user_label = ->(user) { user.email }
53
- config.parent_controller = "::ApplicationController"
46
+ config.features = %i[ env safe cache apm ]
54
47
  end
55
48
  ```
56
49
 
57
- Opening `/bro_hub` sends people to the first section they may use.
50
+ The `:apm` feature needs `gem "dead_bro"` in your Gemfile; without it BroHub raises an error at boot that says so. Re-run `bin/rails g bro_hub:install` and `bin/rails db:migrate` after upgrading the gem.
51
+
52
+ ## Configuration
53
+
54
+ Everything is in `config/initializers/bro_hub.rb`, as `BroHub.configure { |config| ... }`, readable afterwards as `BroHub.config.<key>`.
55
+
56
+ ### Shared settings
58
57
 
59
- ## For gem authors: plugging in
58
+ | Setting | What it does |
59
+ | --- | --- |
60
+ | `features` | Which features are on, any of `:safe :env :apm :cache`. Default none. |
61
+ | `current_user` | `->(controller) { user }`. Default calls `current_user`, then `Current.user` (Rails 8 authentication generator). |
62
+ | `user_label` | `->(user) { text }` shown top right. Default tries `email`, `email_address`, `name`. |
63
+ | `parent_controller` | Controller the hub's pages inherit from, so your authentication runs. Default `"::ApplicationController"`. |
64
+ | `require_ssl` | Refuse plain HTTP. Default `Rails.env.production?`. |
60
65
 
61
- **1. Register your section** (in your engine file, so it runs before routes are drawn):
66
+ Every hub response sends `Cache-Control: no-store`. Permissions are lambdas that receive the signed-in user (nil when signed out). A feature's section only shows for people holding one of its permissions.
67
+
68
+ ### SafeBro (`BroHub.config.safe`)
69
+
70
+ Audited, guarded Rails console access, with a built-in review UI. Every session records who opened it and why. Every command is checked before it runs and stored encrypted. Sensitive data stays locked until the person explains why they need it, and reviewers approve or flag each session.
62
71
 
63
72
  ```ruby
64
- BroHub.register :safe_bro,
65
- label: "SafeBro · audit", # sidebar heading
66
- name: "SafeBro", # breadcrumb (default: label up to the "·")
67
- accent: :red, # :red :green :violet :amber :blue, or "#hex"
68
- engine: "SafeBro::Engine", # BroHub mounts it at /<key>
69
- order: 10, # lower comes first
70
- visible_to: ->(user) { SafeBro.config.can_audit.call(user) },
71
- items: [
72
- { label: "Sessions", path: "/", badge: -> { SafeBro::Session.pending.count } },
73
- { label: "Flagged commands", path: "/flagged" },
74
- { label: "Rules", path: "/rules", match: %r{\A/rules} },
75
- { label: "Raw export", path: "/export", visible_to: ->(user) { user.admin? } } # per-item visibility
76
- ]
73
+ config.safe.can_audit = ->(user) { user.admin? } # who may review sessions
77
74
  ```
78
75
 
79
- - `path` is relative to your engine. Badges are hidden when `nil` or `0`, and a badge that raises is skipped.
80
- - Active item: exact match for `"/"`, prefix match otherwise, or your own `match:` regexp.
81
- - A section only shows up in the shared sidebar when you pass `engine:` (BroHub then knows where it lives). A gem the host mounts elsewhere still gets the shell, with just its own section.
82
- - Never put anything that came from a user in `accent` or `label`. `accent` is validated; `label` is escaped.
76
+ Nothing is installed by loading the gem. Protection starts when a console starts, in `config.safe.protected_environments` (default `production`).
77
+
78
+ **Telling SafeBro who is at the console.** It reads `CONSOLE_USER`. A name is only as trustworthy as whatever sets it. Prefer the server setting it:
79
+
80
+ ```
81
+ # ~/.ssh/authorized_keys on the server, one line per person
82
+ environment="CONSOLE_USER=jane" ssh-ed25519 AAAA... jane@laptop
83
+ ```
83
84
 
84
- **2. Render inside the shell:**
85
+ (needs `PermitUserEnvironment yes` in `sshd_config`). A client-side `SetEnv` works too, but then anyone can claim to be anyone. With no `CONSOLE_USER` the console refuses to start. Use `config.safe.username_resolver = -> { ... }`, or `config.safe.ask_for_username_if_empty = true` to ask on the terminal. With no terminal, set `CONSOLE_REASON` (for example `CONSOLE_REASON="deploy check" rails console`); `unlock!` always needs a terminal.
86
+
87
+ **What it looks like**
88
+
89
+ ```
90
+ $ rails console
91
+ You have access to production data here. That's a big deal. ...
92
+ jane, why are you using this console today?
93
+ > Fixing invoice 12, ticket #4521
94
+
95
+ myapp(prod):002> ENV["STRIPE_SECRET_KEY"]
96
+ SafeBro: flagged for review - Access to env is protected. (STRIPE_SECRET_KEY) Run unlock! ...
97
+
98
+ myapp(prod):003> unlock! :env
99
+ > Ticket #4521: customer asked us to rotate their key
100
+ Unlocked. We pay extra close attention to everything you do now. Type lock! to protect it again.
101
+ ```
102
+
103
+ | Command | What it does |
104
+ | --- | --- |
105
+ | `unlock!` | Unlock everything. Asks for a justification and records it. |
106
+ | `unlock! :credentials` | Unlock only some resources (`encrypted_data`, `credentials`, `env`, `files`, `network`). |
107
+ | `lock!` | Lock again. |
108
+
109
+ **What is protected** (`protected_resources`, all on by default)
110
+
111
+ | Resource | While locked |
112
+ | --- | --- |
113
+ | `encrypted_data` | Active Record encrypted attributes read as ciphertext and can't be written. |
114
+ | `credentials` | `Rails.application.credentials`, `secret_key_base`, `key_generator`, `message_verifier` and the encrypted file classes raise when called from the console. |
115
+ | `env` | `ENV["SOME_SECRET"]` raises for names matching `protected_env_patterns`. Dumping `ENV` (`to_h`, `each`, `each_value`, `except`, `filter_map`, `inspect`, ...) filters those values. |
116
+ | `files` | `master.key`, `credentials/*.key` and `.env*` files can't be read or copied. |
117
+ | `network` | Connections to `protected_urls` fail, including writes on connections opened earlier. |
118
+
119
+ Library code is not affected: Rails reading its credentials while it runs your command works. Only calls made directly from what the person typed are blocked, except `network` and `inspect`, which apply to anything running during a command.
120
+
121
+ **Guardrails.** Each command is checked before it runs. Blocked: reopening protected classes, referencing `BroHub`/`IRB`/`ObjectSpace`, and calling `eval`, `Binding#eval`, `instance_eval`, `class_eval`, `system`, `exec`, `spawn`, backticks, `IO.popen`, and others. `load` is blocked at runtime when called from the console. Flagged: commands mentioning `secret`, `credentials`, `ENV`, `master.key`, ... run but are marked suspicious. Runtime backstops cover `send(:eval, ...)` and `Object.const_get("Bro" + "Hub")`. `super_method` is forbidden, so a guard can't be walked past to the original method. A probe the command rescues itself (`ENV["X_SECRET"] rescue nil`) is still flagged. The audit trail is guarded from what's typed at the console: SQL mentioning BroHub's tables is refused, and `raw_connection` and `with_raw_connection` are locked (under `credentials`, so `unlock!` opens them). SafeBro's classes, its guard modules and its config are frozen once installed. See [What this is not](#what-this-is-not) for the limits.
122
+
123
+ | Setting | Default and purpose |
124
+ | --- | --- |
125
+ | `protected_environments` | `%w[production]`. Consoles in these environments are recorded and guarded. |
126
+ | `enabled` | Force on or off regardless of environment. |
127
+ | `audit_runner` | `false`. Also record `rails runner` as sessions (`CONSOLE_USER` and `CONSOLE_REASON` are used if set). Runners aren't shielded. |
128
+ | `username_resolver`, `ask_for_username_if_empty` | See above. |
129
+ | `client_ip_resolver` | Where the person connected from. Default `CONSOLE_CLIENT_IP`, then `SSH_CLIENT` / `SSH_CONNECTION`; anything that isn't an IP is ignored. Behind a container wrapper pass it in: `docker run -e CONSOLE_USER -e CONSOLE_CLIENT_IP="${SSH_CLIENT%% *}" ...`. |
130
+ | `protected_resources`, `protected_urls`, `protected_env_patterns`, `protected_file_patterns` | What is locked. |
131
+ | `forbidden_reopening`, `forbidden_constants`, `forbidden_methods`, `runtime_only_methods`, `suspicious_terms` | Guardrails, for example `config.safe.forbidden_methods += [ "Kernel#open" ]`, `config.safe.forbidden_constants[:locked] += [ "Net::HTTP" ]`, `config.safe.suspicious_terms += [ "DROP TABLE" ]`. |
132
+ | `freeze_internals` | `true`. Freeze the classes and config once installed. |
133
+ | `can_audit` | `->(user) { }` may review sessions. Denied by default. |
134
+ | `allow_self_audit` | `false`. May a reviewer approve their own sessions. |
135
+ | `console_username` | `->(user) { name }` the name a hub user goes by at the console (what `username_resolver` returns for them), so nobody reviews their own sessions. Default `BroHub.config.user_label`. **Set it:** `user_label` is usually an email and `CONSOLE_USER` a login name, so with the default they never match and self review isn't prevented. The Rules page warns until it's mapped, e.g. `config.safe.console_username = ->(user) { user.console_name }`. |
136
+ | `retention`, `keep_flagged` | `90.days`, `true`. Sessions older than this are removed (flagged ones kept). `nil` keeps everything. |
137
+ | `encryption_key` | 32 bytes (or a lambda) encrypting recorded commands. Default derived from `secret_key_base`. |
138
+ | `welcome_message`, `unlock_prompt`, `unlocked_message`, `locked_message` | Texts shown in the console. |
139
+
140
+ **Reviewing sessions** at `/<mount>/safe`: Sessions (filterable; open one to read it and approve it or flag it with notes), Flagged commands, Unlocks (who, which resources, the reason) and Rules (a read-only view of what's locked, blocked and flagged). Reviews are append-only: the latest decides the status and the history is kept. Command text and notes are encrypted at rest. When a session is purged, the hub audit trail rows for it are removed too.
141
+
142
+ **Retention.** Schedule `BroHub::Safe::PurgeJob` (daily) or run `bin/rails bro_hub:safe:purge`.
143
+
144
+ **Notifications.** SafeBro instruments `ActiveSupport::Notifications` with `<event>.bro_hub_safe`:
145
+
146
+ | Event | Payload |
147
+ | --- | --- |
148
+ | `session_started.bro_hub_safe` | `session:` |
149
+ | `unlocked.bro_hub_safe` | `session:`, `unlock:` |
150
+ | `command_flagged.bro_hub_safe` | `session:`, `command:`, `status:`, `reason:` |
151
+ | `session_flagged.bro_hub_safe` | `session:`, `reviewer:`, `notes:` |
85
152
 
86
153
  ```ruby
87
- class SafeBro::ApplicationController < ::ApplicationController
88
- include BroHub::ControllerBase
89
- bro_hub_section :safe_bro
154
+ ActiveSupport::Notifications.subscribe(/\.bro_hub_safe\z/) do |name, _start, _finish, _id, payload|
155
+ SecurityMailer.console_event(name, payload).deliver_later
90
156
  end
91
157
  ```
92
158
 
93
- `BroHub::ControllerBase` includes `BroHub::Layout` (the layout, and `bro_hub_user`), turns on CSRF protection and opts out of the host app's Pundit `verify_authorized` checks, so every Bro controller behaves the same under any host `ApplicationController`. Override it if your gem has its own idea of who is signed in.
159
+ **For gem authors.** Anything sensitive your gem exposes can use the same lock: add `config.safe.protected_resources += [ :billing ]` in the host app, and call `BroHub::Safe.guard!(:billing)` before revealing something. It does nothing outside a console command, and raises inside one until the person runs `unlock! :billing`.
160
+
161
+ #### What this is not
162
+
163
+ Ruby is too dynamic to sandbox. Someone determined who has a console on your production box can get around the static checks and the runtime guards, for example by reading process memory, speaking to the database driver directly, using `dbconsole`, or loading code from disk. SafeBro raises the cost, catches mistakes, makes honest people stop and explain, and leaves a record that dishonest attempts show up in. That's valuable, but it's not a boundary.
164
+
165
+ Specifically:
166
+
167
+ - Someone at the console can still reach SafeBro's own objects and change their state with `instance_variable_set`. That is flagged for review, not blocked.
168
+ - The audit trail is not tamper-proof. After `unlock! :credentials` the driver connection (`raw_connection`) runs SQL that SafeBro doesn't see. Keep a copy elsewhere (backups, database audit logs) if you need one nobody at the console can touch.
169
+ - Code run outside the console (a `require` of a file, ERB, another thread) isn't seen by the console-call guards.
170
+ - `reload!` and `debug=` don't work once internals are frozen. `config.safe.freeze_internals = false` turns freezing off.
171
+
172
+ If you need a boundary, build it outside Ruby:
173
+
174
+ - Don't leave the credentials key on machines people shell into. Deliver it to the app process only.
175
+ - Give console users a database role that can't read what they shouldn't.
176
+ - Control who can SSH in, and set `CONSOLE_USER` from the server side.
177
+ - Have a reviewer actually review sessions. An unreviewed audit trail deters nothing.
178
+
179
+ Not covered: `rails dbconsole`, a raw `psql`/`mysql` session, other process managers or shells, and Pry (SafeBro hooks IRB).
180
+
181
+ **Moving from console1984 and audits1984?** SafeBro is its own implementation, written from scratch, not a fork or copy of either. Don't run them side by side: both hook IRB and would record every command twice. Their `console1984_*` and `audits1984_*` tables are not read or migrated; keep them as long as you need the history. Map `protected_environments` to the same option and the auditor check to `config.safe.can_audit`.
182
+
183
+ ### EnvBro (`BroHub.config.env`)
184
+
185
+ Propose changes to Rails encrypted credentials without holding the key. EnvBro manages the credentials of the environment it runs in (`production.yml.enc` on production). It never writes to the credentials file.
186
+
187
+ 1. **Propose.** The user sees key names, never values. EnvBro decrypts the deployed file in memory, applies the changes, re-encrypts it with the same key and returns it as a download to commit on a branch.
188
+ 2. **Metadata.** The new file includes a top-level key (`config.env.metadata_key`, default `env_bro`) with a proposal id, the SHA-256 of the file it was built on, the author, the time and the declared changes.
189
+ 3. **Review.** The reviewer uploads the proposed file and sees added, changed and removed keys, values masked unless revealed. *Outdated base* appears when it was built on a different version than the one compared against; *not built with EnvBro* when it has no metadata.
190
+ 4. **Audit.** Proposals, reviews and every reveal go to the audit log with key names only.
191
+ 5. **Deployment tracking.** On boot EnvBro records when the running credentials came from a proposal, so the log shows each as *live since ...*, *deployed, replaced since*, *superseded* or *not deployed yet*. Every deployment is recorded, so rollbacks show the right live proposal. The check never stops the app from booting.
192
+
193
+ ```ruby
194
+ config.env.can_review = ->(user) { user.admin? } # upload a proposal and see what changed
195
+ config.env.can_create = ->(user) { user.developer? } # propose new keys
196
+ config.env.can_update = ->(user) { user.developer? } # propose new values
197
+ config.env.can_destroy = ->(user) { user.developer? } # propose removing keys
198
+ config.env.can_reveal = ->(user) { user.admin? } # see actual values in a review (audited)
199
+ ```
200
+
201
+ Higher includes lower: review includes create, update and destroy; reveal includes everything. `config.env.can_propose = ->(user) { ... }` sets create, update and destroy at once. Other settings: `metadata_key` (`nil` disables metadata, the outdated-base warning and deployment tracking), `track_deployments` (default `true`), `max_upload_bytes` (default 1 MB; caps both the uploaded files and the files the reveal form sends back).
94
202
 
95
- **3. Give your gem an installer** that sets up BroHub first, then itself. Name it `<section key>:install` so
96
- `bro_hub:install` finds it:
203
+ ### CacheBro (`BroHub.config.cache`)
204
+
205
+ Browse, inspect and purge the Rails **Redis** cache: search keys (plain text matches anywhere, `*` and `?` make a pattern), see expiry and size, reveal a value (its own permission, always audited), delete one key, purge a pattern (you see the count and a sample first), flush a store's namespace (you type its name to confirm).
206
+
207
+ Give your cache store a namespace, so CacheBro stays out of keys that aren't the cache's:
97
208
 
98
209
  ```ruby
99
- # lib/generators/safe_bro/install/install_generator.rb
100
- require "generators/bro_hub/installable"
210
+ config.cache_store = :redis_cache_store, { url: ENV["REDIS_URL"], namespace: "myapp" }
211
+ ```
101
212
 
102
- class SafeBro::Generators::InstallGenerator < Rails::Generators::Base
103
- include BroHub::Generators::Installable # keep this first: it adds the BroHub step and --mount-path
213
+ Without one you get a warning and Flush is disabled.
104
214
 
105
- def copy_initializer ... end
106
- def next_steps
107
- say "Visit #{bro_hub_path}/safe_bro" # bro_hub_path: where BroHub ended up
108
- end
109
- end
215
+ ```ruby
216
+ config.cache.can_view_keys = ->(user) { user.developer? } # open CacheBro, search keys, TTL and size
217
+ config.cache.can_view_values = ->(user) { user.senior? } # reveal values (audited; may hold personal data)
218
+ config.cache.can_delete = ->(user) { user.developer? }
219
+ config.cache.can_purge = ->(user) { user.senior? }
220
+ config.cache.can_flush = ->(user) { user.admin? }
221
+ config.cache.can_audit = ->(user) { user.admin? }
110
222
  ```
111
223
 
112
- **4. Fill in the page:**
224
+ Higher includes lower: flush includes purge and delete, purge includes delete, and all include view_keys. `can_view_values` and `can_audit` stand alone. Other settings: `stores` (`{ "Name" => -> { store } }`, only `:redis_cache_store` is supported), `reason_required_for` (any of `:reveal :delete :purge :flush`, default `%i[purge flush]`), `max_purge_keys` (100,000), `page_size`, `scan_count`, `scan_budget`, `value_preview_length`, `audit_sample_size`.
225
+
226
+ ### DeadBro (`BroHub.config.apm`)
113
227
 
114
- ```erb
115
- <% bro_hub_crumb "Console sessions", "#5" %> <%# "SafeBro / Console sessions / #5" %>
116
- <% content_for :bro_hub_crumb_actions do %>
117
- <%= bro_hub_crumb_link "← All sessions", sessions_path %> <%# right side of the bar %>
118
- <% end %>
228
+ Reads DeadBro with the API key the `dead_bro` gem already uses, so there is nothing to configure except who may look. Pages: Overview, Endpoints, Errors, SQL queries, Findings, Deploys and Status. The pages show SQL, exception messages and request paths, so keep it to people who can already see that. It keeps no audit trail of its own.
119
229
 
120
- <%= bro_hub_hero eyebrow: "console · web-1 (pid 4417)", title: "Session #5", subtitle: "Pending",
121
- subtitle_tone: :green, # :red :green :amber :violet :muted
122
- lead: "jane was on a production console for 1m.",
123
- stats: [ [ 4, "unlocks", :amber ], [ 6, "flagged commands", :red ] ] %>
230
+ ```ruby
231
+ config.apm.can_view = ->(user) { user.admin? }
124
232
  ```
125
233
 
126
- Page styles are plain CSS classes prefixed `bh-` (see `lib/bro_hub/styles.css`): `bh-block`, `bh-split`, `bh-table`/`bh-row`, `bh-tag`, `bh-filters`, `bh-input`, `bh-btn`, `bh-seg`, `bh-log`/`bh-cmd`, `bh-dl`, `bh-pager`, and so on. `bh-tag` is uppercase; add `is-code` for case-sensitive text such as method names. Tables take their columns from `style="--cols: 150px 1fr 80px"`.
234
+ Other settings: `cache_ttl` (seconds a response is cached in `Rails.cache`, default 30, 0 = off), `api_key`, `base_url`, `open_timeout`, `read_timeout`. Needs `gem "dead_bro"` and `:apm` in `config.features`.
235
+
236
+ ### Audit log
237
+
238
+ Every feature writes its key moments (a proposal, a reveal, a purge, a session starting, an unlock with its justification, a reviewer's verdict) to one table, shown at `/<mount>/audit`. People only see the rows of features whose audit they may read. Rows are append-only.
127
239
 
128
- ## Notes
240
+ ## Uninstall
129
241
 
130
- - The layout loads IBM Plex Mono and Archivo from Google Fonts, and embeds the skull as a `data:` image. Under a strict Content Security Policy allow `fonts.googleapis.com`, `fonts.gstatic.com` and `img-src data:`. Without the fonts it falls back to system fonts. The inline `<style>` uses your CSP nonce when you have one.
131
- - The shell's own pages send `Cache-Control: no-store` and refuse plain HTTP in production (`config.require_ssl`).
132
- - The look is dark only.
242
+ ```sh
243
+ bin/rails g bro_hub:uninstall [--force]
244
+ bin/rails db:migrate
245
+ ```
246
+
247
+ This writes a migration that drops every BroHub table (the audit history and console sessions go with it), removes the initializer and the mount, then you remove `gem "bro_hub"` from the Gemfile. Without a terminal it needs `--force`.
133
248
 
134
249
  ## Development
135
250
 
136
251
  ```sh
137
- bin/rails test # Rails 8.x
138
- BUNDLE_GEMFILE=gemfiles/rails_7_1.gemfile bundle exec rails test # Rails 7.1
252
+ bin/rails test
253
+ REDIS_URL=redis://localhost:6379/15 bin/rails test # includes the Redis tests
254
+ BUNDLE_GEMFILE=gemfiles/rails_7_1.gemfile bin/rails test # Rails 7.1
139
255
  bin/rubocop
140
- cd test/dummy && bin/rails server # try it: /hub (Alpha, Beta), /solo (Gamma)
141
256
  ```
142
257
 
143
- The dummy app has three fake Bro gems (`test/support/fake_bros`). Alpha and Beta live in the hub, Gamma is mounted on its own.
144
-
145
258
  ## License
146
259
 
147
260
  MIT.
261
+
262
+ ---
263
+
264
+ Built by the [DeadBro team](https://www.deadbro.com).
@@ -0,0 +1,26 @@
1
+ module BroHub
2
+ module Apm
3
+ class ApplicationController < BroHub::FeatureController
4
+ bro_hub_feature :apm
5
+
6
+ private
7
+ def apm_api
8
+ BroHub::Apm.api
9
+ end
10
+
11
+ # Runs the block (a call to DeadBro) and returns its data. When DeadBro can't answer, @error is set
12
+ # and the page renders a notice instead of failing: the hub must not 500 because DeadBro is down.
13
+ def apm_fetch
14
+ yield
15
+ rescue BroHub::Apm::Api::Error => error
16
+ @error = error
17
+ nil
18
+ end
19
+
20
+ # Only these reach DeadBro; everything else in the query string is dropped.
21
+ def apm_params(*keys)
22
+ params.permit(*keys).to_h.symbolize_keys
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,7 @@
1
+ module BroHub::Apm
2
+ class DeploysController < ApplicationController
3
+ def index
4
+ @data = apm_fetch { apm_api.deploys }
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,8 @@
1
+ module BroHub::Apm
2
+ class EndpointsController < ApplicationController
3
+ def index
4
+ @filters = apm_params(:window, :sort)
5
+ @data = apm_fetch { apm_api.endpoints(@filters) }
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,15 @@
1
+ module BroHub::Apm
2
+ class ErrorsController < ApplicationController
3
+ PER_PAGE = 25
4
+
5
+ def index
6
+ @offset = [ params[:offset].to_i, 0 ].max
7
+ @data = apm_fetch { apm_api.errors(offset: @offset, limit: PER_PAGE) }
8
+ end
9
+
10
+ def show
11
+ @error_issue = apm_fetch { apm_api.error(params[:id]) }
12
+ render :not_found, status: :not_found if @error&.kind == :not_found
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,8 @@
1
+ module BroHub::Apm
2
+ class FindingsController < ApplicationController
3
+ def index
4
+ @filters = apm_params(:status)
5
+ @data = apm_fetch { apm_api.findings(@filters) }
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,7 @@
1
+ module BroHub::Apm
2
+ class OverviewController < ApplicationController
3
+ def show
4
+ @overview = apm_fetch { apm_api.overview }
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,11 @@
1
+ module BroHub::Apm
2
+ class QueriesController < ApplicationController
3
+ PER_PAGE = 25
4
+
5
+ def index
6
+ @filters = apm_params(:window, :sort, :search)
7
+ @offset = [ params[:offset].to_i, 0 ].max
8
+ @data = apm_fetch { apm_api.queries(@filters.merge(offset: @offset, limit: PER_PAGE)) }
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ module BroHub::Apm
2
+ class StatusController < ApplicationController
3
+ # Two halves: what the gem has in memory here, and what DeadBro tells it to do.
4
+ def show
5
+ @local = LocalStatus.snapshot
6
+ @remote = apm_fetch { apm_api.settings }
7
+ end
8
+ end
9
+ end
@@ -2,10 +2,26 @@ module BroHub
2
2
  class ApplicationController < BroHub.config.parent_controller.constantize
3
3
  include BroHub::ControllerBase
4
4
 
5
+ # First among BroHub's own callbacks, so BroHub's and the features' authorization never query a table that
6
+ # doesn't exist yet. Callbacks on the host's parent_controller (say authenticate_user!) run earlier and are
7
+ # out of our control.
8
+ before_action :bro_hub_require_schema
5
9
  before_action :bro_hub_require_ssl
6
10
  before_action :bro_hub_no_store
7
11
 
12
+ rescue_from BroHub::Forbidden do
13
+ render "bro_hub/shared/forbidden", status: :forbidden
14
+ end
15
+
8
16
  private
17
+ def bro_hub_require_schema
18
+ return if BroHub::Schema.ready?
19
+
20
+ bro_hub_no_store
21
+ @missing = BroHub::Schema.missing
22
+ render "bro_hub/shared/schema_missing", layout: false, status: :service_unavailable
23
+ end
24
+
9
25
  def bro_hub_require_ssl
10
26
  head :forbidden if BroHub.config.require_ssl && !request.ssl?
11
27
  end
@@ -0,0 +1,56 @@
1
+ module BroHub
2
+ # The audit trail of every Bro gem in one place. Each person sees the rows of the gems they may audit.
3
+ class AuditEventsController < ApplicationController
4
+ PER_PAGE = 50
5
+
6
+ before_action :require_auditor
7
+ helper_method :filter_params, :sections
8
+
9
+ def index
10
+ @filter = filter_params
11
+ @events = filtered.recent.limit(PER_PAGE).offset(page * PER_PAGE)
12
+ @total = filtered.count
13
+ @page = page
14
+ @counts = visible.group(:source).count
15
+ @event_names = visible.distinct.order(:event).pluck(:event)
16
+ end
17
+
18
+ def show
19
+ @event = visible.find(params[:id])
20
+ end
21
+
22
+ private
23
+ def require_auditor
24
+ render "bro_hub/shared/forbidden", status: :forbidden if sections.empty?
25
+ end
26
+
27
+ def sections
28
+ @sections ||= BroHub.registry.audited_by(bro_hub_user)
29
+ end
30
+
31
+ # Only the rows of enabled features this person may audit (sections are registered per enabled feature).
32
+ def visible
33
+ sections.map { |section| AuditEvent.for_feature(section.key) }.reduce(AuditEvent.none, :or)
34
+ end
35
+
36
+ def filtered
37
+ scope = visible
38
+ scope = scope.where(source: filter_params[:source]) if filter_params[:source].present?
39
+ scope = scope.where(event: filter_params[:event]) if filter_params[:event].present?
40
+ scope = scope.where(user_label: filter_params[:user]) if filter_params[:user].present?
41
+ if filter_params[:q].present?
42
+ like = "%#{AuditEvent.sanitize_sql_like(filter_params[:q])}%"
43
+ scope = scope.where("target LIKE :like OR reason LIKE :like", like: like)
44
+ end
45
+ scope
46
+ end
47
+
48
+ def filter_params
49
+ @filter_params ||= params.permit(:source, :event, :user, :q).to_h.symbolize_keys.compact_blank
50
+ end
51
+
52
+ def page
53
+ [ params[:page].to_i, 0 ].max
54
+ end
55
+ end
56
+ end