Class: Puppet::Provider::ElasticUserCommand

Inherits:
Puppet::Provider
  • Object
show all
Defined in:
lib/puppet/provider/elastic_user_command.rb

Overview

Parent provider for Elasticsearch Shield/X-Pack file-based user management tools.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = {}) ⇒ ElasticUserCommand

Returns a new instance of ElasticUserCommand.



81
82
83
84
# File 'lib/puppet/provider/elastic_user_command.rb', line 81

def initialize(value = {})
  super(value)
  @property_flush = {}
end

Instance Attribute Details

#homedirObject

Returns the value of attribute homedir.



6
7
8
# File 'lib/puppet/provider/elastic_user_command.rb', line 6

def homedir
  @homedir
end

Class Method Details

.command_with_path(args, configdir = nil) ⇒ Object

Run the user management command with specified tool arguments.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/puppet/provider/elastic_user_command.rb', line 21

def self.command_with_path(args, configdir = nil)
  options = {
    combine: true,
    custom_environment: {
      'ES_PATH_CONF' => configdir || '/etc/elasticsearch'
    },
    failonfail: true
  }

  execute(
    [command(:users_cli)] + (args.is_a?(Array) ? args : [args]),
    options
  )
end

.fetch_usersObject

Gather local file-based users into an array of Hash objects.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/puppet/provider/elastic_user_command.rb', line 37

def self.fetch_users
  begin
    output = command_with_path('list')
  rescue Puppet::ExecutionFailure => e
    debug("#fetch_users had an error: #{e.inspect}")
    return nil
  end

  debug("Raw command output: #{output}")
  matching_lines = output.split("\n").select do |u|
    # Keep only expected "user : role1,role2" formatted lines
    u[%r{^[^:]+:\s+\S+$}]
  end

  users = matching_lines.map do |u|
    # Break into ["user ", " role1,role2"]
    u.split(':').first.strip
  end

  users.map do |user|
    {
      name: user,
      ensure: :present,
      provider: name
    }
  end
end

.homedirObject

Elasticsearch’s home directory.

Returns:

  • String



11
12
13
14
15
16
17
18
# File 'lib/puppet/provider/elastic_user_command.rb', line 11

def self.homedir
  @homedir ||= case Facter.value('osfamily')
               when 'OpenBSD'
                 '/usr/local/elasticsearch'
               else
                 '/usr/share/elasticsearch'
               end
end

.instancesObject

Fetch an array of provider objects from the the list of local users.



66
67
68
69
70
# File 'lib/puppet/provider/elastic_user_command.rb', line 66

def self.instances
  fetch_users.map do |user|
    new user
  end
end

.prefetch(resources) ⇒ Object

Generic prefetch boilerplate.



73
74
75
76
77
78
79
# File 'lib/puppet/provider/elastic_user_command.rb', line 73

def self.prefetch(resources)
  instances.each do |prov|
    if (resource = resources[prov.name])
      resource.provider = prov
    end
  end
end

Instance Method Details

#createObject

Set this provider’s ‘:ensure` property to `:present`.



107
108
109
# File 'lib/puppet/provider/elastic_user_command.rb', line 107

def create
  @property_flush[:ensure] = :present
end

#destroyObject

Set this provider’s ‘:ensure` property to `:absent`.



116
117
118
# File 'lib/puppet/provider/elastic_user_command.rb', line 116

def destroy
  @property_flush[:ensure] = :absent
end

#exists?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/puppet/provider/elastic_user_command.rb', line 111

def exists?
  @property_hash[:ensure] == :present
end

#flushObject

Enforce the desired state for this user on-disk.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/puppet/provider/elastic_user_command.rb', line 87

def flush
  arguments = []

  case @property_flush[:ensure]
  when :absent
    arguments << 'userdel'
    arguments << resource[:name]
  else
    arguments << 'useradd'
    arguments << resource[:name]
    arguments << '-p' << resource[:password]
  end

  self.class.command_with_path(arguments, resource[:configdir])
  @property_hash = self.class.fetch_users.find do |u|
    u[:name] == resource[:name]
  end
end

#passwdObject

Manually set this user’s password.



121
122
123
124
125
126
127
128
129
130
# File 'lib/puppet/provider/elastic_user_command.rb', line 121

def passwd
  self.class.command_with_path(
    [
      'passwd',
      resource[:name],
      '-p', resource[:password]
    ],
    resource[:configdir]
  )
end