legion-crypt 1.5.37 → 1.5.42
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/.github/workflows/ci.yml +30 -3
- data/.rspec +2 -0
- data/AGENTS.md +2 -0
- data/CHANGELOG.md +34 -0
- data/CLAUDE.md +64 -192
- data/CODEOWNERS +4 -4
- data/Gemfile +4 -1
- data/README.md +66 -16
- data/legion-crypt.gemspec +1 -0
- data/lib/legion/crypt/cert_rotation.rb +7 -21
- data/lib/legion/crypt/cluster_secret.rb +3 -14
- data/lib/legion/crypt/ed25519.rb +2 -10
- data/lib/legion/crypt/erasure.rb +3 -13
- data/lib/legion/crypt/jwks_client.rb +4 -4
- data/lib/legion/crypt/kerberos_auth.rb +2 -2
- data/lib/legion/crypt/ldap_auth.rb +2 -2
- data/lib/legion/crypt/lease_manager.rb +191 -26
- data/lib/legion/crypt/mtls.rb +8 -46
- data/lib/legion/crypt/partition_keys.rb +2 -6
- data/lib/legion/crypt/settings.rb +24 -21
- data/lib/legion/crypt/spiffe/svid_rotation.rb +6 -9
- data/lib/legion/crypt/spiffe.rb +14 -39
- data/lib/legion/crypt/token_renewer.rb +6 -6
- data/lib/legion/crypt/vault/client.rb +355 -0
- data/lib/legion/crypt/vault/cluster.rb +330 -0
- data/lib/legion/crypt/vault/entity.rb +80 -0
- data/lib/legion/crypt/vault/jwt_auth.rb +99 -0
- data/lib/legion/crypt/vault/kerberos_auth.rb +53 -0
- data/lib/legion/crypt/vault/operation.rb +29 -0
- data/lib/legion/crypt/vault.rb +6 -11
- data/lib/legion/crypt/version.rb +1 -1
- data/lib/legion/crypt.rb +69 -80
- metadata +23 -8
- data/lib/legion/crypt/vault_client.rb +0 -355
- data/lib/legion/crypt/vault_cluster.rb +0 -331
- data/lib/legion/crypt/vault_entity.rb +0 -78
- data/lib/legion/crypt/vault_jwt_auth.rb +0 -97
- data/lib/legion/crypt/vault_kerberos_auth.rb +0 -51
- data/lib/legion/crypt/vault_operation.rb +0 -27
- /data/lib/legion/crypt/{vault_renewer.rb → vault/renewer.rb} +0 -0
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'legion/logging/helper'
|
|
4
|
+
require 'uri'
|
|
5
|
+
require 'legion/crypt/vault/client'
|
|
6
|
+
require 'legion/crypt/tls'
|
|
7
|
+
require 'legion/crypt/vault/operation'
|
|
8
|
+
|
|
9
|
+
module Legion
|
|
10
|
+
module Crypt
|
|
11
|
+
module Vault
|
|
12
|
+
module Cluster
|
|
13
|
+
include Legion::Logging::Helper
|
|
14
|
+
|
|
15
|
+
def vault_client(name = nil)
|
|
16
|
+
name = resolve_cluster_name(name)
|
|
17
|
+
loop do
|
|
18
|
+
candidate = accepted = nil
|
|
19
|
+
signature, generation = vault_clients_mutex.synchronize do
|
|
20
|
+
return @vault_clients&.dig(name, :client) if Legion::Crypt.respond_to?(:stopping) && Legion::Crypt.stopping
|
|
21
|
+
|
|
22
|
+
@vault_clients ||= {}
|
|
23
|
+
entry = @vault_clients[name]
|
|
24
|
+
signature = vault_client_signature(config: name.nil? ? Legion::Settings[:crypt][:vault] : clusters[name])
|
|
25
|
+
cached = cached_vault_client(entry: entry, signature: signature)
|
|
26
|
+
return cached if cached
|
|
27
|
+
|
|
28
|
+
unless signature
|
|
29
|
+
(@retired_vault_clients ||= ObjectSpace::WeakMap.new)[entry[:client]] = true if entry
|
|
30
|
+
@vault_clients.delete(name)
|
|
31
|
+
return nil
|
|
32
|
+
end
|
|
33
|
+
signature.transform_values! { |value| value.is_a?(String) ? value.dup.freeze : value }
|
|
34
|
+
[signature, @vault_client_generation || 0]
|
|
35
|
+
end
|
|
36
|
+
candidate = build_vault_client(signature, name: name)
|
|
37
|
+
accepted = install_vault_client(name: name, signature: signature, client: candidate, generation: generation)
|
|
38
|
+
return accepted if accepted
|
|
39
|
+
ensure
|
|
40
|
+
retire_vault_client(client: candidate) unless accepted.equal?(candidate)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def publish_vault_grant(client:, config:, grant:, auth_method:, issued_at: Time.now, **)
|
|
45
|
+
token = grant.fetch(:token)
|
|
46
|
+
ttl = grant[:lease_duration].to_f
|
|
47
|
+
expires_at = issued_at + ttl if ttl.positive?
|
|
48
|
+
vault_clients_mutex.synchronize do
|
|
49
|
+
pair = @vault_clients&.find { |_name, entry| entry[:client].equal?(client) }
|
|
50
|
+
raise IOError, 'Vault client replaced during authentication' if @retired_vault_clients&.key?(client)
|
|
51
|
+
|
|
52
|
+
if pair
|
|
53
|
+
name, entry = pair
|
|
54
|
+
current_config = name.nil? ? Legion::Settings[:crypt][:vault] : clusters[name]
|
|
55
|
+
unless config.equal?(current_config) && current_vault_client?(entry: entry, signature: vault_client_signature(config: current_config))
|
|
56
|
+
raise IOError, 'Vault client configuration changed during authentication'
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
client.token = token
|
|
60
|
+
connected = expires_at.nil? || expires_at > Time.now
|
|
61
|
+
config.merge!(token: token, lease_duration: grant[:lease_duration], renewable: grant[:renewable],
|
|
62
|
+
expires_at: expires_at, connected: connected, relogin_required: !connected, auth_method: auth_method)
|
|
63
|
+
entry[:signature][:token] = token&.dup&.freeze if entry
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def clear_vault_clients(deadline: nil, **)
|
|
68
|
+
deadline ||= ::Process.clock_gettime(::Process::CLOCK_MONOTONIC) + Legion::Crypt::Vault::Operation::SHUTDOWN_TIMEOUT
|
|
69
|
+
clients = vault_clients_mutex.synchronize do
|
|
70
|
+
@vault_client_generation = (@vault_client_generation || 0) + 1
|
|
71
|
+
previous = (@vault_clients || {}).values.map { |entry| entry[:client] }
|
|
72
|
+
previous.concat(@retired_vault_clients.keys) if @retired_vault_clients
|
|
73
|
+
@vault_clients = {}
|
|
74
|
+
@retired_vault_clients = nil
|
|
75
|
+
previous.uniq
|
|
76
|
+
end
|
|
77
|
+
clients.each { |client| retire_vault_client(client: client, deadline: deadline) }
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def cluster(name = nil)
|
|
81
|
+
name = resolve_cluster_name(name)
|
|
82
|
+
clusters[name]
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def default_cluster_name
|
|
86
|
+
name = Legion::Settings[:crypt][:vault][:default]
|
|
87
|
+
name ? name.to_sym : clusters.keys.first
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def clusters
|
|
91
|
+
Legion::Settings[:crypt][:vault][:clusters]
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def connected_clusters
|
|
95
|
+
vault_clients_mutex.synchronize do
|
|
96
|
+
now = Time.now
|
|
97
|
+
clusters.select do |_, config|
|
|
98
|
+
config[:token] && config[:connected] && !config[:relogin_required] &&
|
|
99
|
+
(!config[:expires_at] || config[:expires_at] > now)
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def connect_all_clusters
|
|
105
|
+
log.info "Vault cluster connect requested configured_clusters=#{clusters.size}"
|
|
106
|
+
log_vault_debug("connect_all_clusters: #{clusters.size} cluster(s) configured")
|
|
107
|
+
results = {}
|
|
108
|
+
clusters.each do |name, config|
|
|
109
|
+
log_vault_debug("connect_all_clusters: #{name} (auth_method=#{config[:auth_method].inspect})")
|
|
110
|
+
case config[:auth_method]&.to_s
|
|
111
|
+
when 'kerberos'
|
|
112
|
+
results[name] = connect_kerberos_cluster(name, config)
|
|
113
|
+
when 'ldap'
|
|
114
|
+
next # handled by ldap_login_all
|
|
115
|
+
else
|
|
116
|
+
next unless config[:token]
|
|
117
|
+
|
|
118
|
+
client = vault_client(name)
|
|
119
|
+
config[:connected] = cluster_healthy?(client)
|
|
120
|
+
results[name] = config[:connected]
|
|
121
|
+
log_cluster_connected(name, config) if config[:connected]
|
|
122
|
+
end
|
|
123
|
+
rescue StandardError => e
|
|
124
|
+
config[:connected] = false
|
|
125
|
+
results[name] = false
|
|
126
|
+
log_vault_error(name, e, operation: 'crypt.vault_cluster.connect_all_clusters')
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
connected = results.select { |_, v| v }
|
|
130
|
+
log.info "Vault cluster connect complete connected=#{connected.size} attempted=#{results.size}"
|
|
131
|
+
log_vault_debug("connect_all_clusters: #{connected.size}/#{results.size} connected")
|
|
132
|
+
sync_vault_connected(connected.any?)
|
|
133
|
+
results
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
private
|
|
137
|
+
|
|
138
|
+
def cluster_healthy?(client)
|
|
139
|
+
client.sys.health_status.initialized?
|
|
140
|
+
rescue Legion::Crypt::Vault::HTTPError => e
|
|
141
|
+
return true if [429, 472, 473].include?(e.code)
|
|
142
|
+
|
|
143
|
+
handle_exception(e, level: :warn, operation: 'crypt.vault_cluster.cluster_healthy')
|
|
144
|
+
raise
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def sync_vault_connected(connected)
|
|
148
|
+
Legion::Settings[:crypt][:vault][:connected] = connected
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
def selected_connected_cluster_name(name = nil)
|
|
152
|
+
active_clusters = connected_clusters
|
|
153
|
+
return nil if active_clusters.empty?
|
|
154
|
+
|
|
155
|
+
if name
|
|
156
|
+
cluster_name = name.to_sym
|
|
157
|
+
raise ArgumentError, "Vault cluster not connected: #{cluster_name}" unless active_clusters.key?(cluster_name)
|
|
158
|
+
|
|
159
|
+
return cluster_name
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
default_name = Legion::Settings[:crypt][:vault][:default]&.to_sym
|
|
163
|
+
return default_name if default_name && active_clusters.key?(default_name)
|
|
164
|
+
|
|
165
|
+
active_clusters.keys.first
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def resolve_cluster_name(name)
|
|
169
|
+
return name.to_sym if name
|
|
170
|
+
|
|
171
|
+
default_cluster_name
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
def vault_clients_mutex
|
|
175
|
+
@vault_clients_mutex ||= Mutex.new
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def current_vault_client?(entry:, signature:, **)
|
|
179
|
+
return false unless entry && signature
|
|
180
|
+
return false unless entry[:signature].all? { |key, value| key == :token || value == signature[key] }
|
|
181
|
+
|
|
182
|
+
entry[:signature][:token] == signature[:token] || entry[:client].token == signature[:token]
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
def cached_vault_client(entry:, signature:, **)
|
|
186
|
+
return unless current_vault_client?(entry: entry, signature: signature)
|
|
187
|
+
|
|
188
|
+
entry[:signature][:token] = signature[:token]&.dup if entry[:signature][:token] != signature[:token]
|
|
189
|
+
entry[:client]
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
def install_vault_client(name:, signature:, client:, generation:, **)
|
|
193
|
+
vault_clients_mutex.synchronize do
|
|
194
|
+
raise IOError, 'Vault clients cleared during construction' unless generation == (@vault_client_generation || 0)
|
|
195
|
+
raise IOError, 'Crypt stopped during client construction' if Legion::Crypt.respond_to?(:stopping) && Legion::Crypt.stopping
|
|
196
|
+
|
|
197
|
+
entry = @vault_clients[name]
|
|
198
|
+
current_signature = vault_client_signature(config: name.nil? ? Legion::Settings[:crypt][:vault] : clusters[name])
|
|
199
|
+
cached = cached_vault_client(entry: entry, signature: current_signature)
|
|
200
|
+
return cached if cached
|
|
201
|
+
return unless signature == current_signature
|
|
202
|
+
|
|
203
|
+
(@retired_vault_clients ||= ObjectSpace::WeakMap.new)[entry[:client]] = true if entry
|
|
204
|
+
@vault_clients[name] = { client: client, signature: signature }
|
|
205
|
+
client
|
|
206
|
+
end
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
def resolve_vault_address(vault_settings:, **)
|
|
210
|
+
protocol = vault_settings[:protocol] || 'http'
|
|
211
|
+
address = vault_settings[:address] || 'localhost'
|
|
212
|
+
port = vault_settings[:port] || 8200
|
|
213
|
+
|
|
214
|
+
if address.match?(%r{\Ahttps?://})
|
|
215
|
+
uri = URI.parse(address)
|
|
216
|
+
protocol = uri.scheme
|
|
217
|
+
address = uri.host
|
|
218
|
+
port = uri.port if vault_settings[:port].nil?
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
"#{protocol}://#{address}:#{port}"
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
def vault_client_signature(config:, **)
|
|
225
|
+
return unless config.is_a?(Hash)
|
|
226
|
+
|
|
227
|
+
tls = Legion::Crypt::TLS.migrate_legacy(config[:tls] || {})
|
|
228
|
+
verify = %w[none mutual].include?(tls[:verify].to_s) ? tls[:verify].to_s : 'peer'
|
|
229
|
+
|
|
230
|
+
namespace =
|
|
231
|
+
if config.key?(:namespace)
|
|
232
|
+
config[:namespace]
|
|
233
|
+
else
|
|
234
|
+
Legion::Settings[:crypt][:vault][:vault_namespace]
|
|
235
|
+
end
|
|
236
|
+
{
|
|
237
|
+
address: resolve_vault_address(vault_settings: config), namespace: namespace, token: config[:token],
|
|
238
|
+
verify: verify, ca: tls[:ca], cert: tls[:cert], key: tls[:key],
|
|
239
|
+
timeout: Legion::Settings[:crypt][:vault][:operation_timeout]
|
|
240
|
+
}
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
def build_vault_client(signature, name:)
|
|
244
|
+
building = Thread.current[:legion_vault_clients_building] ||= []
|
|
245
|
+
build_key = [self, name]
|
|
246
|
+
raise IOError, 'Circular Vault TLS material dependency' if building.include?(build_key)
|
|
247
|
+
|
|
248
|
+
building << build_key
|
|
249
|
+
begin
|
|
250
|
+
tls = Legion::Crypt::TLS.resolve(signature.slice(:verify, :ca, :cert, :key))
|
|
251
|
+
%i[ca cert key].each do |attribute|
|
|
252
|
+
raise IOError, "Configured Vault TLS #{attribute} could not be resolved" if signature[attribute] && tls[attribute].nil?
|
|
253
|
+
end
|
|
254
|
+
options = signature.slice(:address, :namespace, :token, :timeout).merge(tls: tls)
|
|
255
|
+
log.info "Building Vault client address=#{options[:address]} namespace=#{options[:namespace].inspect} tls_verify=#{tls[:verify]}"
|
|
256
|
+
Legion::Crypt::Vault::Client.new(**options)
|
|
257
|
+
ensure
|
|
258
|
+
building.delete(build_key)
|
|
259
|
+
end
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
def retire_vault_client(client:, deadline: nil, **)
|
|
263
|
+
return unless client.respond_to?(:shutdown)
|
|
264
|
+
|
|
265
|
+
Legion::Crypt::Vault::Operation.with_timeout(deadline: deadline) do
|
|
266
|
+
client.shutdown
|
|
267
|
+
end
|
|
268
|
+
rescue StandardError => e
|
|
269
|
+
handle_exception(e, level: :warn, operation: 'crypt.vault_cluster.retire_vault_client')
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
def log_vault_error(name, error, operation: 'crypt.vault_cluster.error')
|
|
273
|
+
handle_exception(error, level: :error, operation: operation, cluster_name: name)
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
def connect_kerberos_cluster(name, config)
|
|
277
|
+
krb_config = config[:kerberos] || {}
|
|
278
|
+
spn = krb_config[:service_principal]
|
|
279
|
+
auth_path = krb_config[:auth_path] || Legion::Crypt::KerberosAuth::DEFAULT_AUTH_PATH
|
|
280
|
+
|
|
281
|
+
log_vault_debug("connect_kerberos_cluster[#{name}]: SPN=#{spn}, auth_path=#{auth_path}, namespace=#{config[:namespace].inspect}")
|
|
282
|
+
|
|
283
|
+
unless spn
|
|
284
|
+
log_vault_warn(name, 'Kerberos auth missing service_principal, skipping')
|
|
285
|
+
config[:connected] = false
|
|
286
|
+
return false
|
|
287
|
+
end
|
|
288
|
+
|
|
289
|
+
require 'legion/crypt/kerberos_auth'
|
|
290
|
+
client = vault_client(name)
|
|
291
|
+
log_vault_debug("connect_kerberos_cluster[#{name}]: client.namespace=#{client.respond_to?(:namespace) ? client.namespace.inspect : 'n/a'}")
|
|
292
|
+
log.info "Connecting Vault cluster #{name} via Kerberos auth_path=#{auth_path}"
|
|
293
|
+
|
|
294
|
+
result = Legion::Crypt::KerberosAuth.login(
|
|
295
|
+
vault_client: client,
|
|
296
|
+
service_principal: spn,
|
|
297
|
+
auth_path: auth_path
|
|
298
|
+
)
|
|
299
|
+
|
|
300
|
+
publish_vault_grant(client: client, config: config, grant: result, auth_method: 'kerberos')
|
|
301
|
+
log_vault_debug("connect_kerberos_cluster[#{name}]: policies=#{result[:policies].inspect}")
|
|
302
|
+
log_cluster_connected(name, config)
|
|
303
|
+
true
|
|
304
|
+
rescue Legion::Crypt::KerberosAuth::GemMissingError => e
|
|
305
|
+
handle_exception(e, level: :warn, operation: 'crypt.vault_cluster.connect_kerberos_cluster', cluster_name: name)
|
|
306
|
+
log_vault_warn(name, e.message)
|
|
307
|
+
config[:connected] = false
|
|
308
|
+
false
|
|
309
|
+
rescue Legion::Crypt::KerberosAuth::AuthError => e
|
|
310
|
+
handle_exception(e, level: :warn, operation: 'crypt.vault_cluster.connect_kerberos_cluster', cluster_name: name)
|
|
311
|
+
log_vault_warn(name, "Kerberos auth failed: #{e.message}")
|
|
312
|
+
config[:connected] = false
|
|
313
|
+
false
|
|
314
|
+
end
|
|
315
|
+
|
|
316
|
+
def log_cluster_connected(name, config)
|
|
317
|
+
log.info "Vault cluster connected: #{name} at #{config[:address]}"
|
|
318
|
+
end
|
|
319
|
+
|
|
320
|
+
def log_vault_warn(name, message)
|
|
321
|
+
log.warn("Vault cluster #{name}: #{message}")
|
|
322
|
+
end
|
|
323
|
+
|
|
324
|
+
def log_vault_debug(message)
|
|
325
|
+
log.debug(message)
|
|
326
|
+
end
|
|
327
|
+
end
|
|
328
|
+
end
|
|
329
|
+
end
|
|
330
|
+
end
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'legion/logging/helper'
|
|
4
|
+
require 'legion/crypt/vault/client'
|
|
5
|
+
require 'legion/crypt/lease_manager'
|
|
6
|
+
|
|
7
|
+
module Legion
|
|
8
|
+
module Crypt
|
|
9
|
+
module Vault
|
|
10
|
+
module Entity
|
|
11
|
+
extend Legion::Logging::Helper
|
|
12
|
+
|
|
13
|
+
# Create or lookup a Vault entity for a Legion principal.
|
|
14
|
+
# Returns the Vault entity ID string, or nil on failure.
|
|
15
|
+
def self.ensure_entity(principal_id:, canonical_name:, metadata: {})
|
|
16
|
+
existing = find_by_name(canonical_name: canonical_name)
|
|
17
|
+
return existing if existing
|
|
18
|
+
|
|
19
|
+
response = Legion::Crypt::LeaseManager.instance.vault_logical.write(
|
|
20
|
+
path: 'identity/entity',
|
|
21
|
+
data: {
|
|
22
|
+
name: "legion-#{canonical_name}",
|
|
23
|
+
metadata: metadata.merge(
|
|
24
|
+
legion_principal_id: principal_id,
|
|
25
|
+
legion_canonical_name: canonical_name,
|
|
26
|
+
managed_by: 'legion'
|
|
27
|
+
)
|
|
28
|
+
}
|
|
29
|
+
)
|
|
30
|
+
extract_id(response)
|
|
31
|
+
rescue StandardError => e
|
|
32
|
+
handle_exception(e, level: :warn, operation: 'crypt.vault_entity.ensure_entity', canonical_name: canonical_name)
|
|
33
|
+
nil
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Create an alias linking an auth method mount to the entity.
|
|
37
|
+
# Idempotent — swallows "already exists" 4xx errors.
|
|
38
|
+
def self.ensure_alias(entity_id:, mount_accessor:, alias_name:)
|
|
39
|
+
Legion::Crypt::LeaseManager.instance.vault_logical.write(
|
|
40
|
+
path: 'identity/entity-alias',
|
|
41
|
+
data: { name: alias_name, canonical_id: entity_id, mount_accessor: mount_accessor }
|
|
42
|
+
)
|
|
43
|
+
rescue Legion::Crypt::Vault::HTTPClientError => e
|
|
44
|
+
if e.message.include?('already exists')
|
|
45
|
+
log.debug 'Vault entity alias already exists (idempotent)'
|
|
46
|
+
else
|
|
47
|
+
handle_exception(e, level: :warn, operation: 'crypt.vault_entity.ensure_alias', alias_name: alias_name)
|
|
48
|
+
end
|
|
49
|
+
nil
|
|
50
|
+
rescue StandardError => e
|
|
51
|
+
handle_exception(e, level: :warn, operation: 'crypt.vault_entity.ensure_alias', alias_name: alias_name)
|
|
52
|
+
nil
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# Look up a Vault entity by its Legion canonical name.
|
|
56
|
+
# Returns the Vault entity ID string, or nil if not found.
|
|
57
|
+
def self.find_by_name(canonical_name:)
|
|
58
|
+
response = Legion::Crypt::LeaseManager.instance.vault_logical.read(path: "identity/entity/name/legion-#{canonical_name}")
|
|
59
|
+
extract_id(response)
|
|
60
|
+
rescue StandardError => e
|
|
61
|
+
handle_exception(e, level: :warn, operation: 'crypt.vault_entity.find_by_name', canonical_name: canonical_name)
|
|
62
|
+
nil
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# ---------------------------------------------------------------------------
|
|
66
|
+
# Private helpers
|
|
67
|
+
# ---------------------------------------------------------------------------
|
|
68
|
+
|
|
69
|
+
# Extract the entity ID from the client's symbol-keyed response data.
|
|
70
|
+
def self.extract_id(response)
|
|
71
|
+
data = response&.data
|
|
72
|
+
return nil unless data
|
|
73
|
+
|
|
74
|
+
data[:id]
|
|
75
|
+
end
|
|
76
|
+
private_class_method :extract_id
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'legion/logging/helper'
|
|
4
|
+
require 'legion/crypt/vault/client'
|
|
5
|
+
|
|
6
|
+
module Legion
|
|
7
|
+
module Crypt
|
|
8
|
+
# Vault JWT auth backend integration.
|
|
9
|
+
#
|
|
10
|
+
# Allows Legion workers to authenticate to Vault using JWT tokens
|
|
11
|
+
# via Vault's JWT/OIDC auth method. The worker presents a signed JWT
|
|
12
|
+
# and receives a Vault token with policies scoped to the worker's role.
|
|
13
|
+
#
|
|
14
|
+
# Vault config prerequisites:
|
|
15
|
+
# vault auth enable jwt
|
|
16
|
+
# vault write auth/jwt/config jwks_url="..." (or bound_issuer + jwt_validation_pubkeys)
|
|
17
|
+
# vault write auth/jwt/role/legion-worker bound_audiences="legion" ...
|
|
18
|
+
module Vault
|
|
19
|
+
module JwtAuth
|
|
20
|
+
DEFAULT_AUTH_PATH = 'auth/jwt/login'
|
|
21
|
+
DEFAULT_ROLE = 'legion-worker'
|
|
22
|
+
|
|
23
|
+
class AuthError < StandardError; end
|
|
24
|
+
|
|
25
|
+
extend Legion::Logging::Helper
|
|
26
|
+
|
|
27
|
+
# Authenticate to Vault using a JWT token.
|
|
28
|
+
# Returns a Vault token string on success.
|
|
29
|
+
#
|
|
30
|
+
# @param jwt [String] Signed JWT token (issued by Legion or Entra ID)
|
|
31
|
+
# @param role [String] Vault JWT auth role name (default: 'legion-worker')
|
|
32
|
+
# @param auth_path [String] Vault auth mount path (default: 'auth/jwt/login')
|
|
33
|
+
# @return [Hash] { token:, lease_duration:, policies:, metadata: }
|
|
34
|
+
def self.login(jwt:, role: DEFAULT_ROLE, auth_path: DEFAULT_AUTH_PATH)
|
|
35
|
+
raise AuthError, 'Vault is not connected' unless Legion::Crypt.vault_connected?
|
|
36
|
+
|
|
37
|
+
log.info "[crypt:vault_jwt] authenticating role=#{role} auth_path=#{auth_path}"
|
|
38
|
+
response = Legion::Crypt.vault_client.logical.write(path: auth_path, data: { role: role, jwt: jwt })
|
|
39
|
+
|
|
40
|
+
raise AuthError, 'Vault JWT auth returned no auth data' unless response&.auth
|
|
41
|
+
|
|
42
|
+
{
|
|
43
|
+
token: response.auth.client_token,
|
|
44
|
+
lease_duration: response.auth.lease_duration,
|
|
45
|
+
renewable: response.auth.renewable?,
|
|
46
|
+
policies: response.auth.policies,
|
|
47
|
+
metadata: response.auth.metadata
|
|
48
|
+
}
|
|
49
|
+
rescue Legion::Crypt::Vault::HTTPClientError => e
|
|
50
|
+
handle_exception(e, level: :warn, operation: 'crypt.vault_jwt_auth.login', role: role, auth_path: auth_path,
|
|
51
|
+
category: 'client_error')
|
|
52
|
+
raise AuthError, "Vault JWT auth failed: #{e.message}"
|
|
53
|
+
rescue Legion::Crypt::Vault::HTTPServerError => e
|
|
54
|
+
handle_exception(e, level: :warn, operation: 'crypt.vault_jwt_auth.login', role: role, auth_path: auth_path,
|
|
55
|
+
category: 'server_error')
|
|
56
|
+
raise AuthError, "Vault server error during JWT auth: #{e.message}"
|
|
57
|
+
rescue StandardError => e
|
|
58
|
+
handle_exception(e, level: :error, operation: 'crypt.vault_jwt_auth.login', role: role, auth_path: auth_path)
|
|
59
|
+
raise if e.is_a?(AuthError)
|
|
60
|
+
|
|
61
|
+
raise AuthError, "Vault JWT auth failed: #{e.message}"
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# Authenticate and set the Vault client token for subsequent operations.
|
|
65
|
+
# This replaces the current Vault token with the JWT-authenticated one.
|
|
66
|
+
#
|
|
67
|
+
# @return [Hash] Same as login
|
|
68
|
+
def self.login!(jwt:, role: DEFAULT_ROLE, auth_path: DEFAULT_AUTH_PATH)
|
|
69
|
+
generation = Legion::Crypt.vault_auth_generation
|
|
70
|
+
client = Legion::Crypt.vault_client
|
|
71
|
+
config = Legion::Crypt.cluster || Legion::Settings[:crypt][:vault]
|
|
72
|
+
result = login(jwt: jwt, role: role, auth_path: auth_path)
|
|
73
|
+
Legion::Crypt.install_vault_grant(client: client, config: config, grant: result, auth_method: 'jwt', generation: generation)
|
|
74
|
+
log.info "[crypt:vault_jwt] authenticated via JWT auth, policies=#{result[:policies].join(',')}"
|
|
75
|
+
result
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# Issue a Legion JWT and use it to authenticate to Vault in one step.
|
|
79
|
+
# Convenience method for workers that need Vault access.
|
|
80
|
+
#
|
|
81
|
+
# @param worker_id [String] Digital worker ID
|
|
82
|
+
# @param owner_msid [String] Worker's owner MSID
|
|
83
|
+
# @param role [String] Vault JWT auth role name
|
|
84
|
+
# @return [Hash] Same as login
|
|
85
|
+
def self.worker_login(worker_id:, owner_msid:, role: DEFAULT_ROLE)
|
|
86
|
+
log.info "[crypt:vault_jwt] worker login requested role=#{role} worker_id=#{worker_id}"
|
|
87
|
+
jwt = Legion::Crypt::JWT.issue(
|
|
88
|
+
{ worker_id: worker_id, sub: owner_msid, scope: 'vault', aud: 'legion' },
|
|
89
|
+
signing_key: Legion::Crypt.cluster_secret,
|
|
90
|
+
ttl: 300,
|
|
91
|
+
issuer: 'legion'
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
login(jwt: jwt, role: role)
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
end
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'legion/logging/helper'
|
|
4
|
+
require 'legion/crypt/vault/client'
|
|
5
|
+
|
|
6
|
+
module Legion
|
|
7
|
+
module Crypt
|
|
8
|
+
module Vault
|
|
9
|
+
module KerberosAuth
|
|
10
|
+
DEFAULT_AUTH_PATH = 'auth/kerberos/login'
|
|
11
|
+
|
|
12
|
+
class AuthError < StandardError; end
|
|
13
|
+
|
|
14
|
+
extend Legion::Logging::Helper
|
|
15
|
+
|
|
16
|
+
def self.login(spnego_token:, auth_path: DEFAULT_AUTH_PATH)
|
|
17
|
+
raise AuthError, 'Vault is not connected' unless Legion::Crypt.vault_connected?
|
|
18
|
+
|
|
19
|
+
log.info "[crypt:vault_kerberos] login requested auth_path=#{auth_path}"
|
|
20
|
+
data = Legion::Crypt.vault_client.put(
|
|
21
|
+
path: auth_path, body: '{}', headers: { 'Authorization' => "Negotiate #{spnego_token}" }
|
|
22
|
+
)
|
|
23
|
+
response = Legion::Crypt::Vault::Client::Secret.decode(data: data)
|
|
24
|
+
raise AuthError, 'Vault Kerberos auth returned no auth data' unless response&.auth
|
|
25
|
+
|
|
26
|
+
{
|
|
27
|
+
token: response.auth.client_token,
|
|
28
|
+
lease_duration: response.auth.lease_duration,
|
|
29
|
+
renewable: response.auth.renewable?,
|
|
30
|
+
policies: response.auth.policies,
|
|
31
|
+
metadata: response.auth.metadata
|
|
32
|
+
}
|
|
33
|
+
rescue Legion::Crypt::Vault::HTTPClientError => e
|
|
34
|
+
handle_exception(e, level: :warn, operation: 'crypt.vault_kerberos_auth.login', auth_path: auth_path)
|
|
35
|
+
raise AuthError, "Vault Kerberos auth failed: #{e.message}"
|
|
36
|
+
rescue StandardError => e
|
|
37
|
+
handle_exception(e, level: :error, operation: 'crypt.vault_kerberos_auth.login', auth_path: auth_path)
|
|
38
|
+
raise
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def self.login!(spnego_token:, auth_path: DEFAULT_AUTH_PATH)
|
|
42
|
+
generation = Legion::Crypt.vault_auth_generation
|
|
43
|
+
client = Legion::Crypt.vault_client
|
|
44
|
+
config = Legion::Crypt.cluster || Legion::Settings[:crypt][:vault]
|
|
45
|
+
result = login(spnego_token: spnego_token, auth_path: auth_path)
|
|
46
|
+
Legion::Crypt.install_vault_grant(client: client, config: config, grant: result, auth_method: 'kerberos', generation: generation)
|
|
47
|
+
log.info "[crypt:vault_kerberos] authenticated via Kerberos auth, policies=#{result[:policies].join(',')}"
|
|
48
|
+
result
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'timeout'
|
|
4
|
+
require 'legion/crypt/settings'
|
|
5
|
+
|
|
6
|
+
module Legion
|
|
7
|
+
module Crypt
|
|
8
|
+
module Vault
|
|
9
|
+
module Operation
|
|
10
|
+
DEFAULT_TIMEOUT = 5
|
|
11
|
+
SHUTDOWN_TIMEOUT = 10
|
|
12
|
+
|
|
13
|
+
def self.with_timeout(deadline: nil, **)
|
|
14
|
+
timeout = Legion::Settings[:crypt][:vault][:operation_timeout]
|
|
15
|
+
raise ArgumentError, 'Vault operation_timeout must be a finite positive number' unless timeout.is_a?(Numeric) && timeout.finite? && timeout.positive?
|
|
16
|
+
|
|
17
|
+
if deadline
|
|
18
|
+
remaining = deadline - ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
|
|
19
|
+
raise Timeout::Error, 'Vault operation deadline expired' unless remaining.positive?
|
|
20
|
+
|
|
21
|
+
timeout = [timeout, remaining].min
|
|
22
|
+
end
|
|
23
|
+
operation_deadline = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC) + timeout
|
|
24
|
+
Timeout.timeout(timeout) { yield(operation_deadline) }
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
data/lib/legion/crypt/vault.rb
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require 'legion/logging/helper'
|
|
4
|
-
require 'legion/crypt/
|
|
5
|
-
require 'legion/crypt/
|
|
4
|
+
require 'legion/crypt/vault/operation'
|
|
5
|
+
require 'legion/crypt/vault/client'
|
|
6
6
|
|
|
7
7
|
module Legion
|
|
8
8
|
module Crypt
|
|
@@ -11,15 +11,10 @@ module Legion
|
|
|
11
11
|
|
|
12
12
|
attr_accessor :sessions
|
|
13
13
|
|
|
14
|
-
def settings
|
|
15
|
-
Legion::Settings[:crypt][:vault]
|
|
16
|
-
end
|
|
17
|
-
|
|
18
14
|
def connect_vault
|
|
19
15
|
return unless start_session_tracking
|
|
20
16
|
|
|
21
17
|
configuration = Legion::Settings[:crypt][:vault]
|
|
22
|
-
configuration[:token] = ENV['VAULT_DEV_ROOT_TOKEN_ID'] if ENV.key? 'VAULT_DEV_ROOT_TOKEN_ID'
|
|
23
18
|
return nil if configuration[:token].nil?
|
|
24
19
|
|
|
25
20
|
client = vault_client
|
|
@@ -119,7 +114,7 @@ module Legion
|
|
|
119
114
|
end
|
|
120
115
|
|
|
121
116
|
def close_sessions(deadline: nil)
|
|
122
|
-
deadline ||= ::Process.clock_gettime(::Process::CLOCK_MONOTONIC) +
|
|
117
|
+
deadline ||= ::Process.clock_gettime(::Process::CLOCK_MONOTONIC) + Legion::Crypt::Vault::Operation::SHUTDOWN_TIMEOUT
|
|
123
118
|
pending = session_mutex.synchronize do
|
|
124
119
|
@sessions_stopped = true
|
|
125
120
|
@session_generation = (@session_generation || 0) + 1
|
|
@@ -132,7 +127,7 @@ module Legion
|
|
|
132
127
|
pending.each do |session|
|
|
133
128
|
break if ::Process.clock_gettime(::Process::CLOCK_MONOTONIC) >= deadline
|
|
134
129
|
|
|
135
|
-
|
|
130
|
+
Legion::Crypt::Vault::Operation.with_timeout(deadline: deadline) { close_session(session: session) }
|
|
136
131
|
closed << session
|
|
137
132
|
rescue StandardError => e
|
|
138
133
|
handle_exception(e, level: :error, operation: 'crypt.vault.close_session')
|
|
@@ -175,7 +170,7 @@ module Legion
|
|
|
175
170
|
pending.each do |session|
|
|
176
171
|
break unless session_maintenance_active?(generation: generation)
|
|
177
172
|
|
|
178
|
-
|
|
173
|
+
Legion::Crypt::Vault::Operation.with_timeout { renew_session(session: session) }
|
|
179
174
|
renewed << session
|
|
180
175
|
rescue StandardError => e
|
|
181
176
|
handle_exception(e, level: :error, operation: 'crypt.vault.renew_session')
|
|
@@ -192,7 +187,7 @@ module Legion
|
|
|
192
187
|
connected_clusters.each_key do |name|
|
|
193
188
|
break unless session_maintenance_active?(generation: generation)
|
|
194
189
|
|
|
195
|
-
|
|
190
|
+
Legion::Crypt::Vault::Operation.with_timeout { vault_client(name).auth_token.renew_self }
|
|
196
191
|
renewed << name
|
|
197
192
|
log.info "Vault token renewed for cluster #{name}"
|
|
198
193
|
rescue StandardError => e
|