legion-crypt 1.5.42 → 1.6.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 +71 -0
- data/CLAUDE.md +12 -11
- data/lib/legion/crypt/lease_manager.rb +5 -831
- data/lib/legion/crypt/settings.rb +7 -0
- data/lib/legion/crypt/vault/auth/jwt.rb +28 -0
- data/lib/legion/crypt/vault/auth/kerberos.rb +60 -0
- data/lib/legion/crypt/vault/auth/ldap.rb +29 -0
- data/lib/legion/crypt/vault/auth.rb +50 -0
- data/lib/legion/crypt/vault/client.rb +9 -2
- data/lib/legion/crypt/vault/cluster.rb +47 -115
- data/lib/legion/crypt/vault/entity.rb +3 -4
- data/lib/legion/crypt/vault/leases.rb +871 -0
- data/lib/legion/crypt/vault/session.rb +405 -0
- data/lib/legion/crypt/vault.rb +16 -21
- data/lib/legion/crypt/version.rb +1 -1
- data/lib/legion/crypt.rb +105 -119
- metadata +7 -6
- data/lib/legion/crypt/kerberos_auth.rb +0 -119
- data/lib/legion/crypt/ldap_auth.rb +0 -48
- data/lib/legion/crypt/token_renewer.rb +0 -318
- data/lib/legion/crypt/vault/jwt_auth.rb +0 -99
- data/lib/legion/crypt/vault/kerberos_auth.rb +0 -53
|
@@ -0,0 +1,405 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'time'
|
|
4
|
+
require 'faraday'
|
|
5
|
+
require 'legion/logging/helper'
|
|
6
|
+
require 'legion/crypt/vault/client'
|
|
7
|
+
require 'legion/crypt/vault/auth'
|
|
8
|
+
require 'legion/crypt/vault/leases'
|
|
9
|
+
require 'legion/crypt/vault/operation'
|
|
10
|
+
|
|
11
|
+
module Legion
|
|
12
|
+
module Crypt
|
|
13
|
+
module Vault
|
|
14
|
+
# Owns one cluster's token + leases lifecycle. Holds the current grant (the login auth
|
|
15
|
+
# response — the source of truth for the token's identity and lifetime) directly, so renewal
|
|
16
|
+
# and revocation work for both minted (auth-method) and adopted (static/config) tokens.
|
|
17
|
+
# Network I/O runs outside @state_mutex; only state transitions hold it briefly, so a
|
|
18
|
+
# concurrent request_stop is never blocked behind a network call. One instance per cluster.
|
|
19
|
+
class Session
|
|
20
|
+
include Legion::Logging::Helper
|
|
21
|
+
|
|
22
|
+
RENEWAL_RATIO = 0.75
|
|
23
|
+
INITIAL_BACKOFF = 30
|
|
24
|
+
MAX_BACKOFF = 600
|
|
25
|
+
MINIMUM_WAIT = 0.01
|
|
26
|
+
RENEWAL_CHECK_INTERVAL = 5
|
|
27
|
+
RETRYABLE_CODES = [408, 429].freeze
|
|
28
|
+
STANDBY_CODES = [429, 472, 473].freeze # standby, DR secondary, performance standby
|
|
29
|
+
FAILURES = [IOError, SystemCallError, Timeout::Error, Faraday::Error,
|
|
30
|
+
Legion::Crypt::Vault::HTTPError].freeze
|
|
31
|
+
|
|
32
|
+
attr_reader :cluster_name, :client
|
|
33
|
+
|
|
34
|
+
def initialize(cluster_name:, client:, leases: nil, lease_defs: nil, **)
|
|
35
|
+
@cluster_name = cluster_name
|
|
36
|
+
@client = client
|
|
37
|
+
@leases = leases
|
|
38
|
+
@lease_defs = lease_defs
|
|
39
|
+
@auth = nil
|
|
40
|
+
@auth_method = nil
|
|
41
|
+
@grant = nil
|
|
42
|
+
@current_token = nil
|
|
43
|
+
@lease_duration = 0
|
|
44
|
+
@renewable = false
|
|
45
|
+
@expires_at = nil
|
|
46
|
+
@rejected = false
|
|
47
|
+
@disconnected = false
|
|
48
|
+
@backoff = INITIAL_BACKOFF
|
|
49
|
+
@minted = false
|
|
50
|
+
@stop = false
|
|
51
|
+
@started = false
|
|
52
|
+
@thread = nil
|
|
53
|
+
@state_mutex = Mutex.new
|
|
54
|
+
@wake = ConditionVariable.new
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# Kerberos and static-token clusters authenticate at start; LDAP/JWT wait for an explicit login.
|
|
58
|
+
def start(**)
|
|
59
|
+
return false unless begin_lifecycle
|
|
60
|
+
|
|
61
|
+
@auth_method = cluster_config[:auth_method]&.to_s
|
|
62
|
+
authenticate if @auth_method == 'kerberos'
|
|
63
|
+
adopt_config_token if @auth_method.nil? || @auth_method.empty?
|
|
64
|
+
start_maintenance
|
|
65
|
+
true
|
|
66
|
+
rescue Legion::Crypt::Vault::Auth::AuthError, *FAILURES => e
|
|
67
|
+
handle_exception(e, level: :error, operation: 'crypt.vault_session.start', cluster_name: @cluster_name)
|
|
68
|
+
false
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# Explicit LDAP/JWT login. Returns the grant Hash (the pre-refactor ldap_login contract);
|
|
72
|
+
# a failed login raises to the caller, which owns reporting it.
|
|
73
|
+
def login(**creds)
|
|
74
|
+
return nil if stopped?
|
|
75
|
+
|
|
76
|
+
grant = auth.authenticate(client: @client, **creds)
|
|
77
|
+
commit_grant(grant: grant, minted: true)
|
|
78
|
+
start_maintenance
|
|
79
|
+
grant_hash
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Stops the maintenance thread, shuts down the leases, and then revokes a MINTED (auth-method)
|
|
83
|
+
# token. An adopted static/config token is never revoked — it belongs to the operator.
|
|
84
|
+
# Returns whether the leases and the token were actually cleaned up in Vault.
|
|
85
|
+
def stop(deadline:, **)
|
|
86
|
+
request_stop
|
|
87
|
+
thread = @state_mutex.synchronize { @thread }
|
|
88
|
+
if thread && thread != Thread.current
|
|
89
|
+
remaining = deadline - ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
|
|
90
|
+
thread.join(remaining) if remaining.positive?
|
|
91
|
+
end
|
|
92
|
+
leases_released = @leases.nil? || @leases.shutdown(deadline: deadline)
|
|
93
|
+
token_released = !@state_mutex.synchronize { @minted && revocable_locked? } || revoke_token
|
|
94
|
+
if leases_released && token_released
|
|
95
|
+
log.info "Vault session stopped cluster=#{label}"
|
|
96
|
+
else
|
|
97
|
+
log.error "Vault session stopped with incomplete cleanup cluster=#{label} leases=#{leases_released} token=#{token_released}"
|
|
98
|
+
end
|
|
99
|
+
leases_released && token_released
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# Signals the maintenance thread to stop. Public so Legion::Crypt.shutdown can request an
|
|
103
|
+
# early stop without racing the lifecycle lock across a network call.
|
|
104
|
+
def request_stop
|
|
105
|
+
@state_mutex.synchronize do
|
|
106
|
+
@stop = true
|
|
107
|
+
@wake.broadcast
|
|
108
|
+
end
|
|
109
|
+
@leases&.request_stop
|
|
110
|
+
nil
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def running?
|
|
114
|
+
@state_mutex.synchronize { @thread&.alive? == true }
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def revocable?
|
|
118
|
+
@state_mutex.synchronize { revocable_locked? }
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def principal
|
|
122
|
+
@state_mutex.synchronize { @grant&.metadata&.dig(:username) }
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# Called (as a 0-arg callable or with a block) to test/drive work while this session is the
|
|
126
|
+
# live grant owner. Used by Vault::Leases#reissue_all during the handoff.
|
|
127
|
+
def grant_active
|
|
128
|
+
@state_mutex.synchronize do
|
|
129
|
+
next false if @stop || !@started || crypt_stopping?
|
|
130
|
+
|
|
131
|
+
yield if block_given?
|
|
132
|
+
true
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
private
|
|
137
|
+
|
|
138
|
+
def label
|
|
139
|
+
@cluster_name || 'default'
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def crypt_stopping?
|
|
143
|
+
Legion::Crypt.respond_to?(:stopping) && Legion::Crypt.stopping
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def cluster_config
|
|
147
|
+
vault = Legion::Settings[:crypt][:vault]
|
|
148
|
+
@cluster_name ? vault[:clusters].fetch(@cluster_name) : vault
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
def auth
|
|
152
|
+
@auth ||= case cluster_config[:auth_method]&.to_s
|
|
153
|
+
when 'kerberos' then Legion::Crypt::Vault::Auth::Kerberos.new(cluster_name: @cluster_name)
|
|
154
|
+
when 'ldap' then Legion::Crypt::Vault::Auth::LDAP.new(cluster_name: @cluster_name)
|
|
155
|
+
when 'jwt' then Legion::Crypt::Vault::Auth::JWT.new(cluster_name: @cluster_name)
|
|
156
|
+
else
|
|
157
|
+
raise ArgumentError, "unsupported Vault auth_method: #{cluster_config[:auth_method].inspect}"
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def stopped?
|
|
162
|
+
@state_mutex.synchronize { @stop }
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
def begin_lifecycle
|
|
166
|
+
@state_mutex.synchronize do
|
|
167
|
+
return false if @stop || @started
|
|
168
|
+
|
|
169
|
+
@started = true
|
|
170
|
+
true
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
def revocable_locked?
|
|
175
|
+
!@rejected && (@expires_at.nil? || @expires_at > Time.now)
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
# Network auth runs outside the mutex; the grant commit is a brief mutex hold.
|
|
179
|
+
def authenticate
|
|
180
|
+
grant = auth.authenticate(client: @client)
|
|
181
|
+
commit_grant(grant: grant, minted: true)
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
# Adopts a pre-configured (static) token once Vault answers a health check. It is non-minted,
|
|
185
|
+
# so it is never revoked on stop, and it is renewed only if its configuration says renewable.
|
|
186
|
+
def adopt_config_token
|
|
187
|
+
token = cluster_config[:token]
|
|
188
|
+
return if token.nil?
|
|
189
|
+
|
|
190
|
+
@auth_method = 'token'
|
|
191
|
+
return Legion::Crypt.disconnect_vault_grant(cluster_name: @cluster_name, token: token, relogin_required: false) unless cluster_healthy?
|
|
192
|
+
|
|
193
|
+
grant = Legion::Crypt::Vault::Client::Auth.new(data: { client_token: token,
|
|
194
|
+
lease_duration: cluster_config[:lease_duration],
|
|
195
|
+
renewable: cluster_config[:renewable] })
|
|
196
|
+
commit_grant(grant: grant, minted: false)
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
# Brief mutex hold: installs the grant as current and publishes it to the cluster config.
|
|
200
|
+
# previous_token guards against a concurrent token change; nil bypasses it (a first auth or a
|
|
201
|
+
# rollback restore). Returns the current token, or nil when the session is stopping.
|
|
202
|
+
def commit_grant(grant:, minted:, previous_token: nil, issued_at: Time.now)
|
|
203
|
+
@state_mutex.synchronize do
|
|
204
|
+
return if @stop
|
|
205
|
+
|
|
206
|
+
@grant = grant
|
|
207
|
+
@minted = minted
|
|
208
|
+
@disconnected = false
|
|
209
|
+
@rejected = false
|
|
210
|
+
@backoff = INITIAL_BACKOFF
|
|
211
|
+
@current_token = grant.client_token
|
|
212
|
+
@lease_duration = grant.lease_duration.to_f
|
|
213
|
+
@renewable = grant.renewable?
|
|
214
|
+
@expires_at = Legion::Crypt.publish_vault_grant(cluster_name: @cluster_name, client: @client, grant: grant_hash,
|
|
215
|
+
auth_method: @auth_method, issued_at: issued_at,
|
|
216
|
+
current_token: previous_token)
|
|
217
|
+
@current_token
|
|
218
|
+
end
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
def grant_hash
|
|
222
|
+
g = @grant
|
|
223
|
+
{ token: g.client_token, lease_duration: g.lease_duration, renewable: g.renewable?,
|
|
224
|
+
policies: g.policies, metadata: g.metadata }
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
def cluster_healthy?
|
|
228
|
+
Legion::Crypt::Vault::Operation.with_timeout { @client.sys.health_status.initialized? }
|
|
229
|
+
rescue Legion::Crypt::Vault::HTTPError => e
|
|
230
|
+
return true if STANDBY_CODES.include?(e.code)
|
|
231
|
+
|
|
232
|
+
handle_exception(e, level: :error, operation: 'crypt.vault_session.health', cluster_name: @cluster_name)
|
|
233
|
+
false
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
def start_maintenance
|
|
237
|
+
return if stopped? || @state_mutex.synchronize { @current_token.nil? }
|
|
238
|
+
|
|
239
|
+
if @leases
|
|
240
|
+
@leases.start(definitions: @lease_defs, client: @client)
|
|
241
|
+
@leases.start_renewal_thread
|
|
242
|
+
end
|
|
243
|
+
@state_mutex.synchronize do
|
|
244
|
+
return if @stop || @thread&.alive?
|
|
245
|
+
|
|
246
|
+
@thread = Thread.new { maintenance_loop }
|
|
247
|
+
@thread.name = "vault-session-#{label}"
|
|
248
|
+
end
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
def maintenance_loop
|
|
252
|
+
return unless @state_mutex.synchronize { @expires_at }
|
|
253
|
+
|
|
254
|
+
wait(renewal_delay)
|
|
255
|
+
while grant_active
|
|
256
|
+
case maintain_token
|
|
257
|
+
when :renewed
|
|
258
|
+
@state_mutex.synchronize { @backoff = INITIAL_BACKOFF }
|
|
259
|
+
wait(renewal_delay)
|
|
260
|
+
when :pending
|
|
261
|
+
wait(renewal_delay)
|
|
262
|
+
when :failed
|
|
263
|
+
wait(retry_delay)
|
|
264
|
+
else
|
|
265
|
+
break
|
|
266
|
+
end
|
|
267
|
+
end
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
# Renewal is attempted only when Vault's last auth response said the token is renewable.
|
|
271
|
+
# A token that cannot be renewed is replaced by re-authentication (Kerberos) ahead of its
|
|
272
|
+
# expiry, or left to expire and disconnect (LDAP/JWT/static).
|
|
273
|
+
def maintain_token
|
|
274
|
+
expired, renewable = @state_mutex.synchronize do
|
|
275
|
+
return :finished if @expires_at.nil?
|
|
276
|
+
|
|
277
|
+
[@expires_at <= Time.now, @renewable]
|
|
278
|
+
end
|
|
279
|
+
if renewable && !expired
|
|
280
|
+
outcome = renew_token
|
|
281
|
+
return outcome unless outcome == :rejected
|
|
282
|
+
elsif !expired && !reauthable?
|
|
283
|
+
return :pending
|
|
284
|
+
end
|
|
285
|
+
return handoff if reauthable?
|
|
286
|
+
|
|
287
|
+
disconnect_token(reason: 'expired')
|
|
288
|
+
:finished
|
|
289
|
+
end
|
|
290
|
+
|
|
291
|
+
def reauthable?
|
|
292
|
+
@auth&.can_reauth? == true
|
|
293
|
+
end
|
|
294
|
+
|
|
295
|
+
# Renewal uses the client directly so it works for both minted and adopted (static) tokens.
|
|
296
|
+
def renew_token
|
|
297
|
+
deadline, minted, previous_token = @state_mutex.synchronize do
|
|
298
|
+
remaining = @expires_at ? @expires_at - Time.now : 0
|
|
299
|
+
[::Process.clock_gettime(::Process::CLOCK_MONOTONIC) + [remaining, 0].max, @minted, @current_token]
|
|
300
|
+
end
|
|
301
|
+
renewed = Legion::Crypt::Vault::Operation.with_timeout(deadline: deadline) { @client.auth_token.renew_self }&.auth
|
|
302
|
+
raise Legion::Crypt::Vault::Auth::AuthError, 'Vault token renewal returned no auth data' if renewed.nil?
|
|
303
|
+
|
|
304
|
+
commit_grant(grant: renewed, minted: minted, previous_token: previous_token)
|
|
305
|
+
log.info "Vault token renewed cluster=#{label} ttl=#{@state_mutex.synchronize { @lease_duration }}s"
|
|
306
|
+
:renewed
|
|
307
|
+
rescue Legion::Crypt::Vault::GrantRejectedError => e
|
|
308
|
+
handle_exception(e, level: :error, operation: 'crypt.vault_session.renew', cluster_name: @cluster_name)
|
|
309
|
+
:finished
|
|
310
|
+
rescue *FAILURES, Legion::Crypt::Vault::Auth::AuthError => e
|
|
311
|
+
handle_exception(e, level: :error, operation: 'crypt.vault_session.renew', cluster_name: @cluster_name)
|
|
312
|
+
rejected = e.is_a?(Legion::Crypt::Vault::HTTPClientError) && !RETRYABLE_CODES.include?(e.code)
|
|
313
|
+
@state_mutex.synchronize { @rejected = true } if rejected
|
|
314
|
+
rejected ? :rejected : :failed
|
|
315
|
+
end
|
|
316
|
+
|
|
317
|
+
# The atomic re-auth handoff: re-auth, publish the candidate grant, reissue the leases under
|
|
318
|
+
# it, then commit (commit the expiry, revoke the old token) or roll back (revoke the
|
|
319
|
+
# candidate, restore the prior grant and its expiry).
|
|
320
|
+
def handoff
|
|
321
|
+
old_grant, old_expires, old_minted = @state_mutex.synchronize { [@grant, @expires_at, @minted] }
|
|
322
|
+
candidate = @auth.reauth(client: @client)
|
|
323
|
+
committed = grant_active && commit_grant(grant: candidate, minted: true, previous_token: old_grant.client_token)
|
|
324
|
+
recovered = committed && (@leases.nil? || @leases.reissue_all(client: @client, active: method(:grant_active)))
|
|
325
|
+
if recovered && grant_active
|
|
326
|
+
revoke_token(token: old_grant.client_token)
|
|
327
|
+
log.info "Vault token re-authenticated cluster=#{label} ttl=#{candidate.lease_duration}s"
|
|
328
|
+
return :renewed
|
|
329
|
+
end
|
|
330
|
+
revoke_token(token: candidate.client_token)
|
|
331
|
+
return :finished unless committed
|
|
332
|
+
|
|
333
|
+
commit_grant(grant: old_grant, minted: old_minted, issued_at: old_expires - old_grant.lease_duration.to_f)
|
|
334
|
+
log.error "Vault token re-auth incomplete cluster=#{label}; previous token retained"
|
|
335
|
+
:failed
|
|
336
|
+
rescue Legion::Crypt::Vault::GrantRejectedError => e
|
|
337
|
+
handle_exception(e, level: :error, operation: 'crypt.vault_session.handoff', cluster_name: @cluster_name)
|
|
338
|
+
:finished
|
|
339
|
+
rescue *FAILURES, Legion::Crypt::Vault::Auth::AuthError => e
|
|
340
|
+
handle_exception(e, level: :error, operation: 'crypt.vault_session.handoff', cluster_name: @cluster_name)
|
|
341
|
+
:failed
|
|
342
|
+
end
|
|
343
|
+
|
|
344
|
+
def disconnect_token(reason:)
|
|
345
|
+
@state_mutex.synchronize do
|
|
346
|
+
return if @disconnected
|
|
347
|
+
|
|
348
|
+
@disconnected = true
|
|
349
|
+
Legion::Crypt.disconnect_vault_grant(cluster_name: @cluster_name, token: @current_token,
|
|
350
|
+
relogin_required: !reauthable?)
|
|
351
|
+
log.error "Vault token #{reason} cluster=#{label}; #{reauthable? ? 're-authentication pending' : 'login required'}"
|
|
352
|
+
end
|
|
353
|
+
end
|
|
354
|
+
|
|
355
|
+
# Returns whether the token is gone from Vault; a failed revocation is reported, not hidden.
|
|
356
|
+
def revoke_token(token: @state_mutex.synchronize { @current_token })
|
|
357
|
+
return true if token.nil?
|
|
358
|
+
|
|
359
|
+
Legion::Crypt.revoke_vault_token(client: @client, token: token)
|
|
360
|
+
true
|
|
361
|
+
rescue *FAILURES => e
|
|
362
|
+
handle_exception(e, level: :error, operation: 'crypt.vault_session.revoke', cluster_name: @cluster_name,
|
|
363
|
+
outcome: 'incomplete')
|
|
364
|
+
false
|
|
365
|
+
end
|
|
366
|
+
|
|
367
|
+
def renewal_delay
|
|
368
|
+
@state_mutex.synchronize do
|
|
369
|
+
return RENEWAL_CHECK_INTERVAL if @expires_at.nil?
|
|
370
|
+
|
|
371
|
+
remaining = @expires_at - Time.now
|
|
372
|
+
delay = @renewable || reauthable? ? remaining - (@lease_duration * (1 - RENEWAL_RATIO)) : remaining
|
|
373
|
+
[delay, MINIMUM_WAIT].max
|
|
374
|
+
end
|
|
375
|
+
end
|
|
376
|
+
|
|
377
|
+
def retry_delay
|
|
378
|
+
@state_mutex.synchronize do
|
|
379
|
+
delay = @backoff
|
|
380
|
+
@backoff = [@backoff * 2, MAX_BACKOFF].min
|
|
381
|
+
|
|
382
|
+
remaining = @expires_at ? @expires_at - Time.now : 0
|
|
383
|
+
return delay if remaining <= 0
|
|
384
|
+
|
|
385
|
+
floor = remaining / 2
|
|
386
|
+
floor = MINIMUM_WAIT if floor < MINIMUM_WAIT
|
|
387
|
+
[delay, floor].min
|
|
388
|
+
end
|
|
389
|
+
end
|
|
390
|
+
|
|
391
|
+
def wait(seconds)
|
|
392
|
+
deadline = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC) + seconds
|
|
393
|
+
@state_mutex.synchronize do
|
|
394
|
+
until @stop
|
|
395
|
+
remaining = deadline - ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
|
|
396
|
+
break unless remaining.positive?
|
|
397
|
+
|
|
398
|
+
@wake.wait(@state_mutex, remaining)
|
|
399
|
+
end
|
|
400
|
+
end
|
|
401
|
+
end
|
|
402
|
+
end
|
|
403
|
+
end
|
|
404
|
+
end
|
|
405
|
+
end
|
data/lib/legion/crypt/vault.rb
CHANGED
|
@@ -3,6 +3,9 @@
|
|
|
3
3
|
require 'legion/logging/helper'
|
|
4
4
|
require 'legion/crypt/vault/operation'
|
|
5
5
|
require 'legion/crypt/vault/client'
|
|
6
|
+
require 'legion/crypt/vault/auth'
|
|
7
|
+
require 'legion/crypt/vault/leases'
|
|
8
|
+
require 'legion/crypt/vault/session'
|
|
6
9
|
|
|
7
10
|
module Legion
|
|
8
11
|
module Crypt
|
|
@@ -113,7 +116,8 @@ module Legion
|
|
|
113
116
|
end
|
|
114
117
|
end
|
|
115
118
|
|
|
116
|
-
|
|
119
|
+
# Revokes every tracked session until the deadline or a token rejection; returns whether all were revoked.
|
|
120
|
+
def close_sessions(deadline: nil, denied_clients: {}.compare_by_identity, **)
|
|
117
121
|
deadline ||= ::Process.clock_gettime(::Process::CLOCK_MONOTONIC) + Legion::Crypt::Vault::Operation::SHUTDOWN_TIMEOUT
|
|
118
122
|
pending = session_mutex.synchronize do
|
|
119
123
|
@sessions_stopped = true
|
|
@@ -122,17 +126,24 @@ module Legion
|
|
|
122
126
|
@sessions = []
|
|
123
127
|
detached
|
|
124
128
|
end
|
|
129
|
+
return true if pending.empty?
|
|
130
|
+
|
|
125
131
|
log.info 'Closing all Legion::Crypt vault sessions'
|
|
126
|
-
|
|
132
|
+
client = vault_client
|
|
133
|
+
closed = 0
|
|
127
134
|
pending.each do |session|
|
|
128
|
-
break if ::Process.clock_gettime(::Process::CLOCK_MONOTONIC) >= deadline
|
|
135
|
+
break if ::Process.clock_gettime(::Process::CLOCK_MONOTONIC) >= deadline || denied_clients.key?(client)
|
|
129
136
|
|
|
130
137
|
Legion::Crypt::Vault::Operation.with_timeout(deadline: deadline) { close_session(session: session) }
|
|
131
|
-
closed
|
|
138
|
+
closed += 1
|
|
132
139
|
rescue StandardError => e
|
|
140
|
+
denied_clients[client] = true if e.is_a?(Legion::Crypt::Vault::HTTPClientError) && e.code == 403
|
|
133
141
|
handle_exception(e, level: :error, operation: 'crypt.vault.close_session')
|
|
134
142
|
end
|
|
135
|
-
closed
|
|
143
|
+
return true if closed == pending.size
|
|
144
|
+
|
|
145
|
+
log.error "Vault session cleanup incomplete: #{pending.size - closed} of #{pending.size} session(s) not revoked"
|
|
146
|
+
false
|
|
136
147
|
end
|
|
137
148
|
|
|
138
149
|
def shutdown_renewer
|
|
@@ -164,8 +175,6 @@ module Legion
|
|
|
164
175
|
[@session_generation, (@sessions || []).dup]
|
|
165
176
|
end
|
|
166
177
|
log.debug 'Vault renewal cycle start'
|
|
167
|
-
return renew_cluster_tokens(generation: generation) if respond_to?(:connected_clusters) && connected_clusters.any?
|
|
168
|
-
|
|
169
178
|
renewed = []
|
|
170
179
|
pending.each do |session|
|
|
171
180
|
break unless session_maintenance_active?(generation: generation)
|
|
@@ -182,20 +191,6 @@ module Legion
|
|
|
182
191
|
raise
|
|
183
192
|
end
|
|
184
193
|
|
|
185
|
-
def renew_cluster_tokens(generation: session_mutex.synchronize { @session_generation })
|
|
186
|
-
renewed = []
|
|
187
|
-
connected_clusters.each_key do |name|
|
|
188
|
-
break unless session_maintenance_active?(generation: generation)
|
|
189
|
-
|
|
190
|
-
Legion::Crypt::Vault::Operation.with_timeout { vault_client(name).auth_token.renew_self }
|
|
191
|
-
renewed << name
|
|
192
|
-
log.info "Vault token renewed for cluster #{name}"
|
|
193
|
-
rescue StandardError => e
|
|
194
|
-
log_vault_error(name, e)
|
|
195
|
-
end
|
|
196
|
-
renewed
|
|
197
|
-
end
|
|
198
|
-
|
|
199
194
|
def vault_exists?(name)
|
|
200
195
|
vault_client.sys.mounts.key?(name.to_sym)
|
|
201
196
|
end
|