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.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ci.yml +30 -3
  3. data/.rspec +2 -0
  4. data/AGENTS.md +2 -0
  5. data/CHANGELOG.md +34 -0
  6. data/CLAUDE.md +64 -192
  7. data/CODEOWNERS +4 -4
  8. data/Gemfile +4 -1
  9. data/README.md +66 -16
  10. data/legion-crypt.gemspec +1 -0
  11. data/lib/legion/crypt/cert_rotation.rb +7 -21
  12. data/lib/legion/crypt/cluster_secret.rb +3 -14
  13. data/lib/legion/crypt/ed25519.rb +2 -10
  14. data/lib/legion/crypt/erasure.rb +3 -13
  15. data/lib/legion/crypt/jwks_client.rb +4 -4
  16. data/lib/legion/crypt/kerberos_auth.rb +2 -2
  17. data/lib/legion/crypt/ldap_auth.rb +2 -2
  18. data/lib/legion/crypt/lease_manager.rb +191 -26
  19. data/lib/legion/crypt/mtls.rb +8 -46
  20. data/lib/legion/crypt/partition_keys.rb +2 -6
  21. data/lib/legion/crypt/settings.rb +24 -21
  22. data/lib/legion/crypt/spiffe/svid_rotation.rb +6 -9
  23. data/lib/legion/crypt/spiffe.rb +14 -39
  24. data/lib/legion/crypt/token_renewer.rb +6 -6
  25. data/lib/legion/crypt/vault/client.rb +355 -0
  26. data/lib/legion/crypt/vault/cluster.rb +330 -0
  27. data/lib/legion/crypt/vault/entity.rb +80 -0
  28. data/lib/legion/crypt/vault/jwt_auth.rb +99 -0
  29. data/lib/legion/crypt/vault/kerberos_auth.rb +53 -0
  30. data/lib/legion/crypt/vault/operation.rb +29 -0
  31. data/lib/legion/crypt/vault.rb +6 -11
  32. data/lib/legion/crypt/version.rb +1 -1
  33. data/lib/legion/crypt.rb +69 -80
  34. metadata +23 -8
  35. data/lib/legion/crypt/vault_client.rb +0 -355
  36. data/lib/legion/crypt/vault_cluster.rb +0 -331
  37. data/lib/legion/crypt/vault_entity.rb +0 -78
  38. data/lib/legion/crypt/vault_jwt_auth.rb +0 -97
  39. data/lib/legion/crypt/vault_kerberos_auth.rb +0 -51
  40. data/lib/legion/crypt/vault_operation.rb +0 -27
  41. /data/lib/legion/crypt/{vault_renewer.rb → vault/renewer.rb} +0 -0
@@ -1,78 +0,0 @@
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 VaultEntity
10
- extend Legion::Logging::Helper
11
-
12
- # Create or lookup a Vault entity for a Legion principal.
13
- # Returns the Vault entity ID string, or nil on failure.
14
- def self.ensure_entity(principal_id:, canonical_name:, metadata: {})
15
- existing = find_by_name(canonical_name: canonical_name)
16
- return existing if existing
17
-
18
- response = Legion::Crypt::LeaseManager.instance.vault_logical.write(
19
- path: 'identity/entity',
20
- data: {
21
- name: "legion-#{canonical_name}",
22
- metadata: metadata.merge(
23
- legion_principal_id: principal_id,
24
- legion_canonical_name: canonical_name,
25
- managed_by: 'legion'
26
- )
27
- }
28
- )
29
- extract_id(response)
30
- rescue StandardError => e
31
- handle_exception(e, level: :warn, operation: 'crypt.vault_entity.ensure_entity', canonical_name: canonical_name)
32
- nil
33
- end
34
-
35
- # Create an alias linking an auth method mount to the entity.
36
- # Idempotent — swallows "already exists" 4xx errors.
37
- def self.ensure_alias(entity_id:, mount_accessor:, alias_name:)
38
- Legion::Crypt::LeaseManager.instance.vault_logical.write(
39
- path: 'identity/entity-alias',
40
- data: { name: alias_name, canonical_id: entity_id, mount_accessor: mount_accessor }
41
- )
42
- rescue Legion::Crypt::Vault::HTTPClientError => e
43
- if e.message.include?('already exists')
44
- log.debug 'Vault entity alias already exists (idempotent)'
45
- else
46
- handle_exception(e, level: :warn, operation: 'crypt.vault_entity.ensure_alias', alias_name: alias_name)
47
- end
48
- nil
49
- rescue StandardError => e
50
- handle_exception(e, level: :warn, operation: 'crypt.vault_entity.ensure_alias', alias_name: alias_name)
51
- nil
52
- end
53
-
54
- # Look up a Vault entity by its Legion canonical name.
55
- # Returns the Vault entity ID string, or nil if not found.
56
- def self.find_by_name(canonical_name:)
57
- response = Legion::Crypt::LeaseManager.instance.vault_logical.read(path: "identity/entity/name/legion-#{canonical_name}")
58
- extract_id(response)
59
- rescue StandardError => e
60
- handle_exception(e, level: :warn, operation: 'crypt.vault_entity.find_by_name', canonical_name: canonical_name)
61
- nil
62
- end
63
-
64
- # ---------------------------------------------------------------------------
65
- # Private helpers
66
- # ---------------------------------------------------------------------------
67
-
68
- # Extract the entity ID from the client's symbol-keyed response data.
69
- def self.extract_id(response)
70
- data = response&.data
71
- return nil unless data
72
-
73
- data[:id]
74
- end
75
- private_class_method :extract_id
76
- end
77
- end
78
- end
@@ -1,97 +0,0 @@
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 VaultJwtAuth
19
- DEFAULT_AUTH_PATH = 'auth/jwt/login'
20
- DEFAULT_ROLE = 'legion-worker'
21
-
22
- class AuthError < StandardError; end
23
-
24
- extend Legion::Logging::Helper
25
-
26
- # Authenticate to Vault using a JWT token.
27
- # Returns a Vault token string on success.
28
- #
29
- # @param jwt [String] Signed JWT token (issued by Legion or Entra ID)
30
- # @param role [String] Vault JWT auth role name (default: 'legion-worker')
31
- # @param auth_path [String] Vault auth mount path (default: 'auth/jwt/login')
32
- # @return [Hash] { token:, lease_duration:, policies:, metadata: }
33
- def self.login(jwt:, role: DEFAULT_ROLE, auth_path: DEFAULT_AUTH_PATH)
34
- raise AuthError, 'Vault is not connected' unless Legion::Crypt.vault_connected?
35
-
36
- log.info "[crypt:vault_jwt] authenticating role=#{role} auth_path=#{auth_path}"
37
- response = Legion::Crypt.vault_client.logical.write(path: auth_path, data: { role: role, jwt: jwt })
38
-
39
- raise AuthError, 'Vault JWT auth returned no auth data' unless response&.auth
40
-
41
- {
42
- token: response.auth.client_token,
43
- lease_duration: response.auth.lease_duration,
44
- renewable: response.auth.renewable?,
45
- policies: response.auth.policies,
46
- metadata: response.auth.metadata
47
- }
48
- rescue Legion::Crypt::Vault::HTTPClientError => e
49
- handle_exception(e, level: :warn, operation: 'crypt.vault_jwt_auth.login', role: role, auth_path: auth_path,
50
- category: 'client_error')
51
- raise AuthError, "Vault JWT auth failed: #{e.message}"
52
- rescue Legion::Crypt::Vault::HTTPServerError => e
53
- handle_exception(e, level: :warn, operation: 'crypt.vault_jwt_auth.login', role: role, auth_path: auth_path,
54
- category: 'server_error')
55
- raise AuthError, "Vault server error during JWT auth: #{e.message}"
56
- rescue StandardError => e
57
- handle_exception(e, level: :error, operation: 'crypt.vault_jwt_auth.login', role: role, auth_path: auth_path)
58
- raise if e.is_a?(AuthError)
59
-
60
- raise AuthError, "Vault JWT auth failed: #{e.message}"
61
- end
62
-
63
- # Authenticate and set the Vault client token for subsequent operations.
64
- # This replaces the current Vault token with the JWT-authenticated one.
65
- #
66
- # @return [Hash] Same as login
67
- def self.login!(jwt:, role: DEFAULT_ROLE, auth_path: DEFAULT_AUTH_PATH)
68
- generation = Legion::Crypt.vault_auth_generation
69
- client = Legion::Crypt.vault_client
70
- config = Legion::Crypt.cluster || Legion::Settings[:crypt][:vault]
71
- result = login(jwt: jwt, role: role, auth_path: auth_path)
72
- Legion::Crypt.install_vault_grant(client: client, config: config, grant: result, auth_method: 'jwt', generation: generation)
73
- log.info "[crypt:vault_jwt] authenticated via JWT auth, policies=#{result[:policies].join(',')}"
74
- result
75
- end
76
-
77
- # Issue a Legion JWT and use it to authenticate to Vault in one step.
78
- # Convenience method for workers that need Vault access.
79
- #
80
- # @param worker_id [String] Digital worker ID
81
- # @param owner_msid [String] Worker's owner MSID
82
- # @param role [String] Vault JWT auth role name
83
- # @return [Hash] Same as login
84
- def self.worker_login(worker_id:, owner_msid:, role: DEFAULT_ROLE)
85
- log.info "[crypt:vault_jwt] worker login requested role=#{role} worker_id=#{worker_id}"
86
- jwt = Legion::Crypt::JWT.issue(
87
- { worker_id: worker_id, sub: owner_msid, scope: 'vault', aud: 'legion' },
88
- signing_key: Legion::Crypt.cluster_secret,
89
- ttl: 300,
90
- issuer: 'legion'
91
- )
92
-
93
- login(jwt: jwt, role: role)
94
- end
95
- end
96
- end
97
- end
@@ -1,51 +0,0 @@
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 VaultKerberosAuth
9
- DEFAULT_AUTH_PATH = 'auth/kerberos/login'
10
-
11
- class AuthError < StandardError; end
12
-
13
- extend Legion::Logging::Helper
14
-
15
- def self.login(spnego_token:, auth_path: DEFAULT_AUTH_PATH)
16
- raise AuthError, 'Vault is not connected' unless Legion::Crypt.vault_connected?
17
-
18
- log.info "[crypt:vault_kerberos] login requested auth_path=#{auth_path}"
19
- data = Legion::Crypt.vault_client.put(
20
- path: auth_path, body: '{}', headers: { 'Authorization' => "Negotiate #{spnego_token}" }
21
- )
22
- response = Legion::Crypt::VaultClient::Secret.decode(data: data)
23
- raise AuthError, 'Vault Kerberos auth returned no auth data' unless response&.auth
24
-
25
- {
26
- token: response.auth.client_token,
27
- lease_duration: response.auth.lease_duration,
28
- renewable: response.auth.renewable?,
29
- policies: response.auth.policies,
30
- metadata: response.auth.metadata
31
- }
32
- rescue Legion::Crypt::Vault::HTTPClientError => e
33
- handle_exception(e, level: :warn, operation: 'crypt.vault_kerberos_auth.login', auth_path: auth_path)
34
- raise AuthError, "Vault Kerberos auth failed: #{e.message}"
35
- rescue StandardError => e
36
- handle_exception(e, level: :error, operation: 'crypt.vault_kerberos_auth.login', auth_path: auth_path)
37
- raise
38
- end
39
-
40
- def self.login!(spnego_token:, auth_path: DEFAULT_AUTH_PATH)
41
- generation = Legion::Crypt.vault_auth_generation
42
- client = Legion::Crypt.vault_client
43
- config = Legion::Crypt.cluster || Legion::Settings[:crypt][:vault]
44
- result = login(spnego_token: spnego_token, auth_path: auth_path)
45
- Legion::Crypt.install_vault_grant(client: client, config: config, grant: result, auth_method: 'kerberos', generation: generation)
46
- log.info "[crypt:vault_kerberos] authenticated via Kerberos auth, policies=#{result[:policies].join(',')}"
47
- result
48
- end
49
- end
50
- end
51
- end
@@ -1,27 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'timeout'
4
-
5
- module Legion
6
- module Crypt
7
- module VaultOperation
8
- DEFAULT_TIMEOUT = 5
9
- SHUTDOWN_TIMEOUT = 10
10
-
11
- def self.with_timeout(deadline: nil, **)
12
- configuration = defined?(Legion::Settings) ? Legion::Settings[:crypt] : nil
13
- timeout = configuration&.dig(:vault, :operation_timeout) || DEFAULT_TIMEOUT
14
- raise ArgumentError, 'Vault operation_timeout must be a finite positive number' unless timeout.is_a?(Numeric) && timeout.finite? && timeout.positive?
15
-
16
- if deadline
17
- remaining = deadline - ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
18
- raise Timeout::Error, 'Vault operation deadline expired' unless remaining.positive?
19
-
20
- timeout = [timeout, remaining].min
21
- end
22
- operation_deadline = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC) + timeout
23
- Timeout.timeout(timeout) { yield(operation_deadline) }
24
- end
25
- end
26
- end
27
- end