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
@@ -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
@@ -28,9 +28,9 @@ module BroHub
28
28
  @sections ||= BroHub.registry.audited_by(bro_hub_user)
29
29
  end
30
30
 
31
- # Only the rows of gems this person may audit, and only the ones still installed.
31
+ # Only the rows of enabled features this person may audit (sections are registered per enabled feature).
32
32
  def visible
33
- AuditEvent.where(source: sections.map { |section| section.key.to_s })
33
+ sections.map { |section| AuditEvent.for_feature(section.key) }.reduce(AuditEvent.none, :or)
34
34
  end
35
35
 
36
36
  def filtered
@@ -0,0 +1,55 @@
1
+ module BroHub
2
+ module Cache
3
+ class ApplicationController < BroHub::FeatureController
4
+ bro_hub_feature :cache
5
+
6
+ helper_method :cache_current_store, :cache_allows?
7
+
8
+ rescue_from BroHub::Cache::UnknownStore, with: :cache_unknown_store
9
+ rescue_from BroHub::Cache::UnsupportedStore, with: :cache_unsupported
10
+ rescue_from BroHub::Cache::ReasonRequired, BroHub::Cache::InvalidPattern, with: :cache_rejected
11
+
12
+ private
13
+ def cache_allows?(permission)
14
+ BroHub.config.cache.allows?(bro_hub_user, permission)
15
+ end
16
+
17
+ # Anyone who may use some part of the feature gets in (FeatureController); each action then checks its own
18
+ # permission.
19
+ def authorize!(permission)
20
+ raise BroHub::Forbidden unless cache_allows?(permission)
21
+ end
22
+
23
+ # The store the page is about: ?store=Name, or the first configured one.
24
+ def cache_current_store
25
+ @cache_current_store ||= BroHub::Cache.store(params[:store])
26
+ end
27
+
28
+ # The typed reason, or nil. Raises when the action needs one and there is none.
29
+ def reason_for(action)
30
+ reason = params[:reason].to_s.strip.presence
31
+ raise BroHub::Cache::ReasonRequired, "Say why. #{action.to_s.capitalize} needs a reason." if reason.nil? && BroHub.config.cache.reason_required?(action)
32
+
33
+ reason
34
+ end
35
+
36
+ def cache_unknown_store
37
+ head :not_found
38
+ end
39
+
40
+ def cache_unsupported(error)
41
+ @unsupported = error.message
42
+ render "bro_hub/cache/shared/unsupported", status: :unprocessable_entity
43
+ end
44
+
45
+ def cache_rejected(error)
46
+ redirect_back fallback_location: cache_root_path(store: params[:store]), alert: error.message
47
+ end
48
+
49
+ # A SCAN cursor is a number. Anything else starts over.
50
+ def cursor_param
51
+ params[:cursor].to_s.match?(/\A\d+\z/) ? params[:cursor].to_s : "0"
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,40 @@
1
+ module BroHub
2
+ module Cache
3
+ class AuditEventsController < ApplicationController
4
+ PER_PAGE = BroHub::Cache::ApplicationHelper::PER_PAGE
5
+
6
+ before_action { authorize!(:audit) }
7
+ helper_method :filter_params
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 = filtered.group(:event).count
15
+ end
16
+
17
+ def show
18
+ @event = AuditEvent.find(params[:id])
19
+ end
20
+
21
+ private
22
+ def filtered
23
+ scope = AuditEvent.all
24
+ scope = scope.where(event: filter_params[:event]) if AuditEvent::EVENTS.include?(filter_params[:event])
25
+ scope = scope.where(user_label: filter_params[:user]) if filter_params[:user].present?
26
+ scope = scope.where(context: filter_params[:store]) if filter_params[:store].present?
27
+ scope = scope.where("target LIKE ?", "%#{AuditEvent.sanitize_sql_like(filter_params[:q])}%") if filter_params[:q].present?
28
+ scope
29
+ end
30
+
31
+ def filter_params
32
+ @filter_params ||= params.permit(:event, :user, :store, :q).to_h.symbolize_keys.compact_blank
33
+ end
34
+
35
+ def page
36
+ [ params[:page].to_i, 0 ].max
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,34 @@
1
+ module BroHub
2
+ module Cache
3
+ class FlushesController < ApplicationController
4
+ before_action { authorize!(:flush) }
5
+
6
+ def new
7
+ end
8
+
9
+ # Typing the store's name is the confirmation.
10
+ def create
11
+ reason = reason_for(:flush)
12
+ unless params[:confirm].to_s == cache_current_store.name
13
+ return redirect_to cache_flush_path(store: cache_current_store.name), alert: "Type the store name to confirm. Nothing was deleted."
14
+ end
15
+
16
+ result = begin
17
+ cache_current_store.flush
18
+ rescue BroHub::Cache::SweepFailed => error
19
+ record(error.matches, reason, completed: false)
20
+ raise
21
+ end
22
+ record(result, reason, completed: true)
23
+
24
+ redirect_to cache_flush_path(store: cache_current_store.name), notice: "Flushed #{cache_current_store.name}: #{helpers.cache_count(result.count, "key")} deleted."
25
+ end
26
+
27
+ private
28
+ def record(result, reason, completed:)
29
+ Audit.record(:store_flushed, user: bro_hub_user, store: cache_current_store.name, count: result.count, reason: reason,
30
+ namespace: cache_current_store.namespace, sample: result.sample, completed: completed)
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,41 @@
1
+ module BroHub
2
+ module Cache
3
+ class KeysController < ApplicationController
4
+ before_action { authorize!(:view_keys) }
5
+ before_action :load_key, only: %i[ show reveal destroy ]
6
+
7
+ def index
8
+ @query = params[:q].to_s.strip.presence
9
+ @listing = cache_current_store.list(query: @query, cursor: cursor_param)
10
+ end
11
+
12
+ def show
13
+ end
14
+
15
+ # A POST, so a link prefetcher can't "view" a value. The audit entry is written first: if it can't be
16
+ # recorded, nothing is shown.
17
+ def reveal
18
+ authorize!(:view_values)
19
+ reason = reason_for(:reveal)
20
+ Audit.record(:value_viewed, user: bro_hub_user, store: cache_current_store.name, target: @key, reason: reason)
21
+ @value = cache_current_store.read(@key)
22
+ render :show
23
+ end
24
+
25
+ def destroy
26
+ authorize!(:delete)
27
+ reason = reason_for(:delete)
28
+ deleted = cache_current_store.delete(@key)
29
+ Audit.record(:key_deleted, user: bro_hub_user, store: cache_current_store.name, target: @key, count: deleted, reason: reason)
30
+ redirect_to cache_root_path(store: cache_current_store.name), notice: "Deleted #{@key}."
31
+ end
32
+
33
+ private
34
+ def load_key
35
+ @key = params[:key].to_s
36
+ @entry = cache_current_store.stat(@key) unless @key.empty?
37
+ redirect_to cache_root_path(store: cache_current_store.name), alert: "That key no longer exists." unless @entry
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,40 @@
1
+ module BroHub
2
+ module Cache
3
+ class PurgesController < ApplicationController
4
+ before_action { authorize!(:purge) }
5
+
6
+ def new
7
+ end
8
+
9
+ # Counts what the pattern matches, without deleting, and asks to confirm.
10
+ def preview
11
+ @pattern = params[:pattern].to_s.strip
12
+ @reason = reason_for(:purge)
13
+ @matches = cache_current_store.preview(@pattern)
14
+ end
15
+
16
+ def create
17
+ pattern = params[:pattern].to_s.strip
18
+ reason = reason_for(:purge)
19
+ result = begin
20
+ cache_current_store.purge(pattern)
21
+ rescue BroHub::Cache::SweepFailed => error
22
+ # Whatever got deleted before the failure is still recorded.
23
+ record(pattern, error.matches, reason, completed: false)
24
+ raise
25
+ end
26
+ record(pattern, result, reason, completed: true)
27
+
28
+ notice = "Deleted #{helpers.cache_count(result.count, "key")} matching #{pattern}."
29
+ notice += " That is the limit for one purge, run it again to continue." if result.truncated
30
+ redirect_to cache_purge_path(store: cache_current_store.name), notice: notice
31
+ end
32
+
33
+ private
34
+ def record(pattern, result, reason, completed:)
35
+ Audit.record(:keys_purged, user: bro_hub_user, store: cache_current_store.name, target: pattern, count: result.count,
36
+ reason: reason, sample: result.sample, truncated: result.truncated, completed: completed)
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,27 @@
1
+ module BroHub
2
+ module Env
3
+ class ApplicationController < BroHub::FeatureController
4
+ bro_hub_feature :env
5
+
6
+ before_action { @environment = Environment.current }
7
+
8
+ helper_method :env_can?
9
+
10
+ private
11
+ # permission: :create, :update, :destroy, :review or :reveal (higher ones include
12
+ # lower ones), or :propose for "any of create, update, destroy".
13
+ def env_can?(permission)
14
+ BroHub.config.env.allows?(bro_hub_user, permission)
15
+ end
16
+
17
+ def env_authorize!(permission)
18
+ raise BroHub::Forbidden unless env_can?(permission)
19
+ end
20
+
21
+ # The server needs this environment's key to do anything with the file.
22
+ def env_require_ready
23
+ render "bro_hub/env/shared/not_ready", status: :service_unavailable unless @environment.ready?
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,10 @@
1
+ module BroHub::Env
2
+ class AuditController < ApplicationController
3
+ before_action { env_authorize!(:review) }
4
+
5
+ def index
6
+ @events = AuditEvent.recent.includes(:deployment, proposal: :deployment).limit(200)
7
+ @live_proposal = AuditEvent.live_proposal
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,6 @@
1
+ module BroHub::Env
2
+ class HomeController < ApplicationController
3
+ def index
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,86 @@
1
+ module BroHub::Env
2
+ class ProposalsController < ApplicationController
3
+ before_action { env_authorize!(:propose) } # any of create, update, destroy
4
+ before_action :env_require_ready
5
+
6
+ helper_method :can_create?, :can_update?, :can_destroy?
7
+
8
+ def new
9
+ @new_keys = [ Editor::Change.new(action: "set") ]
10
+ @edits = {}
11
+ load_outline
12
+ end
13
+
14
+ def create
15
+ @new_keys = new_key_params
16
+ @edits = edit_params.index_by(&:key)
17
+ changes = @new_keys + @edits.values.reject { |c| c.action == "set" && c.value.blank? }
18
+
19
+ authorize_changes!(changes)
20
+
21
+ result = Editor.new(@environment).apply(changes, author: BroHub.config.user_label.call(bro_hub_user))
22
+
23
+ Audit.record(:proposal_created, user: bro_hub_user,
24
+ columns: { proposal_token: result.proposal_token, file_digest: result.file_digest },
25
+ changes: result.changes.map { |c| { "action" => c.action, "key" => c.key } },
26
+ base_digest: result.base_digest)
27
+
28
+ send_data result.cipher_text, filename: result.filename, type: "application/octet-stream", disposition: "attachment"
29
+ rescue *BroHub::Env::USER_FACING_ERRORS => e
30
+ @error = e.message
31
+ @new_keys = [ Editor::Change.new(action: "set") ] if @new_keys.blank?
32
+ @edits ||= {}
33
+ load_outline
34
+ render :new, status: :unprocessable_entity
35
+ end
36
+
37
+ private
38
+ def can_create? = env_can?(:create)
39
+ def can_update? = env_can?(:update)
40
+ def can_destroy? = env_can?(:destroy)
41
+
42
+ # Each change needs the matching permission: removing a key is destroy, setting a key
43
+ # that already exists is update, setting a new one is create. Checked on the server, so
44
+ # a hand-built request can't do more than the form offers.
45
+ def authorize_changes!(changes)
46
+ existing = @environment.current_credentials
47
+ changes.each do |change|
48
+ key = change.key.to_s.strip
49
+ next if key.empty?
50
+ permission = change.action == "delete" ? :destroy : (existing.key?(key) ? :update : :create)
51
+ env_authorize!(permission)
52
+ end
53
+ end
54
+
55
+ # new_keys[0][key], new_keys[0][secret_value], ...
56
+ def new_key_params
57
+ indexed_rows(:new_keys).map do |row|
58
+ Editor::Change.new(action: "set", key: row[:key], value: row[:secret_value])
59
+ end
60
+ end
61
+
62
+ # edits[0][key], edits[0][secret_value], edits[0][remove], ... one per existing key.
63
+ def edit_params
64
+ indexed_rows(:edits).map do |row|
65
+ remove = row[:remove] == "1"
66
+ Editor::Change.new(action: remove ? "delete" : "set", key: row[:key], value: remove ? nil : row[:secret_value])
67
+ end
68
+ end
69
+
70
+ # Form rows posted with numeric indexes (which may have gaps), in order.
71
+ def indexed_rows(name)
72
+ rows = params[name]
73
+ return [] unless rows.respond_to?(:to_unsafe_h)
74
+ rows.to_unsafe_h.sort_by { |index, _| index.to_i }.map do |_, row|
75
+ ActionController::Parameters.new(row).permit(:key, :secret_value, :remove)
76
+ end
77
+ end
78
+
79
+ def load_outline
80
+ @outline = @environment.current_credentials.outline
81
+ rescue *BroHub::Env::USER_FACING_ERRORS => e
82
+ @outline = []
83
+ @error ||= e.message
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,72 @@
1
+ module BroHub::Env
2
+ class ReviewsController < ApplicationController
3
+ REVEAL_ALL = "*"
4
+
5
+ before_action { env_authorize!(:review) }
6
+ before_action(only: :reveal) { env_authorize!(:reveal) }
7
+ before_action :env_require_ready
8
+
9
+ def new
10
+ end
11
+
12
+ def create
13
+ @proposed_enc = read_upload(:proposed_file) or raise BroHub::Env::Error, "Choose the proposed #{@environment.filename} to review."
14
+ @base_enc = read_upload(:base_file)
15
+ build_diff
16
+
17
+ Audit.record(:review_viewed, user: bro_hub_user,
18
+ changed_keys: @differ.changes.map { |e| "#{e.status} #{e.key}" },
19
+ base: @base_enc ? "uploaded" : "deployed")
20
+
21
+ @revealed = []
22
+ render :show
23
+ rescue *BroHub::Env::USER_FACING_ERRORS => e
24
+ @error = e.message
25
+ render :new, status: :unprocessable_entity
26
+ end
27
+
28
+ # The encrypted files travel back with the form, so the server never stores them.
29
+ def reveal
30
+ @proposed_enc = params[:proposed_enc].to_s
31
+ @base_enc = params[:base_enc].to_s.presence
32
+ [ @proposed_enc, @base_enc ].each { |text| check_size!("The file", text) }
33
+ build_diff
34
+
35
+ requested = params[:reveal_key].to_s
36
+ @revealed = requested == REVEAL_ALL ? @differ.changes.map(&:key) : [ requested ] & @differ.entries.map(&:key)
37
+
38
+ Audit.record(:values_revealed, user: bro_hub_user, keys: @revealed) if @revealed.any?
39
+
40
+ render :show
41
+ rescue *BroHub::Env::USER_FACING_ERRORS => e
42
+ @error = e.message
43
+ render :new, status: :unprocessable_entity
44
+ end
45
+
46
+ private
47
+ def build_diff
48
+ base_cipher_text = @base_enc || @environment.current_cipher_text
49
+ @base_digest = Environment.digest(base_cipher_text)
50
+ @differ = Differ.new(@environment.decrypt(base_cipher_text), @environment.decrypt(@proposed_enc))
51
+ @base_status = @differ.base_status(@base_digest)
52
+
53
+ token = @differ.proposed_metadata&.fetch("proposal_id", nil)
54
+ @proposal_event = AuditEvent.proposals.includes(:deployment).find_by(proposal_token: token) if token.present?
55
+ end
56
+
57
+ # Uploads are limited, and so are the files that travel back with the reveal form: nothing the server
58
+ # decrypts is bigger than max_upload_bytes.
59
+ def check_size!(name, file_or_text)
60
+ return if file_or_text.nil?
61
+ size = file_or_text.respond_to?(:bytesize) ? file_or_text.bytesize : file_or_text.size
62
+ raise BroHub::Env::Error, "#{name} is too large." if size > BroHub.config.env.max_upload_bytes
63
+ end
64
+
65
+ def read_upload(name)
66
+ file = params[name]
67
+ return nil if file.blank?
68
+ check_size!(file.original_filename, file)
69
+ file.read.strip
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,21 @@
1
+ module BroHub
2
+ # The base of every feature's controllers. Names the feature, which puts its pages in its sidebar section and
3
+ # refuses (403) anyone the feature's config doesn't let in:
4
+ #
5
+ # class BroHub::Safe::SessionsController < BroHub::FeatureController
6
+ # bro_hub_feature :safe
7
+ class FeatureController < ApplicationController
8
+ before_action :bro_hub_authorize_feature
9
+
10
+ class << self
11
+ def bro_hub_feature(key)
12
+ bro_hub_section key
13
+ end
14
+ end
15
+
16
+ private
17
+ def bro_hub_authorize_feature
18
+ raise BroHub::Forbidden unless BroHub.config.public_send(bro_hub_section_key).allows_any?(bro_hub_user)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,24 @@
1
+ module BroHub::Safe
2
+ # Signing in, SSL, no-store, the schema check and who may review (BroHub.config.safe.can_audit) are
3
+ # BroHub::FeatureController's.
4
+ class ApplicationController < BroHub::FeatureController
5
+ bro_hub_feature :safe
6
+
7
+ helper_method :safe_user_label
8
+
9
+ private
10
+ # Who is reviewing, as stored on reviews.
11
+ def safe_user_label
12
+ BroHub.config.user_label.call(bro_hub_user).to_s if bro_hub_user
13
+ end
14
+
15
+ # May the signed-in user review this session? Not when it's their own (by the name they use at the console),
16
+ # unless config.safe.allow_self_audit.
17
+ def safe_can_review?(session)
18
+ return true if BroHub.config.safe.allow_self_audit
19
+
20
+ console_name = BroHub.config.safe.console_username.call(bro_hub_user).to_s if bro_hub_user
21
+ console_name.blank? || console_name != session.username
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,14 @@
1
+ module BroHub::Safe
2
+ # Every command that was blocked or flagged, newest first.
3
+ class FlaggedCommandsController < ApplicationController
4
+ PER_PAGE = BroHub::Safe::ApplicationHelper::PER_PAGE
5
+
6
+ def index
7
+ @page = [ params[:page].to_i, 0 ].max
8
+ scope = Command.flagged
9
+ @total = scope.count
10
+ @commands = scope.includes(:session).order(created_at: :desc, id: :desc).limit(PER_PAGE).offset(@page * PER_PAGE)
11
+ @blocked_total = Command.blocked.count
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,39 @@
1
+ module BroHub::Safe
2
+ class ReviewsController < ApplicationController
3
+ def create
4
+ session = Session.find(params[:session_id])
5
+ unless safe_can_review?(session)
6
+ return redirect_to safe_session_path(session), alert: "You can't review your own session."
7
+ end
8
+
9
+ unless Review.statuses.key?(review_params[:status].to_s)
10
+ return redirect_to safe_session_path(session, filter_params), alert: "Choose approve or flag."
11
+ end
12
+
13
+ review = session.reviews.new(review_params.merge(reviewer: safe_user_label))
14
+ if review.save
15
+ redirect_to next_path_after(session), notice: "Session ##{session.id} #{review.status}."
16
+ else
17
+ redirect_to safe_session_path(session, filter_params), alert: review.errors.full_messages.to_sentence
18
+ end
19
+ end
20
+
21
+ private
22
+ def review_params
23
+ params.require(:review).permit(:status, :notes)
24
+ end
25
+
26
+ def filter_params
27
+ params.permit(:attention, :username, :from, :to).to_h.symbolize_keys.merge(status: params[:filter_status]).compact_blank
28
+ end
29
+
30
+ # After deciding, move on to the next session still waiting, within the same filter.
31
+ def next_path_after(session)
32
+ scope = Session.review_pending.where.not(id: session.id)
33
+ scope = scope.needing_attention if params[:attention] == "1"
34
+ scope = scope.where(username: params[:username]) if params[:username].present?
35
+ following = scope.chronological.first
36
+ following ? safe_session_path(following, filter_params) : safe_root_path(status: params[:filter_status].presence || "pending")
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,18 @@
1
+ module BroHub::Safe
2
+ # What the safe feature blocks, flags and locks right now. Read only: change it in the initializer.
3
+ class RulesController < ApplicationController
4
+ def show
5
+ @config = BroHub.config.safe
6
+ @protected_hosts = @config.protected_urls.filter_map { |url| host_of(url) }
7
+ end
8
+
9
+ private
10
+ # Only host and port: a URL can carry a password.
11
+ def host_of(url)
12
+ uri = URI.parse(url.to_s.include?("://") ? url.to_s : "tcp://#{url}")
13
+ [ uri.host, uri.port ].compact.join(":").presence
14
+ rescue URI::InvalidURIError
15
+ "(unreadable URL)"
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,63 @@
1
+ module BroHub::Safe
2
+ class SessionsController < ApplicationController
3
+ PER_PAGE = BroHub::Safe::ApplicationHelper::PER_PAGE
4
+ STATUSES = %w[ pending approved flagged ].freeze
5
+
6
+ helper_method :filter_params
7
+
8
+ def index
9
+ @filter = filter_params
10
+ @sessions = filtered_sessions.chronological.limit(PER_PAGE).offset(page * PER_PAGE)
11
+ @total = filtered_sessions.count
12
+ @page = page
13
+ load_counts
14
+ end
15
+
16
+ def show
17
+ @session = Session.find(params[:id])
18
+ @commands = @session.commands.includes(:unlock).chronological
19
+ @reviews = @session.reviews.order(:id)
20
+ @can_review_this = safe_can_review?(@session)
21
+ @next_session = filtered_sessions.review_pending.where.not(id: @session.id).chronological.first
22
+ end
23
+
24
+ private
25
+ # One query each, instead of two per row, plus the totals for the page heading.
26
+ def load_counts
27
+ ids = @sessions.map(&:id)
28
+ @unlock_counts = Unlock.where(session_id: ids).group(:session_id).count
29
+ @flag_counts = Command.flagged.where(session_id: ids).group(:session_id).count
30
+ matching = filtered_sessions.select(:id)
31
+ @unlock_total = Unlock.where(session_id: matching).count
32
+ @flag_total = Command.flagged.where(session_id: matching).count
33
+ end
34
+
35
+ def filtered_sessions
36
+ scope = Session.all
37
+ scope = scope.where(review_status: filter_params[:status]) if STATUSES.include?(filter_params[:status])
38
+ scope = scope.needing_attention if filter_params[:attention] == "1"
39
+ scope = scope.where(username: filter_params[:username]) if filter_params[:username].present?
40
+ scope = scope.on_or_after(parse_date(filter_params[:from]).beginning_of_day) if parse_date(filter_params[:from])
41
+ scope = scope.before(parse_date(filter_params[:to]).end_of_day) if parse_date(filter_params[:to])
42
+ scope
43
+ end
44
+
45
+ def filter_params
46
+ @filter_params ||= begin
47
+ permitted = params.permit(:status, :attention, :username, :from, :to).to_h.symbolize_keys
48
+ permitted[:status] = "pending" unless params.key?(:status)
49
+ permitted.compact_blank
50
+ end
51
+ end
52
+
53
+ def page
54
+ [ params[:page].to_i, 0 ].max
55
+ end
56
+
57
+ def parse_date(value)
58
+ Date.iso8601(value.to_s) if value.present?
59
+ rescue ArgumentError
60
+ nil
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,14 @@
1
+ module BroHub::Safe
2
+ # Every time someone unlocked sensitive data, with the reason they gave.
3
+ class UnlocksController < ApplicationController
4
+ PER_PAGE = BroHub::Safe::ApplicationHelper::PER_PAGE
5
+
6
+ def index
7
+ @page = [ params[:page].to_i, 0 ].max
8
+ @total = Unlock.count
9
+ @unlocks = Unlock.includes(:session).order(started_at: :desc, id: :desc).limit(PER_PAGE).offset(@page * PER_PAGE)
10
+ @command_counts = Command.where(unlock_id: @unlocks.map(&:id)).group(:unlock_id).count
11
+ @open_total = Unlock.where(ended_at: nil).count
12
+ end
13
+ end
14
+ end