brainiac 0.6.0 → 0.7.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 +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +16 -3
- data/bin/brainiac +48 -0
- data/bin/brainiac-completion.bash +1 -1
- data/docs/multi-machine.md +380 -0
- data/lib/brainiac/cron.rb +4 -0
- data/lib/brainiac/handlers/shared/inline_tags.rb +10 -1
- data/lib/brainiac/helpers.rb +72 -3
- data/lib/brainiac/hooks.rb +2 -0
- data/lib/brainiac/hub/claims.rb +36 -0
- data/lib/brainiac/hub/cli.rb +217 -0
- data/lib/brainiac/hub/client.rb +49 -0
- data/lib/brainiac/hub/inbox.rb +84 -0
- data/lib/brainiac/hub/ledger.rb +41 -0
- data/lib/brainiac/hub/node.rb +207 -0
- data/lib/brainiac/hub/settings.rb +46 -0
- data/lib/brainiac/hub/tunnel.rb +83 -0
- data/lib/brainiac/hub.rb +86 -0
- data/lib/brainiac/version.rb +1 -1
- data/receiver.rb +33 -13
- data/templates/cli-providers/hooks/kiro-agent-setup.sh.example +4 -2
- metadata +11 -1
data/lib/brainiac/helpers.rb
CHANGED
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
# Shared helpers: project identification, card map, run_cmd, run_agent, signatures, model detection.
|
|
4
4
|
|
|
5
5
|
require "English"
|
|
6
|
+
require "shellwords"
|
|
7
|
+
require "openssl"
|
|
6
8
|
CLI_PROVIDERS_DIR = File.join(BRAINIAC_DIR, "cli-providers")
|
|
7
9
|
|
|
8
10
|
# Load a CLI provider config from ~/.brainiac/cli-providers/<name>.json.
|
|
@@ -1584,7 +1586,74 @@ def resolve_effort_level(level, allowed)
|
|
|
1584
1586
|
end
|
|
1585
1587
|
|
|
1586
1588
|
def notify_unauthorized(action, creator_name, card_info)
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1589
|
+
security_alert("unauthorized:#{action}", "Unauthorized: #{creator_name} triggered #{action} on #{card_info}")
|
|
1590
|
+
end
|
|
1591
|
+
|
|
1592
|
+
# --- Access control and security alerts ---
|
|
1593
|
+
|
|
1594
|
+
# Headers a tunnel or reverse proxy adds. A request carrying any of them did
|
|
1595
|
+
# not start on this machine, whatever its Host header or source address says:
|
|
1596
|
+
# ngrok and cloudflared both connect from loopback.
|
|
1597
|
+
FORWARDED_REQUEST_HEADERS = %w[
|
|
1598
|
+
HTTP_X_FORWARDED_FOR HTTP_X_FORWARDED_HOST HTTP_X_FORWARDED_PROTO HTTP_FORWARDED
|
|
1599
|
+
HTTP_X_REAL_IP HTTP_CF_CONNECTING_IP HTTP_CF_RAY HTTP_TRUE_CLIENT_IP
|
|
1600
|
+
].freeze
|
|
1601
|
+
LOCAL_HOSTS = ["localhost", "127.0.0.1", "[::1]", "::1"].freeze
|
|
1602
|
+
LOOPBACK_ADDRESS = /\A(127\.\d+\.\d+\.\d+|::1|::ffff:127\.\d+\.\d+\.\d+)\z/
|
|
1603
|
+
SECURITY_ALERT_INTERVAL = 600
|
|
1604
|
+
SECURITY_ALERTS = { mutex: Mutex.new, sent: {} }.freeze
|
|
1605
|
+
|
|
1606
|
+
# True only for a request made on this machine, straight to the server (CLI,
|
|
1607
|
+
# waybar, daemon). Anything that came through a tunnel or proxy is remote.
|
|
1608
|
+
def local_request?(env)
|
|
1609
|
+
return false if FORWARDED_REQUEST_HEADERS.any? { |header| !env[header].to_s.empty? }
|
|
1610
|
+
return false unless env["REMOTE_ADDR"].to_s.match?(LOOPBACK_ADDRESS)
|
|
1611
|
+
|
|
1612
|
+
LOCAL_HOSTS.include?(env["HTTP_HOST"].to_s.downcase.sub(/:\d+\z/, ""))
|
|
1613
|
+
end
|
|
1614
|
+
|
|
1615
|
+
# Where a request claims to come from, for logs and alerts.
|
|
1616
|
+
def request_origin(env)
|
|
1617
|
+
%w[HTTP_CF_CONNECTING_IP HTTP_X_FORWARDED_FOR HTTP_X_REAL_IP REMOTE_ADDR].each do |header|
|
|
1618
|
+
value = env[header].to_s.split(",").first.to_s.strip
|
|
1619
|
+
return value[0, 64] unless value.empty?
|
|
1620
|
+
end
|
|
1621
|
+
"unknown"
|
|
1622
|
+
end
|
|
1623
|
+
|
|
1624
|
+
# Decide an admin API / dashboard request. Returns :ok, :no_token (remote
|
|
1625
|
+
# request and no dashboard_token configured, so nothing remote is allowed) or
|
|
1626
|
+
# :bad_token.
|
|
1627
|
+
def admin_access(env, provided, token: DASHBOARD_TOKEN)
|
|
1628
|
+
return :ok if local_request?(env)
|
|
1629
|
+
return :no_token if token.to_s.empty?
|
|
1630
|
+
|
|
1631
|
+
provided = provided.to_s
|
|
1632
|
+
token = token.to_s
|
|
1633
|
+
matches = provided.bytesize == token.bytesize && OpenSSL.fixed_length_secure_compare(provided, token)
|
|
1634
|
+
matches ? :ok : :bad_token
|
|
1635
|
+
end
|
|
1636
|
+
|
|
1637
|
+
# Tell the operator someone tried something they are not allowed to do: a log
|
|
1638
|
+
# line every time, plus the desktop notification command and a "security"
|
|
1639
|
+
# notification at most once per SECURITY_ALERT_INTERVAL per kind, because public
|
|
1640
|
+
# tunnel URLs get scanned and a scan must not become a notification storm.
|
|
1641
|
+
def security_alert(kind, message, now: Time.now.to_i)
|
|
1642
|
+
LOG.warn "[Security] #{message}"
|
|
1643
|
+
due = SECURITY_ALERTS[:mutex].synchronize do
|
|
1644
|
+
last = SECURITY_ALERTS[:sent][kind.to_s]
|
|
1645
|
+
next false if last && now - last < SECURITY_ALERT_INTERVAL
|
|
1646
|
+
|
|
1647
|
+
SECURITY_ALERTS[:sent][kind.to_s] = now
|
|
1648
|
+
true
|
|
1649
|
+
end
|
|
1650
|
+
return false unless due
|
|
1651
|
+
|
|
1652
|
+
# Argument list, never a shell string: the message carries text from outside.
|
|
1653
|
+
system(*Shellwords.split(NOTIFICATION_COMMAND), message.to_s) if NOTIFICATION_COMMAND
|
|
1654
|
+
send_notification(:security, "🚨 #{message}") if respond_to?(:send_notification, true)
|
|
1655
|
+
true
|
|
1656
|
+
rescue StandardError => e
|
|
1657
|
+
LOG.error "[Security] Alert failed: #{e.message}"
|
|
1658
|
+
false
|
|
1590
1659
|
end
|
data/lib/brainiac/hooks.rb
CHANGED
|
@@ -20,6 +20,8 @@
|
|
|
20
20
|
# :post_comment — After an agent posts a comment/response
|
|
21
21
|
# :resolve_base_branch — Resolve custom base branch for worktree creation
|
|
22
22
|
# :resolve_pr_target — Resolve custom PR target branch (instead of default branch)
|
|
23
|
+
# :hub_leader_acquired — Multi-machine: this machine now holds the leader role (start single-holder work)
|
|
24
|
+
# :hub_leader_lost — Multi-machine: this machine gave up the leader role (stop single-holder work)
|
|
23
25
|
#
|
|
24
26
|
# Usage (in plugin .register):
|
|
25
27
|
# Brainiac.on(:agent_completed) do |ctx|
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "open3"
|
|
4
|
+
|
|
5
|
+
module Brainiac
|
|
6
|
+
module Hub
|
|
7
|
+
# Branches checked out in worktrees on this machine, as hub work keys.
|
|
8
|
+
#
|
|
9
|
+
# GitHub events name a branch, not a card, so this is how the hub learns
|
|
10
|
+
# which machine a pull request belongs to. The main checkout is skipped:
|
|
11
|
+
# every machine has one.
|
|
12
|
+
module Claims
|
|
13
|
+
module_function
|
|
14
|
+
|
|
15
|
+
def local_branch_keys(projects)
|
|
16
|
+
projects.each_value.flat_map do |project|
|
|
17
|
+
repo = project["github_repo"]
|
|
18
|
+
path = project["repo_path"]
|
|
19
|
+
next [] if repo.to_s.empty? || path.to_s.empty? || !Dir.exist?(path)
|
|
20
|
+
|
|
21
|
+
output, status = Open3.capture2e("git", "-C", path, "worktree", "list", "--porcelain")
|
|
22
|
+
next [] unless status.success?
|
|
23
|
+
|
|
24
|
+
worktree_branches(output).map { |branch| "branch:#{repo}:#{branch}" }
|
|
25
|
+
end.uniq
|
|
26
|
+
rescue StandardError => e
|
|
27
|
+
LOG.warn "[Hub] Could not list worktree branches: #{e.message}"
|
|
28
|
+
[]
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def worktree_branches(porcelain)
|
|
32
|
+
porcelain.split(/\n\n+/).drop(1).filter_map { |block| block[%r{^branch refs/heads/(.+)$}, 1] }
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "fileutils"
|
|
5
|
+
require "logger"
|
|
6
|
+
require "optparse"
|
|
7
|
+
require "uri"
|
|
8
|
+
|
|
9
|
+
LOG = Logger.new($stderr, level: Logger::WARN) unless defined?(LOG)
|
|
10
|
+
|
|
11
|
+
require_relative "../hub"
|
|
12
|
+
|
|
13
|
+
module Brainiac
|
|
14
|
+
module Hub
|
|
15
|
+
CLI_USAGE = <<~TEXT
|
|
16
|
+
Usage: brainiac hub <command>
|
|
17
|
+
|
|
18
|
+
setup --url URL --token TOKEN [options] Make this machine a node of a hub
|
|
19
|
+
--machine NAME Name for this machine (default: hostname)
|
|
20
|
+
--priority N Lower number = preferred for new work (default: 100)
|
|
21
|
+
--tunnel cloudflared Let Brainiac run a tunnel and report its URL
|
|
22
|
+
--public-url URL Or: a fixed public URL that reaches this machine
|
|
23
|
+
--interval SECONDS Seconds between check-ins (default: 120)
|
|
24
|
+
--no-leader Never hold the Discord gateways / cron on this machine
|
|
25
|
+
show Show this machine's hub settings
|
|
26
|
+
status Machines, who is online, who leads, what is queued
|
|
27
|
+
assignments [PREFIX] Which machine owns which card / branch (e.g. PREFIX fizzy:)
|
|
28
|
+
move KEY MACHINE Move a card or branch to another machine (e.g. fizzy:1834 mini)
|
|
29
|
+
release KEY Forget the owner, so the next event picks a machine afresh
|
|
30
|
+
disable Remove the hub settings from this machine
|
|
31
|
+
|
|
32
|
+
`brainiac machines` is short for `brainiac hub status`.
|
|
33
|
+
Restart the server after setup or disable: brainiac restart
|
|
34
|
+
TEXT
|
|
35
|
+
|
|
36
|
+
# `brainiac hub ...` — set this machine up as a hub node and inspect or
|
|
37
|
+
# change where work lives.
|
|
38
|
+
class CLI
|
|
39
|
+
def initialize(config_path:, out: $stdout, node: nil)
|
|
40
|
+
@config_path = config_path
|
|
41
|
+
@out = out
|
|
42
|
+
@node = node
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# @return [Integer] exit status
|
|
46
|
+
def run(args)
|
|
47
|
+
command = args.shift
|
|
48
|
+
case command
|
|
49
|
+
when "setup" then setup(args)
|
|
50
|
+
when "show" then show
|
|
51
|
+
when "status", "machines", nil then status
|
|
52
|
+
when "assignments", "list", "ls" then assignments(args.first)
|
|
53
|
+
when "move", "assign" then move(args[0], args[1])
|
|
54
|
+
when "release", "unassign" then release(args.first)
|
|
55
|
+
when "disable" then disable
|
|
56
|
+
else
|
|
57
|
+
@out.puts CLI_USAGE
|
|
58
|
+
%w[help --help -h].include?(command) ? 0 : 1
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
private
|
|
63
|
+
|
|
64
|
+
def load_config
|
|
65
|
+
File.exist?(@config_path) ? JSON.parse(File.read(@config_path)) : {}
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def save_config(config)
|
|
69
|
+
FileUtils.mkdir_p(File.dirname(@config_path))
|
|
70
|
+
File.write(@config_path, "#{JSON.pretty_generate(config)}\n")
|
|
71
|
+
File.chmod(0o600, @config_path) # holds the node token
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def node
|
|
75
|
+
@node ||= Node.new(load_config["hub"]).tap do |node|
|
|
76
|
+
node.client = Client.new(url: node.settings.url, token: node.settings.token)
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def hub_ready?
|
|
81
|
+
return true if node.enabled?
|
|
82
|
+
|
|
83
|
+
@out.puts "No hub configured on this machine. Run: brainiac hub setup --url URL --token TOKEN"
|
|
84
|
+
false
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def setup(args)
|
|
88
|
+
hub = (load_config["hub"] || {}).dup
|
|
89
|
+
parse_setup_options(args, hub)
|
|
90
|
+
if hub["url"].to_s.empty? || (hub["token"].to_s.empty? && ENV["BRAINIAC_HUB_TOKEN"].to_s.empty?)
|
|
91
|
+
@out.puts "Both --url and --token are needed (or set BRAINIAC_HUB_TOKEN instead of --token)."
|
|
92
|
+
return 1
|
|
93
|
+
end
|
|
94
|
+
unless hub["tunnel"] || !hub["public_url"].to_s.empty?
|
|
95
|
+
@out.puts "The hub needs a way to reach this machine. Pass --tunnel cloudflared or --public-url URL."
|
|
96
|
+
return 1
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
save_config(load_config.merge("hub" => hub))
|
|
100
|
+
settings = Settings.new(hub)
|
|
101
|
+
@out.puts "✓ Hub settings saved to #{@config_path}"
|
|
102
|
+
@out.puts " machine: #{settings.machine_name} (priority #{settings.priority})"
|
|
103
|
+
@out.puts " reach me: #{hub["public_url"] || "#{hub["tunnel"]} tunnel (URL reported on each start)"}"
|
|
104
|
+
@out.puts " leader: #{settings.wants_leader? ? "may hold Discord gateways and cron" : "never"}"
|
|
105
|
+
@out.puts "Restart the server to connect: brainiac restart"
|
|
106
|
+
0
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def parse_setup_options(args, hub)
|
|
110
|
+
OptionParser.new do |opts|
|
|
111
|
+
opts.on("--url URL") { |value| hub["url"] = value.chomp("/") }
|
|
112
|
+
opts.on("--token TOKEN") { |value| hub["token"] = value }
|
|
113
|
+
opts.on("--machine NAME") { |value| hub["machine"] = value }
|
|
114
|
+
opts.on("--priority N", Integer) { |value| hub["priority"] = value }
|
|
115
|
+
opts.on("--interval SECONDS", Integer) { |value| hub["interval"] = value }
|
|
116
|
+
opts.on("--tunnel NAME") do |value|
|
|
117
|
+
hub["tunnel"] = value
|
|
118
|
+
hub.delete("public_url")
|
|
119
|
+
end
|
|
120
|
+
opts.on("--public-url URL") do |value|
|
|
121
|
+
hub["public_url"] = value.chomp("/")
|
|
122
|
+
hub.delete("tunnel")
|
|
123
|
+
end
|
|
124
|
+
opts.on("--[no-]leader") { |value| hub["leader"] = value }
|
|
125
|
+
end.parse!(args)
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def show
|
|
129
|
+
hub = load_config["hub"]
|
|
130
|
+
return @out.puts("No hub configured on this machine.").then { 0 } unless hub
|
|
131
|
+
|
|
132
|
+
shown = hub.merge("token" => hub["token"] ? "#{hub["token"][0, 4]}… (hidden)" : nil).compact
|
|
133
|
+
@out.puts JSON.pretty_generate(shown)
|
|
134
|
+
0
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def disable
|
|
138
|
+
config = load_config
|
|
139
|
+
return @out.puts("No hub configured on this machine.").then { 0 } unless config.delete("hub")
|
|
140
|
+
|
|
141
|
+
save_config(config)
|
|
142
|
+
@out.puts "✓ Hub settings removed. Restart the server: brainiac restart"
|
|
143
|
+
0
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def status
|
|
147
|
+
return 1 unless hub_ready?
|
|
148
|
+
|
|
149
|
+
data = node.client.get("/nodes")
|
|
150
|
+
return @out.puts("Could not reach the hub at #{node.settings.url}").then { 1 } unless data
|
|
151
|
+
|
|
152
|
+
nodes = data["nodes"] || []
|
|
153
|
+
@out.puts "Hub: #{node.settings.url}"
|
|
154
|
+
@out.puts "Leader (Discord gateways, cron): #{data["leader"] || "nobody"}"
|
|
155
|
+
@out.puts nodes.empty? ? "No machines have checked in yet." : ""
|
|
156
|
+
nodes.each { |entry| @out.puts status_line(entry, data) }
|
|
157
|
+
print_queue(data["queued"] || {})
|
|
158
|
+
0
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def status_line(entry, data)
|
|
162
|
+
marks = []
|
|
163
|
+
marks << "this machine" if entry["name"] == node.settings.machine_name
|
|
164
|
+
marks << "leader" if entry["name"] == data["leader"]
|
|
165
|
+
state = entry["online"] ? "online " : "OFFLINE"
|
|
166
|
+
seen = entry["last_seen_at"].to_i.positive? ? Time.at(entry["last_seen_at"].to_i).strftime("%Y-%m-%d %H:%M:%S") : "never"
|
|
167
|
+
suffix = marks.empty? ? "" : " (#{marks.join(", ")})"
|
|
168
|
+
format(" %-16<name>s %<state>s priority %-4<priority>d last seen %<seen>s%<suffix>s",
|
|
169
|
+
name: entry["name"], state: state, priority: entry["priority"].to_i, seen: seen, suffix: suffix)
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
def print_queue(queued)
|
|
173
|
+
return if queued.empty?
|
|
174
|
+
|
|
175
|
+
@out.puts ""
|
|
176
|
+
@out.puts "Waiting in the hub:"
|
|
177
|
+
queued.each { |machine, count| @out.puts " #{count} for #{machine == "*" ? "the first machine to check in" : machine}" }
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
def assignments(prefix)
|
|
181
|
+
return 1 unless hub_ready?
|
|
182
|
+
|
|
183
|
+
path = prefix.to_s.empty? ? "/assignments" : "/assignments?prefix=#{URI.encode_www_form_component(prefix)}"
|
|
184
|
+
rows = (node.client.get(path) || {})["assignments"]
|
|
185
|
+
return @out.puts("Could not reach the hub.").then { 1 } unless rows
|
|
186
|
+
|
|
187
|
+
@out.puts "Nothing assigned#{" under #{prefix}" unless prefix.to_s.empty?}." if rows.empty?
|
|
188
|
+
rows.each { |row| @out.puts format(" %-60<key>s %<machine>s", key: row["key"], machine: row["machine"]) }
|
|
189
|
+
0
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
def move(key, machine)
|
|
193
|
+
return @out.puts("Usage: brainiac hub move KEY MACHINE (e.g. fizzy:1834 mini)").then { 1 } unless key && machine
|
|
194
|
+
return 1 unless hub_ready?
|
|
195
|
+
|
|
196
|
+
result = node.client.put("/assignments", { machine: machine.downcase, keys: [key], overwrite: true })
|
|
197
|
+
return @out.puts("Could not reach the hub.").then { 1 } unless result
|
|
198
|
+
|
|
199
|
+
@out.puts "✓ #{key} now belongs to #{machine.downcase}."
|
|
200
|
+
@out.puts " The worktree and CLI session stay on the old machine. Push the branch there first;"
|
|
201
|
+
@out.puts " the new machine picks up from the pushed branch and the brain's memory."
|
|
202
|
+
0
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
def release(key)
|
|
206
|
+
return @out.puts("Usage: brainiac hub release KEY").then { 1 } unless key
|
|
207
|
+
return 1 unless hub_ready?
|
|
208
|
+
|
|
209
|
+
result = node.client.delete("/assignments?key=#{URI.encode_www_form_component(key)}")
|
|
210
|
+
return @out.puts("Could not reach the hub.").then { 1 } unless result
|
|
211
|
+
|
|
212
|
+
@out.puts "✓ #{key} has no owner. The next event for it picks a machine by priority."
|
|
213
|
+
0
|
|
214
|
+
end
|
|
215
|
+
end
|
|
216
|
+
end
|
|
217
|
+
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "net/http"
|
|
5
|
+
require "uri"
|
|
6
|
+
|
|
7
|
+
module Brainiac
|
|
8
|
+
module Hub
|
|
9
|
+
# Minimal JSON client for the hub API. Returns the parsed body on 2xx, nil otherwise.
|
|
10
|
+
class Client
|
|
11
|
+
def initialize(url:, token:, timeout: 10)
|
|
12
|
+
@base = url.to_s.chomp("/")
|
|
13
|
+
@token = token
|
|
14
|
+
@timeout = timeout
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def get(path) = request(Net::HTTP::Get, path)
|
|
18
|
+
def post(path, payload) = request(Net::HTTP::Post, path, payload)
|
|
19
|
+
def put(path, payload) = request(Net::HTTP::Put, path, payload)
|
|
20
|
+
def delete(path) = request(Net::HTTP::Delete, path)
|
|
21
|
+
|
|
22
|
+
private
|
|
23
|
+
|
|
24
|
+
def request(klass, path, payload = nil)
|
|
25
|
+
uri = URI("#{@base}#{path}")
|
|
26
|
+
request = klass.new(uri.request_uri)
|
|
27
|
+
request["Authorization"] = "Bearer #{@token}"
|
|
28
|
+
if payload
|
|
29
|
+
request["Content-Type"] = "application/json"
|
|
30
|
+
request.body = JSON.generate(payload)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https",
|
|
34
|
+
open_timeout: @timeout, read_timeout: @timeout) do |http|
|
|
35
|
+
http.request(request)
|
|
36
|
+
end
|
|
37
|
+
unless response.code.to_i.between?(200, 299)
|
|
38
|
+
LOG.warn "[Hub] #{klass::METHOD} #{path} -> #{response.code} #{response.body.to_s[0, 200]}"
|
|
39
|
+
return nil
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
response.body.to_s.empty? ? {} : JSON.parse(response.body)
|
|
43
|
+
rescue StandardError => e
|
|
44
|
+
LOG.warn "[Hub] #{klass::METHOD} #{path} failed: #{e.message}"
|
|
45
|
+
nil
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "stringio"
|
|
4
|
+
|
|
5
|
+
module Brainiac
|
|
6
|
+
module Hub
|
|
7
|
+
# Rack middleware for deliveries pushed by the hub.
|
|
8
|
+
#
|
|
9
|
+
# A hub delivery is acknowledged with 202 before the plugin route runs. The
|
|
10
|
+
# webhook routes do real work before they answer (git fetch, worktree setup,
|
|
11
|
+
# API calls), and the hub treats a slow answer as a dead machine and tries
|
|
12
|
+
# the next one. Answering first is what stops two machines running one job.
|
|
13
|
+
#
|
|
14
|
+
# Anything under /hub/ is hub-only: it is refused without a valid signature.
|
|
15
|
+
class Inbox
|
|
16
|
+
ACCEPTED = [202, { "content-type" => "application/json" }, ['{"status":"accepted"}']].freeze
|
|
17
|
+
DUPLICATE = [200, { "content-type" => "application/json" }, ['{"status":"duplicate"}']].freeze
|
|
18
|
+
REFUSED = [401, { "content-type" => "application/json" }, ['{"error":"invalid hub signature"}']].freeze
|
|
19
|
+
FORBIDDEN = [403, { "content-type" => "application/json" }, ['{"error":"not a hub route"}']].freeze
|
|
20
|
+
# The admin API and dashboard are never hub deliveries. Queued work is
|
|
21
|
+
# replayed from localhost, where their token check is skipped, so a hub
|
|
22
|
+
# job must not be able to name them.
|
|
23
|
+
OFF_LIMITS = %r{\A/(api|dashboard)(/|\z)}i
|
|
24
|
+
|
|
25
|
+
def initialize(app, hub: Brainiac::Hub, async: true)
|
|
26
|
+
@app = app
|
|
27
|
+
@hub = hub
|
|
28
|
+
@async = async
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def call(env)
|
|
32
|
+
job_id = env["HTTP_X_BRAINIAC_HUB_JOB"]
|
|
33
|
+
hub_only = env["PATH_INFO"].to_s.start_with?("/hub/")
|
|
34
|
+
return @app.call(env) unless hub_only || (job_id && @hub.enabled?)
|
|
35
|
+
|
|
36
|
+
return refuse(env, FORBIDDEN, "names an admin route") if off_limits?(env["PATH_INFO"].to_s)
|
|
37
|
+
|
|
38
|
+
body = env["rack.input"]&.read.to_s
|
|
39
|
+
return refuse(env, REFUSED, "bad or missing hub signature") unless job_id && signed?(env, job_id, body)
|
|
40
|
+
return DUPLICATE unless @hub.first_delivery?(job_id)
|
|
41
|
+
|
|
42
|
+
deliver(env.merge("rack.input" => StringIO.new(body), "brainiac.hub.job" => job_id))
|
|
43
|
+
ACCEPTED
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
private
|
|
47
|
+
|
|
48
|
+
# Someone reached this machine's public URL claiming to be the hub. Say so:
|
|
49
|
+
# it means the tunnel URL is known to a stranger, or the node token is wrong.
|
|
50
|
+
def refuse(env, response, reason)
|
|
51
|
+
origin = env["HTTP_CF_CONNECTING_IP"] || env["HTTP_X_FORWARDED_FOR"] || env["REMOTE_ADDR"]
|
|
52
|
+
message = "Refused hub delivery to #{env["PATH_INFO"]} from #{origin.to_s[0, 64]} (#{reason})"
|
|
53
|
+
if respond_to?(:security_alert, true)
|
|
54
|
+
security_alert("hub", message)
|
|
55
|
+
else
|
|
56
|
+
LOG.warn "[Security] #{message}"
|
|
57
|
+
end
|
|
58
|
+
response
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def off_limits?(path)
|
|
62
|
+
path.match?(OFF_LIMITS) || path.split("/").include?("..")
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def signed?(env, job_id, body)
|
|
66
|
+
@hub.valid_signature?(
|
|
67
|
+
timestamp: env["HTTP_X_BRAINIAC_HUB_TIMESTAMP"], job_id: job_id, path: env["PATH_INFO"].to_s,
|
|
68
|
+
body: body, signature: env["HTTP_X_BRAINIAC_HUB_SIGNATURE"].to_s
|
|
69
|
+
)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def deliver(env)
|
|
73
|
+
work = lambda do
|
|
74
|
+
status, _headers, response = @app.call(env)
|
|
75
|
+
response.close if response.respond_to?(:close)
|
|
76
|
+
LOG.info "[Hub] Job #{env["brainiac.hub.job"]} #{env["PATH_INFO"]} -> #{status}"
|
|
77
|
+
rescue StandardError => e
|
|
78
|
+
LOG.error "[Hub] Job #{env["brainiac.hub.job"]} failed: #{e.message}"
|
|
79
|
+
end
|
|
80
|
+
@async ? Thread.new(&work) : work.call
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "openssl"
|
|
4
|
+
|
|
5
|
+
module Brainiac
|
|
6
|
+
module Hub
|
|
7
|
+
# Decides whether a delivery really came from the hub and whether it has
|
|
8
|
+
# already run on this machine.
|
|
9
|
+
class Ledger
|
|
10
|
+
SIGNATURE_MAX_AGE = 300
|
|
11
|
+
SEEN_JOBS_LIMIT = 2000
|
|
12
|
+
|
|
13
|
+
def initialize(settings)
|
|
14
|
+
@settings = settings
|
|
15
|
+
@mutex = Mutex.new
|
|
16
|
+
@seen_jobs = []
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def valid_signature?(timestamp:, job_id:, path:, body:, signature:, now: Time.now.to_i)
|
|
20
|
+
token = @settings.token.to_s
|
|
21
|
+
return false if token.empty? || signature.to_s.empty?
|
|
22
|
+
return false if (now - timestamp.to_i).abs > SIGNATURE_MAX_AGE
|
|
23
|
+
|
|
24
|
+
expected = OpenSSL::HMAC.hexdigest("SHA256", token, "#{timestamp}.#{job_id}.#{path}.#{body}")
|
|
25
|
+
expected.bytesize == signature.bytesize && OpenSSL.fixed_length_secure_compare(expected, signature)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# True the first time a job id is seen. The hub can deliver a job twice
|
|
29
|
+
# (a slow acknowledgement looks like a failure), so each id runs once.
|
|
30
|
+
def first_delivery?(job_id)
|
|
31
|
+
@mutex.synchronize do
|
|
32
|
+
return false if @seen_jobs.include?(job_id)
|
|
33
|
+
|
|
34
|
+
@seen_jobs << job_id
|
|
35
|
+
@seen_jobs.shift while @seen_jobs.size > SEEN_JOBS_LIMIT
|
|
36
|
+
true
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|