Module: Legion::Rbac::KerberosClaimsMapper

Extended by:
Logging::Helper
Defined in:
lib/legion/rbac/kerberos_claims_mapper.rb

Constant Summary collapse

DEFAULT_ROLE =
'worker'
DEFAULT_TEAM_KEYS =
%i[team legion_team].freeze

Class Method Summary collapse

Class Method Details

.entra_fallback_claims(principal:, role_map:, default_role:, team_resolution:, profile:) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/legion/rbac/kerberos_claims_mapper.rb', line 120

def entra_fallback_claims(principal:, role_map:, default_role:, team_resolution:, profile:)
  entra_claims = { sub: principal, preferred_username: principal, **profile }.compact
  result = EntraClaimsMapper.map_claims(
    entra_claims,
    role_map:     role_map,
    default_role: default_role,
    **team_resolution
  )
  result&.merge(**profile, auth_method: 'kerberos', team: result[:team]) || mapped_claims(
    principal:       principal,
    groups:          [],
    role_map:        role_map,
    default_role:    default_role,
    team_resolution: team_resolution,
    profile:         profile
  )
end

.extract_team_resolution(profile) ⇒ Object



100
101
102
103
104
105
106
107
# File 'lib/legion/rbac/kerberos_claims_mapper.rb', line 100

def extract_team_resolution(profile)
  sanitized_profile = profile.dup
  team_resolution = {
    team_keys: sanitized_profile.delete(:team_keys) || DEFAULT_TEAM_KEYS,
    team_map:  sanitized_profile.delete(:team_map)
  }
  [sanitized_profile, team_resolution]
end

.map(principal:, groups:, role_map: {}, default_role: DEFAULT_ROLE, team_keys: DEFAULT_TEAM_KEYS, team_map: nil, **profile) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/legion/rbac/kerberos_claims_mapper.rb', line 15

def map(principal:, groups:, role_map: {}, default_role: DEFAULT_ROLE, team_keys: DEFAULT_TEAM_KEYS,
        team_map: nil, **profile)
  parts = principal.split('@', 2)
  username = parts.first
  realm = parts.length > 1 ? parts.last : nil
  roles = Array(groups).filter_map { |g| role_map[g] }.uniq
  used_default_role = roles.empty?
  roles = [default_role] if used_default_role
  team = resolve_team(profile, team_keys: team_keys, team_map: team_map)

  claims = {
    sub:            username,
    samaccountname: username,
    ad_fqdn:        realm&.downcase,
    roles:          roles,
    scope:          'human',
    auth_method:    'kerberos',
    **profile,
    team:           team
  }.compact
  log.info(
    "RBAC kerberos_claims map principal=#{username} roles=#{claims[:roles].size} " \
    "default_role=#{used_default_role} realm=#{claims[:ad_fqdn]} team=#{claims[:team] || 'none'}"
  )
  claims
rescue StandardError => e
  handle_exception(e, level: :error, operation: 'rbac.kerberos_claims_mapper.map', principal: principal)
  raise
end

.map_with_fallback(principal:, groups: nil, fallback: :entra, role_map: {}, default_role: DEFAULT_ROLE, **profile) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/legion/rbac/kerberos_claims_mapper.rb', line 45

def map_with_fallback(principal:, groups: nil, fallback: :entra, role_map: {},
                      default_role: DEFAULT_ROLE, **profile)
  profile, team_resolution = extract_team_resolution(profile)
  if groups&.any?
    claims = mapped_claims(
      principal:       principal,
      groups:          groups,
      role_map:        role_map,
      default_role:    default_role,
      team_resolution: team_resolution,
      profile:         profile
    )
    path = 'groups'
  elsif fallback == :entra && defined?(Legion::Rbac::EntraClaimsMapper)
    claims = entra_fallback_claims(
      principal:       principal,
      role_map:        role_map,
      default_role:    default_role,
      team_resolution: team_resolution,
      profile:         profile
    )
    path = 'entra'
  else
    claims = mapped_claims(
      principal:       principal,
      groups:          [],
      role_map:        role_map,
      default_role:    default_role,
      team_resolution: team_resolution,
      profile:         profile
    )
    path = 'default_role'
  end
  log.info("RBAC kerberos_claims fallback principal=#{principal} path=#{path}")
  claims
rescue StandardError => e
  handle_exception(
    e,
    level:     :error,
    operation: 'rbac.kerberos_claims_mapper.map_with_fallback',
    principal: principal,
    fallback:  fallback
  )
  raise
end

.mapped_claims(principal:, groups:, role_map:, default_role:, team_resolution:, profile:) ⇒ Object



109
110
111
112
113
114
115
116
117
118
# File 'lib/legion/rbac/kerberos_claims_mapper.rb', line 109

def mapped_claims(principal:, groups:, role_map:, default_role:, team_resolution:, profile:)
  map(
    principal:    principal,
    groups:       groups,
    role_map:     role_map,
    default_role: default_role,
    **team_resolution,
    **profile
  )
end

.resolve_team(profile, team_keys:, team_map:) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/legion/rbac/kerberos_claims_mapper.rb', line 91

def resolve_team(profile, team_keys:, team_map:)
  Array(team_keys).each do |key|
    value = profile[key] || profile[key.to_s]
    return value if value && (team_map.nil? || team_map.empty?)
    return team_map[value] || team_map[value.to_s] || team_map[value.to_sym] if value
  end
  nil
end