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

Class Method Summary collapse

Class Method Details

.bind!(provider, identity_hash) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/legion/identity/process.rb', line 76

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

.bind_fallback!Object



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/legion/identity/process.rb', line 91

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

.canonical_nameObject



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

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

.idObject



21
22
23
24
# File 'lib/legion/identity/process.rb', line 21

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

.identity_hashObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/legion/identity/process.rb', line 61

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

.kindObject



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

def kind
  @state.get[:kind]
end

.modeObject



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

def mode
  Legion::Mode.current
end

.persistent?Boolean

Returns:

  • (Boolean)


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

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

.queue_prefixObject



39
40
41
42
43
44
45
46
47
# File 'lib/legion/identity/process.rb', line 39

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



105
106
107
108
109
# File 'lib/legion/identity/process.rb', line 105

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

  @provider.refresh
end

.reset!Object



111
112
113
114
115
# File 'lib/legion/identity/process.rb', line 111

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

.resolved?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/legion/identity/process.rb', line 49

def resolved?
  @resolved.true?
end

.sourceObject



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

def source
  @state.get[:source]
end