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.
Files changed (147) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +21 -0
  3. data/README.md +175 -165
  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 +2 -2
  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 +5 -19
  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/audit_event.rb +2 -6
  38. data/app/models/bro_hub/cache/audit_event.rb +15 -0
  39. data/app/models/bro_hub/env/audit_event.rb +72 -0
  40. data/app/models/bro_hub/safe/command.rb +22 -0
  41. data/app/models/bro_hub/safe/review.rb +22 -0
  42. data/app/models/bro_hub/safe/session.rb +57 -0
  43. data/app/models/bro_hub/safe/unlock.rb +15 -0
  44. data/app/views/bro_hub/apm/deploys/index.html.erb +21 -0
  45. data/app/views/bro_hub/apm/endpoints/index.html.erb +27 -0
  46. data/app/views/bro_hub/apm/errors/index.html.erb +22 -0
  47. data/app/views/bro_hub/apm/errors/not_found.html.erb +3 -0
  48. data/app/views/bro_hub/apm/errors/show.html.erb +43 -0
  49. data/app/views/bro_hub/apm/findings/index.html.erb +24 -0
  50. data/app/views/bro_hub/apm/overview/show.html.erb +59 -0
  51. data/app/views/bro_hub/apm/queries/index.html.erb +37 -0
  52. data/app/views/bro_hub/apm/shared/_page_styles.html.erb +15 -0
  53. data/app/views/bro_hub/apm/status/show.html.erb +47 -0
  54. data/app/views/bro_hub/audit_events/index.html.erb +1 -1
  55. data/app/views/bro_hub/cache/audit_events/index.html.erb +39 -0
  56. data/app/views/bro_hub/cache/audit_events/show.html.erb +48 -0
  57. data/app/views/bro_hub/cache/flushes/new.html.erb +19 -0
  58. data/app/views/bro_hub/cache/keys/index.html.erb +36 -0
  59. data/app/views/bro_hub/cache/keys/show.html.erb +57 -0
  60. data/app/views/bro_hub/cache/purges/new.html.erb +17 -0
  61. data/app/views/bro_hub/cache/purges/preview.html.erb +37 -0
  62. data/app/views/bro_hub/cache/shared/_store_tabs.html.erb +9 -0
  63. data/app/views/bro_hub/cache/shared/unsupported.html.erb +3 -0
  64. data/app/views/bro_hub/env/audit/index.html.erb +31 -0
  65. data/app/views/bro_hub/env/home/index.html.erb +25 -0
  66. data/app/views/bro_hub/env/proposals/_new_key_row.html.erb +7 -0
  67. data/app/views/bro_hub/env/proposals/new.html.erb +99 -0
  68. data/app/views/bro_hub/env/reviews/new.html.erb +29 -0
  69. data/app/views/bro_hub/env/reviews/show.html.erb +122 -0
  70. data/app/views/bro_hub/env/shared/_page_styles.html.erb +57 -0
  71. data/app/views/bro_hub/env/shared/_script.html.erb +114 -0
  72. data/app/views/bro_hub/env/shared/not_ready.html.erb +17 -0
  73. data/app/views/bro_hub/safe/flagged_commands/index.html.erb +22 -0
  74. data/app/views/bro_hub/safe/rules/show.html.erb +59 -0
  75. data/app/views/bro_hub/safe/sessions/index.html.erb +51 -0
  76. data/app/views/bro_hub/safe/sessions/show.html.erb +92 -0
  77. data/app/views/bro_hub/safe/unlocks/index.html.erb +25 -0
  78. data/app/views/bro_hub/shared/schema_missing.html.erb +25 -0
  79. data/app/views/layouts/bro_hub/application.html.erb +1 -1
  80. data/config/routes/apm.rb +10 -0
  81. data/config/routes/cache.rb +17 -0
  82. data/config/routes/env.rb +10 -0
  83. data/config/routes/safe.rb +10 -0
  84. data/config/routes.rb +9 -4
  85. data/db/migrate/20261129000000_create_bro_hub_tables.rb +85 -0
  86. data/lib/bro_hub/apm/api.rb +88 -0
  87. data/lib/bro_hub/apm/configuration.rb +58 -0
  88. data/lib/bro_hub/apm/local_status.rb +49 -0
  89. data/lib/bro_hub/apm.rb +28 -0
  90. data/lib/bro_hub/audit.rb +2 -9
  91. data/lib/bro_hub/cache/audit.rb +15 -0
  92. data/lib/bro_hub/cache/configuration.rb +105 -0
  93. data/lib/bro_hub/cache/errors.rb +26 -0
  94. data/lib/bro_hub/cache/redis_store.rb +220 -0
  95. data/lib/bro_hub/cache.rb +38 -0
  96. data/lib/bro_hub/configuration.rb +19 -4
  97. data/lib/bro_hub/controller_base.rb +3 -6
  98. data/lib/bro_hub/engine.rb +60 -1
  99. data/lib/bro_hub/env/audit.rb +17 -0
  100. data/lib/bro_hub/env/configuration.rb +72 -0
  101. data/lib/bro_hub/env/credentials.rb +135 -0
  102. data/lib/bro_hub/env/deployments.rb +45 -0
  103. data/lib/bro_hub/env/differ.rb +58 -0
  104. data/lib/bro_hub/env/editor.rb +104 -0
  105. data/lib/bro_hub/env/encrypted_blob.rb +18 -0
  106. data/lib/bro_hub/env/environment.rb +74 -0
  107. data/lib/bro_hub/env.rb +36 -0
  108. data/lib/bro_hub/features.rb +13 -0
  109. data/lib/bro_hub/layout.rb +2 -5
  110. data/lib/bro_hub/safe/cipher.rb +46 -0
  111. data/lib/bro_hub/safe/commands.rb +25 -0
  112. data/lib/bro_hub/safe/configuration.rb +191 -0
  113. data/lib/bro_hub/safe/encrypted_type.rb +16 -0
  114. data/lib/bro_hub/safe/errors.rb +48 -0
  115. data/lib/bro_hub/safe/executor.rb +142 -0
  116. data/lib/bro_hub/safe/guards/const_guard.rb +42 -0
  117. data/lib/bro_hub/safe/guards/credentials_guard.rb +31 -0
  118. data/lib/bro_hub/safe/guards/env_guard.rb +90 -0
  119. data/lib/bro_hub/safe/guards/file_guard.rb +28 -0
  120. data/lib/bro_hub/safe/guards/guarded.rb +51 -0
  121. data/lib/bro_hub/safe/guards/irb_context.rb +23 -0
  122. data/lib/bro_hub/safe/guards/method_guard.rb +32 -0
  123. data/lib/bro_hub/safe/guards/network_guard.rb +98 -0
  124. data/lib/bro_hub/safe/guards/sql_guard.rb +26 -0
  125. data/lib/bro_hub/safe/parsed_command.rb +66 -0
  126. data/lib/bro_hub/safe/recorder.rb +82 -0
  127. data/lib/bro_hub/safe/shield.rb +83 -0
  128. data/lib/bro_hub/safe/supervisor.rb +81 -0
  129. data/lib/bro_hub/safe/terminal.rb +55 -0
  130. data/lib/bro_hub/safe/trail.rb +24 -0
  131. data/lib/bro_hub/safe/validator.rb +74 -0
  132. data/lib/bro_hub/safe.rb +134 -0
  133. data/lib/bro_hub/schema.rb +36 -0
  134. data/lib/bro_hub/section.rb +7 -13
  135. data/lib/bro_hub/testing.rb +23 -6
  136. data/lib/bro_hub/version.rb +1 -1
  137. data/lib/bro_hub.rb +31 -6
  138. data/lib/generators/bro_hub/install/install_generator.rb +89 -71
  139. data/lib/generators/bro_hub/install/templates/bro_hub.rb.tt +115 -0
  140. data/lib/generators/bro_hub/uninstall/templates/drop_bro_hub_tables.rb.tt +16 -0
  141. data/lib/generators/bro_hub/uninstall/uninstall_generator.rb +57 -0
  142. data/lib/tasks/bro_hub.rake +9 -0
  143. metadata +148 -13
  144. data/db/migrate/20261116000000_create_bro_hub_audit_events.rb +0 -35
  145. data/lib/bro_hub/root_middleware.rb +0 -17
  146. data/lib/generators/bro_hub/install/templates/bro_hub.rb +0 -11
  147. data/lib/generators/bro_hub/installable.rb +0 -63
@@ -0,0 +1,92 @@
1
+ <% bro_hub_crumb "Console sessions", "##{@session.id}" %>
2
+ <% content_for :bro_hub_crumb_actions do %>
3
+ <%= bro_hub_crumb_link "← All sessions", safe_root_path(filter_params) %>
4
+ <% end %>
5
+
6
+ <% flagged_count = @commands.count(&:flagged?) %>
7
+ <%= bro_hub_hero eyebrow: "#{@session.source} · #{@session.hostname} (pid #{@session.pid})",
8
+ title: "Session ##{@session.id}",
9
+ subtitle: safe_review_status(@session.review_status),
10
+ lead: safe_join([ content_tag(:strong, @session.username), " was on a console for #{safe_duration(@session.duration)}. Reason given: “#{@session.reason}”" ]) %>
11
+
12
+ <div class="bh-split">
13
+ <section class="bh-split-main">
14
+ <div class="bh-block-head">
15
+ <h2 class="bh-h2">What they ran</h2>
16
+ <span class="bh-meta"><%= safe_count(@commands.size, "command") %> · <%= flagged_count %> flagged</span>
17
+ </div>
18
+
19
+ <div class="bh-log">
20
+ <% if @commands.empty? %><div class="bh-cmd"><div class="bh-cmd-meta"><span>Nothing was typed.</span></div></div><% end %>
21
+ <% previous_access = nil %>
22
+ <% @commands.each do |command| %>
23
+ <% if command.unlock && command.unlock != previous_access %>
24
+ <div class="bh-unlock">
25
+ <span style="text-transform:uppercase">Unlocked</span> (<%= command.unlock.resources %>) at <%= safe_time(command.unlock.started_at) %>:
26
+ “<%= command.unlock.justification %>”
27
+ </div>
28
+ <% elsif command.unlock.nil? && previous_access %>
29
+ <div class="bh-cmd"><div class="bh-cmd-meta"><span>Locked again</span></div></div>
30
+ <% end %>
31
+ <% previous_access = command.unlock %>
32
+ <div class="bh-cmd <%= "is-flagged is-#{command.status}" if command.flagged? %> <%= "is-unlocked" if command.unlock && !command.flagged? %> <%= "is-sensitive" if command.unlock %>">
33
+ <div class="bh-cmd-meta">
34
+ <%= safe_time(command.created_at) %>
35
+ <%= safe_command_flag(command) %>
36
+ <% if command.flag_reason.present? %><span class="bh-note"><%= command.flag_reason %></span><% end %>
37
+ </div>
38
+ <pre class="bh-cmd-code"><%= command.statements %></pre>
39
+ </div>
40
+ <% end %>
41
+ </div>
42
+ </section>
43
+
44
+ <div class="bh-split-aside">
45
+ <section class="bh-block">
46
+ <h2 class="bh-h2">Details</h2>
47
+ <dl class="bh-dl">
48
+ <dt>User</dt><dd><%= @session.username %></dd>
49
+ <dt>Reason</dt><dd><%= @session.reason %></dd>
50
+ <dt>Started</dt><dd><%= safe_time(@session.started_at) %></dd>
51
+ <dt>Ended</dt><dd><%= @session.ended_at ? safe_time(@session.ended_at) : "still open, or ended abruptly" %></dd>
52
+ <dt>Source</dt><dd><%= @session.source %></dd>
53
+ <dt>From</dt><dd><%= @session.client_ip || "unknown" %><% other_ips = BroHub::Safe::Session.where(username: @session.username).where.not(client_ip: [ nil, @session.client_ip ]).distinct.limit(5).pluck(:client_ip) %><% if other_ips.any? %> <span class="bh-cell-muted">(this user has also come from <%= other_ips.to_sentence %>)</span><% end %></dd>
54
+ <dt>Host</dt><dd><%= @session.hostname %> (pid <%= @session.pid %>)</dd>
55
+ <dt>Unlocks</dt><dd class="bh-tone-amber"><%= @session.unlocks.size %></dd>
56
+ <dt>Flagged</dt><dd class="bh-tone-red"><%= flagged_count %></dd>
57
+ </dl>
58
+ </section>
59
+
60
+ <section class="bh-block">
61
+ <h2 class="bh-h2">Verdict</h2>
62
+ <% if @can_review_this %>
63
+ <%= form_with url: safe_session_reviews_path(@session), scope: :review do |form| %>
64
+ <% filter_params.each do |key, value| %>
65
+ <%= hidden_field_tag(key == :status ? :filter_status : key, value) %>
66
+ <% end %>
67
+ <div class="bh-seg">
68
+ <label class="is-ok"><%= form.radio_button :status, "approved", checked: true %><span>Appropriate</span></label>
69
+ <label class="is-bad"><%= form.radio_button :status, "flagged" %><span>Flag</span></label>
70
+ </div>
71
+ <label class="bh-field" style="margin-top:22px">Notes
72
+ <%= form.text_area :notes, rows: 4, class: "bh-textarea" %>
73
+ </label>
74
+ <div class="bh-hint">Required when flagging. Stored encrypted.</div>
75
+ <%= form.submit "Save and next", class: "bh-btn", style: "margin-top:20px" %>
76
+ <% end %>
77
+ <% else %>
78
+ <p class="bh-hint">This is your own session. Someone else has to review it.</p>
79
+ <% end %>
80
+
81
+ <% if @reviews.any? %>
82
+ <h2 class="bh-h2" style="margin-top:32px">History</h2>
83
+ <dl class="bh-dl">
84
+ <% @reviews.each do |review| %>
85
+ <dt><%= safe_review_status(review.status) %></dt>
86
+ <dd><%= review.reviewer %> <span class="bh-cell-muted"><%= safe_time(review.created_at) %></span><br><%= review.notes %></dd>
87
+ <% end %>
88
+ </dl>
89
+ <% end %>
90
+ </section>
91
+ </div>
92
+ </div>
@@ -0,0 +1,25 @@
1
+ <% bro_hub_crumb "Unlocks" %>
2
+ <%= bro_hub_hero eyebrow: "Unlocks", title: @total, tone: (:amber if @total > 0), subtitle: (@total == 1 ? "unlock" : "unlocks"),
3
+ lead: "Every time someone unlocked sensitive data, with the reason they gave. Newest first.",
4
+ stats: [ [ @open_total, "still open", :amber ] ] %>
5
+
6
+ <div class="bh-table-wrap">
7
+ <div class="bh-table" style="--cols: 150px 120px 140px minmax(240px,1fr) 90px 80px; --min: 900px">
8
+ <div class="bh-row is-head"><span>Unlocked</span><span>User</span><span>Scope</span><span>Why</span><span>Commands</span><span>Session</span></div>
9
+ <% @unlocks.each do |unlock| %>
10
+ <%= link_to safe_session_path(unlock.session_id), class: "bh-row bh-edge-amber" do %>
11
+ <span class="bh-tone-violet"><%= safe_time(unlock.started_at) %></span>
12
+ <span class="bh-cell-strong"><%= unlock.session.username %></span>
13
+ <span class="bh-tags">
14
+ <span class="bh-tag is-amber"><%= unlock.resources %></span>
15
+ <%= content_tag(:span, "open", class: "bh-tag") if unlock.open? %>
16
+ </span>
17
+ <span class="bh-cell-text"><%= unlock.justification %></span>
18
+ <span><%= @command_counts.fetch(unlock.id, 0) %></span>
19
+ <span class="bh-tone-violet">#<%= unlock.session_id %></span>
20
+ <% end %>
21
+ <% end %>
22
+ <% if @unlocks.empty? %><div class="bh-empty">Nobody unlocked anything.</div><% end %>
23
+ <%= safe_pager total: @total, page: @page, noun: "unlock" %>
24
+ </div>
25
+ </div>
@@ -0,0 +1,25 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1">
6
+ <meta name="robots" content="noindex, nofollow">
7
+ <title>BroHub needs a migration</title>
8
+ <style>
9
+ body { font-family: system-ui, sans-serif; max-width: 40rem; margin: 4rem auto; padding: 0 1rem; line-height: 1.5; }
10
+ code, pre { font-family: ui-monospace, monospace; background: rgba(127, 127, 127, .15); padding: .1em .35em; border-radius: 4px; }
11
+ pre { padding: .75rem 1rem; }
12
+ @media (prefers-color-scheme: dark) { body { background: #111; color: #eee; } }
13
+ </style>
14
+ </head>
15
+ <body>
16
+ <h1>BroHub needs a migration</h1>
17
+ <p>The database is missing part of what BroHub stores. Run:</p>
18
+ <pre>bin/rails g bro_hub:install
19
+ bin/rails db:migrate</pre>
20
+ <p>Then reload this page.</p>
21
+ <% if @missing.present? %>
22
+ <p>Missing: <%= @missing.map { |name| content_tag(:code, name) }.to_sentence.html_safe %></p>
23
+ <% end %>
24
+ </body>
25
+ </html>
@@ -17,7 +17,7 @@
17
17
  <div class="bh-top">
18
18
  <span class="bh-brand">
19
19
  <img src="<%= BroHub.skull_data_uri %>" alt="" width="26" height="26">
20
- <span>powered by <%= link_to BroHub.config.brand_name, BroHub.config.brand_url, target: "_blank", rel: "noopener" %></span>
20
+ <span>powered by <%= link_to "DeadBro", "https://www.deadbro.com", target: "_blank", rel: "noopener" %></span>
21
21
  </span>
22
22
  <% if bro_hub_user_label.present? %><span class="bh-user"><%= bro_hub_user_label %></span><% end %>
23
23
  </div>
@@ -0,0 +1,10 @@
1
+ # Drawn inside scope "apm", module: "apm", as: "apm" (see config/routes.rb).
2
+ root "overview#show"
3
+
4
+ get "endpoints" => "endpoints#index", as: :endpoints
5
+ get "errors" => "errors#index", as: :errors
6
+ get "errors/:id" => "errors#show", as: :error, constraints: { id: /\d+/ }
7
+ get "queries" => "queries#index", as: :queries
8
+ get "findings" => "findings#index", as: :findings
9
+ get "deploys" => "deploys#index", as: :deploys
10
+ get "status" => "status#show", as: :status
@@ -0,0 +1,17 @@
1
+ # Drawn inside scope "cache", module: "cache", as: "cache" (see config/routes.rb).
2
+ root "keys#index"
3
+
4
+ # Keys can contain slashes, so they travel as a parameter, not in the path.
5
+ get "key" => "keys#show", as: :key
6
+ post "key/reveal" => "keys#reveal", as: :reveal_key
7
+ delete "key" => "keys#destroy"
8
+
9
+ get "purge" => "purges#new", as: :purge
10
+ post "purge/preview" => "purges#preview", as: :purge_preview
11
+ post "purge" => "purges#create"
12
+
13
+ get "flush" => "flushes#new", as: :flush
14
+ post "flush" => "flushes#create"
15
+
16
+ get "audit" => "audit_events#index", as: :audit
17
+ get "audit/:id" => "audit_events#show", as: :audit_event
@@ -0,0 +1,10 @@
1
+ # Drawn inside scope "env", module: "env", as: "env" (see config/routes.rb).
2
+ root "home#index"
3
+
4
+ get "proposal/new" => "proposals#new", as: :new_proposal
5
+ post "proposal" => "proposals#create", as: :proposal
6
+ get "review/new" => "reviews#new", as: :new_review
7
+ post "review" => "reviews#create", as: :review
8
+ post "review/reveal" => "reviews#reveal", as: :reveal_review
9
+
10
+ get "audit" => "audit#index", as: :audit
@@ -0,0 +1,10 @@
1
+ # Drawn inside scope "safe", module: "safe", as: "safe" (see config/routes.rb).
2
+ root "sessions#index"
3
+
4
+ resources :sessions, only: %i[ index show ] do
5
+ resources :reviews, only: :create
6
+ end
7
+
8
+ get "flagged" => "flagged_commands#index", as: :flagged_commands
9
+ get "unlocks" => "unlocks#index", as: :unlocks
10
+ get "rules" => "rules#show", as: :rules
data/config/routes.rb CHANGED
@@ -1,12 +1,17 @@
1
1
  BroHub::Engine.routes.draw do
2
2
  root "home#index"
3
3
 
4
- # Before the sections, so a gem can never shadow them.
4
+ # Before the features, so none can ever shadow them.
5
5
  get "audit", to: "audit_events#index", as: :audit
6
6
  get "audit/:id", to: "audit_events#show", as: :audit_event
7
7
 
8
- # Every registered engine lives under the hub: /<mount>/<section key>/...
9
- BroHub.registry.each do |section|
10
- mount section.engine => "/#{section.key}", as: section.key if section.engine
8
+ # Every enabled feature lives under the hub: /<mount>/<key>/..., drawn from config/routes/<key>.rb.
9
+ # A feature that is off has no routes at all.
10
+ BroHub::FEATURES.each_key do |key|
11
+ next unless BroHub.feature?(key)
12
+
13
+ scope key.to_s, module: key.to_s, as: key.to_s do
14
+ draw key
15
+ end
11
16
  end
12
17
  end
@@ -0,0 +1,85 @@
1
+ # Every table BroHub (and its opt-in features) uses. Unused features leave their tables empty.
2
+ class CreateBroHubTables < ActiveRecord::Migration[7.1]
3
+ def change
4
+ # One audit trail for every feature. Each feature writes its own rows (the `source` column) through BroHub::Audit.
5
+ create_table :bro_hub_audit_events do |t|
6
+ # The feature that wrote the row ("env", "safe", "cache") and what happened.
7
+ t.string :source, null: false
8
+ t.string :event, null: false
9
+ t.string :user_label
10
+ t.string :environment, null: false
11
+ # Where it happened, when that is a thing: a cache store name, a console host.
12
+ t.string :context
13
+ # What it happened to: a key, a key pattern, a session.
14
+ t.text :target
15
+ t.integer :count
16
+ t.text :reason
17
+ t.string :ip
18
+ # Lookups for features that link rows together: env keeps a proposal's token and the digest of its file here.
19
+ t.string :reference
20
+ t.string :digest
21
+ t.references :parent, foreign_key: { to_table: :bro_hub_audit_events }, index: true
22
+ # No column default: MySQL rejects defaults on JSON columns. BroHub::Audit.record always sets it.
23
+ t.json :details, null: false
24
+ t.datetime :created_at, null: false
25
+ end
26
+
27
+ add_index :bro_hub_audit_events, :created_at
28
+ add_index :bro_hub_audit_events, [ :source, :created_at ]
29
+ add_index :bro_hub_audit_events, [ :source, :event, :created_at ]
30
+ add_index :bro_hub_audit_events, [ :user_label, :created_at ]
31
+ add_index :bro_hub_audit_events, :reference
32
+ add_index :bro_hub_audit_events, :digest
33
+
34
+ # The safe feature: audited console sessions.
35
+ create_table :bro_hub_console_sessions do |t|
36
+ t.string :username, null: false
37
+ t.text :reason, null: false
38
+ t.string :source, null: false, default: "console"
39
+ t.string :hostname
40
+ t.integer :pid
41
+ t.string :client_ip
42
+ t.integer :review_status, null: false, default: 0
43
+ t.datetime :started_at, null: false
44
+ t.datetime :ended_at
45
+
46
+ t.timestamps
47
+
48
+ t.index :started_at
49
+ t.index [ :username, :started_at ]
50
+ t.index [ :review_status, :started_at ]
51
+ end
52
+
53
+ create_table :bro_hub_console_unlocks do |t|
54
+ t.references :session, null: false, index: true, foreign_key: { to_table: :bro_hub_console_sessions }
55
+ t.string :resources, null: false, default: "all"
56
+ t.text :justification, null: false
57
+ t.datetime :started_at, null: false
58
+ t.datetime :ended_at
59
+
60
+ t.timestamps
61
+ end
62
+
63
+ create_table :bro_hub_console_commands do |t|
64
+ t.references :session, null: false, index: false, foreign_key: { to_table: :bro_hub_console_sessions }
65
+ t.references :unlock, index: true, foreign_key: { to_table: :bro_hub_console_unlocks }
66
+ t.text :statements
67
+ t.integer :status, null: false, default: 0
68
+ t.text :flag_reason
69
+
70
+ t.timestamps
71
+
72
+ t.index [ :session_id, :created_at ]
73
+ t.index :status
74
+ end
75
+
76
+ create_table :bro_hub_console_reviews do |t|
77
+ t.references :session, null: false, index: true, foreign_key: { to_table: :bro_hub_console_sessions }
78
+ t.string :reviewer, null: false
79
+ t.integer :status, null: false
80
+ t.text :notes
81
+
82
+ t.timestamps
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,88 @@
1
+ require "net/http"
2
+ require "json"
3
+
4
+ module BroHub::Apm
5
+ # Reads the hub pages' data from DeadBro (/api/v1/hub/*), authenticated with the app's API key.
6
+ # One method per page, so a page fetches only what it shows.
7
+ class Api
8
+ # kind: :not_configured, :unauthorized, :not_found, :unavailable, :network or :bad_response
9
+ class Error < StandardError
10
+ attr_reader :kind
11
+
12
+ def initialize(kind, message)
13
+ @kind = kind
14
+ super(message)
15
+ end
16
+ end
17
+
18
+ PREFIX = "/api/v1/hub".freeze
19
+
20
+ def initialize(config = BroHub.config.apm)
21
+ @config = config
22
+ end
23
+
24
+ def overview = get("overview")
25
+ def endpoints(params = {}) = get("endpoints", params)
26
+ def errors(params = {}) = get("errors", params)
27
+ def error(id) = get("errors/#{Integer(id)}")
28
+ def queries(params = {}) = get("queries", params)
29
+ def findings(params = {}) = get("findings", params)
30
+ def deploys(params = {}) = get("deploys", params)
31
+ def settings = get("settings")
32
+
33
+ private
34
+ attr_reader :config
35
+
36
+ def get(path, params = {})
37
+ raise Error.new(:not_configured, "No DeadBro API key: configure the dead_bro gem first") unless config.api_key
38
+
39
+ params = params.to_h.compact_blank
40
+ return request(path, params) unless config.cache_ttl.to_i.positive?
41
+
42
+ # Only successful responses are cached: a failure raises out of the block.
43
+ Rails.cache.fetch(cache_key(path, params), expires_in: config.cache_ttl) { request(path, params) }
44
+ end
45
+
46
+ def environment
47
+ ::DeadBro.env.to_s
48
+ end
49
+
50
+ # The key is hashed in: it must not end up in a cache store or its logs.
51
+ # The environment is part of it: staging and production share a key but not data.
52
+ def cache_key(path, params)
53
+ [ "bro_hub_apm", Digest::SHA256.hexdigest(config.api_key)[0, 16], environment, path, params.sort.to_h.to_query ].join("/")
54
+ end
55
+
56
+ def request(path, params)
57
+ uri = URI("#{config.base_url}#{PREFIX}/#{path}")
58
+ uri.query = params.to_query if params.any?
59
+
60
+ http = Net::HTTP.new(uri.host, uri.port)
61
+ http.use_ssl = uri.scheme == "https"
62
+ http.open_timeout = config.open_timeout
63
+ http.read_timeout = config.read_timeout
64
+
65
+ req = Net::HTTP::Get.new(uri)
66
+ req["Authorization"] = "Bearer #{config.api_key}"
67
+ req["Accept"] = "application/json"
68
+ # One API key serves every environment of an app, so say which one this process is.
69
+ req["X-DeadBro-Env"] = environment
70
+
71
+ parse(http.request(req))
72
+ rescue Timeout::Error, SocketError, SystemCallError, EOFError, OpenSSL::SSL::SSLError => error
73
+ raise Error.new(:network, "Could not reach DeadBro (#{error.class})")
74
+ end
75
+
76
+ def parse(response)
77
+ case response.code.to_i
78
+ when 200 then JSON.parse(response.body)
79
+ when 401 then raise Error.new(:unauthorized, "DeadBro rejected this app's API key")
80
+ when 404 then raise Error.new(:not_found, "Not found")
81
+ when 502, 503, 504 then raise Error.new(:unavailable, "DeadBro is temporarily unavailable")
82
+ else raise Error.new(:bad_response, "Unexpected response from DeadBro (HTTP #{response.code})")
83
+ end
84
+ rescue JSON::ParserError
85
+ raise Error.new(:bad_response, "DeadBro sent a response that is not JSON")
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,58 @@
1
+ module BroHub
2
+ module Apm
3
+ # BroHub.config.apm. Nothing here touches ::DeadBro except inside methods: this class loads (via app/) even
4
+ # when the feature is off and the dead_bro gem is absent.
5
+ class Configuration
6
+ # ->(user) { true/false }. Who may see DeadBro's data here. Nobody until you say so: the pages show
7
+ # SQL statements, exception messages and request paths.
8
+ attr_accessor :can_view
9
+
10
+ # Both default to what the dead_bro gem already uses, so there is normally nothing to set.
11
+ attr_writer :api_key, :base_url
12
+
13
+ # Seconds a DeadBro response is kept in Rails.cache, so browsing the hub doesn't hit DeadBro on
14
+ # every click. 0 turns caching off.
15
+ attr_accessor :cache_ttl
16
+
17
+ attr_accessor :open_timeout, :read_timeout
18
+
19
+ def initialize
20
+ @can_view = ->(_user) { false }
21
+ @cache_ttl = 30
22
+ @open_timeout = 2
23
+ @read_timeout = 5
24
+ end
25
+
26
+ def allows?(user)
27
+ !user.nil? && !!can_view.call(user)
28
+ end
29
+
30
+ # May this person see the section at all.
31
+ def allows_any?(user)
32
+ allows?(user)
33
+ end
34
+
35
+ # It keeps no audit trail: nothing of it shows in the hub's audit log.
36
+ def audit_visible?(_user)
37
+ false
38
+ end
39
+
40
+ def api_key
41
+ (@api_key.presence || ::DeadBro.configuration.api_key).to_s.presence
42
+ end
43
+
44
+ # Same endpoint selection as the dead_bro client uses to send metrics.
45
+ def base_url
46
+ return @base_url.to_s.chomp("/") if @base_url.present?
47
+
48
+ if ::DeadBro.configuration.ruby_dev
49
+ "http://localhost:3100"
50
+ elsif ENV["USE_STAGING_ENDPOINT"].present?
51
+ "https://deadbro.aberatii.com"
52
+ else
53
+ "https://www.deadbro.com"
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,49 @@
1
+ module BroHub::Apm
2
+ # What the dead_bro gem is doing inside this process, read from its in-memory state. This is the half of
3
+ # the status page DeadBro itself can't know: whether the gem is on, sending and healthy *here*.
4
+ #
5
+ # Every read is guarded: the page must keep rendering against a dead_bro version that lacks one of these.
6
+ module LocalStatus
7
+ module_function
8
+
9
+ def snapshot
10
+ config = ::DeadBro.configuration
11
+
12
+ {
13
+ gem_version: ::DeadBro::VERSION,
14
+ enabled: read { config.enabled },
15
+ api_key_set: read { config.api_key.present? },
16
+ environment: read { ::DeadBro.env },
17
+ endpoint: BroHub.config.apm.base_url,
18
+ sending_paused_until: read { config.skip_until if config.skip_tracking? },
19
+ settings_received_at: read { config.settings_received_at },
20
+ last_heartbeat_at: read { config.last_heartbeat_at },
21
+ sample_rate: read { config.sample_rate },
22
+ monitor_enabled: read { config.monitor_enabled },
23
+ explain_active: read { config.explain_active? },
24
+ circuit_breaker: circuit_breaker,
25
+ dropped_payloads: read { ::DeadBro::Dispatcher.instance.dropped_count },
26
+ unserializable_payloads: read { ::DeadBro.client.serialization_failures },
27
+ deploy_revision: read { config.resolve_deploy_id }
28
+ }
29
+ end
30
+
31
+ # The client keeps its breaker private; this is the one place that reaches in for it.
32
+ def circuit_breaker
33
+ client = ::DeadBro.client
34
+ breaker = client.instance_variable_get(:@circuit_breaker)
35
+ return nil unless breaker
36
+
37
+ { state: breaker.state, failures: breaker.failure_count, last_failure_at: breaker.last_failure_time,
38
+ last_success_at: breaker.last_success_time }
39
+ rescue StandardError
40
+ nil
41
+ end
42
+
43
+ def read
44
+ yield
45
+ rescue StandardError
46
+ nil
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,28 @@
1
+ require "bro_hub/apm/configuration"
2
+
3
+ module BroHub
4
+ # The apm feature (was DeadBroHub): DeadBro's performance data, read through its API and shown in the hub.
5
+ #
6
+ # lib/bro_hub/apm/api.rb and local_status.rb reference ::DeadBro, so they are loaded only by
7
+ # BroHub::Engine.check_dependencies!, after "dead_bro" itself loaded. Nothing else in the gem (or under app/)
8
+ # touches ::DeadBro at load time.
9
+ module Apm
10
+ # The sidebar items of this feature's section, relative to <mount>/apm. Showing an item is navigation only:
11
+ # every controller authorizes each request.
12
+ SIDEBAR_ITEMS = [
13
+ { label: "Overview", path: "/" },
14
+ { label: "Endpoints", path: "/endpoints" },
15
+ { label: "Errors", path: "/errors", match: %r{\A/errors} },
16
+ { label: "SQL queries", path: "/queries" },
17
+ { label: "Findings", path: "/findings" },
18
+ { label: "Deploys", path: "/deploys" },
19
+ { label: "Status", path: "/status" }
20
+ ].freeze
21
+
22
+ class << self
23
+ def api
24
+ Api.new(BroHub.config.apm)
25
+ end
26
+ end
27
+ end
28
+ end
data/lib/bro_hub/audit.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  module BroHub
2
- # The one place every Bro gem writes its audit trail.
2
+ # The one place every feature writes its audit trail.
3
3
  #
4
- # BroHub::Audit.record(source: :cache_bro, event: :keys_purged, user: "jane@example.com",
4
+ # BroHub::Audit.record(source: :cache, event: :keys_purged, user: "jane@example.com",
5
5
  # context: "Rails.cache", target: "posts/*", count: 12, reason: "stale", sample: [ "posts/1" ])
6
6
  #
7
7
  # Never put secrets in it: keys, patterns and names, never values. It fails closed: when the row can't be
@@ -37,12 +37,5 @@ module BroHub
37
37
  Rails.logger&.error("BroHub: could not write the audit log (#{error.class}: #{error.message}). Entry: #{attributes.inspect}")
38
38
  raise
39
39
  end
40
-
41
- # False until the migration has run: lets a gem show "run db:migrate" instead of an error page.
42
- def ready?
43
- BroHub::AuditEvent.table_exists?
44
- rescue ActiveRecord::ActiveRecordError
45
- false
46
- end
47
40
  end
48
41
  end
@@ -0,0 +1,15 @@
1
+ module BroHub::Cache
2
+ # Records who did what, through BroHub's audit table. Keys and patterns are recorded, never values.
3
+ module Audit
4
+ module_function
5
+
6
+ # target: the key, or the pattern. count: how many keys the action touched.
7
+ def record(event, user:, store:, target: nil, count: nil, reason: nil, **details)
8
+ BroHub::Audit.record(
9
+ source: BroHub::Cache::AuditEvent::SOURCE, model: BroHub::Cache::AuditEvent, event: event,
10
+ user: user,
11
+ context: store, target: target, count: count, reason: reason, **details
12
+ )
13
+ end
14
+ end
15
+ end