Module: Legion::Identity::Process

Defined in:
lib/legion/identity/process.rb

Constant Summary collapse

EMPTY_STATE =
{
  id:              nil,
  canonical_name:  nil,
  kind:            nil,
  source:          nil,
  persistent:      false,
  groups:          [].freeze,
  metadata:        {}.freeze,
  trust:           nil,
  aliases:         {}.freeze,
  providers:       {}.freeze,
  profile:         {}.freeze,
  db_principal_id: nil,
  db_identity_id:  nil
}.freeze

Class Method Summary collapse

Class Method Details

.aliasesObject



71
72
73
# File 'lib/legion/identity/process.rb', line 71

def aliases
  @state.get[:aliases] || {}.freeze
end

.bind!(provider, identity_hash) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/legion/identity/process.rb', line 112

def bind!(provider, identity_hash)
  @provider = provider
  provider_source = provider.respond_to?(:provider_name) ? provider.provider_name : nil
  @state.set({
               id:              identity_hash[:id],
               canonical_name:  identity_hash[:canonical_name],
               kind:            identity_hash[:kind],
               source:          identity_hash.key?(:source) ? identity_hash[:source] : provider_source,
               persistent:      identity_hash.fetch(:persistent, true),
               groups:          Array(identity_hash[:groups]).compact.freeze,
               metadata:        identity_hash[:metadata].is_a?(Hash) ? identity_hash[:metadata].dup.freeze : {}.freeze,
               trust:           identity_hash[:trust],
               aliases:         identity_hash[:aliases].is_a?(Hash) ? identity_hash[:aliases].dup.freeze : {}.freeze,
               providers:       identity_hash[:providers].is_a?(Hash) ? identity_hash[:providers].dup.freeze : {}.freeze,
               profile:         identity_hash[:profile].is_a?(Hash) ? identity_hash[:profile].dup.freeze : {}.freeze,
               db_principal_id: identity_hash[:db_principal_id],
               db_identity_id:  identity_hash[:db_identity_id]
             })
  @resolved.make_true
end

.bind_fallback!Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/legion/identity/process.rb', line 133

def bind_fallback!
  user = ENV.fetch('USER', 'anonymous')
  @state.set({
               id:             nil,
               canonical_name: user,
               kind:           :human,
               source:         :system,
               persistent:     false,
               groups:         [].freeze,
               metadata:       {}.freeze,
               trust:          nil,
               aliases:        {}.freeze,
               providers:      {}.freeze,
               profile:        {}.freeze
             })
  @resolved.make_false
end

.canonical_nameObject



32
33
34
35
# File 'lib/legion/identity/process.rb', line 32

def canonical_name
  state = @state.get
  state[:canonical_name] || 'anonymous'
end

.db_identity_idObject



87
88
89
# File 'lib/legion/identity/process.rb', line 87

def db_identity_id
  @state.get[:db_identity_id]
end

.db_principal_idObject



83
84
85
# File 'lib/legion/identity/process.rb', line 83

def db_principal_id
  @state.get[:db_principal_id]
end

.idObject



27
28
29
30
# File 'lib/legion/identity/process.rb', line 27

def id
  state = @state.get
  state[:id] || Legion.instance_id
end

.identity_hashObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/legion/identity/process.rb', line 91

def identity_hash
  {
    id:              id,
    canonical_name:  canonical_name,
    kind:            kind,
    source:          source,
    mode:            mode,
    queue_prefix:    queue_prefix,
    resolved:        resolved?,
    persistent:      persistent?,
    groups:          @state.get[:groups] || [],
    metadata:        @state.get[:metadata] || {},
    trust:           trust,
    aliases:         aliases,
    providers:       providers,
    profile:         profile,
    db_principal_id: @state.get[:db_principal_id],
    db_identity_id:  @state.get[:db_identity_id]
  }
end

.kindObject



37
38
39
# File 'lib/legion/identity/process.rb', line 37

def kind
  @state.get[:kind]
end

.modeObject



41
42
43
# File 'lib/legion/identity/process.rb', line 41

def mode
  Legion::Mode.current
end

.persistent?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/legion/identity/process.rb', line 59

def persistent?
  @state.get[:persistent] == true
end

.profileObject



79
80
81
# File 'lib/legion/identity/process.rb', line 79

def profile
  @state.get[:profile] || {}.freeze
end

.providersObject



75
76
77
# File 'lib/legion/identity/process.rb', line 75

def providers
  @state.get[:providers] || {}.freeze
end

.queue_prefixObject



45
46
47
48
49
50
51
52
53
# File 'lib/legion/identity/process.rb', line 45

def queue_prefix
  name = canonical_name
  case mode
  when :worker then "worker.#{name}.#{Legion.instance_id}"
  when :infra  then "infra.#{name}.#{safe_hostname}"
  when :lite   then "lite.#{name}.#{Legion.instance_id}"
  else              "agent.#{name}.#{safe_hostname}"
  end
end

.refresh_credentialsObject



151
152
153
154
155
# File 'lib/legion/identity/process.rb', line 151

def refresh_credentials
  return unless defined?(@provider) && @provider.respond_to?(:refresh)

  @provider.refresh
end

.reset!Object



157
158
159
160
161
# File 'lib/legion/identity/process.rb', line 157

def reset!
  @state    = Concurrent::AtomicReference.new(EMPTY_STATE.dup)
  @resolved = Concurrent::AtomicBoolean.new(false)
  @provider = nil
end

.resolved?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/legion/identity/process.rb', line 55

def resolved?
  @resolved.true?
end

.sourceObject



63
64
65
# File 'lib/legion/identity/process.rb', line 63

def source
  @state.get[:source]
end

.trustObject



67
68
69
# File 'lib/legion/identity/process.rb', line 67

def trust
  @state.get[:trust]
end