safe_bro 0.2.1 → 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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d17a1c99003b3b0e928039ce383eb78a7019798ce2d2b086ca69d37a300545af
4
- data.tar.gz: 52b04aba0f5c5b6bb24b62bfde87645f9dac4769c2a815857db651678ace7886
3
+ metadata.gz: debb2f669172d42d193479461e65ceec6911991d1d780f90a42e30b901335e1d
4
+ data.tar.gz: f17558ac24b7de5c545bd3fee56ab8fd6cd45cac67ab6df2c9dab80c1bad1dba
5
5
  SHA512:
6
- metadata.gz: a981691a95856d98537cb878585568090d3b2f6860f3ff0df5be7fe160ebfe6867398ee641de1774ad7627bffebca223ed04a2341498b81193eb8d2b45441fe4
7
- data.tar.gz: cf25517ea561073575695b29fd0e23bf6132aeaa86534418e8dc7af6767b33df1dddb0d2ac660ee78907f21c2e7e1253f586455f60bcd18ebfe98292f5a5dd46
6
+ metadata.gz: e5680f69baa01fdcabf1cd93de51e0500b201f329b1701a80ad50f969514d40a7c9b255f1810274beeeaca978722a5ac2682f91fb6072f476ce56562c26d01df
7
+ data.tar.gz: 68dee152c3a833da7d8a8bea6000c12f1d03f82d5b7e742d1e2217659ab59dd6f7c5dc2c09aa82afad9dd765fa753d2849ff3e193279833661c5d83295b4da09
data/README.md CHANGED
@@ -193,7 +193,11 @@ Not covered: `rails dbconsole`, a raw `psql`/`mysql` session, other process mana
193
193
 
194
194
  SafeBro stores the client IP with each session and shows it on the session page, with a note when the same user has also come from other addresses. It reads `CONSOLE_CLIENT_IP`, then `SSH_CLIENT` / `SSH_CONNECTION` (set by sshd, so a user can't change them), and ignores anything that isn't an IP address. Behind a wrapper that starts a container, pass it in: `docker run -e CONSOLE_USER -e CONSOLE_CLIENT_IP="${SSH_CLIENT%% *}" ...`. Or set `config.client_ip_resolver`. It is the NAT or VPN address, not necessarily the person's machine.
195
195
 
196
- Upgrading from 0.1.x: `bin/rails safe_bro:install:migrations && bin/rails db:migrate`.
196
+ ### Shared audit log
197
+
198
+ Sessions, commands and unlocks stay in SafeBro's own tables. The key moments (a session starting, an unlock with its justification, a flagged command, an auditor's verdict) are also written to BroHub's shared audit table, so they show up in BroHub's combined audit log next to EnvBro and CacheBro. Commands, their output and auditors' notes are never copied there. Writing it is best effort: if BroHub's migration hasn't run, consoles still open and SafeBro's own tables are still written. The console can't read or change the shared table either (it is covered by the same guard as SafeBro's tables).
199
+
200
+ Upgrading from 0.2.x or earlier: `bundle update safe_bro`, then `bin/rails g bro_hub:install` (copies BroHub's audit table migration; SafeBro's own tables are unchanged) and `bin/rails db:migrate`. From 0.1.x also run `bin/rails safe_bro:install:migrations` first.
197
201
 
198
202
  ## Differences from console1984 + audits1984
199
203
 
@@ -14,6 +14,7 @@ module SafeBro
14
14
  validates :notes, presence: true, if: :flagged?
15
15
 
16
16
  after_create { session.update!(review_status: status) }
17
+ after_create { Trail.record(:session_reviewed, session: session, user: auditor, status: status, session_user: session.username) }
17
18
  after_create { SafeBro.notify(:session_flagged, session: session, auditor: auditor, notes: notes) if flagged? }
18
19
  end
19
20
  end
@@ -10,7 +10,7 @@ module SafeBro
10
10
 
11
11
  source_root File.expand_path("templates", __dir__)
12
12
 
13
- desc "Sets up the BroHub dashboard (the review UI lives in it), adds the SafeBro initializer and copies the migrations."
13
+ desc "Sets up the BroHub dashboard (the review UI lives in it), adds the SafeBro initializer and copies its migrations (the shared audit table comes with BroHub)."
14
14
 
15
15
  def copy_initializer
16
16
  copy_config_once "safe_bro.rb", "config/initializers/safe_bro.rb"
@@ -9,6 +9,7 @@ module SafeBro
9
9
  name: "SafeBro", label: "SafeBro · audit", accent: :red, order: 10,
10
10
  engine: "SafeBro::Engine",
11
11
  visible_to: ->(user) { SafeBro.config.can_audit.call(user) },
12
+ audit_visible_to: ->(user) { SafeBro.config.can_audit.call(user) },
12
13
  items: [
13
14
  { label: "Sessions", path: "/", match: %r{\A/(sessions(/|\z)|\z)}, badge: -> { SafeBro::Session.review_pending.count } },
14
15
  { label: "Flagged commands", path: "/flagged" },
@@ -1,9 +1,10 @@
1
1
  module SafeBro
2
2
  module Guards
3
3
  # Every statement ActiveRecord runs goes through the adapter's #log. A command that
4
- # mentions SafeBro's tables (to read, change or delete the audit trail) is stopped here.
4
+ # mentions SafeBro's tables (or BroHub's audit table) (to read, change or delete the audit trail) is stopped here.
5
5
  module SqlGuard
6
- TABLES = /safe_bro_(sessions|sensitive_accesses|commands|audits)/i
6
+ # The audit table every Bro gem shares holds SafeBro's key moments too.
7
+ TABLES = /safe_bro_(sessions|sensitive_accesses|commands|audits)|bro_hub_audit_events/i
7
8
 
8
9
  private
9
10
  def log(sql, *args, **kwargs, &block)
@@ -10,6 +10,7 @@ module SafeBro
10
10
  username: username, reason: reason, source: source, client_ip: client_ip,
11
11
  hostname: Socket.gethostname, pid: Process.pid, started_at: Time.current
12
12
  )
13
+ Trail.record(:session_started, session: @session, reason: reason, via: source)
13
14
  end
14
15
  SafeBro.notify(:session_started, session: @session)
15
16
  @session
@@ -32,6 +33,7 @@ module SafeBro
32
33
  @sensitive_access = session.sensitive_accesses.create!(
33
34
  resources: resources.join(","), justification: justification, started_at: Time.current
34
35
  )
36
+ Trail.record(:sensitive_unlocked, session: session, reason: justification, resources: resources.map(&:to_s))
35
37
  end
36
38
  SafeBro.notify(:unlocked, session: session, access: @sensitive_access)
37
39
  @sensitive_access
@@ -52,7 +54,10 @@ module SafeBro
52
54
  end
53
55
 
54
56
  def flag(command, status, reason)
55
- quietly { command.update!(status: status, flag_reason: reason) }
57
+ quietly do
58
+ command.update!(status: status, flag_reason: reason)
59
+ Trail.record(:command_flagged, session: session, reason: reason, status: status.to_s, command_id: command.id)
60
+ end
56
61
  SafeBro.notify(:command_flagged, session: session, command: command, status: status, reason: reason)
57
62
  command
58
63
  end
@@ -0,0 +1,24 @@
1
+ module SafeBro
2
+ # The key moments of a session, copied to BroHub's shared audit log so they show up next to what the other
3
+ # Bro gems record: a session starting, the console being unlocked, a command being flagged, a verdict.
4
+ #
5
+ # SafeBro's own tables stay the full record (every command, every unlock window). This is the short version,
6
+ # and it never holds a command, its output or an auditor's notes. It is best effort: SafeBro's own tables are
7
+ # written first, so a missing audit table (BroHub's migration not run yet) must not stop a console from opening.
8
+ module Trail
9
+ SOURCE = "safe_bro".freeze
10
+
11
+ module_function
12
+
13
+ def record(event, session:, user: session.username, reason: nil, **details)
14
+ BroHub::Audit.record(
15
+ source: SOURCE, event: event, user: user.to_s,
16
+ context: session.hostname, target: "session ##{session.id}", reference: session.id.to_s,
17
+ reason: reason, ip: session.client_ip, **details
18
+ )
19
+ rescue StandardError => error
20
+ Rails.logger&.error("SafeBro: could not copy #{event} to the BroHub audit log (#{error.class}: #{error.message})")
21
+ nil
22
+ end
23
+ end
24
+ end
@@ -1,3 +1,3 @@
1
1
  module SafeBro
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/safe_bro.rb CHANGED
@@ -11,6 +11,7 @@ require "safe_bro/cipher"
11
11
  require "safe_bro/parsed_command"
12
12
  require "safe_bro/validator"
13
13
  require "safe_bro/terminal"
14
+ require "safe_bro/trail"
14
15
  require "safe_bro/recorder"
15
16
  require "safe_bro/executor"
16
17
  require "safe_bro/guards/guarded"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: safe_bro
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ruby Dev SRL
@@ -43,14 +43,14 @@ dependencies:
43
43
  requirements:
44
44
  - - ">="
45
45
  - !ruby/object:Gem::Version
46
- version: 0.1.2
46
+ version: 0.2.0
47
47
  type: :runtime
48
48
  prerelease: false
49
49
  version_requirements: !ruby/object:Gem::Requirement
50
50
  requirements:
51
51
  - - ">="
52
52
  - !ruby/object:Gem::Version
53
- version: 0.1.2
53
+ version: 0.2.0
54
54
  description: Records Rails console sessions (who, why, what), asks for a justification
55
55
  before sensitive data is touched, blocks tampering and risky methods, and ships
56
56
  a mountable engine where auditors review sessions. One gem instead of console1984
@@ -110,6 +110,7 @@ files:
110
110
  - lib/safe_bro/shield.rb
111
111
  - lib/safe_bro/supervisor.rb
112
112
  - lib/safe_bro/terminal.rb
113
+ - lib/safe_bro/trail.rb
113
114
  - lib/safe_bro/validator.rb
114
115
  - lib/safe_bro/version.rb
115
116
  - lib/tasks/safe_bro.rake