legionio 1.7.30 → 1.7.31

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 537bbc9c431e5e8b9fdaf96145ea124a0c4b8e84b4094673cbc4f6b07d4edb21
4
- data.tar.gz: da76364c12123a27f644f4fdef175c14796b2542800a93b054b26cb5f27a5e79
3
+ metadata.gz: 962639f3a96de99418ba1cb7feb2ba06b488776df2c2c3a1ec7dfd0d353e6759
4
+ data.tar.gz: 87f2e206fab455fac86e36efdc497ff67d4614bb0699be00eb146c764ef43797
5
5
  SHA512:
6
- metadata.gz: bee0c19f368dffa21a46fc964a43ae7448356912cbc5f0ac6884c9bcd4e00660821bc66c37e2b8d2cc2e05c77ec0fbce17d2f5a6c70b5b2bfb0d5eb4c3c278a6
7
- data.tar.gz: 60f48041416adb178c7aae384647c7fb15860aa2a25dde1418500ef947ac323ebf36340ab242e9abcdd2893e717e3bdef94bd62aea43955e927c26acf377aecf
6
+ metadata.gz: ef4a6788ec894fe2107c834c470fc469d866d36e445618bee17d6149738e2b31ce49c708619f0aaff12d140e799a92562e5383e9515f6d88e1ce15bbaeb9834c
7
+ data.tar.gz: 3a8a85d88ba670d683c196de7e60c32f37650e3ce420869a5e7b781f0741dcadce758f6c42b6cdb71b61c32ca68ac23d05d1571b00f2b974423da8779d975b82
data/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # Legion Changelog
2
2
 
3
+ ## [1.7.31] - 2026-04-08
4
+
5
+ ### Added
6
+ - Phase 7 RBAC enrichment: `Identity::Request` gains `roles:` constructor kwarg, `#roles` reader, `#id` alias for `principal_id`, and `roles:` in `identity_hash`
7
+ - `Identity::Middleware#build_request` now separates `claims[:groups]` (group OIDs/names) from `claims[:roles]` (Entra app roles), fixing the pre-existing conflation via `||`
8
+ - Worker token principal_id now correctly uses `claims[:worker_id]` when present, preventing worker tokens owned by a human from sharing the human's RBAC identity
9
+ - `Identity::Middleware` enriches resolved roles via `Legion::Rbac::GroupRoleMapper` when legion-rbac is loaded and enabled (including audit mode)
10
+ - `Identity::Middleware` builds `env['legion.rbac_principal']` (a `Legion::Rbac::Principal`) after setting `env['legion.principal']`, bridging identity to RBAC
11
+ - Middleware mount order fix: `Legion::Rbac::Middleware` removed from class-level `use` in `api.rb`; both `Identity::Middleware` and `Rbac::Middleware` now registered in `service.rb#setup_api` in the correct order (Identity first, then RBAC)
12
+
13
+ ### Changed
14
+ - `Legion::Identity::Request.from_auth_context` now reads `claims[:resolved_roles]` to populate `roles`
15
+
3
16
  ## [1.7.30] - 2026-04-08
4
17
 
5
18
  ### Added
data/lib/legion/api.rb CHANGED
@@ -221,7 +221,6 @@ module Legion
221
221
  register Routes::GraphQL if defined?(Routes::GraphQL)
222
222
 
223
223
  use Legion::API::Middleware::RequestLogger
224
- use Legion::Rbac::Middleware if defined?(Legion::Rbac::Middleware)
225
224
  use ElasticAPM::Middleware if defined?(ElasticAPM::Middleware) &&
226
225
  Legion::Settings.dig(:api, :elastic_apm, :enabled)
227
226
  end
@@ -18,17 +18,39 @@ module Legion
18
18
  auth_claims = env['legion.auth']
19
19
  auth_method = env['legion.auth_method']
20
20
 
21
- env['legion.principal'] = if auth_claims
22
- build_request(auth_claims, auth_method)
23
- elsif @require_auth
24
- # Auth middleware already handled 401 for protected paths;
25
- # this is a safety net for any path that slipped through.
26
- nil
27
- else
28
- # No auth required (loopback bind, lite mode, etc.).
29
- # Set a system-level principal so audit trails always have an identity.
30
- system_principal
31
- end
21
+ request = if auth_claims
22
+ build_request(auth_claims, auth_method)
23
+ elsif @require_auth
24
+ # Auth middleware already handled 401 for protected paths;
25
+ # this is a safety net for any path that slipped through.
26
+ nil
27
+ else
28
+ # No auth required (loopback bind, lite mode, etc.).
29
+ # Set a system-level principal so audit trails always have an identity.
30
+ system_principal
31
+ end
32
+
33
+ env['legion.principal'] = request
34
+
35
+ # Bridge to RBAC principal if legion-rbac is loaded.
36
+ # This is a data bridge — set regardless of enforce/audit mode so
37
+ # the RBAC middleware always has a typed principal to evaluate.
38
+ # Guard: require Legion::Rbac.enabled? to confirm the real gem is loaded
39
+ # (not a minimal test stub), and rescue construction errors defensively.
40
+ if request && defined?(Legion::Rbac::Principal) &&
41
+ defined?(Legion::Rbac) && Legion::Rbac.respond_to?(:enabled?) &&
42
+ Legion::Rbac.enabled?
43
+ begin
44
+ env['legion.rbac_principal'] = Legion::Rbac::Principal.new(
45
+ id: request.principal_id,
46
+ type: request.kind == :service ? :worker : request.kind,
47
+ roles: request.roles,
48
+ team: request.metadata&.dig(:team)
49
+ )
50
+ rescue StandardError
51
+ # Best-effort bridge: leave legion.rbac_principal unset on construction errors.
52
+ end
53
+ end
32
54
 
33
55
  @app.call(env)
34
56
  end
@@ -49,12 +71,39 @@ module Legion
49
71
  end
50
72
 
51
73
  def build_request(claims, method)
74
+ # Use worker_id as principal_id when present — worker tokens encode both
75
+ # worker_id and sub=owner_msid, and we want the worker's identity, not the owner's.
76
+ principal_id = claims[:worker_id] || claims[:sub] || claims[:owner_msid]
77
+
78
+ # For worker tokens (scope: 'worker' or worker_id present), derive canonical_name
79
+ # from the worker's own identity. Production worker JWTs omit :name and carry
80
+ # sub=owner_msid, so falling back to claims[:sub] would inherit the owner's identity.
81
+ worker_token = claims[:scope] == 'worker' || claims[:worker_id]
82
+ display_name = claims[:name] || (worker_token ? principal_id : claims[:sub])
83
+
84
+ # Separate group OIDs/names from Entra app roles — they are NOT equivalent.
85
+ # claims[:groups] = group OIDs/names (for GroupRoleMapper)
86
+ # claims[:roles] = Entra app roles (pre-assigned at token-exchange time)
87
+ groups = Array(claims[:groups])
88
+ roles = Array(claims[:roles])
89
+
90
+ # Enrich with group-derived RBAC roles when legion-rbac is loaded (including audit mode).
91
+ resolved_roles = if defined?(Legion::Rbac::GroupRoleMapper) &&
92
+ Legion::Rbac.respond_to?(:enabled?) &&
93
+ Legion::Rbac.enabled?
94
+ group_roles = Legion::Rbac::GroupRoleMapper.resolve_roles(groups: groups)
95
+ (roles + group_roles).uniq
96
+ else
97
+ roles
98
+ end
99
+
52
100
  Identity::Request.from_auth_context({
53
- sub: claims[:sub] || claims[:worker_id] || claims[:owner_msid],
54
- name: claims[:name] || claims[:sub],
55
- kind: determine_kind(claims, method),
56
- groups: Array(claims[:roles] || claims[:groups]),
57
- source: method&.to_sym
101
+ sub: principal_id,
102
+ name: display_name,
103
+ kind: determine_kind(claims, method),
104
+ groups: groups,
105
+ resolved_roles: resolved_roles,
106
+ source: method&.to_sym
58
107
  })
59
108
  end
60
109
 
@@ -16,13 +16,16 @@ module Legion
16
16
  system: :system
17
17
  }.freeze
18
18
 
19
- attr_reader :principal_id, :canonical_name, :kind, :groups, :source, :metadata
19
+ attr_reader :principal_id, :canonical_name, :kind, :groups, :roles, :source, :metadata
20
20
 
21
- def initialize(principal_id:, canonical_name:, kind:, groups: [], source: nil, metadata: {}) # rubocop:disable Metrics/ParameterLists
21
+ alias id principal_id
22
+
23
+ def initialize(principal_id:, canonical_name:, kind:, groups: [], roles: [], source: nil, metadata: {}) # rubocop:disable Metrics/ParameterLists
22
24
  @principal_id = principal_id
23
25
  @canonical_name = canonical_name
24
26
  @kind = kind
25
27
  @groups = groups.freeze
28
+ @roles = roles.freeze
26
29
  @source = SOURCE_NORMALIZATION.fetch(source&.to_sym, source)
27
30
  @metadata = metadata.freeze
28
31
  freeze
@@ -35,7 +38,9 @@ module Legion
35
38
  end
36
39
 
37
40
  # Builds a Request from a parsed auth claims hash with symbol keys:
38
- # { sub:, name:, preferred_username:, kind:, groups:, source: }
41
+ # { sub:, name:, preferred_username:, kind:, groups:, resolved_roles:, source: }
42
+ # resolved_roles is the final merged set of Entra app roles + group-derived RBAC
43
+ # roles (populated by Identity::Middleware before calling this method).
39
44
  # The source value is normalized via SOURCE_NORMALIZATION at construction time.
40
45
  def self.from_auth_context(claims_hash)
41
46
  raw_name = claims_hash[:name] || claims_hash[:preferred_username] || ''
@@ -48,6 +53,7 @@ module Legion
48
53
  canonical_name: canonical,
49
54
  kind: claims_hash[:kind] || :human,
50
55
  groups: claims_hash[:groups] || [],
56
+ roles: Array(claims_hash[:resolved_roles]),
51
57
  source: normalized_source
52
58
  )
53
59
  end
@@ -58,6 +64,7 @@ module Legion
58
64
  canonical_name: canonical_name,
59
65
  kind: kind,
60
66
  groups: groups,
67
+ roles: roles,
61
68
  source: source
62
69
  }
63
70
  end
@@ -356,7 +356,7 @@ module Legion
356
356
  handle_exception(e, level: :warn, operation: 'service.shutdown_apm')
357
357
  end
358
358
 
359
- def setup_api # rubocop:disable Metrics/MethodLength
359
+ def setup_api # rubocop:disable Metrics/MethodLength,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
360
360
  if @api_thread&.alive?
361
361
  log.warn 'API already running, skipping duplicate setup_api call'
362
362
  return
@@ -394,12 +394,25 @@ module Legion
394
394
  log.info "Starting Legion API on #{bind}:#{port}"
395
395
  end
396
396
 
397
- # Mount identity middleware — bridges legion.auth to legion.principal
397
+ # Mount identity middleware — bridges legion.auth to legion.principal.
398
+ # Identity MUST be mounted before RBAC so env['legion.rbac_principal'] is
399
+ # populated before the RBAC middleware reads it.
398
400
  if defined?(Legion::Identity::Middleware)
399
401
  require_auth = Legion::Identity::Middleware.require_auth?(bind: bind, mode: Legion::Mode.current)
400
402
  Legion::API.use Legion::Identity::Middleware, require_auth: require_auth
401
403
  end
402
404
 
405
+ # Mount RBAC middleware after Identity — reads env['legion.rbac_principal']
406
+ # set by Identity::Middleware above. Only mount when a compatible RBAC
407
+ # integration is present and enabled to avoid mixed-version request
408
+ # failures.
409
+ if defined?(Legion::Rbac::Middleware) &&
410
+ defined?(Legion::Rbac::Principal) &&
411
+ Legion::Rbac.respond_to?(:enabled?) &&
412
+ Legion::Rbac.enabled?
413
+ Legion::API.use Legion::Rbac::Middleware
414
+ end
415
+
403
416
  @api_thread = Thread.new do
404
417
  retries = 0
405
418
  max_retries = api_settings[:bind_retries]
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Legion
4
- VERSION = '1.7.30'
4
+ VERSION = '1.7.31'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: legionio
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.30
4
+ version: 1.7.31
5
5
  platform: ruby
6
6
  authors:
7
7
  - Esity