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.



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

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

Instance Attribute Details

#homedirObject

Returns the value of attribute homedir.



4
5
6
# File 'lib/puppet/provider/elastic_user_command.rb', line 4

def homedir
  @homedir
end

Class Method Details

.command_with_path(args, configdir = nil) ⇒ Object

Run the user management command with specified tool arguments.



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

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.



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

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}")
  output.split("\n").select { |u|
    # Keep only expected "user : role1,role2" formatted lines
    u[/^[^:]+:\s+\S+$/]
  }.map { |u|
    # Break into ["user ", " role1,role2"]
    u.split(':').first.strip
  }.map do |user|
    {
      :name => user,
      :ensure => :present,
      :provider => name
    }
  end
end

.homedirObject

Elasticsearch’s home directory.

Returns:

  • String



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

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.



60
61
62
63
64
# File 'lib/puppet/provider/elastic_user_command.rb', line 60

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

.prefetch(resources) ⇒ Object

Generic prefetch boilerplate.



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

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`.



101
102
103
# File 'lib/puppet/provider/elastic_user_command.rb', line 101

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

#destroyObject

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



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

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

#exists?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/puppet/provider/elastic_user_command.rb', line 105

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

#flushObject

Enforce the desired state for this user on-disk.



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

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.detect do |u|
    u[:name] == resource[:name]
  end
end

#passwdObject

Manually set this user’s password.



115
116
117
118
119
120
121
122
123
124
# File 'lib/puppet/provider/elastic_user_command.rb', line 115

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