legion-crypt 1.5.38 → 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.
Files changed (45) 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 +99 -0
  6. data/CLAUDE.md +65 -192
  7. data/CODEOWNERS +4 -4
  8. data/Gemfile +4 -1
  9. data/README.md +60 -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/lease_manager.rb +5 -831
  17. data/lib/legion/crypt/mtls.rb +8 -46
  18. data/lib/legion/crypt/partition_keys.rb +2 -6
  19. data/lib/legion/crypt/settings.rb +31 -21
  20. data/lib/legion/crypt/spiffe/svid_rotation.rb +6 -9
  21. data/lib/legion/crypt/spiffe.rb +14 -39
  22. data/lib/legion/crypt/vault/auth/jwt.rb +28 -0
  23. data/lib/legion/crypt/vault/auth/kerberos.rb +60 -0
  24. data/lib/legion/crypt/vault/auth/ldap.rb +29 -0
  25. data/lib/legion/crypt/vault/auth.rb +50 -0
  26. data/lib/legion/crypt/vault/client.rb +362 -0
  27. data/lib/legion/crypt/vault/cluster.rb +262 -0
  28. data/lib/legion/crypt/vault/entity.rb +79 -0
  29. data/lib/legion/crypt/vault/leases.rb +871 -0
  30. data/lib/legion/crypt/vault/operation.rb +29 -0
  31. data/lib/legion/crypt/vault/session.rb +405 -0
  32. data/lib/legion/crypt/vault.rb +21 -31
  33. data/lib/legion/crypt/version.rb +1 -1
  34. data/lib/legion/crypt.rb +152 -177
  35. metadata +27 -11
  36. data/lib/legion/crypt/kerberos_auth.rb +0 -119
  37. data/lib/legion/crypt/ldap_auth.rb +0 -48
  38. data/lib/legion/crypt/token_renewer.rb +0 -318
  39. data/lib/legion/crypt/vault_client.rb +0 -355
  40. data/lib/legion/crypt/vault_cluster.rb +0 -331
  41. data/lib/legion/crypt/vault_entity.rb +0 -78
  42. data/lib/legion/crypt/vault_jwt_auth.rb +0 -97
  43. data/lib/legion/crypt/vault_kerberos_auth.rb +0 -51
  44. data/lib/legion/crypt/vault_operation.rb +0 -27
  45. /data/lib/legion/crypt/{vault_renewer.rb → vault/renewer.rb} +0 -0
@@ -1,47 +1,31 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'legion/logging/helper'
4
+ require 'legion/crypt/settings'
4
5
  require 'socket'
5
6
 
6
7
  module Legion
7
8
  module Crypt
8
9
  module Mtls
9
- DEFAULT_PKI_PATH = 'pki/issue/legion-internal'
10
- DEFAULT_TTL = '24h'
11
10
  extend Legion::Logging::Helper
12
11
 
13
12
  class << self
14
13
  def enabled?
15
- security = safe_security_settings
16
- return false if security.nil?
17
-
18
- mtls = security[:mtls] || security['mtls']
19
- return false if mtls.nil?
20
-
21
- mtls[:enabled] || mtls['enabled'] || false
22
- end
23
-
24
- def pki_path
25
- security = safe_security_settings
26
- return DEFAULT_PKI_PATH if security.nil?
27
-
28
- mtls = security[:mtls] || security['mtls'] || {}
29
- mtls[:vault_pki_path] || mtls['vault_pki_path'] || DEFAULT_PKI_PATH
14
+ Legion::Settings[:crypt][:mtls][:enabled] == true
30
15
  end
31
16
 
32
- def issue_cert(common_name:, ttl: nil)
33
- resolved_ttl = ttl || cert_ttl_setting || DEFAULT_TTL
17
+ def issue_cert(common_name:, alt_names: '', ttl: nil, **)
18
+ pki_path = Legion::Settings[:crypt][:mtls][:vault_pki_path]
19
+ resolved_ttl = ttl.nil? ? Legion::Settings[:crypt][:mtls][:cert_ttl] : ttl
34
20
  log.info "[mTLS] certificate issue requested common_name=#{common_name} ttl=#{resolved_ttl}"
35
21
 
36
22
  response = Legion::Crypt.vault_client.logical.write(
37
23
  path: pki_path,
38
- data: { common_name: common_name, ttl: resolved_ttl, ip_sans: local_ip, alt_names: '' }
24
+ data: { common_name: common_name, ttl: resolved_ttl, ip_sans: local_ip, alt_names: alt_names }
39
25
  )
40
-
41
26
  raise "Vault PKI returned nil for #{pki_path} (common_name=#{common_name})" if response.nil?
42
27
 
43
28
  data = response.data
44
-
45
29
  {
46
30
  cert: data[:certificate],
47
31
  key: data[:private_key],
@@ -54,34 +38,12 @@ module Legion
54
38
  raise
55
39
  end
56
40
 
57
- def local_ip
41
+ def local_ip(**)
58
42
  Socket.ip_address_list.find { |a| a.ipv4? && !a.ipv4_loopback? }&.ip_address || '127.0.0.1'
59
- rescue StandardError => e
43
+ rescue SocketError, SystemCallError, IOError => e
60
44
  handle_exception(e, level: :warn, operation: 'crypt.mtls.local_ip')
61
45
  '127.0.0.1'
62
46
  end
63
-
64
- private
65
-
66
- def safe_security_settings
67
- return nil unless defined?(Legion::Settings)
68
-
69
- Legion::Settings[:security]
70
- rescue StandardError => e
71
- handle_exception(e, level: :debug, operation: 'crypt.mtls.safe_security_settings')
72
- nil
73
- end
74
-
75
- def cert_ttl_setting
76
- security = safe_security_settings
77
- return nil if security.nil?
78
-
79
- mtls = security[:mtls] || security['mtls'] || {}
80
- mtls[:cert_ttl] || mtls['cert_ttl']
81
- rescue StandardError => e
82
- handle_exception(e, level: :debug, operation: 'crypt.mtls.cert_ttl_setting')
83
- nil
84
- end
85
47
  end
86
48
  end
87
49
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'openssl'
4
4
  require 'legion/logging/helper'
5
+ require 'legion/crypt/settings'
5
6
 
6
7
  module Legion
7
8
  module Crypt
@@ -10,12 +11,7 @@ module Legion
10
11
 
11
12
  class << self
12
13
  def derive_key(master_key:, tenant_id:, context: nil)
13
- context ||= begin
14
- Legion::Settings[:crypt][:partition_keys][:derivation_context]
15
- rescue StandardError => e
16
- handle_exception(e, level: :debug, operation: 'crypt.partition_keys.derivation_context', tenant_id: tenant_id)
17
- nil
18
- end || 'legion-partition'
14
+ context = Legion::Settings[:crypt][:partition_keys][:derivation_context] if context.nil?
19
15
  log.debug "PartitionKeys deriving key for tenant #{tenant_id}"
20
16
  salt = OpenSSL::Digest::SHA256.digest(tenant_id.to_s)
21
17
  OpenSSL::KDF.hkdf(master_key, salt: salt, info: context, length: 32, hash: 'SHA256')
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'legion/logging/helper'
4
+ require 'legion/settings'
4
5
 
5
6
  module Legion
6
7
  module Crypt
@@ -17,6 +18,15 @@ module Legion
17
18
  }
18
19
  end
19
20
 
21
+ def self.mtls
22
+ {
23
+ enabled: false,
24
+ vault_pki_path: 'pki/issue/legion-internal',
25
+ cert_ttl: '24h',
26
+ renewal_window: 0.5
27
+ }
28
+ end
29
+
20
30
  def self.spiffe
21
31
  {
22
32
  enabled: false,
@@ -30,14 +40,19 @@ module Legion
30
40
 
31
41
  def self.default
32
42
  {
33
- vault: vault,
34
- jwt: jwt,
35
- tls: tls,
36
- cs_encrypt_ready: false,
37
- dynamic_keys: true,
38
- cluster_secret: nil,
39
- save_private_key: true,
40
- read_private_key: true
43
+ vault: vault,
44
+ jwt: jwt,
45
+ tls: tls,
46
+ mtls: mtls,
47
+ cs_encrypt_ready: false,
48
+ dynamic_keys: true,
49
+ cluster_secret: nil,
50
+ cluster_length: 32,
51
+ cluster_secret_timeout: 5,
52
+ save_private_key: true,
53
+ read_private_key: true,
54
+ ed25519: { vault_key_prefix: 'keys' },
55
+ partition_keys: { derivation_context: 'legion-partition', vault_tenant_prefix: 'secret/data/legion/tenants' }
41
56
  }
42
57
  end
43
58
 
@@ -74,6 +89,13 @@ module Legion
74
89
  service_principal: nil,
75
90
  auth_path: 'auth/kerberos/login'
76
91
  },
92
+ jwt: {
93
+ auth_path: 'auth/jwt/login',
94
+ role: 'legion-worker'
95
+ },
96
+ ldap: {
97
+ auth_path: 'auth/ldap/login'
98
+ },
77
99
  tls: {
78
100
  verify: 'peer'
79
101
  },
@@ -87,16 +109,4 @@ module Legion
87
109
  end
88
110
  end
89
111
 
90
- begin
91
- if Legion.const_defined?('Settings')
92
- Legion::Settings.merge_settings('crypt', Legion::Crypt::Settings.default)
93
- Legion::Crypt::Settings.log.info('Legion::Crypt settings defaults merged')
94
- end
95
- rescue StandardError => e
96
- if Legion::Crypt::Settings.respond_to?(:handle_exception)
97
- Legion::Crypt::Settings.handle_exception(e, level: :fatal, operation: 'crypt.settings.merge_defaults')
98
- else
99
- puts e.message
100
- puts e.backtrace
101
- end
102
- end
112
+ Legion::Settings.register_library(:crypt, Legion::Crypt::Settings.default)
@@ -130,16 +130,13 @@ module Legion
130
130
  end
131
131
 
132
132
  def renewal_window
133
- return SVID_RENEWAL_RATIO unless defined?(Legion::Settings)
134
-
135
- security = Legion::Settings[:security]
136
- return SVID_RENEWAL_RATIO if security.nil?
133
+ window = Legion::Settings.dig(:security, :spiffe, :renewal_window)
134
+ window = SVID_RENEWAL_RATIO if window.nil?
135
+ unless window.is_a?(Numeric) && window.finite? && window >= 0 && window < 1
136
+ raise ArgumentError, 'SVID renewal window must be finite and between zero and one (exclusive)'
137
+ end
137
138
 
138
- spiffe = security[:spiffe] || security['spiffe'] || {}
139
- spiffe[:renewal_window] || spiffe['renewal_window'] || SVID_RENEWAL_RATIO
140
- rescue StandardError => e
141
- handle_exception(e, level: :debug, operation: 'crypt.spiffe.svid_rotation.renewal_window')
142
- SVID_RENEWAL_RATIO
139
+ window
143
140
  end
144
141
  end
145
142
  end
@@ -1,8 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'legion/logging/helper'
4
3
  require 'uri'
5
4
  require 'openssl'
5
+ require 'legion/logging/helper'
6
+ require 'legion/crypt/settings'
7
+ require 'legion/crypt/spiffe/workload_api_client'
8
+ require 'legion/crypt/spiffe/svid_rotation'
9
+ require 'legion/crypt/spiffe/identity_helpers'
6
10
 
7
11
  module Legion
8
12
  module Crypt
@@ -10,7 +14,7 @@ module Legion
10
14
  SPIFFE_SCHEME = 'spiffe'
11
15
  DEFAULT_SOCKET_PATH = '/tmp/spire-agent/public/api.sock'
12
16
  DEFAULT_TRUST_DOMAIN = 'legion.internal'
13
- SVID_RENEWAL_RATIO = 0.5
17
+ SVID_RENEWAL_RATIO = 0.5
14
18
 
15
19
  class Error < StandardError; end
16
20
  class InvalidSpiffeIdError < Error; end
@@ -80,50 +84,30 @@ module Legion
80
84
  parse_id(spiffe_id_string)
81
85
  true
82
86
  rescue InvalidSpiffeIdError => e
83
- handle_exception(e, level: :debug, operation: 'crypt.spiffe.valid_id', spiffe_id: spiffe_id_string)
87
+ handle_exception(e, level: :warn, operation: 'crypt.spiffe.valid_id', spiffe_id: spiffe_id_string)
84
88
  false
85
89
  end
86
90
 
87
91
  def enabled?
88
- security = safe_security_settings
89
- return false if security.nil?
90
-
91
- spiffe = security[:spiffe] || security['spiffe']
92
- return false if spiffe.nil?
93
-
94
- spiffe[:enabled] || spiffe['enabled'] || false
92
+ Legion::Settings.dig(:security, :spiffe, :enabled) == true
95
93
  end
96
94
 
97
95
  def socket_path
98
- security = safe_security_settings
99
- return DEFAULT_SOCKET_PATH if security.nil?
100
-
101
- spiffe = security[:spiffe] || security['spiffe'] || {}
102
- spiffe[:socket_path] || spiffe['socket_path'] || DEFAULT_SOCKET_PATH
96
+ path = Legion::Settings.dig(:security, :spiffe, :socket_path)
97
+ path.nil? ? DEFAULT_SOCKET_PATH : path
103
98
  end
104
99
 
105
100
  def trust_domain
106
- security = safe_security_settings
107
- return DEFAULT_TRUST_DOMAIN if security.nil?
108
-
109
- spiffe = security[:spiffe] || security['spiffe'] || {}
110
- spiffe[:trust_domain] || spiffe['trust_domain'] || DEFAULT_TRUST_DOMAIN
101
+ domain = Legion::Settings.dig(:security, :spiffe, :trust_domain)
102
+ domain.nil? ? DEFAULT_TRUST_DOMAIN : domain
111
103
  end
112
104
 
113
105
  def workload_id
114
- security = safe_security_settings
115
- return nil if security.nil?
116
-
117
- spiffe = security[:spiffe] || security['spiffe'] || {}
118
- spiffe[:workload_id] || spiffe['workload_id']
106
+ Legion::Settings.dig(:security, :spiffe, :workload_id)
119
107
  end
120
108
 
121
109
  def allow_x509_fallback?
122
- security = safe_security_settings
123
- return false if security.nil?
124
-
125
- spiffe = security[:spiffe] || security['spiffe'] || {}
126
- spiffe[:allow_x509_fallback] || spiffe['allow_x509_fallback'] || false
110
+ Legion::Settings.dig(:security, :spiffe, :allow_x509_fallback) == true
127
111
  end
128
112
 
129
113
  private
@@ -138,15 +122,6 @@ module Legion
138
122
 
139
123
  raise InvalidSpiffeIdError, "SPIFFE ID must not contain userinfo, port, query, or fragment in '#{raw}'"
140
124
  end
141
-
142
- def safe_security_settings
143
- return nil unless defined?(Legion::Settings)
144
-
145
- Legion::Settings[:security]
146
- rescue StandardError => e
147
- handle_exception(e, level: :debug, operation: 'crypt.spiffe.safe_security_settings')
148
- nil
149
- end
150
125
  end
151
126
  end
152
127
  end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Legion
4
+ module Crypt
5
+ module Vault
6
+ module Auth
7
+ # JWT auth. Presents a signed JWT to Vault's JWT/OIDC method. Cannot re-authenticate on
8
+ # its own — a fresh login with a new JWT is required once the token is unusable.
9
+ class JWT
10
+ include Legion::Crypt::Vault::Auth
11
+
12
+ def can_reauth?
13
+ false
14
+ end
15
+
16
+ # Issues a grant by presenting the given JWT to the configured role.
17
+ def authenticate(client:, jwt:, **)
18
+ jwt_config = cluster_config[:jwt]
19
+ secret = client.logical.write(path: jwt_config[:auth_path], data: { role: jwt_config[:role], jwt: jwt })
20
+ install_grant(secret)
21
+ rescue Legion::Crypt::Vault::HTTPError => e
22
+ raise AuthError, "Vault JWT auth failed: #{e.message}"
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Legion
4
+ module Crypt
5
+ module Vault
6
+ module Auth
7
+ # Kerberos (SPNEGO) auth. The SPNEGO token is sent as the HTTP Authorization header and
8
+ # the client keeps its configured namespace. Can re-authenticate from the ticket cache.
9
+ class Kerberos
10
+ include Legion::Crypt::Vault::Auth
11
+
12
+ class GemMissingError < AuthError; end
13
+
14
+ def can_reauth?
15
+ true
16
+ end
17
+
18
+ # Issues a grant from the current Kerberos ticket.
19
+ def authenticate(client:, **)
20
+ raise GemMissingError, 'lex-kerberos gem is required for Kerberos auth' unless spnego_available?
21
+
22
+ kerberos = cluster_config[:kerberos]
23
+ service_principal = kerberos[:service_principal]
24
+ raise AuthError, 'Kerberos service_principal is not configured' unless service_principal
25
+
26
+ spnego_token = obtain_spnego_token(service_principal: service_principal)
27
+ data = client.put(path: kerberos[:auth_path], body: '{}',
28
+ headers: { 'Authorization' => "Negotiate #{spnego_token}" })
29
+ install_grant(Legion::Crypt::Vault::Client::Secret.decode(data: data))
30
+ rescue Legion::Crypt::Vault::HTTPError => e
31
+ raise AuthError, "Vault Kerberos auth failed: #{e.message}"
32
+ end
33
+
34
+ # Re-authenticates from the ticket cache to produce a replacement grant.
35
+ def reauth(client:, **)
36
+ authenticate(client: client)
37
+ end
38
+
39
+ private
40
+
41
+ def spnego_available?
42
+ require 'legion/extensions/kerberos/helpers/spnego'
43
+ true
44
+ rescue LoadError => e
45
+ handle_exception(e, level: :warn, operation: 'crypt.vault_auth_kerberos.spnego_available')
46
+ defined?(Legion::Extensions::Kerberos::Helpers::Spnego) ? true : false
47
+ end
48
+
49
+ def obtain_spnego_token(service_principal:)
50
+ helper = Object.new.extend(Legion::Extensions::Kerberos::Helpers::Spnego)
51
+ result = helper.obtain_spnego_token(service_principal: service_principal)
52
+ raise AuthError, "SPNEGO token acquisition failed: #{result[:error]}" unless result[:success]
53
+
54
+ result[:token]
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Legion
4
+ module Crypt
5
+ module Vault
6
+ module Auth
7
+ # LDAP auth. Requires explicit username/password and cannot re-authenticate on its own —
8
+ # a fresh login is required once the token is unusable.
9
+ class LDAP
10
+ include Legion::Crypt::Vault::Auth
11
+
12
+ def can_reauth?
13
+ false
14
+ end
15
+
16
+ # Issues a grant with the given credentials.
17
+ def authenticate(client:, username:, password:, **)
18
+ raise ArgumentError, 'LDAP username must be a single path segment' if username.include?('/')
19
+
20
+ secret = client.logical.write(path: "#{cluster_config[:ldap][:auth_path]}/#{username}", data: { password: password })
21
+ install_grant(secret)
22
+ rescue Legion::Crypt::Vault::HTTPError => e
23
+ raise AuthError, "Vault LDAP auth failed: #{e.message}"
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'legion/logging/helper'
4
+ require 'legion/crypt/vault/client'
5
+ require 'legion/crypt/vault/operation'
6
+
7
+ module Legion
8
+ module Crypt
9
+ module Vault
10
+ # Shared login behavior for a Vault auth method, mixed into the concrete
11
+ # Vault::Auth::Kerberos / LDAP / JWT classes. Reads this cluster's config live from
12
+ # Legion::Settings with bracket access; the token's lifecycle belongs to Vault::Session.
13
+ module Auth
14
+ include Legion::Logging::Helper
15
+
16
+ class AuthError < StandardError; end
17
+
18
+ # The login auth response. The session holds the live grant and renews it; this is the
19
+ # strategy's own record of what it last issued.
20
+ attr_reader :grant
21
+
22
+ def initialize(cluster_name:, **)
23
+ @cluster_name = cluster_name
24
+ @grant = nil
25
+ end
26
+
27
+ private
28
+
29
+ # The effective config for this cluster: the named cluster's settings, or the top-level
30
+ # vault settings for the single-cluster (nil) case. A named cluster that is not
31
+ # configured is a configuration error, not a fallback.
32
+ def cluster_config
33
+ vault = Legion::Settings[:crypt][:vault]
34
+ @cluster_name ? vault[:clusters].fetch(@cluster_name) : vault
35
+ end
36
+
37
+ # Validates a login secret and installs its auth response as the current grant.
38
+ def install_grant(secret)
39
+ raise AuthError, 'Vault auth returned no auth data' unless secret&.auth
40
+
41
+ @grant = secret.auth
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ require 'legion/crypt/vault/auth/kerberos'
49
+ require 'legion/crypt/vault/auth/ldap'
50
+ require 'legion/crypt/vault/auth/jwt'