devfortress-sdk 0.1.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 +7 -0
- data/.gitignore +4 -0
- data/Gemfile +7 -0
- data/LICENSE +63 -0
- data/README.md +121 -0
- data/Rakefile +11 -0
- data/lib/devfortress/agent_adapter.rb +185 -0
- data/lib/devfortress/agent_loop_detector.rb +167 -0
- data/lib/devfortress/agent_output_exfiltration_detector.rb +115 -0
- data/lib/devfortress/agent_security.rb +431 -0
- data/lib/devfortress/alias_verify.rb +87 -0
- data/lib/devfortress/api_key_aliasing.rb +240 -0
- data/lib/devfortress/bounded_map.rb +80 -0
- data/lib/devfortress/circuit_breaker.rb +149 -0
- data/lib/devfortress/classifier.rb +133 -0
- data/lib/devfortress/client.rb +380 -0
- data/lib/devfortress/client_ip.rb +28 -0
- data/lib/devfortress/clock_drift_detector.rb +71 -0
- data/lib/devfortress/credential_isolation.rb +340 -0
- data/lib/devfortress/credential_vault.rb +93 -0
- data/lib/devfortress/delivery_result.rb +9 -0
- data/lib/devfortress/graduated_rate_limiter.rb +270 -0
- data/lib/devfortress/internal_closed_loop_engine.rb +658 -0
- data/lib/devfortress/license_manager.rb +297 -0
- data/lib/devfortress/observer.rb +62 -0
- data/lib/devfortress/prompt_injection_filter.rb +73 -0
- data/lib/devfortress/response_webhook_client.rb +165 -0
- data/lib/devfortress/shadow_agent_registry.rb +123 -0
- data/lib/devfortress/signer.rb +134 -0
- data/lib/devfortress/telemetry.rb +407 -0
- data/lib/devfortress/tier_gate.rb +294 -0
- data/lib/devfortress/tool_call_sequence_validator.rb +126 -0
- data/lib/devfortress/transport.rb +31 -0
- data/lib/devfortress/unified_audit_trail.rb +213 -0
- data/lib/devfortress/version.rb +5 -0
- data/lib/devfortress/webhook_verification.rb +9 -0
- data/lib/devfortress/webhook_verifier.rb +93 -0
- data/lib/devfortress.rb +96 -0
- metadata +110 -0
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "openssl"
|
|
4
|
+
require "digest"
|
|
5
|
+
require "json"
|
|
6
|
+
require "securerandom"
|
|
7
|
+
|
|
8
|
+
module DevFortress
|
|
9
|
+
# Credential Isolation Security — Session Indirection (Hardened)
|
|
10
|
+
#
|
|
11
|
+
# Replaces real session tokens with opaque, random aliases before they
|
|
12
|
+
# leave the client app's trust boundary. DevFortress only ever sees the
|
|
13
|
+
# alias — never the real token. Mirrors
|
|
14
|
+
# packages/devfortress-sdk/src/credential-isolation.ts (IP-M3 through
|
|
15
|
+
# IP-M7, IP-S5).
|
|
16
|
+
#
|
|
17
|
+
# A cache adapter is any object responding to #get(key), #set(key, value,
|
|
18
|
+
# ttl_seconds) and #del(key); falls back to an in-memory store for
|
|
19
|
+
# development/testing. For production use with Redis, implement this
|
|
20
|
+
# duck type against your Redis client.
|
|
21
|
+
|
|
22
|
+
# In-memory LRU cache (IP-S5) — fallback for Redis.
|
|
23
|
+
class LRUCache
|
|
24
|
+
def initialize(max_entries = 1000, default_ttl_seconds = 60)
|
|
25
|
+
@max_entries = max_entries
|
|
26
|
+
@default_ttl = default_ttl_seconds
|
|
27
|
+
@cache = {}
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def get(key)
|
|
31
|
+
entry = @cache[key]
|
|
32
|
+
return nil unless entry
|
|
33
|
+
if Time.now.to_f > entry[:expires_at]
|
|
34
|
+
@cache.delete(key)
|
|
35
|
+
return nil
|
|
36
|
+
end
|
|
37
|
+
# Move to end (most recently used)
|
|
38
|
+
@cache.delete(key)
|
|
39
|
+
@cache[key] = entry
|
|
40
|
+
entry[:value]
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def set(key, value, ttl_seconds = nil)
|
|
44
|
+
if @cache.size >= @max_entries && !@cache.key?(key)
|
|
45
|
+
@cache.delete(@cache.first[0])
|
|
46
|
+
end
|
|
47
|
+
@cache[key] = { value: value, expires_at: Time.now.to_f + (ttl_seconds || @default_ttl) }
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def del(key)
|
|
51
|
+
@cache.delete(key)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def has?(key)
|
|
55
|
+
!get(key).nil?
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# In-memory cache adapter (development fallback), shared process-wide —
|
|
60
|
+
# mirrors the Node SDK's module-level memStore.
|
|
61
|
+
class MemoryCacheAdapter
|
|
62
|
+
def initialize
|
|
63
|
+
@mutex = Mutex.new
|
|
64
|
+
@store = {}
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def get(key)
|
|
68
|
+
@mutex.synchronize do
|
|
69
|
+
entry = @store[key]
|
|
70
|
+
return nil unless entry
|
|
71
|
+
if Time.now.to_f > entry[:expires_at]
|
|
72
|
+
@store.delete(key)
|
|
73
|
+
return nil
|
|
74
|
+
end
|
|
75
|
+
entry[:value]
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def set(key, value, ttl_seconds)
|
|
80
|
+
@mutex.synchronize { @store[key] = { value: value, expires_at: Time.now.to_f + ttl_seconds } }
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def del(key)
|
|
84
|
+
@mutex.synchronize { @store.delete(key) }
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
DEFAULT_MEMORY_ADAPTER = MemoryCacheAdapter.new
|
|
89
|
+
private_constant :DEFAULT_MEMORY_ADAPTER
|
|
90
|
+
|
|
91
|
+
# Local emergency blocklist (IP-M6) — synchronous, pre-webhook IP
|
|
92
|
+
# blocking. When a critical event fires, the IP is written here BEFORE
|
|
93
|
+
# the webhook is sent, so the IP is blocked even if the webhook receiver
|
|
94
|
+
# is down.
|
|
95
|
+
class ThreatInterceptor
|
|
96
|
+
# Default TTL: 15 minutes (10 min webhook retry window + 5 min safety)
|
|
97
|
+
def initialize(cache: nil, ttl_seconds: 900)
|
|
98
|
+
@cache = cache || DEFAULT_MEMORY_ADAPTER
|
|
99
|
+
@lru = LRUCache.new(5000, ttl_seconds)
|
|
100
|
+
@default_ttl_seconds = ttl_seconds
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def block_ip(ip, reason: "critical_event")
|
|
104
|
+
value = JSON.generate({ reason: reason, blocked_at: (Time.now.to_f * 1000).to_i })
|
|
105
|
+
@lru.set("emergency:block:#{ip}", value, @default_ttl_seconds)
|
|
106
|
+
begin
|
|
107
|
+
@cache.set("emergency:block:#{ip}", value, @default_ttl_seconds)
|
|
108
|
+
rescue StandardError
|
|
109
|
+
# LRU cache is already set — cache failure doesn't prevent blocking
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def blocked?(ip)
|
|
114
|
+
return true if @lru.has?("emergency:block:#{ip}")
|
|
115
|
+
|
|
116
|
+
begin
|
|
117
|
+
!@cache.get("emergency:block:#{ip}").nil?
|
|
118
|
+
rescue StandardError
|
|
119
|
+
false # On cache failure, LRU was already checked
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def unblock_ip(ip)
|
|
124
|
+
@lru.del("emergency:block:#{ip}")
|
|
125
|
+
begin
|
|
126
|
+
@cache.del("emergency:block:#{ip}")
|
|
127
|
+
rescue StandardError
|
|
128
|
+
# Best-effort
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
REVOCATION_REASONS = %w[active_threat rotation_expired logout].freeze
|
|
134
|
+
|
|
135
|
+
# Credential Isolation Manager (Hardened).
|
|
136
|
+
class CredentialIsolationManager
|
|
137
|
+
def initialize(cache_adapter: nil, encryption_key: nil, lru_max_entries: 1000, lru_ttl_seconds: 60,
|
|
138
|
+
rotation_grace_period_seconds: 1200, on_fallback_revocation: nil)
|
|
139
|
+
@cache = cache_adapter || DEFAULT_MEMORY_ADAPTER
|
|
140
|
+
@lru = LRUCache.new(lru_max_entries, lru_ttl_seconds)
|
|
141
|
+
@rotation_grace_period = rotation_grace_period_seconds
|
|
142
|
+
@on_fallback_revocation = on_fallback_revocation
|
|
143
|
+
|
|
144
|
+
# Encryption is mandatory: credentials must never be cached in
|
|
145
|
+
# plaintext. Fail loudly at construction time rather than silently
|
|
146
|
+
# no-op'ing in #encrypt_value/#decrypt_value.
|
|
147
|
+
key = encryption_key || ENV["DEVFORTRESS_REDIS_ENCRYPTION_KEY"]
|
|
148
|
+
if key.nil? || key.empty?
|
|
149
|
+
raise ArgumentError,
|
|
150
|
+
"DEVFORTRESS_REDIS_ENCRYPTION_KEY is required — CredentialIsolationManager refuses to store " \
|
|
151
|
+
"credentials without encryption. Set this env var to a 32-byte hex string (64 hex characters), " \
|
|
152
|
+
"e.g. `openssl rand -hex 32`, or pass encryption_key: explicitly."
|
|
153
|
+
end
|
|
154
|
+
if key.length != 64
|
|
155
|
+
raise ArgumentError, "DEVFORTRESS_REDIS_ENCRYPTION_KEY must be a 32-byte hex string (64 hex characters)"
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
@encryption_key = key
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
ENCRYPTED_PREFIX = "enc:v1:"
|
|
162
|
+
|
|
163
|
+
# Generate a cryptographically random alias for a real session token.
|
|
164
|
+
# The alias has NO mathematical relationship to the real token.
|
|
165
|
+
def create_alias(real_token, user_id, ttl_seconds: 3600)
|
|
166
|
+
token_hash = hash_token(real_token)
|
|
167
|
+
|
|
168
|
+
existing_alias = cache_get("cred:alias:#{token_hash}")
|
|
169
|
+
return existing_alias if existing_alias
|
|
170
|
+
|
|
171
|
+
alias_id = "df_sid_#{SecureRandom.hex(16)}"
|
|
172
|
+
data = { "realToken" => real_token, "userId" => user_id, "createdAt" => now_ms }
|
|
173
|
+
|
|
174
|
+
encrypted_data = encrypt_value(JSON.generate(data))
|
|
175
|
+
cache_set("alias:#{alias_id}", encrypted_data, ttl_seconds)
|
|
176
|
+
cache_set("cred:alias:#{token_hash}", alias_id, ttl_seconds)
|
|
177
|
+
|
|
178
|
+
alias_id
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
# Resolve an alias back to the real token and user. Called ONLY when a
|
|
182
|
+
# revocation webhook arrives from DevFortress.
|
|
183
|
+
def resolve_alias(alias_id)
|
|
184
|
+
raw = cache_get("alias:#{alias_id}")
|
|
185
|
+
return nil unless raw
|
|
186
|
+
|
|
187
|
+
begin
|
|
188
|
+
decrypted = decrypt_value(raw)
|
|
189
|
+
data = JSON.parse(decrypted)
|
|
190
|
+
{ "realToken" => data["realToken"], "userId" => data["userId"], "createdAt" => data["createdAt"] }
|
|
191
|
+
rescue StandardError
|
|
192
|
+
nil
|
|
193
|
+
end
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
# Revoke a session by alias. The real token is resolved within the
|
|
197
|
+
# client app — DevFortress never touches the real token.
|
|
198
|
+
def revoke_by_alias(alias_id, reason: "active_threat", subject_id: nil)
|
|
199
|
+
resolved = resolve_alias(alias_id)
|
|
200
|
+
if resolved.nil?
|
|
201
|
+
if subject_id && @on_fallback_revocation
|
|
202
|
+
@on_fallback_revocation.call(subject_id, "[DF] alias resolution failed for #{alias_id} — falling back to subjectId revocation")
|
|
203
|
+
end
|
|
204
|
+
return nil
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
token_hash = hash_token(resolved["realToken"])
|
|
208
|
+
cache_del("alias:#{alias_id}")
|
|
209
|
+
cache_del("cred:alias:#{token_hash}")
|
|
210
|
+
|
|
211
|
+
marker = JSON.generate({ "revoked" => true, "reason" => reason, "revokedAt" => now_ms, "revocation_scope" => "precise" })
|
|
212
|
+
cache_set("revoked:alias:#{alias_id}", marker, 86_400)
|
|
213
|
+
|
|
214
|
+
resolved
|
|
215
|
+
rescue StandardError => e
|
|
216
|
+
fallback_subject = subject_id || "unknown"
|
|
217
|
+
@on_fallback_revocation&.call(fallback_subject, "[DF] cache error during revoke_by_alias(#{alias_id}) — falling back to subjectId revocation. Error: #{e.message}")
|
|
218
|
+
begin
|
|
219
|
+
marker = JSON.generate({ "revoked" => true, "reason" => reason, "revokedAt" => now_ms, "revocation_scope" => "broad" })
|
|
220
|
+
cache_set("revoked:alias:#{alias_id}", marker, 86_400)
|
|
221
|
+
rescue StandardError
|
|
222
|
+
# Best-effort
|
|
223
|
+
end
|
|
224
|
+
nil
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
# Rotate alias — replaces the alias for a live session. IP-M7: the old
|
|
228
|
+
# alias is NOT deleted immediately; it gets a grace-period TTL and stays
|
|
229
|
+
# resolvable for revocation purposes.
|
|
230
|
+
def rotate_alias(old_alias, ttl_seconds: 3600)
|
|
231
|
+
resolved = resolve_alias(old_alias)
|
|
232
|
+
return nil unless resolved
|
|
233
|
+
|
|
234
|
+
rotating_data = resolved.merge("status" => "rotating", "canCreateNewEvents" => false)
|
|
235
|
+
encrypted_rotating = encrypt_value(JSON.generate(rotating_data))
|
|
236
|
+
cache_set("alias:#{old_alias}", encrypted_rotating, @rotation_grace_period)
|
|
237
|
+
|
|
238
|
+
token_hash = hash_token(resolved["realToken"])
|
|
239
|
+
cache_del("cred:alias:#{token_hash}")
|
|
240
|
+
|
|
241
|
+
create_alias(resolved["realToken"], resolved["userId"], ttl_seconds: ttl_seconds)
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
# Clean up alias when user logs out normally. Always call this on
|
|
245
|
+
# logout — don't wait for TTL expiry.
|
|
246
|
+
def revoke_on_logout(real_token)
|
|
247
|
+
token_hash = hash_token(real_token)
|
|
248
|
+
alias_id = cache_get("cred:alias:#{token_hash}")
|
|
249
|
+
return unless alias_id
|
|
250
|
+
|
|
251
|
+
marker = JSON.generate({ "revoked" => true, "reason" => "logout", "revokedAt" => now_ms, "revocation_scope" => "precise" })
|
|
252
|
+
cache_set("revoked:alias:#{alias_id}", marker, 86_400)
|
|
253
|
+
cache_del("alias:#{alias_id}")
|
|
254
|
+
cache_del("cred:alias:#{token_hash}")
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
# Check if an alias was already revoked (idempotency for webhook retries).
|
|
258
|
+
def revoked?(alias_id)
|
|
259
|
+
!cache_get("revoked:alias:#{alias_id}").nil?
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
def revocation_details(alias_id)
|
|
263
|
+
raw = cache_get("revoked:alias:#{alias_id}")
|
|
264
|
+
return nil unless raw
|
|
265
|
+
|
|
266
|
+
begin
|
|
267
|
+
JSON.parse(raw)
|
|
268
|
+
rescue JSON::ParserError
|
|
269
|
+
{ "reason" => "active_threat", "revokedAt" => now_ms, "revocation_scope" => "precise" }
|
|
270
|
+
end
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
private
|
|
274
|
+
|
|
275
|
+
def now_ms
|
|
276
|
+
(Time.now.to_f * 1000).to_i
|
|
277
|
+
end
|
|
278
|
+
|
|
279
|
+
def encrypt_value(plaintext)
|
|
280
|
+
key = [@encryption_key].pack("H*")
|
|
281
|
+
iv = SecureRandom.random_bytes(12)
|
|
282
|
+
cipher = OpenSSL::Cipher.new("aes-256-gcm")
|
|
283
|
+
cipher.encrypt
|
|
284
|
+
cipher.key = key
|
|
285
|
+
cipher.iv = iv
|
|
286
|
+
encrypted = cipher.update(plaintext) + cipher.final
|
|
287
|
+
"#{ENCRYPTED_PREFIX}#{iv.unpack1('H*')}:#{encrypted.unpack1('H*')}:#{cipher.auth_tag.unpack1('H*')}"
|
|
288
|
+
end
|
|
289
|
+
|
|
290
|
+
def decrypt_value(ciphertext)
|
|
291
|
+
return ciphertext unless ciphertext.start_with?(ENCRYPTED_PREFIX)
|
|
292
|
+
|
|
293
|
+
parts = ciphertext[ENCRYPTED_PREFIX.length..].split(":")
|
|
294
|
+
raise "Invalid encrypted format" unless parts.length == 3
|
|
295
|
+
|
|
296
|
+
key = [@encryption_key].pack("H*")
|
|
297
|
+
decipher = OpenSSL::Cipher.new("aes-256-gcm")
|
|
298
|
+
decipher.decrypt
|
|
299
|
+
decipher.key = key
|
|
300
|
+
decipher.iv = [parts[0]].pack("H*")
|
|
301
|
+
decipher.auth_tag = [parts[2]].pack("H*")
|
|
302
|
+
decipher.update([parts[1]].pack("H*")) + decipher.final
|
|
303
|
+
end
|
|
304
|
+
|
|
305
|
+
def hash_token(real_token)
|
|
306
|
+
Digest::SHA256.hexdigest(real_token)
|
|
307
|
+
end
|
|
308
|
+
|
|
309
|
+
def cache_get(key)
|
|
310
|
+
begin
|
|
311
|
+
value = @cache.get(key)
|
|
312
|
+
if !value.nil?
|
|
313
|
+
@lru.set(key)
|
|
314
|
+
return value
|
|
315
|
+
end
|
|
316
|
+
rescue StandardError
|
|
317
|
+
# Cache failure — fall back to LRU (IP-S5)
|
|
318
|
+
end
|
|
319
|
+
@lru.get(key)
|
|
320
|
+
end
|
|
321
|
+
|
|
322
|
+
def cache_set(key, value, ttl_seconds)
|
|
323
|
+
@lru.set(key, value, ttl_seconds)
|
|
324
|
+
begin
|
|
325
|
+
@cache.set(key, value, ttl_seconds)
|
|
326
|
+
rescue StandardError
|
|
327
|
+
# LRU is set — cache failure is survivable
|
|
328
|
+
end
|
|
329
|
+
end
|
|
330
|
+
|
|
331
|
+
def cache_del(key)
|
|
332
|
+
@lru.del(key)
|
|
333
|
+
begin
|
|
334
|
+
@cache.del(key)
|
|
335
|
+
rescue StandardError
|
|
336
|
+
# Best-effort
|
|
337
|
+
end
|
|
338
|
+
end
|
|
339
|
+
end
|
|
340
|
+
end
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "openssl"
|
|
4
|
+
require "securerandom"
|
|
5
|
+
|
|
6
|
+
module DevFortress
|
|
7
|
+
# Shared AES-256-GCM reversible-encryption primitive.
|
|
8
|
+
#
|
|
9
|
+
# Mirrors packages/devfortress-sdk/src/credential-vault.ts. A generic
|
|
10
|
+
# reversible-encryption primitive; ApiKeyAliasingService and
|
|
11
|
+
# tier2_gated/identity_projection.rb each wire into their own
|
|
12
|
+
# alias-keyed stores.
|
|
13
|
+
#
|
|
14
|
+
# Key derivation: HKDF-SHA256 with a zero salt and a namespaced info
|
|
15
|
+
# string, matching tier2_gated/alias_generator.rb's hkdf_alias.
|
|
16
|
+
module CredentialVault
|
|
17
|
+
VaultEncryptedRecord = Struct.new(:ciphertext, :iv, :auth_tag, keyword_init: true)
|
|
18
|
+
|
|
19
|
+
# Derive a 32-byte AES-256 key from an application secret + context via
|
|
20
|
+
# HKDF-SHA256.
|
|
21
|
+
def self.derive_vault_key(application_secret, context)
|
|
22
|
+
info = "devfortress.credential-vault.#{context}"
|
|
23
|
+
OpenSSL::KDF.hkdf(application_secret.to_s.b, salt: "\x00".b * 32, info: info.b, length: 32, hash: "SHA256")
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Pure primitive: encrypt a real credential with an explicit key.
|
|
27
|
+
def self.encrypt(real_credential, key)
|
|
28
|
+
iv = SecureRandom.random_bytes(12)
|
|
29
|
+
cipher = OpenSSL::Cipher.new("aes-256-gcm")
|
|
30
|
+
cipher.encrypt
|
|
31
|
+
cipher.key = key
|
|
32
|
+
cipher.iv = iv
|
|
33
|
+
ciphertext = cipher.update(real_credential.to_s.b) + cipher.final
|
|
34
|
+
VaultEncryptedRecord.new(
|
|
35
|
+
ciphertext: ciphertext.unpack1("H*"),
|
|
36
|
+
iv: iv.unpack1("H*"),
|
|
37
|
+
auth_tag: cipher.auth_tag.unpack1("H*"),
|
|
38
|
+
)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Pure primitive: decrypt a ciphertext bundle with an explicit key.
|
|
42
|
+
# Raises OpenSSL::Cipher::CipherError (does not return garbage
|
|
43
|
+
# plaintext) on auth-tag mismatch.
|
|
44
|
+
def self.decrypt(record, key)
|
|
45
|
+
decipher = OpenSSL::Cipher.new("aes-256-gcm")
|
|
46
|
+
decipher.decrypt
|
|
47
|
+
decipher.key = key
|
|
48
|
+
decipher.iv = [record.iv].pack("H*")
|
|
49
|
+
decipher.auth_tag = [record.auth_tag].pack("H*")
|
|
50
|
+
decipher.update([record.ciphertext].pack("H*")) + decipher.final
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# A self-contained, in-memory vault instance: encrypts + stores real
|
|
55
|
+
# credentials under an alias, later resolves the alias back to plaintext.
|
|
56
|
+
# Used wherever a class needs its own alias-keyed encrypted store.
|
|
57
|
+
class CredentialVaultStore
|
|
58
|
+
MAX_TRACKED_RECORDS = 50_000
|
|
59
|
+
|
|
60
|
+
def initialize(max_entries = MAX_TRACKED_RECORDS)
|
|
61
|
+
@max_entries = max_entries
|
|
62
|
+
@store = {}
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def register(alias_id, real_credential, key)
|
|
66
|
+
record = CredentialVault.encrypt(real_credential, key)
|
|
67
|
+
if @store.key?(alias_id)
|
|
68
|
+
@store.delete(alias_id)
|
|
69
|
+
elsif @store.size >= @max_entries && !@store.empty?
|
|
70
|
+
@store.delete(@store.first[0])
|
|
71
|
+
end
|
|
72
|
+
@store[alias_id] = { record: record, key: key }
|
|
73
|
+
record
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# Raises on auth-tag mismatch (does not return garbage plaintext) —
|
|
77
|
+
# matches the Node primitive's #resolve, which does not rescue.
|
|
78
|
+
def resolve(alias_id)
|
|
79
|
+
stored = @store[alias_id]
|
|
80
|
+
return nil unless stored
|
|
81
|
+
|
|
82
|
+
CredentialVault.decrypt(stored[:record], stored[:key])
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def revoke(alias_id)
|
|
86
|
+
!@store.delete(alias_id).nil?
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def has?(alias_id)
|
|
90
|
+
@store.key?(alias_id)
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|