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,135 @@
1
+ require "yaml"
2
+
3
+ module BroHub::Env
4
+ # Decrypted credentials as a nested hash, addressed with dotted key paths
5
+ # ("aws.access_key_id"). The EnvBro metadata key is kept apart from the rest.
6
+ class Credentials
7
+ SEGMENT = /\A[A-Za-z0-9_\-]+\z/
8
+
9
+ attr_reader :data
10
+
11
+ def self.parse(yaml)
12
+ data = YAML.respond_to?(:unsafe_load) ? YAML.unsafe_load(yaml) : YAML.load(yaml)
13
+ data = {} if data.blank?
14
+ raise Error, "Credentials must be a YAML mapping at the top level" unless data.is_a?(Hash)
15
+ new(data)
16
+ end
17
+
18
+ def self.split(path)
19
+ segments = path.to_s.strip.split(".")
20
+ if segments.empty? || segments.any? { |s| !s.match?(SEGMENT) }
21
+ raise InvalidChange, "Invalid key #{path.inspect}. Use letters, digits, _ and -, nested with dots (aws.secret_key)."
22
+ end
23
+ segments
24
+ end
25
+
26
+ def initialize(data)
27
+ @data = data
28
+ end
29
+
30
+ def metadata
31
+ key = BroHub.config.env.metadata_key
32
+ key ? (data[key] || data[key.to_sym]) : nil
33
+ end
34
+
35
+ def metadata=(value)
36
+ key = BroHub.config.env.metadata_key or return
37
+ data.delete(key.to_sym)
38
+ data[key] = value
39
+ end
40
+
41
+ # One line of the YAML-like outline shown to proposers: a group ("aws:") or a key.
42
+ Line = Struct.new(:path, :name, :depth, :group, keyword_init: true)
43
+
44
+ # { "aws.access_key_id" => "AKIA...", "secret_key_base" => "..." }, without metadata.
45
+ def flatten
46
+ flatten_hash(without_metadata)
47
+ end
48
+
49
+ # Groups and keys in file order, with their nesting depth. No values.
50
+ def outline(hash = without_metadata, prefix = nil, depth = 0, lines = [])
51
+ hash.each do |key, value|
52
+ path = [ prefix, key.to_s ].compact.join(".")
53
+ group = value.is_a?(Hash) && value.any?
54
+ lines << Line.new(path: path, name: key.to_s, depth: depth, group: group)
55
+ outline(value, path, depth + 1, lines) if group
56
+ end
57
+ lines
58
+ end
59
+
60
+ def key_paths
61
+ flatten.keys
62
+ end
63
+
64
+ def key?(path)
65
+ flatten.key?(path)
66
+ end
67
+
68
+ def set(path, value)
69
+ *parents, leaf = self.class.split(path)
70
+ node = data
71
+ parents.each do |segment|
72
+ key = find_key(node, segment) || segment
73
+ if node[key].nil?
74
+ node = node[key] = {}
75
+ elsif node[key].is_a?(Hash)
76
+ node = node[key]
77
+ else
78
+ raise InvalidChange, "Cannot set #{path}: #{segment} already holds a value, not a group of keys."
79
+ end
80
+ end
81
+ existing_leaf = find_key(node, leaf)
82
+ if existing_leaf && node[existing_leaf].is_a?(Hash)
83
+ raise InvalidChange, "Cannot set #{path}: it is a group of keys, set one of its children instead."
84
+ end
85
+ node[existing_leaf || leaf] = value
86
+ end
87
+
88
+ def delete(path)
89
+ *parents, leaf = self.class.split(path)
90
+ chain = [ data ]
91
+ parents.each do |segment|
92
+ child = chain.last[find_key(chain.last, segment)]
93
+ raise InvalidChange, "Cannot remove #{path}: key does not exist." unless child.is_a?(Hash)
94
+ chain << child
95
+ end
96
+ existing_leaf = find_key(chain.last, leaf)
97
+ raise InvalidChange, "Cannot remove #{path}: key does not exist." if existing_leaf.nil?
98
+ chain.last.delete(existing_leaf)
99
+
100
+ # Drop groups left empty by the removal.
101
+ parents.reverse.each_with_index do |segment, i|
102
+ parent = chain[-(i + 2)]
103
+ key = find_key(parent, segment)
104
+ parent.delete(key) if parent[key].is_a?(Hash) && parent[key].empty?
105
+ end
106
+ end
107
+
108
+ def to_yaml
109
+ YAML.dump(data)
110
+ end
111
+
112
+ private
113
+ def without_metadata
114
+ data.except(BroHub.config.env.metadata_key, BroHub.config.env.metadata_key&.to_sym)
115
+ end
116
+
117
+ # Credentials keys are usually strings but may be symbols (":foo:" in YAML).
118
+ def find_key(hash, segment)
119
+ if hash.key?(segment) then segment
120
+ elsif hash.key?(segment.to_sym) then segment.to_sym
121
+ end
122
+ end
123
+
124
+ def flatten_hash(hash, prefix = nil)
125
+ hash.each_with_object({}) do |(key, value), out|
126
+ path = [ prefix, key.to_s ].compact.join(".")
127
+ if value.is_a?(Hash) && value.any?
128
+ out.merge!(flatten_hash(value, path))
129
+ else
130
+ out[path] = value
131
+ end
132
+ end
133
+ end
134
+ end
135
+ end
@@ -0,0 +1,45 @@
1
+ module BroHub::Env
2
+ # Rails reads credentials once, at boot. On boot we look at the credentials file
3
+ # this app is running with and, if it came from an EnvBro proposal, record when
4
+ # that proposal went live.
5
+ module Deployments
6
+ module_function
7
+
8
+ # Records a deployment when the running file is not the one the latest deployment row is about: a
9
+ # proposal_deployed row pointing at the matching proposal, or a credentials_deployed row when no proposal
10
+ # matches (a file edited by hand). Rolling back to an earlier file is a new deployment, so it shows as
11
+ # live again. Returns the row created, or nil. Never raises: this runs during boot and must not take
12
+ # the app down (no DB during assets:precompile, migrations not run yet, ...).
13
+ def track
14
+ return unless BroHub.config.env.track_deployments
15
+ return unless AuditEvent.table_exists?
16
+
17
+ environment = Environment.current
18
+ return unless environment.ready?
19
+
20
+ digest = Environment.digest(environment.current_cipher_text)
21
+ return if AuditEvent.deployments.recent.first&.file_digest == digest
22
+
23
+ proposal = AuditEvent.proposals.find_by(file_digest: digest)
24
+ modified = false
25
+
26
+ # The file was re-encrypted after review but still carries the proposal's id.
27
+ if proposal.nil?
28
+ token = environment.current_credentials.metadata.to_h["proposal_id"]
29
+ proposal = AuditEvent.proposals.find_by(proposal_token: token) if token.present?
30
+ modified = proposal.present?
31
+ end
32
+
33
+ if proposal
34
+ Audit.record(:proposal_deployed, user: "deploy",
35
+ columns: { proposal: proposal, file_digest: digest },
36
+ modified_after_review: modified)
37
+ else
38
+ Audit.record(:credentials_deployed, user: "deploy", columns: { file_digest: digest })
39
+ end
40
+ rescue StandardError => e
41
+ Rails.logger&.warn("[EnvBro] could not check deployed credentials: #{e.class}: #{e.message}")
42
+ nil
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,58 @@
1
+ module BroHub::Env
2
+ # Compares two decrypted credentials (base vs proposed) key by key.
3
+ class Differ
4
+ Entry = Struct.new(:key, :status, :old_value, :new_value, keyword_init: true) do
5
+ def changed?
6
+ status != :unchanged
7
+ end
8
+ end
9
+
10
+ STATUS_ORDER = { added: 0, changed: 1, removed: 2, unchanged: 3 }.freeze
11
+
12
+ attr_reader :base, :proposed
13
+
14
+ def initialize(base, proposed)
15
+ @base = base
16
+ @proposed = proposed
17
+ end
18
+
19
+ def entries
20
+ @entries ||= begin
21
+ old_flat = base.flatten
22
+ new_flat = proposed.flatten
23
+ keys = (old_flat.keys | new_flat.keys)
24
+
25
+ keys.map do |key|
26
+ status =
27
+ if !old_flat.key?(key) then :added
28
+ elsif !new_flat.key?(key) then :removed
29
+ elsif old_flat[key] == new_flat[key] then :unchanged
30
+ else :changed
31
+ end
32
+ Entry.new(key: key, status: status, old_value: old_flat[key], new_value: new_flat[key])
33
+ end.sort_by { |e| [ STATUS_ORDER[e.status], e.key ] }
34
+ end
35
+ end
36
+
37
+ def changes
38
+ entries.select(&:changed?)
39
+ end
40
+
41
+ def counts
42
+ entries.group_by(&:status).transform_values(&:size)
43
+ end
44
+
45
+ def proposed_metadata
46
+ proposed.metadata.is_a?(Hash) ? proposed.metadata : nil
47
+ end
48
+
49
+ # :current - proposal was built on the base we compare against
50
+ # :stale - proposal was built on a different version of the file
51
+ # :unknown - proposal carries no EnvBro metadata (made by hand, or an old file)
52
+ def base_status(base_digest)
53
+ recorded = proposed_metadata&.fetch("base_digest", nil)
54
+ return :unknown if recorded.blank?
55
+ recorded == base_digest ? :current : :stale
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,104 @@
1
+ require "time"
2
+ require "securerandom"
3
+
4
+ module BroHub::Env
5
+ # Applies a list of changes to an environment's current credentials, in memory,
6
+ # and returns the re-encrypted file. Nothing is written to disk.
7
+ class Editor
8
+ ACTIONS = %w[set delete].freeze
9
+
10
+ Change = Struct.new(:action, :key, :value, keyword_init: true) do
11
+ def description
12
+ action == "delete" ? "remove #{key}" : "set #{key}"
13
+ end
14
+ end
15
+
16
+ Result = Struct.new(:cipher_text, :filename, :changes, :base_digest, :proposal_token, keyword_init: true) do
17
+ # SHA-256 of the file handed out; seeing it deployed means the proposal is live.
18
+ def file_digest
19
+ Environment.digest(cipher_text)
20
+ end
21
+ end
22
+
23
+ attr_reader :environment
24
+
25
+ def initialize(environment)
26
+ @environment = environment
27
+ end
28
+
29
+ # changes: array of hashes/Changes with action ("set"/"delete"), key and value.
30
+ def apply(changes, author:)
31
+ changes = normalize(changes)
32
+ raise InvalidChange, "Add at least one change." if changes.empty?
33
+
34
+ base_cipher_text = environment.current_cipher_text
35
+ credentials = environment.decrypt(base_cipher_text)
36
+
37
+ changes.each do |change|
38
+ if change.action == "delete"
39
+ credentials.delete(change.key)
40
+ else
41
+ credentials.set(change.key, change.value)
42
+ end
43
+ end
44
+
45
+ base_digest = Environment.digest(base_cipher_text)
46
+ proposal_token = SecureRandom.uuid
47
+ credentials.metadata = {
48
+ "proposal_id" => proposal_token,
49
+ "base_digest" => base_digest,
50
+ "proposed_by" => author.to_s,
51
+ "proposed_at" => Time.now.utc.iso8601,
52
+ "changes" => changes.map(&:description)
53
+ }
54
+
55
+ Result.new(
56
+ cipher_text: environment.encrypt(credentials),
57
+ filename: environment.filename,
58
+ changes: changes,
59
+ base_digest: base_digest,
60
+ proposal_token: proposal_token
61
+ )
62
+ end
63
+
64
+ private
65
+ def normalize(changes)
66
+ changes = Array(changes).map { |c| c.is_a?(Change) ? c : Change.new(**c.to_h.symbolize_keys.slice(:action, :key, :value)) }
67
+ changes = changes.reject { |c| c.key.blank? && c.value.blank? }
68
+
69
+ changes.each do |change|
70
+ change.action = change.action.presence || "set"
71
+ change.key = change.key.to_s.strip
72
+ raise InvalidChange, "Unknown action #{change.action.inspect}." unless ACTIONS.include?(change.action)
73
+ Credentials.split(change.key)
74
+ if metadata_key?(change.key)
75
+ raise InvalidChange, "#{change.key} is reserved for EnvBro metadata."
76
+ end
77
+
78
+ if change.action == "set"
79
+ change.value = clean_value(change.value)
80
+ raise InvalidChange, "#{change.key} needs a value." if change.value.empty?
81
+ else
82
+ change.value = nil
83
+ end
84
+ end
85
+
86
+ duplicates = changes.map(&:key).tally.select { |_, n| n > 1 }.keys
87
+ raise InvalidChange, "Each key can only be changed once per proposal: #{duplicates.join(', ')}." if duplicates.any?
88
+
89
+ changes
90
+ end
91
+
92
+ # Browsers send textarea newlines as CRLF. Single-line values are stripped
93
+ # because a stray space or newline in an API key is a classic bug.
94
+ def clean_value(value)
95
+ value = value.to_s.gsub("\r\n", "\n").sub(/\n+\z/, "")
96
+ value.include?("\n") ? value : value.strip
97
+ end
98
+
99
+ def metadata_key?(key)
100
+ meta = BroHub.config.env.metadata_key
101
+ meta && Credentials.split(key).first == meta
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,18 @@
1
+ require "active_support/encrypted_file"
2
+
3
+ module BroHub::Env
4
+ # Reuses Rails' own EncryptedFile (cipher, serializer, key lookup) but lets us
5
+ # encrypt/decrypt arbitrary strings in memory instead of writing to disk.
6
+ class EncryptedBlob < ActiveSupport::EncryptedFile
7
+ def encrypt_string(plain)
8
+ encrypt(plain)
9
+ end
10
+
11
+ def decrypt_string(cipher_text)
12
+ decrypt(cipher_text.to_s.strip)
13
+ rescue ActiveSupport::MessageEncryptor::InvalidMessage, ArgumentError
14
+ raise DecryptionError, "This file could not be decrypted with the #{content_path.basename} key. " \
15
+ "It is either for another environment or corrupted."
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,74 @@
1
+ require "digest"
2
+ require "pathname"
3
+
4
+ module BroHub::Env
5
+ # The credentials file of the environment the app runs in, located exactly the
6
+ # way Rails finds it (config.credentials.content_path, RAILS_MASTER_KEY, ...).
7
+ class Environment
8
+ attr_reader :name, :content_path, :key_path, :env_key
9
+
10
+ def self.current
11
+ credentials = Rails.application.credentials
12
+ new(name: Rails.env, content_path: credentials.content_path, key_path: credentials.key_path, env_key: credentials.env_key)
13
+ end
14
+
15
+ def initialize(name:, content_path:, key_path:, env_key:)
16
+ @name = name.to_s
17
+ @content_path = Pathname.new(content_path.to_s)
18
+ @key_path = Pathname.new(key_path.to_s)
19
+ @env_key = env_key
20
+ end
21
+
22
+ def filename
23
+ content_path.basename.to_s
24
+ end
25
+
26
+ def exists?
27
+ content_path.exist?
28
+ end
29
+
30
+ def key?
31
+ blob(raise_if_missing_key: false).key?.present?
32
+ end
33
+
34
+ # :ready, :missing_file, :missing_key or :wrong_key.
35
+ def status
36
+ return :missing_file unless exists?
37
+ return :missing_key unless key?
38
+ current_credentials
39
+ :ready
40
+ rescue DecryptionError, ActiveSupport::EncryptedFile::InvalidKeyLengthError
41
+ :wrong_key
42
+ end
43
+
44
+ def ready?
45
+ status == :ready
46
+ end
47
+
48
+ # Encrypted bytes as deployed on this server.
49
+ def current_cipher_text
50
+ content_path.binread.strip
51
+ end
52
+
53
+ def current_credentials
54
+ decrypt(current_cipher_text)
55
+ end
56
+
57
+ def decrypt(cipher_text)
58
+ Credentials.parse(blob.decrypt_string(cipher_text))
59
+ end
60
+
61
+ def encrypt(credentials)
62
+ blob.encrypt_string(credentials.to_yaml)
63
+ end
64
+
65
+ def self.digest(cipher_text)
66
+ Digest::SHA256.hexdigest(cipher_text.to_s.strip)
67
+ end
68
+
69
+ private
70
+ def blob(raise_if_missing_key: true)
71
+ EncryptedBlob.new(content_path: content_path, key_path: key_path, env_key: env_key, raise_if_missing_key: raise_if_missing_key)
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,36 @@
1
+ require "bro_hub/env/configuration"
2
+ require "bro_hub/env/environment"
3
+ require "bro_hub/env/encrypted_blob"
4
+ require "bro_hub/env/credentials"
5
+ require "bro_hub/env/editor"
6
+ require "bro_hub/env/differ"
7
+ require "bro_hub/env/audit"
8
+ require "bro_hub/env/deployments"
9
+
10
+ module BroHub
11
+ # The env feature (was EnvBro): propose, review and audit changes to the encrypted credentials file.
12
+ module Env
13
+ class Error < StandardError; end
14
+ class DecryptionError < Error; end
15
+ class InvalidChange < Error; end
16
+
17
+ # Problems we show to the user instead of raising a 500.
18
+ USER_FACING_ERRORS = [
19
+ Error,
20
+ ActiveSupport::EncryptedFile::MissingKeyError,
21
+ ActiveSupport::EncryptedFile::InvalidKeyLengthError,
22
+ Errno::ENOENT,
23
+ Psych::SyntaxError
24
+ ].freeze
25
+
26
+ # The sidebar items of this feature's section, relative to <mount>/env. Review and the audit log need the review
27
+ # permission, so people who may only propose don't see links that answer 403.
28
+ REVIEWERS = ->(user) { BroHub.config.env.allows?(user, :review) }
29
+ SIDEBAR_ITEMS = [
30
+ { label: "Environments", path: "/" },
31
+ { label: "Propose a change", path: "/proposal/new" },
32
+ { label: "Review", path: "/review/new", match: %r{\A/review}, visible_to: REVIEWERS },
33
+ { label: "Audit log", path: "/audit", visible_to: REVIEWERS }
34
+ ].freeze
35
+ end
36
+ end
@@ -0,0 +1,13 @@
1
+ module BroHub
2
+ # The opt-in features, in the order the keys are listed in error messages. Sidebar items live with each
3
+ # feature's own namespace, not here.
4
+ FEATURES = {
5
+ safe: { namespace: "BroHub::Safe", label: "SafeBro · audit", accent: :red, order: 10 },
6
+ env: { namespace: "BroHub::Env", label: "EnvBro · config", accent: :green, order: 20 },
7
+ apm: { namespace: "BroHub::Apm", label: "DeadBro · performance", accent: :violet, order: 30 },
8
+ cache: { namespace: "BroHub::Cache", label: "CacheBro · cache", accent: :blue, order: 40 }
9
+ }.freeze
10
+
11
+ # Raised at boot when an enabled feature needs a gem the host app doesn't have.
12
+ class MissingDependency < StandardError; end
13
+ end
@@ -1,9 +1,6 @@
1
1
  module BroHub
2
- # Include in a Bro gem's controller to render inside the shared shell:
3
- #
4
- # class SafeBro::ApplicationController < ...
5
- # include BroHub::Layout
6
- # bro_hub_section :safe_bro
2
+ # Renders a controller inside the shared shell (included through BroHub::ControllerBase). A feature's controllers
3
+ # name their sidebar section with BroHub::FeatureController's bro_hub_feature, e.g. `bro_hub_feature :safe`.
7
4
  module Layout
8
5
  extend ActiveSupport::Concern
9
6
 
@@ -0,0 +1,46 @@
1
+ module BroHub::Safe
2
+ # Recorded commands are encrypted at rest so that someone who can read the database (or a
3
+ # backup) can't read what people typed, which may include customer data.
4
+ module Cipher
5
+ # The encryptor serializes values before encrypting; we only ever pass strings.
6
+ module Raw
7
+ def self.dump(value) = value
8
+ def self.load(value) = value
9
+ end
10
+
11
+ class << self
12
+ def encrypt(text)
13
+ return text if text.nil?
14
+
15
+ encryptor.encrypt_and_sign(text.to_s)
16
+ end
17
+
18
+ def decrypt(cipher_text)
19
+ return cipher_text if cipher_text.nil?
20
+
21
+ encryptor.decrypt_and_verify(cipher_text) || "[could not decrypt]"
22
+ rescue ActiveSupport::MessageEncryptor::InvalidMessage, ActiveSupport::MessageVerifier::InvalidSignature
23
+ "[could not decrypt]"
24
+ end
25
+
26
+ def reset!
27
+ @encryptor = nil
28
+ end
29
+
30
+ private
31
+ def encryptor
32
+ @encryptor ||= ActiveSupport::MessageEncryptor.new(key, cipher: "aes-256-gcm", serializer: Raw)
33
+ end
34
+
35
+ def key
36
+ configured = BroHub::Safe.config.encryption_key
37
+ configured = configured.call if configured.respond_to?(:call)
38
+ # The salt SafeBro used, so commands it recorded still decrypt when they are copied over.
39
+ configured ||= Rails.application.key_generator.generate_key("safe_bro/commands", 32)
40
+ raise Error, "BroHub.config.safe.encryption_key must be 32 bytes" unless configured.bytesize == 32
41
+
42
+ configured
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,25 @@
1
+ module BroHub::Safe
2
+ # The commands people can type into the console: `unlock!` and `lock!`.
3
+ module Commands
4
+ class Unlock < IRB::Command::Base
5
+ category "SafeBro"
6
+ description "Get access to protected data (asks why, and records it). Optionally name what: unlock! :credentials"
7
+
8
+ def execute(arg)
9
+ resources = arg.to_s.scan(/\w+/).map(&:to_sym)
10
+ BroHub::Safe.shield.unlock(resources.presence || [ :all ])
11
+ nil
12
+ end
13
+ end
14
+
15
+ class Lock < IRB::Command::Base
16
+ category "SafeBro"
17
+ description "Protect sensitive data again."
18
+
19
+ def execute(_arg)
20
+ BroHub::Safe.shield.lock
21
+ nil
22
+ end
23
+ end
24
+ end
25
+ end