open-loam 0.2.0 → 0.4.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 (75) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +178 -0
  3. data/README.md +4 -2
  4. data/app/jobs/open_loam/event_log_prune_job.rb +15 -0
  5. data/app/jobs/open_loam/webhook_delivery_job.rb +22 -35
  6. data/app/jobs/open_loam/webhook_sweep_job.rb +22 -0
  7. data/app/models/open_loam/api_token.rb +27 -4
  8. data/app/models/open_loam/event_record.rb +42 -0
  9. data/app/models/open_loam/pending_action.rb +47 -3
  10. data/app/models/open_loam/sso_provider.rb +31 -0
  11. data/app/models/open_loam/webhook_delivery.rb +40 -0
  12. data/app/models/open_loam/webhook_endpoint.rb +130 -11
  13. data/app/views/open_loam/custom_fields/_fields.html.erb +4 -0
  14. data/lib/generators/open_loam/entity/templates/api_controller.rb +1 -0
  15. data/lib/generators/open_loam/entity/templates/controller.rb +11 -1
  16. data/lib/generators/open_loam/entity/templates/views/_form.html.erb +4 -1
  17. data/lib/generators/open_loam/entity/templates/views/index.html.erb +6 -2
  18. data/lib/generators/open_loam/entity/templates/views/show.html.erb +2 -0
  19. data/lib/generators/open_loam/install/install_generator.rb +13 -0
  20. data/lib/generators/open_loam/install/templates/AGENTS.md +6 -2
  21. data/lib/generators/open_loam/install/templates/admin/api_tokens_controller.rb +25 -7
  22. data/lib/generators/open_loam/install/templates/admin/api_tokens_index.html.erb +23 -0
  23. data/lib/generators/open_loam/install/templates/admin/base_controller.rb +63 -0
  24. data/lib/generators/open_loam/install/templates/admin/dashboard_controller.rb +2 -0
  25. data/lib/generators/open_loam/install/templates/admin/events_controller.rb +2 -0
  26. data/lib/generators/open_loam/install/templates/admin/history_controller.rb +26 -0
  27. data/lib/generators/open_loam/install/templates/admin/history_index.html.erb +1 -1
  28. data/lib/generators/open_loam/install/templates/admin/imports_controller.rb +13 -1
  29. data/lib/generators/open_loam/install/templates/admin/inbound_webhook_sources_controller.rb +1 -1
  30. data/lib/generators/open_loam/install/templates/admin/inbound_webhook_sources_new.html.erb +0 -2
  31. data/lib/generators/open_loam/install/templates/admin/mfa_controller.rb +5 -0
  32. data/lib/generators/open_loam/install/templates/admin/notifications_controller.rb +2 -0
  33. data/lib/generators/open_loam/install/templates/admin/perspectives_controller.rb +6 -0
  34. data/lib/generators/open_loam/install/templates/admin/progress_jobs_controller.rb +4 -0
  35. data/lib/generators/open_loam/install/templates/admin/search_controller.rb +2 -0
  36. data/lib/generators/open_loam/install/templates/admin/sessions_controller.rb +3 -4
  37. data/lib/generators/open_loam/install/templates/admin/sso_providers_index.html.erb +8 -1
  38. data/lib/generators/open_loam/install/templates/admin/sudo_controller.rb +3 -1
  39. data/lib/generators/open_loam/install/templates/admin/webhook_endpoints_controller.rb +4 -1
  40. data/lib/generators/open_loam/install/templates/admin/webhook_endpoints_created.html.erb +9 -0
  41. data/lib/generators/open_loam/install/templates/admin/webhook_endpoints_index.html.erb +8 -6
  42. data/lib/generators/open_loam/install/templates/admin/webhook_endpoints_new.html.erb +2 -2
  43. data/lib/generators/open_loam/install/templates/api_base_controller.rb +43 -6
  44. data/lib/generators/open_loam/install/templates/import_job.rb +14 -1
  45. data/lib/generators/open_loam/install/templates/initializer.rb +14 -2
  46. data/lib/generators/open_loam/install/templates/migrations/create_open_loam_api_tokens.rb +2 -2
  47. data/lib/generators/open_loam/install/templates/migrations/create_open_loam_event_records.rb +13 -0
  48. data/lib/generators/open_loam/install/templates/migrations/create_open_loam_inbound_webhooks.rb +0 -1
  49. data/lib/generators/open_loam/install/templates/migrations/create_open_loam_sso_providers.rb +3 -0
  50. data/lib/generators/open_loam/install/templates/migrations/create_open_loam_webhook_deliveries.rb +29 -0
  51. data/lib/generators/open_loam/install/templates/migrations/create_open_loam_webhook_endpoints.rb +12 -2
  52. data/lib/open_loam/auth_throttle.rb +11 -3
  53. data/lib/open_loam/custom_field_index.rb +11 -8
  54. data/lib/open_loam/encryptable.rb +8 -3
  55. data/lib/open_loam/encryption/key_provider.rb +17 -1
  56. data/lib/open_loam/encryption.rb +47 -4
  57. data/lib/open_loam/engine.rb +14 -0
  58. data/lib/open_loam/errors.rb +10 -0
  59. data/lib/open_loam/event_log.rb +77 -0
  60. data/lib/open_loam/export.rb +6 -6
  61. data/lib/open_loam/import.rb +1 -4
  62. data/lib/open_loam/inbound_webhooks.rb +12 -11
  63. data/lib/open_loam/lifecycle.rb +29 -0
  64. data/lib/open_loam/mcp.rb +10 -1
  65. data/lib/open_loam/outbound_url.rb +120 -0
  66. data/lib/open_loam/policy.rb +16 -3
  67. data/lib/open_loam/search/token_driver.rb +1 -1
  68. data/lib/open_loam/sso/http_client.rb +15 -6
  69. data/lib/open_loam/sso.rb +23 -2
  70. data/lib/open_loam/version.rb +1 -1
  71. data/lib/open_loam/webhooks/http_transport.rb +76 -0
  72. data/lib/open_loam/webhooks.rb +271 -17
  73. data/lib/open_loam.rb +3 -0
  74. data/lib/tasks/open_loam.rake +32 -0
  75. metadata +12 -2
@@ -0,0 +1,76 @@
1
+ # lib/open_loam/webhooks/http_transport.rb
2
+ require "net/http"
3
+ require "timeout"
4
+
5
+ module OpenLoam
6
+ module Webhooks
7
+ # The only code that opens a socket for a webhook. Connects to the address
8
+ # OutboundUrl just vetted (no DNS rebinding window), never follows a
9
+ # redirect (a 3xx is the answer), and reads at most EXCERPT_BYTES of the
10
+ # response so a hostile receiver cannot make us buffer a large body.
11
+ #
12
+ # The per-operation timeouts (open/read/write) do not bound a receiver that
13
+ # trickles one byte per read_timeout, so the whole request also runs under
14
+ # an overall deadline of 3 x Webhooks.timeout (30 s by default). Keep that
15
+ # deadline below Webhooks.stuck_after (5 min by default), or the sweep may
16
+ # resend a delivery that is still in flight.
17
+ module HttpTransport
18
+ class ExcerptComplete < StandardError; end
19
+
20
+ module_function
21
+
22
+ def post(url, body:, headers:, timeout:)
23
+ started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
24
+ code = nil
25
+ retry_after = nil
26
+ excerpt = +""
27
+ # Timeout::Error is a StandardError: Webhooks.deliver retries it as a
28
+ # network error, counted against the endpoint like any other.
29
+ Timeout.timeout(timeout * 3) do
30
+ # DNS resolution is inside the deadline too.
31
+ uri, address = OpenLoam::OutboundUrl.resolve!(url, require_https: Webhooks.require_https?,
32
+ ports: Webhooks.allowed_ports)
33
+ # nil proxy: an http_proxy in ENV must not bypass the pinned address.
34
+ http = Net::HTTP.new(uri.host, uri.port, nil)
35
+ http.ipaddr = address
36
+ http.use_ssl = uri.scheme == "https"
37
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER if http.use_ssl?
38
+ http.open_timeout = timeout
39
+ http.read_timeout = timeout
40
+ http.write_timeout = timeout
41
+
42
+ request = Net::HTTP::Post.new(uri.request_uri)
43
+ headers.each { |name, value| request[name] = value }
44
+ request["Accept-Encoding"] = "identity" # a compressed body could inflate past the excerpt cap
45
+ request.body = body
46
+
47
+ http.start do |connection|
48
+ connection.request(request) do |response|
49
+ code = response.code.to_i
50
+ retry_after = response["retry-after"]
51
+ response.read_body do |chunk|
52
+ excerpt << chunk
53
+ raise ExcerptComplete if excerpt.bytesize >= EXCERPT_BYTES
54
+ end
55
+ end
56
+ end
57
+ rescue ExcerptComplete
58
+ # Enough of the body; the connection is closed by http.start's ensure.
59
+ end
60
+
61
+ Response.new(code: code, headers: { "retry-after" => retry_after }, excerpt: safe_excerpt(excerpt),
62
+ duration_ms: ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - started) * 1000).round)
63
+ end
64
+
65
+ # Always valid UTF-8 without NUL (Postgres rejects both in a text column,
66
+ # and a row that cannot be saved would be resent forever), at most
67
+ # EXCERPT_BYTES. Scrubbing can grow the string (one bad byte becomes a
68
+ # 3-byte U+FFFD), so it is cut again, dropping any split character.
69
+ def safe_excerpt(raw)
70
+ text = raw.byteslice(0, EXCERPT_BYTES).to_s.force_encoding(Encoding::UTF_8).scrub.delete("\u0000")
71
+ text = text.byteslice(0, EXCERPT_BYTES).scrub("") if text.bytesize > EXCERPT_BYTES
72
+ text
73
+ end
74
+ end
75
+ end
76
+ end
@@ -1,3 +1,7 @@
1
+ require "json"
2
+ require "openssl"
3
+ require "securerandom"
4
+
1
5
  module OpenLoam
2
6
  # Domain events, delivered over HTTP to whoever asked for them.
3
7
  #
@@ -6,29 +10,279 @@ module OpenLoam
6
10
  # Matching happens at dispatch time rather than at subscribe time because
7
11
  # endpoints are data: a tenant can add one from the admin without a reboot.
8
12
  module Webhooks
9
- # Wired once from OpenLoam::Engine. Idempotent: subscribing twice would deliver
10
- # every event twice.
11
- def self.subscribe!
12
- @subscription ||= Events.subscribe_all { |event_name, payload| dispatch(event_name, payload) }
13
- end
13
+ DEFAULT_RETRY_SCHEDULE = [ 60, 300, 1_800, 7_200, 21_600, 43_200, 86_400 ].freeze
14
+ DISABLED_EVENT = "open_loam.webhook.endpoint_disabled".freeze
15
+ TEST_EVENT = "open_loam.webhook.test".freeze
16
+ SWEEP_KEY = "open_loam_webhook_sweep".freeze
17
+ EXCERPT_BYTES = 512
18
+ TIMESTAMP_TOLERANCE = 300
19
+
20
+ CONFIG_IVARS = %i[
21
+ @header_prefix @user_agent @retry_schedule @disable_after_failures @delivery_retention @body_builder
22
+ @transport @require_https @allowed_ports @encrypt_secrets @test_event_name @timeout
23
+ @secret_rotation_grace @stuck_after @disable_after_duration
24
+ ].freeze
25
+
26
+ Response = Struct.new(:code, :headers, :excerpt, :duration_ms, keyword_init: true)
27
+
28
+ # Raised by a host body builder to drop one delivery on purpose (the record
29
+ # it describes is gone, the receiver may not see this event). The row is
30
+ # cancelled, not failed, and the endpoint's health is untouched.
31
+ class SkipDelivery < OpenLoam::Error; end
14
32
 
15
- # Runs inline in whatever published the event, so it stays cheap: one
16
- # query for the tenant's active endpoints, then the HTTP call is a job.
17
- def self.dispatch(event_name, payload)
18
- tenant = Tenant.find_by(id: payload[:tenant_id])
19
- return if tenant.nil?
33
+ RETRYABLE_CODES = [ 408, 429 ].freeze
34
+ MAX_RETRY_AFTER = 3_600
20
35
 
21
- # Only JSON-safe primitives cross into the job — event payloads are ids
22
- # and scalars by convention, never records.
23
- deliverable = payload.transform_keys(&:to_s)
36
+ class << self
37
+ attr_writer :header_prefix, :user_agent, :retry_schedule, :disable_after_failures, :delivery_retention,
38
+ :body_builder, :transport, :require_https, :allowed_ports, :encrypt_secrets, :test_event_name,
39
+ :timeout, :secret_rotation_grace, :stuck_after, :disable_after_duration
24
40
 
25
- OpenLoam.as_tenant(tenant) do
26
- WebhookEndpoint.active.each do |endpoint|
27
- next unless endpoint.matches?(event_name)
41
+ def header_prefix = @header_prefix || "X-OpenLoam"
42
+ def user_agent = @user_agent || "OpenLoam-Webhooks/#{OpenLoam::VERSION}"
43
+ def retry_schedule = @retry_schedule || DEFAULT_RETRY_SCHEDULE
44
+ def disable_after_failures = defined?(@disable_after_failures) ? @disable_after_failures : 50
45
+ # An endpoint is disabled for failing only when it has failed
46
+ # disable_after_failures times in a row AND has been failing for at least
47
+ # this long, so a short outage with heavy traffic does not switch it off.
48
+ def disable_after_duration = defined?(@disable_after_duration) ? @disable_after_duration : 24.hours
49
+ def delivery_retention = defined?(@delivery_retention) ? @delivery_retention : 30.days
50
+ def body_builder = @body_builder
51
+ def transport = @transport || HttpTransport
52
+ def require_https? = @require_https ? true : false
53
+ def allowed_ports = @allowed_ports
54
+ def encrypt_secrets? = @encrypt_secrets ? true : false
55
+ def test_event_name = @test_event_name || TEST_EVENT
56
+ def timeout = @timeout || 10
57
+ def secret_rotation_grace = @secret_rotation_grace || 24.hours
58
+ def stuck_after = @stuck_after || 5.minutes
28
59
 
29
- WebhookDeliveryJob.perform_later(tenant.id, endpoint.id, event_name, deliverable)
60
+ # Tests (and only tests) put the module back to its defaults.
61
+ def reset_configuration!
62
+ CONFIG_IVARS.each { |ivar| remove_instance_variable(ivar) if instance_variable_defined?(ivar) }
63
+ end
64
+
65
+ # One attempt for one row, inside the row's tenant. Safe to call twice:
66
+ # only the caller that flips pending -> sending sends.
67
+ def deliver(delivery, now: Time.current)
68
+ return unless claim(delivery, now)
69
+
70
+ delivery.reload
71
+ endpoint = delivery.endpoint
72
+ unless endpoint&.active?
73
+ delivery.update!(status: "cancelled", sending_at: nil, last_error: "endpoint inactive")
74
+ return
75
+ end
76
+
77
+ # Building the request is the host's code: SkipDelivery cancels, any
78
+ # other error is retried without counting against the receiver.
79
+ begin
80
+ body = body_for(endpoint, delivery)
81
+ headers = headers_for(endpoint, delivery, body, now)
82
+ rescue SkipDelivery => error
83
+ delivery.update!(status: "cancelled", sending_at: nil, last_error: short(error.message))
84
+ return
85
+ rescue StandardError => error
86
+ retry_later(delivery, endpoint, now, error: short("#{error.class}: #{error.message}"), count_failure: false)
87
+ return
30
88
  end
89
+
90
+ begin
91
+ response = transport.post(endpoint.url, body: body, headers: headers, timeout: timeout)
92
+ rescue OpenLoam::OutboundUrl::UnresolvableError => error
93
+ # DNS being down is the network's fault, not the receiver's: retry.
94
+ retry_later(delivery, endpoint, now, error: short("#{error.class}: #{error.message}"))
95
+ return
96
+ rescue OpenLoam::OutboundUrl::BlockedError => error
97
+ delivery.update!(status: "failed", sending_at: nil, last_error: short("blocked: #{error.message}"))
98
+ endpoint.disable!("blocked_url")
99
+ return
100
+ rescue StandardError => error
101
+ retry_later(delivery, endpoint, now, error: short("#{error.class}: #{error.message}"))
102
+ return
103
+ end
104
+
105
+ # Outside the rescue: a bookkeeping error must surface, not become a retry.
106
+ record_response(delivery, endpoint, response, now)
107
+ end
108
+
109
+ def body_for(endpoint, delivery)
110
+ if body_builder
111
+ JSON.generate(body_builder.call(endpoint: endpoint, delivery: delivery))
112
+ else
113
+ OpenLoam::WebhookDeliveryJob.body_for(delivery.event_name, delivery.payload_hash, delivery.tenant_id)
114
+ end
115
+ end
116
+
117
+ def headers_for(endpoint, delivery, body, now)
118
+ timestamp = now.to_i
119
+ prefix = header_prefix
120
+ {
121
+ "Content-Type" => "application/json",
122
+ "User-Agent" => user_agent,
123
+ "#{prefix}-Event" => delivery.event_name,
124
+ "#{prefix}-Event-Id" => delivery.event_id,
125
+ "#{prefix}-Delivery" => delivery.delivery_id,
126
+ "#{prefix}-Timestamp" => timestamp.to_s,
127
+ "#{prefix}-Signature" => OpenLoam::WebhookDeliveryJob.signature(endpoint.secret, body),
128
+ "#{prefix}-Signature-V2" => signature_v2(endpoint.signing_secrets(now), body, timestamp)
129
+ }
130
+ end
131
+
132
+ # "t=<unix>,v1=<hex>[,v1=<hex>]" — HMAC-SHA256 of "<t>.<body>" under each
133
+ # signing secret. The timestamp is inside the MAC, so a captured request
134
+ # cannot be replayed after the receiver's tolerance window.
135
+ def signature_v2(secrets, body, timestamp)
136
+ macs = Array(secrets).map { |secret| "v1=#{OpenSSL::HMAC.hexdigest('SHA256', secret.to_s, "#{timestamp}.#{body}")}" }
137
+ ([ "t=#{timestamp}" ] + macs).join(",")
138
+ end
139
+
140
+ # Reference verifier (docs and tests): constant-time compare, bounded clock skew.
141
+ def verify_v2(header, body, secret, now: Time.current, tolerance: TIMESTAMP_TOLERANCE)
142
+ parts = header.to_s.split(",").map { |part| part.split("=", 2) }
143
+ timestamp = parts.find { |key, _| key == "t" }&.last.to_i
144
+ return false if timestamp.zero? || (now.to_i - timestamp).abs > tolerance
145
+
146
+ expected = OpenSSL::HMAC.hexdigest("SHA256", secret.to_s, "#{timestamp}.#{body}")
147
+ parts.any? { |key, value| key == "v1" && value && ActiveSupport::SecurityUtils.secure_compare(value, expected) }
148
+ end
149
+
150
+ # Runs inline in whatever published the event, inside its transaction when
151
+ # there is one: the ledger rows commit or roll back with the change itself,
152
+ # and the jobs are enqueued only after that commit.
153
+ def dispatch(event_name, payload)
154
+ tenant = OpenLoam::Tenant.find_by(id: payload[:tenant_id])
155
+ return if tenant.nil?
156
+
157
+ deliverable = payload.transform_keys(&:to_s)
158
+ event_id = SecureRandom.uuid
159
+
160
+ OpenLoam.as_tenant(tenant) do
161
+ OpenLoam::WebhookEndpoint.active.each do |endpoint|
162
+ next unless endpoint.matches?(event_name)
163
+
164
+ enqueue(endpoint.deliveries.create!(event_id: event_id, event_name: event_name.to_s, payload: deliverable))
165
+ end
166
+ end
167
+ end
168
+
169
+ def send_test!(endpoint)
170
+ delivery = endpoint.deliveries.create!(event_id: SecureRandom.uuid, event_name: test_event_name,
171
+ payload: { "test" => true, "tenant_id" => endpoint.tenant_id })
172
+ enqueue(delivery)
173
+ delivery
31
174
  end
175
+
176
+ # Manual redelivery of a finished row: restarts the full retry schedule
177
+ # (attempts reset). Non-final (pending/sending) rows are returned untouched.
178
+ def redeliver!(delivery)
179
+ return delivery unless delivery.final?
180
+
181
+ delivery.update!(status: "pending", attempts: 0, next_attempt_at: nil, sending_at: nil)
182
+ enqueue(delivery)
183
+ delivery
184
+ end
185
+
186
+ def enqueue(delivery)
187
+ tenant_id = delivery.tenant_id
188
+ delivery_id = delivery.id
189
+ after_commit { OpenLoam::WebhookDeliveryJob.perform_later(tenant_id, delivery_id) }
190
+ end
191
+
192
+ # Rails >= 7.2 defers to the outermost commit (and runs at once when no
193
+ # transaction is open). On 7.1 the job may start before the row is visible;
194
+ # it then finds nothing and the sweep picks the row up.
195
+ def after_commit(&block)
196
+ if ActiveRecord.respond_to?(:after_all_transactions_commit)
197
+ ActiveRecord.after_all_transactions_commit(&block)
198
+ else
199
+ block.call
200
+ end
201
+ end
202
+
203
+ # Inside one tenant: stuck rows go back to pending (at-least-once — the
204
+ # receiver dedupes by event id), every overdue row gets a job, and final
205
+ # rows past the retention are deleted. Returns the number re-enqueued.
206
+ # A stuck row that has used up the retry schedule fails instead, so a
207
+ # persistent error after the send cannot resend it forever.
208
+ def sweep(now: Time.current, limit: 500)
209
+ stuck = OpenLoam::WebhookDelivery.stuck(now)
210
+ stuck.where("attempts > ?", retry_schedule.size)
211
+ .update_all(status: "failed", sending_at: nil, last_error: "stuck", updated_at: now)
212
+ stuck.update_all(status: "pending", sending_at: nil, next_attempt_at: now, updated_at: now)
213
+
214
+ tenant_id = OpenLoam.tenant!.id
215
+ ids = OpenLoam::WebhookDelivery.overdue(now).order(:id).limit(limit).pluck(:id)
216
+ ids.each { |id| OpenLoam::WebhookDeliveryJob.perform_later(tenant_id, id) }
217
+
218
+ OpenLoam::WebhookDelivery.prunable(now, delivery_retention).delete_all if delivery_retention
219
+ ids.size
220
+ end
221
+
222
+ # Blessed cross-tenant lookup (BACKWARD_COMPATIBILITY.md §1), the webhook
223
+ # twin of the scheduler's due-job claim: tenant ids only, never rows.
224
+ def tenant_ids_needing_sweep(now: Time.current)
225
+ all = OpenLoam::WebhookDelivery.unscoped
226
+ work = all.overdue(now).or(all.stuck(now))
227
+ work = work.or(all.prunable(now, delivery_retention)) if delivery_retention
228
+ work.distinct.pluck(:tenant_id)
229
+ end
230
+
231
+ private
232
+
233
+ def claim(delivery, now)
234
+ OpenLoam::WebhookDelivery.where(id: delivery.id, status: "pending")
235
+ .where("next_attempt_at IS NULL OR next_attempt_at <= ?", now)
236
+ .update_all([ "status = 'sending', sending_at = ?, attempts = attempts + 1, updated_at = ?", now, now ]) == 1
237
+ end
238
+
239
+ def record_response(delivery, endpoint, response, now)
240
+ attrs = { last_response_code: response.code, last_duration_ms: response.duration_ms,
241
+ response_excerpt: response.excerpt, sending_at: nil }
242
+ case response.code
243
+ when 200..299
244
+ delivery.update!(attrs.merge(status: "succeeded", delivered_at: now, last_error: nil, next_attempt_at: nil))
245
+ endpoint.record_success!(now)
246
+ when 410
247
+ delivery.update!(attrs.merge(status: "failed", last_error: "HTTP 410"))
248
+ endpoint.disable!("gone")
249
+ when *RETRYABLE_CODES, 500..599
250
+ delivery.assign_attributes(attrs)
251
+ retry_later(delivery, endpoint, now, error: "HTTP #{response.code}", retry_after: retry_after_seconds(response))
252
+ else
253
+ delivery.update!(attrs.merge(status: "failed", last_error: "HTTP #{response.code}"))
254
+ endpoint.record_failure!(now)
255
+ end
256
+ end
257
+
258
+ # count_failure: false for errors that are the host's, not the receiver's.
259
+ def retry_later(delivery, endpoint, now, error:, retry_after: nil, count_failure: true)
260
+ endpoint&.record_failure!(now) if count_failure
261
+ delay = retry_schedule[delivery.attempts - 1]
262
+ if delay.nil?
263
+ delivery.update!(status: "failed", sending_at: nil, last_error: error)
264
+ return
265
+ end
266
+
267
+ wait = retry_after || (delay * (0.9 + (rand * 0.2))).round
268
+ delivery.update!(status: "pending", sending_at: nil, last_error: error, next_attempt_at: now + wait)
269
+ OpenLoam::WebhookDeliveryJob.set(wait: wait).perform_later(delivery.tenant_id, delivery.id)
270
+ end
271
+
272
+ def retry_after_seconds(response)
273
+ value = response.headers.to_h["retry-after"].to_s
274
+ return nil unless value.match?(/\A\d+\z/)
275
+
276
+ value.to_i.clamp(1, MAX_RETRY_AFTER)
277
+ end
278
+
279
+ def short(message) = message.to_s.truncate(255)
280
+ end
281
+
282
+ # Wired once from OpenLoam::Engine. Idempotent: subscribing twice would deliver
283
+ # every event twice.
284
+ def self.subscribe!
285
+ @subscription ||= Events.subscribe_all { |event_name, payload| dispatch(event_name, payload) }
32
286
  end
33
287
  end
34
288
  end
data/lib/open_loam.rb CHANGED
@@ -7,6 +7,7 @@ require "open_loam/lifecycle"
7
7
  require "open_loam/configs"
8
8
  require "open_loam/features"
9
9
  require "open_loam/auth_throttle"
10
+ require "open_loam/outbound_url"
10
11
  require "open_loam/dictionaries"
11
12
  require "open_loam/custom_field_index"
12
13
  require "open_loam/undo"
@@ -42,8 +43,10 @@ require "open_loam/notifications"
42
43
  require "open_loam/sso"
43
44
  require "open_loam/business_rules"
44
45
  require "open_loam/webhooks"
46
+ require "open_loam/webhooks/http_transport"
45
47
  require "open_loam/inbound_webhooks"
46
48
  require "open_loam/durable_events"
49
+ require "open_loam/event_log"
47
50
  require "open_loam/engine" if defined?(Rails::Engine)
48
51
 
49
52
  module OpenLoam
@@ -103,6 +103,38 @@ namespace :open_loam do
103
103
  end
104
104
  end
105
105
 
106
+ namespace :sso do
107
+ # Domain ownership can only be granted from OUTSIDE the tenant: the manager
108
+ # who typed the domain is the party this check exists to constrain. Confirm
109
+ # ownership out of band (DNS TXT, a signed request from the domain's
110
+ # operator, an existing contract) BEFORE running this.
111
+ #
112
+ # bin/rails open_loam:sso:verify_domain[42]
113
+ desc "Mark an SSO provider's domain as proven-owned (operator only)"
114
+ task :verify_domain, %i[provider_id] => :environment do |_task, args|
115
+ provider = OpenLoam::SsoProvider.unscoped.find(args[:provider_id])
116
+ tenant = OpenLoam::Tenant.find(provider.tenant_id)
117
+ OpenLoam.as_tenant(tenant) { provider.verify_domain! }
118
+ puts "open_loam:sso:verify_domain — #{provider.domain} verified for tenant #{tenant.slug} (provider #{provider.id})."
119
+ end
120
+
121
+ desc "Revoke an SSO provider's domain verification"
122
+ task :unverify_domain, %i[provider_id] => :environment do |_task, args|
123
+ provider = OpenLoam::SsoProvider.unscoped.find(args[:provider_id])
124
+ tenant = OpenLoam::Tenant.find(provider.tenant_id)
125
+ OpenLoam.as_tenant(tenant) { provider.update!(domain_verified_at: nil) }
126
+ puts "open_loam:sso:unverify_domain — #{provider.domain} is no longer verified."
127
+ end
128
+
129
+ desc "List SSO providers and their domain-verification state"
130
+ task providers: :environment do
131
+ OpenLoam::SsoProvider.unscoped.order(:domain).each do |provider|
132
+ state = provider.domain_verified? ? "verified #{provider.domain_verified_at.to_fs(:db)}" : "UNVERIFIED"
133
+ puts format("%-6s %-30s tenant=%-6s %s", provider.id, provider.domain, provider.tenant_id, state)
134
+ end
135
+ end
136
+ end
137
+
106
138
  namespace :encryption do
107
139
  # Rotate a tenant's encrypted data under the current key: read each record's
108
140
  # encrypted fields (old key) and re-seal them (new key), one record at a
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: open-loam
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Grzegorz Smajdor
@@ -64,8 +64,10 @@ files:
64
64
  - README.md
65
65
  - app/jobs/open_loam/custom_field_reindex_job.rb
66
66
  - app/jobs/open_loam/event_delivery_job.rb
67
+ - app/jobs/open_loam/event_log_prune_job.rb
67
68
  - app/jobs/open_loam/event_redelivery_sweep_job.rb
68
69
  - app/jobs/open_loam/webhook_delivery_job.rb
70
+ - app/jobs/open_loam/webhook_sweep_job.rb
69
71
  - app/models/open_loam/api_token.rb
70
72
  - app/models/open_loam/audit_record.rb
71
73
  - app/models/open_loam/auth_attempt.rb
@@ -78,6 +80,7 @@ files:
78
80
  - app/models/open_loam/dictionary.rb
79
81
  - app/models/open_loam/dictionary_entry.rb
80
82
  - app/models/open_loam/event_delivery.rb
83
+ - app/models/open_loam/event_record.rb
81
84
  - app/models/open_loam/field_definition.rb
82
85
  - app/models/open_loam/inbound_webhook_delivery.rb
83
86
  - app/models/open_loam/inbound_webhook_source.rb
@@ -94,6 +97,7 @@ files:
94
97
  - app/models/open_loam/sso_provider.rb
95
98
  - app/models/open_loam/tenant.rb
96
99
  - app/models/open_loam/translation.rb
100
+ - app/models/open_loam/webhook_delivery.rb
97
101
  - app/models/open_loam/webhook_endpoint.rb
98
102
  - app/views/open_loam/attachments/_attachments.html.erb
99
103
  - app/views/open_loam/comments/_comments.html.erb
@@ -192,6 +196,7 @@ files:
192
196
  - lib/generators/open_loam/install/templates/admin/translations_controller.rb
193
197
  - lib/generators/open_loam/install/templates/admin/translations_index.html.erb
194
198
  - lib/generators/open_loam/install/templates/admin/webhook_endpoints_controller.rb
199
+ - lib/generators/open_loam/install/templates/admin/webhook_endpoints_created.html.erb
195
200
  - lib/generators/open_loam/install/templates/admin/webhook_endpoints_index.html.erb
196
201
  - lib/generators/open_loam/install/templates/admin/webhook_endpoints_new.html.erb
197
202
  - lib/generators/open_loam/install/templates/api_base_controller.rb
@@ -210,6 +215,7 @@ files:
210
215
  - lib/generators/open_loam/install/templates/migrations/create_open_loam_dictionaries.rb
211
216
  - lib/generators/open_loam/install/templates/migrations/create_open_loam_dictionary_entries.rb
212
217
  - lib/generators/open_loam/install/templates/migrations/create_open_loam_event_deliveries.rb
218
+ - lib/generators/open_loam/install/templates/migrations/create_open_loam_event_records.rb
213
219
  - lib/generators/open_loam/install/templates/migrations/create_open_loam_field_definitions.rb
214
220
  - lib/generators/open_loam/install/templates/migrations/create_open_loam_inbound_webhooks.rb
215
221
  - lib/generators/open_loam/install/templates/migrations/create_open_loam_memberships.rb
@@ -224,6 +230,7 @@ files:
224
230
  - lib/generators/open_loam/install/templates/migrations/create_open_loam_sso_providers.rb
225
231
  - lib/generators/open_loam/install/templates/migrations/create_open_loam_tenants.rb
226
232
  - lib/generators/open_loam/install/templates/migrations/create_open_loam_translations.rb
233
+ - lib/generators/open_loam/install/templates/migrations/create_open_loam_webhook_deliveries.rb
227
234
  - lib/generators/open_loam/install/templates/migrations/create_open_loam_webhook_endpoints.rb
228
235
  - lib/generators/open_loam/install/templates/migrations/create_users.rb
229
236
  - lib/generators/open_loam/install/templates/user.rb
@@ -256,6 +263,7 @@ files:
256
263
  - lib/open_loam/enrichers.rb
257
264
  - lib/open_loam/errors.rb
258
265
  - lib/open_loam/eval.rb
266
+ - lib/open_loam/event_log.rb
259
267
  - lib/open_loam/event_stream.rb
260
268
  - lib/open_loam/eventful.rb
261
269
  - lib/open_loam/events.rb
@@ -270,6 +278,7 @@ files:
270
278
  - lib/open_loam/mcp/server.rb
271
279
  - lib/open_loam/notifications.rb
272
280
  - lib/open_loam/open_api.rb
281
+ - lib/open_loam/outbound_url.rb
273
282
  - lib/open_loam/overrides.rb
274
283
  - lib/open_loam/pending_actions.rb
275
284
  - lib/open_loam/permissions.rb
@@ -297,6 +306,7 @@ files:
297
306
  - lib/open_loam/undo.rb
298
307
  - lib/open_loam/version.rb
299
308
  - lib/open_loam/webhooks.rb
309
+ - lib/open_loam/webhooks/http_transport.rb
300
310
  - lib/open_loam/widgets.rb
301
311
  - lib/open_loam/workflow.rb
302
312
  - lib/tasks/open_loam.rake
@@ -325,7 +335,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
325
335
  - !ruby/object:Gem::Version
326
336
  version: '0'
327
337
  requirements: []
328
- rubygems_version: 4.0.16
338
+ rubygems_version: 4.0.20
329
339
  specification_version: 4
330
340
  summary: 'AI-native Rails business foundation: tenancy, policies, audit, events, admin
331
341
  — decided, not re-litigated.'