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
}.freeze

Class Method Summary collapse

Class Method Details

.aliasesObject



69
70
71
# File 'lib/legion/identity/process.rb', line 69

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

.bind!(provider, identity_hash) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/legion/identity/process.rb', line 100

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
             })
  @resolved.make_true
end

.bind_fallback!Object



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

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



30
31
32
33
# File 'lib/legion/identity/process.rb', line 30

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

.idObject



25
26
27
28
# File 'lib/legion/identity/process.rb', line 25

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

.identity_hashObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/legion/identity/process.rb', line 81

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
  }
end

.kindObject



35
36
37
# File 'lib/legion/identity/process.rb', line 35

def kind
  @state.get[:kind]
end

.modeObject



39
40
41
# File 'lib/legion/identity/process.rb', line 39

def mode
  Legion::Mode.current
end

.persistent?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/legion/identity/process.rb', line 57

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

.profileObject



77
78
79
# File 'lib/legion/identity/process.rb', line 77

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

.providersObject



73
74
75
# File 'lib/legion/identity/process.rb', line 73

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

.queue_prefixObject



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

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



137
138
139
140
141
# File 'lib/legion/identity/process.rb', line 137

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

  @provider.refresh
end

.reset!Object



143
144
145
146
147
# File 'lib/legion/identity/process.rb', line 143

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

.resolved?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/legion/identity/process.rb', line 53

def resolved?
  @resolved.true?
end

.sourceObject



61
62
63
# File 'lib/legion/identity/process.rb', line 61

def source
  @state.get[:source]
end

.trustObject



65
66
67
# File 'lib/legion/identity/process.rb', line 65

def trust
  @state.get[:trust]
end