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
@@ -0,0 +1,59 @@
1
+ <% bro_hub_crumb "Rules" %>
2
+ <% protected_now = @config.enabled? %>
3
+ <%= bro_hub_hero eyebrow: "Effective configuration · read only", title: "Rules", subtitle: (protected_now ? "active here" : "off here"),
4
+ tone: (protected_now ? :green : :muted),
5
+ lead: "What the console locks, blocks and flags right now. Change BroHub.config.safe in config/initializers/bro_hub.rb." %>
6
+
7
+ <% tags = ->(values, tone = nil) {
8
+ values.any? ? tag.div(safe_join(values.map { |value| tag.span(safe_rule(value), class: [ "bh-tag", tone ].compact.join(" "), style: "text-transform:none") }, " "), class: "bh-tags") : tag.span("none", class: "bh-cell-muted")
9
+ } %>
10
+
11
+ <% if @config.self_review_unguarded? %>
12
+ <div class="bh-alert bh-alert-error" id="self-review-warning">Self review isn't prevented yet: <code>console_username</code> isn't mapped, so a reviewer's hub name (<code>user_label</code>, usually an email) is compared with the console name (<code>CONSOLE_USER</code>, usually a login) and they never match. Set <code>config.safe.console_username = -&gt;(user) { user.console_name }</code> in config/initializers/bro_hub.rb.</div>
13
+ <% end %>
14
+
15
+ <div class="bh-split">
16
+ <section class="bh-split-main">
17
+ <h2 class="bh-h2">Locked until <code>unlock!</code></h2>
18
+ <dl class="bh-dl">
19
+ <dt>Resources</dt><dd><%= tags.call(@config.protected_resources, "is-amber") %></dd>
20
+ <dt>ENV names</dt><dd><%= tags.call(@config.protected_env_patterns) %></dd>
21
+ <dt>Files</dt><dd><%= tags.call(@config.protected_file_patterns) %></dd>
22
+ <dt>Systems</dt><dd><%= tags.call(@protected_hosts) %></dd>
23
+ </dl>
24
+
25
+ <h2 class="bh-h2" style="margin-top:36px">Blocked before it runs</h2>
26
+ <dl class="bh-dl">
27
+ <dt>Can't reopen</dt><dd><%= tags.call(@config.forbidden_reopening, "is-red") %></dd>
28
+ <dt>Never referenced</dt><dd><%= tags.call(Array(@config.forbidden_constants[:always]), "is-red") %></dd>
29
+ <dt>While locked</dt><dd><%= tags.call(Array(@config.forbidden_constants[:locked]), "is-red") %></dd>
30
+ <dt>Can't be called</dt><dd><%= tags.call(@config.forbidden_methods, "is-red") %></dd>
31
+ <dt>Not from the console</dt><dd><%= tags.call(@config.runtime_only_methods, "is-red") %></dd>
32
+ </dl>
33
+
34
+ <h2 class="bh-h2" style="margin-top:36px">Flagged when mentioned</h2>
35
+ <%= tags.call(@config.suspicious_terms, "is-amber") %>
36
+ </section>
37
+
38
+ <div class="bh-split-aside">
39
+ <section class="bh-block">
40
+ <h2 class="bh-h2">Where it runs</h2>
41
+ <dl class="bh-dl">
42
+ <dt>Environments</dt><dd><%= @config.protected_environments.join(", ") %></dd>
43
+ <dt>Forced</dt><dd><%= @config.enabled.nil? ? "no" : @config.enabled ? "on" : "off" %></dd>
44
+ <dt>Runners</dt><dd><%= @config.audit_runner ? "recorded" : "not recorded" %></dd>
45
+ <dt>Ask for a name</dt><dd><%= @config.ask_for_username_if_empty ? "yes" : "no, console refuses to start" %></dd>
46
+ </dl>
47
+ </section>
48
+ <section class="bh-block">
49
+ <h2 class="bh-h2">Review</h2>
50
+ <dl class="bh-dl">
51
+ <dt>Keep sessions</dt><dd><%= @config.retention ? distance_of_time_in_words(@config.retention) : "forever" %></dd>
52
+ <dt>Keep flagged</dt><dd><%= @config.keep_flagged ? "yes, even past that" : "no" %></dd>
53
+ <dt>Self review</dt><dd><%= @config.allow_self_audit ? "allowed" : "not allowed" %></dd>
54
+ <dt>Console names</dt><dd><%= @config.console_username_mapped? ? "mapped by console_username" : "not mapped (user_label is compared)" %></dd>
55
+ <dt>Require SSL</dt><dd><%= BroHub.config.require_ssl ? "yes" : "no" %></dd>
56
+ </dl>
57
+ </section>
58
+ </div>
59
+ </div>
@@ -0,0 +1,51 @@
1
+ <% status = @filter[:status] || "all" %>
2
+ <% bro_hub_crumb "Console sessions" %>
3
+ <% content_for :bro_hub_crumb_actions do %>
4
+ <% [ [ "Pending", "pending" ], [ "Appropriate", "approved" ], [ "Flagged", "flagged" ], [ "All", "all" ] ].each do |label, value| %>
5
+ <%= bro_hub_crumb_link label, safe_root_path(@filter.merge(status: value)), active: status == value %>
6
+ <% end %>
7
+ <% end %>
8
+
9
+ <%= bro_hub_hero eyebrow: "Console sessions",
10
+ title: @total,
11
+ tone: { "approved" => :green, "flagged" => :red }[status],
12
+ subtitle: { "pending" => "awaiting a verdict", "approved" => "approved", "flagged" => "flagged" }.fetch(status, @total == 1 ? "session" : "sessions"),
13
+ lead: "Who used a console, why, and what they did. Review each one: approve it or flag it.",
14
+ stats: [ [ @unlock_total, "unlocks", :amber ], [ @flag_total, "flagged commands", :red ] ] %>
15
+
16
+ <%= form_with url: safe_root_path, method: :get, class: "bh-filters" do %>
17
+ <%= hidden_field_tag :status, status %>
18
+ <%= text_field_tag :username, @filter[:username], placeholder: "Filter by user", class: "bh-input is-strong", "aria-label": "Filter by user" %>
19
+ <label class="bh-field">From <%= date_field_tag :from, @filter[:from], class: "bh-input" %></label>
20
+ <label class="bh-field">To <%= date_field_tag :to, @filter[:to], class: "bh-input" %></label>
21
+ <% attention = @filter[:attention] == "1" %>
22
+ <%= link_to "Needs attention only", safe_root_path(@filter.merge(status: status, attention: (attention ? nil : "1"))),
23
+ class: [ "bh-chip", ("is-on" if attention) ], title: "Unlocked sensitive data or flagged commands" %>
24
+ <%= hidden_field_tag :attention, "1" if attention %>
25
+ <%= submit_tag "Filter", name: nil, class: "bh-chip" %>
26
+ <%= link_to "Reset", safe_root_path(status: status), class: "bh-link-quiet" %>
27
+ <% end %>
28
+
29
+ <div class="bh-table-wrap">
30
+ <div class="bh-table" style="--cols: 150px 120px minmax(220px,1fr) 80px 64px 200px 110px; --min: 920px">
31
+ <div class="bh-row is-head"><span>Started</span><span>User</span><span>Reason</span><span>Source</span><span>Length</span><span>Flags</span><span>Review</span></div>
32
+ <% @sessions.each do |session| %>
33
+ <% unlocked = @unlock_counts.fetch(session.id, 0) %>
34
+ <% flagged = @flag_counts.fetch(session.id, 0) %>
35
+ <%= link_to safe_session_path(session, @filter), class: [ "bh-row", ("bh-edge-red" if flagged > 0), ("bh-edge-amber" if flagged.zero? && unlocked > 0) ] do %>
36
+ <span class="bh-tone-violet"><%= safe_time(session.started_at) %></span>
37
+ <span class="bh-cell-strong"><%= session.username %></span>
38
+ <span class="bh-cell-text"><%= truncate(session.reason, length: 90) %></span>
39
+ <span class="bh-cell-muted"><%= session.source %></span>
40
+ <span><%= safe_duration(session.duration) %><%= " (open)" if session.open? %></span>
41
+ <span class="bh-tags">
42
+ <%= content_tag(:span, safe_count(unlocked, "unlock"), class: "bh-tag is-amber") if unlocked > 0 %>
43
+ <%= content_tag(:span, "#{flagged} flagged", class: "bh-tag is-red") if flagged > 0 %>
44
+ </span>
45
+ <%= safe_review_status(session.review_status) %>
46
+ <% end %>
47
+ <% end %>
48
+ <% if @sessions.empty? %><div class="bh-empty">Nothing to review.</div><% end %>
49
+ <%= safe_pager total: @total, page: @page, noun: "session" %>
50
+ </div>
51
+ </div>
@@ -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,14 +17,15 @@
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>
24
24
 
25
25
  <div class="bh-body">
26
26
  <% sections = bro_hub_sections %>
27
- <% if sections.any? %>
27
+ <% own_items = bro_hub_own_items %>
28
+ <% if sections.any? || own_items.any? %>
28
29
  <nav class="bh-side" aria-label="Bro sections">
29
30
  <% sections.each do |section, base, current| %>
30
31
  <div class="bh-group" style="--accent: <%= section.accent %>">
@@ -38,6 +39,14 @@
38
39
  <% end %>
39
40
  </div>
40
41
  <% end %>
42
+ <% if own_items.any? %>
43
+ <div class="bh-group" style="--accent: <%= BroHub::Section::ACCENTS[:violet] %>">
44
+ <span class="bh-group-label">Bro Hub</span>
45
+ <% own_items.each do |label, path, active| %>
46
+ <%= link_to path, class: [ "bh-nav", ("is-active" if active) ] do %><span><%= label %></span><% end %>
47
+ <% end %>
48
+ </div>
49
+ <% end %>
41
50
  </nav>
42
51
  <% end %>
43
52
 
@@ -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,8 +1,17 @@
1
1
  BroHub::Engine.routes.draw do
2
2
  root "home#index"
3
3
 
4
- # Every registered engine lives under the hub: /<mount>/<section key>/...
5
- BroHub.registry.each do |section|
6
- mount section.engine => "/#{section.key}", as: section.key if section.engine
4
+ # Before the features, so none can ever shadow them.
5
+ get "audit", to: "audit_events#index", as: :audit
6
+ get "audit/:id", to: "audit_events#show", as: :audit_event
7
+
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
7
16
  end
8
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