smplkit 1.0.17 → 1.0.18

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: 22da5481c89c52426de98fac54b278a253202a6a3f28aa1e7a03eac0372ec790
4
- data.tar.gz: '0190db86b79b56f478cab2c0c3814e48e5d7e48917afc6925ca42948277f7afe'
3
+ metadata.gz: 81fcac2865542750a3fd991640f3a45e5281e3bbcf2b86a1c8049d2a7c257d84
4
+ data.tar.gz: 65283cde70a75bf79f15b93caa731fc489a9998102f727199029099c3c93fe56
5
5
  SHA512:
6
- metadata.gz: 538c86b5c363d0e38f21e1c4b009a1cdec528ebc3b6bc9dba353bc5e6ea22f91613dfe15980b044bf4aec91e3c8bfa3d3282875b0889141842bbcb9264743371
7
- data.tar.gz: 6ca828d6c3f0b111ca911328a4a7116dfbe4e5b8807b4a7aa56e9bc2f0a4088ae69f655c16b9d64d2307ed8aa6f4030b3baf34f63eff627ddbd6a21fc0672313
6
+ metadata.gz: 8430e48ac2cfd9176c69eebbdb11a26e74ec0e15261bd41f0d10d216a920c58fb60b8e538b139e8b8b831a38beb0ab97ec0b024d2f758390131d66b0ffe49c25
7
+ data.tar.gz: 87ac206557166ed60a693330484eae8e44c0255a9612bab9aa79ea9f585ef328a313e14adb84172fc41a28eb446d49536f1b77e789e75411c8283b7292a11f49
@@ -0,0 +1,158 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Smplkit
4
+ module Audit
5
+ # Bounded in-memory queue + worker thread for fire-and-forget audit
6
+ # emits (ADR-047 §2.6).
7
+ #
8
+ # +#enqueue+ returns immediately. The worker drains on either a
9
+ # periodic tick or once depth crosses the high-water mark, retries
10
+ # transient failures with exponential backoff, drops permanent 4xx
11
+ # (other than 429), and evicts the oldest item under sustained
12
+ # back-pressure.
13
+ class EventBuffer
14
+ MAX_BUFFER_SIZE = 1000
15
+ WATERMARK = 50
16
+ FLUSH_INTERVAL = 5.0
17
+ MAX_ATTEMPTS = 5
18
+ INITIAL_BACKOFF = 0.25
19
+ MAX_BACKOFF = 8.0
20
+
21
+ def initialize(api)
22
+ @api = api
23
+ @queue = []
24
+ @mutex = Mutex.new
25
+ @cond = ConditionVariable.new
26
+ @closed = false
27
+ @dropped_count = 0
28
+ @worker = Thread.new { run }
29
+ @worker.report_on_exception = false
30
+ end
31
+
32
+ def enqueue(body, idempotency_key)
33
+ depth = nil
34
+ @mutex.synchronize do
35
+ return if @closed
36
+
37
+ if @queue.size >= MAX_BUFFER_SIZE
38
+ @queue.shift
39
+ @dropped_count += 1
40
+ warn "[smplkit.audit] buffer full (size=#{MAX_BUFFER_SIZE}); " \
41
+ "dropped oldest event (total dropped=#{@dropped_count})"
42
+ end
43
+ @queue.push(PendingEvent.new(body, idempotency_key))
44
+ depth = @queue.size
45
+ @cond.signal if depth >= WATERMARK
46
+ end
47
+ end
48
+
49
+ def flush(timeout: 5.0)
50
+ deadline = monotonic_now + timeout
51
+ loop do
52
+ empty = @mutex.synchronize { @queue.empty? }
53
+ return if empty
54
+
55
+ if monotonic_now >= deadline
56
+ warn "[smplkit.audit] flush timed out (timeout=#{timeout}s)"
57
+ return
58
+ end
59
+ @mutex.synchronize { @cond.signal }
60
+ sleep 0.05
61
+ end
62
+ end
63
+
64
+ def close(timeout: 5.0)
65
+ flush(timeout: timeout)
66
+ @mutex.synchronize do
67
+ @closed = true
68
+ @cond.broadcast
69
+ end
70
+ @worker.join(timeout)
71
+ end
72
+
73
+ private
74
+
75
+ def run
76
+ loop do
77
+ drain_once
78
+ should_exit = false
79
+ sleep_for = FLUSH_INTERVAL
80
+ @mutex.synchronize do
81
+ should_exit = @closed && @queue.empty?
82
+ unless @queue.empty?
83
+ head = @queue.first
84
+ if head.next_retry_at
85
+ until_next = head.next_retry_at - monotonic_now
86
+ sleep_for = until_next if until_next.positive? && until_next < sleep_for
87
+ end
88
+ end
89
+ @cond.wait(@mutex, sleep_for) unless should_exit
90
+ end
91
+ break if should_exit
92
+ end
93
+ end
94
+
95
+ def drain_once
96
+ loop do
97
+ item = nil
98
+ @mutex.synchronize do
99
+ return if @queue.empty?
100
+
101
+ head = @queue.first
102
+ return if head.next_retry_at && head.next_retry_at > monotonic_now
103
+
104
+ item = @queue.shift
105
+ end
106
+
107
+ status = 0
108
+ begin
109
+ opts = item.idempotency_key ? { idempotency_key: item.idempotency_key } : {}
110
+ @api.create_event(item.body, opts)
111
+ status = 201
112
+ rescue SmplkitGeneratedClient::Audit::ApiError => e
113
+ status = e.code || 0
114
+ rescue StandardError
115
+ status = 0
116
+ end
117
+
118
+ requeue = handle_outcome(item, status)
119
+ if requeue
120
+ @mutex.synchronize { @queue.unshift(requeue) }
121
+ return
122
+ end
123
+ end
124
+ end
125
+
126
+ def handle_outcome(item, status)
127
+ return nil if status >= 200 && status < 300
128
+
129
+ if status >= 400 && status < 500 && status != 429
130
+ warn "[smplkit.audit] permanent failure status=#{status}; event dropped"
131
+ return nil
132
+ end
133
+
134
+ item.attempts += 1
135
+ if item.attempts >= MAX_ATTEMPTS
136
+ warn "[smplkit.audit] gave up after #{item.attempts} attempts (status=#{status})"
137
+ return nil
138
+ end
139
+ backoff = [MAX_BACKOFF, INITIAL_BACKOFF * (2**(item.attempts - 1))].min
140
+ jitter = rand(0.0..(backoff * 0.25))
141
+ item.next_retry_at = monotonic_now + backoff + jitter
142
+ item
143
+ end
144
+
145
+ def monotonic_now
146
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
147
+ end
148
+
149
+ # Holds an outbound event between attempts.
150
+ PendingEvent = Struct.new(:body, :idempotency_key, :attempts, :next_retry_at) do
151
+ def initialize(body, idempotency_key, attempts = 0, next_retry_at = nil)
152
+ super
153
+ end
154
+ end
155
+ private_constant :PendingEvent
156
+ end
157
+ end
158
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Smplkit
4
+ module Audit
5
+ # Audit-product entry point — accessed via +client.audit+.
6
+ #
7
+ # Today the audit namespace is exclusively +#events+; future
8
+ # iterations may add SIEM exports as additional sub-clients
9
+ # (ADR-047 §2.7 lists SIEM streaming as a Pro-tier capability).
10
+ class AuditClient
11
+ attr_reader :events
12
+
13
+ def initialize(api_key:, base_url:, timeout: 10.0)
14
+ cfg = SmplkitGeneratedClient::Audit::Configuration.new
15
+ cfg.host = URI.parse(base_url).host
16
+ cfg.scheme = URI.parse(base_url).scheme
17
+ cfg.access_token = api_key
18
+ cfg.timeout = timeout
19
+ api_client = SmplkitGeneratedClient::Audit::ApiClient.new(cfg)
20
+ api = SmplkitGeneratedClient::Audit::DefaultApi.new(api_client)
21
+ @events = Events.new(api)
22
+ end
23
+
24
+ def _close
25
+ @events._close
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,125 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Smplkit
4
+ module Audit
5
+ # Audit events surface — accessed via +client.audit.events+.
6
+ #
7
+ # +#create+ is fire-and-forget per ADR-047 §2.6 — the call enqueues
8
+ # the event onto an in-memory bounded buffer and returns
9
+ # immediately. +#list+ and +#get+ are synchronous.
10
+ class Events
11
+ def initialize(api)
12
+ @api = api
13
+ @buffer = EventBuffer.new(api)
14
+ end
15
+
16
+ # Enqueue an audit event for asynchronous delivery.
17
+ #
18
+ # Customer attempts to record events with +resource_type+ starting
19
+ # with +smpl.+ are rejected by the server with a 403 (the buffer
20
+ # logs and drops permanent failures).
21
+ def create(action:, resource_type:, resource_id:,
22
+ occurred_at: nil, snapshot: nil, data: nil, idempotency_key: nil)
23
+ raise ArgumentError, "action is required" if action.nil? || action.to_s.empty?
24
+ raise ArgumentError, "resource_type is required" if resource_type.nil? || resource_type.to_s.empty?
25
+ raise ArgumentError, "resource_id is required" if resource_id.nil? || resource_id.to_s.empty?
26
+
27
+ attrs = SmplkitGeneratedClient::Audit::Event.new(
28
+ action: action,
29
+ resource_type: resource_type,
30
+ resource_id: resource_id,
31
+ occurred_at: occurred_at,
32
+ snapshot: snapshot,
33
+ data: data
34
+ )
35
+ resource = SmplkitGeneratedClient::Audit::EventResource.new(
36
+ id: "",
37
+ type: "event",
38
+ attributes: attrs
39
+ )
40
+ body = SmplkitGeneratedClient::Audit::EventResponse.new(data: resource)
41
+ @buffer.enqueue(body, idempotency_key)
42
+ end
43
+
44
+ # Single-event retrieval.
45
+ #
46
+ # Raises +SmplkitGeneratedClient::Audit::ApiError+ on non-2xx
47
+ # responses (404 if the event is not in the caller's account).
48
+ def get(event_id)
49
+ resp = @api.get_event(event_id)
50
+ AuditEvent.from_resource(resp.data)
51
+ end
52
+
53
+ # List events with filters and cursor pagination. Returns a
54
+ # +Smplkit::Audit::ListEventsPage+ whose +#events+ is the page and
55
+ # +#next_cursor+ is the opaque token for the next page (or nil).
56
+ def list(action: nil, resource_type: nil, resource_id: nil,
57
+ actor_type: nil, actor_id: nil, occurred_at_range: nil,
58
+ page_size: nil, page_after: nil)
59
+ opts = {}
60
+ opts[:filteraction] = action if action
61
+ opts[:filterresource_type] = resource_type if resource_type
62
+ opts[:filterresource_id] = resource_id if resource_id
63
+ opts[:filteractor_type] = actor_type if actor_type
64
+ opts[:filteractor_id] = actor_id if actor_id
65
+ opts[:filteroccurred_at] = occurred_at_range if occurred_at_range
66
+ opts[:pagesize] = page_size if page_size
67
+ opts[:pageafter] = page_after if page_after
68
+
69
+ resp = @api.list_events(opts)
70
+ events = (resp.data || []).map { |r| AuditEvent.from_resource(r) }
71
+ next_cursor = nil
72
+ if resp.links&._next.is_a?(String)
73
+ next_link = resp.links._next
74
+ if (idx = next_link.index("page[after]="))
75
+ next_cursor = next_link[(idx + "page[after]=".length)..]
76
+ if (amp = next_cursor.index("&"))
77
+ next_cursor = next_cursor[0...amp]
78
+ end
79
+ end
80
+ end
81
+ ListEventsPage.new(events, next_cursor)
82
+ end
83
+
84
+ # Block until the in-memory buffer is drained or the timeout elapses.
85
+ def flush(timeout: 5.0)
86
+ @buffer.flush(timeout: timeout)
87
+ end
88
+
89
+ # @api private — drains best-effort and stops the worker thread.
90
+ def _close
91
+ @buffer.close
92
+ end
93
+ end
94
+
95
+ # Public-facing audit event resource. ADR-047 §2.3.1.
96
+ AuditEvent = Struct.new(
97
+ :id, :action, :resource_type, :resource_id,
98
+ :occurred_at, :created_at,
99
+ :actor_type, :actor_id, :actor_label,
100
+ :snapshot, :data, :idempotency_key,
101
+ keyword_init: true
102
+ ) do
103
+ def self.from_resource(resource)
104
+ attrs = resource.attributes
105
+ new(
106
+ id: resource.id,
107
+ action: attrs.action,
108
+ resource_type: attrs.resource_type,
109
+ resource_id: attrs.resource_id,
110
+ occurred_at: attrs.occurred_at,
111
+ created_at: attrs.created_at,
112
+ actor_type: attrs.actor_type,
113
+ actor_id: attrs.actor_id,
114
+ actor_label: attrs.actor_label,
115
+ snapshot: attrs.snapshot,
116
+ data: attrs.data || {},
117
+ idempotency_key: attrs.idempotency_key
118
+ )
119
+ end
120
+ end
121
+
122
+ # One page of events plus a cursor for the next page (nil on the last page).
123
+ ListEventsPage = Struct.new(:events, :next_cursor)
124
+ end
125
+ end
@@ -2,6 +2,8 @@
2
2
 
3
3
  require "concurrent"
4
4
 
5
+ require "smplkit/audit/client"
6
+
5
7
  module Smplkit
6
8
  # Synchronous entry point for the smplkit SDK.
7
9
  #
@@ -28,7 +30,7 @@ module Smplkit
28
30
  class Client
29
31
  PERIODIC_FLUSH_INTERVAL = 60.0
30
32
 
31
- attr_reader :manage, :config, :flags, :logging
33
+ attr_reader :manage, :config, :flags, :logging, :audit
32
34
 
33
35
  # Construct, yield to the block, and close on exit.
34
36
  def self.open(**kwargs)
@@ -40,7 +42,7 @@ module Smplkit
40
42
  end
41
43
  end
42
44
 
43
- def initialize(api_key: nil, environment: nil, service: nil, profile: nil,
45
+ def initialize(api_key: nil, environment: nil, service: nil, profile: nil, # rubocop:disable Metrics/AbcSize
44
46
  base_domain: nil, scheme: nil, debug: nil, telemetry: nil)
45
47
  cfg = ConfigResolution.resolve_config(
46
48
  profile: profile, api_key: api_key, base_domain: base_domain, scheme: scheme,
@@ -70,6 +72,7 @@ module Smplkit
70
72
  app_url = ConfigResolution.service_url(cfg.scheme, "app", cfg.base_domain)
71
73
  flags_url = ConfigResolution.service_url(cfg.scheme, "flags", cfg.base_domain)
72
74
  logging_url = ConfigResolution.service_url(cfg.scheme, "logging", cfg.base_domain)
75
+ audit_url = ConfigResolution.service_url(cfg.scheme, "audit", cfg.base_domain)
73
76
  @app_base_url = app_url
74
77
 
75
78
  @metrics = if cfg.telemetry
@@ -84,6 +87,7 @@ module Smplkit
84
87
  flags_base_url: flags_url, app_base_url: app_url)
85
88
  @logging = Logging::LoggingClient.new(self, manage: @manage, metrics: @metrics,
86
89
  logging_base_url: logging_url, app_base_url: app_url)
90
+ @audit = Audit::AuditClient.new(api_key: cfg.api_key, base_url: audit_url)
87
91
 
88
92
  @closed = false
89
93
  schedule_periodic_flush
@@ -153,6 +157,7 @@ module Smplkit
153
157
  @logging._close
154
158
  @flags._close
155
159
  @config._close
160
+ @audit._close
156
161
  @ws_manager&.stop
157
162
  @ws_manager = nil
158
163
  @manage.close
data/lib/smplkit.rb CHANGED
@@ -16,7 +16,7 @@ require_relative "smplkit/version"
16
16
  # +lib/smplkit/_generated/<svc>/lib+ uses internal +require+ paths like
17
17
  # +smplkit_<svc>_client/api_client+, so the directory has to be on
18
18
  # +$LOAD_PATH+ before its top-level entry is required.
19
- %w[app config flags logging].each do |svc|
19
+ %w[app audit config flags logging].each do |svc|
20
20
  generated_lib = File.expand_path("smplkit/_generated/#{svc}/lib", __dir__)
21
21
  $LOAD_PATH.unshift(generated_lib) if File.directory?(generated_lib)
22
22
  end
@@ -27,7 +27,8 @@ end
27
27
  module SmplkitGeneratedClient # rubocop:disable Style/OneClassPerFile
28
28
  end
29
29
 
30
- %w[smplkit_app_client smplkit_config_client smplkit_flags_client smplkit_logging_client].each do |gem_lib|
30
+ %w[smplkit_app_client smplkit_audit_client smplkit_config_client smplkit_flags_client
31
+ smplkit_logging_client].each do |gem_lib|
31
32
  require gem_lib
32
33
  rescue LoadError
33
34
  # Generated tree may be intentionally absent in development snapshots —
@@ -61,6 +62,9 @@ require_relative "smplkit/management/types"
61
62
  require_relative "smplkit/management/models"
62
63
  require_relative "smplkit/management/buffer"
63
64
  require_relative "smplkit/management/client"
65
+ require_relative "smplkit/audit/buffer"
66
+ require_relative "smplkit/audit/events"
67
+ require_relative "smplkit/audit/client"
64
68
  require_relative "smplkit/client"
65
69
 
66
70
  require_relative "smplkit/railtie" if defined?(Rails::Railtie)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smplkit
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.17
4
+ version: 1.0.18
5
5
  platform: ruby
6
6
  authors:
7
7
  - Smpl Solutions LLC
@@ -560,6 +560,9 @@ files:
560
560
  - lib/smplkit/_generated/logging/spec/models/usage_list_response_spec.rb
561
561
  - lib/smplkit/_generated/logging/spec/models/usage_resource_spec.rb
562
562
  - lib/smplkit/_generated/logging/spec/spec_helper.rb
563
+ - lib/smplkit/audit/buffer.rb
564
+ - lib/smplkit/audit/client.rb
565
+ - lib/smplkit/audit/events.rb
563
566
  - lib/smplkit/client.rb
564
567
  - lib/smplkit/config/client.rb
565
568
  - lib/smplkit/config/helpers.rb