Defined Type: psick::users::managed

Defined in:
manifests/users/managed.pp

Overview

Copied from example42 user::managed sshkey: have to be handed over as the classname

containing the ssh_keys

password: the password in cleartext or as crypted string which

should be set. Default: absent -> no password is set.
To create an encrypted password, you can use:
/usr/bin/mkpasswd -H md5 --salt=$salt $password ,
where $salt is 8 bytes long
Note: On OpenBSD systems we can only manage crypted
      passwords. Therefor the password_crypted
      option doesn't have any effect.

password_crypted: if the supplied password is crypted or not.

Default: true
Note: If you'd like to use unencrypted passwords,
      you have to set a variable $password_salt to
      an 8 character long salt used for the password.

gid: define the gid of the group

absent: let the system take a gid
uid: (*default*) take the same as the uid if present
<value>: take this gid

manage_group: Wether we should add a group with the same name as

well, this works only if you supply a uid.
Default: true

sshkey_content: supply ssh key via ‘key’, ‘comment’ and ‘type’ sshkeys_content: supply ssh keys via an array of ‘key’, ‘comment’ and ‘type’

Parameters:

  • ensure (String) (defaults to: present)
  • comment (String) (defaults to: '')
  • uid (String) (defaults to: 'absent')
  • gid (String) (defaults to: 'uid')
  • groups (Array) (defaults to: [])
  • manage_group (Boolean) (defaults to: true)
  • membership (String) (defaults to: 'minimum')
  • home (Optional[String]) (defaults to: undef)
  • password_max_age (Optional[Integer]) (defaults to: undef)
  • password_min_age (Optional[Integer]) (defaults to: undef)
  • managehome (Boolean) (defaults to: true)
  • homedir_mode (String) (defaults to: '0750')
  • homedir_recurse (Boolean) (defaults to: false)
  • homedir_source (Optional[String]) (defaults to: undef)
  • sshkey (String) (defaults to: 'absent')
  • authorized_keys_source (String) (defaults to: '')
  • bashprofile_source (String) (defaults to: '')
  • known_hosts_source (String) (defaults to: '')
  • password (String) (defaults to: 'absent')
  • password_crypted (Boolean) (defaults to: true)
  • password_salt (String) (defaults to: '')
  • shell (String) (defaults to: '/bin/bash')
  • id_rsa_source (String) (defaults to: '')
  • id_rsa_pub_source (String) (defaults to: '')
  • sshkey_content (Hash) (defaults to: {})
  • sshkeys_content (Array) (defaults to: [])
  • generate_ssh_keypair (Boolean) (defaults to: false)


26
27
28
29
30
31
32
33
34
35
36
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'manifests/users/managed.pp', line 26

define psick::users::managed (
  String $ensure          = present,
  String $comment         = '', # lint:ignore:params_empty_string_assignment
  String $uid             = 'absent',
  String $gid             = 'uid',
  Array $groups           = [],
  Boolean $manage_group   = true,
  String $membership      = 'minimum',
  Optional[String] $home              = undef,
  Optional[Integer] $password_max_age = undef,
  Optional[Integer] $password_min_age = undef,
  Boolean $managehome     = true,
  String $homedir_mode    = '0750',
  Boolean $homedir_recurse = false,
  Optional[String] $homedir_source = undef,
  String $sshkey          = 'absent',
  String $authorized_keys_source = '', # lint:ignore:params_empty_string_assignment
  String $bashprofile_source     = '', # lint:ignore:params_empty_string_assignment
  String $known_hosts_source     = '', # lint:ignore:params_empty_string_assignment
  String $password               = 'absent',
  Boolean $password_crypted      = true,
  String $password_salt          = '', # lint:ignore:params_empty_string_assignment
  String $shell                  = '/bin/bash',
  String $id_rsa_source          = '', # lint:ignore:params_empty_string_assignment
  String $id_rsa_pub_source      = '', # lint:ignore:params_empty_string_assignment
  Hash $sshkey_content           = {},
  Array $sshkeys_content         = [],
  Boolean $generate_ssh_keypair  = false,
) {
  $default_home_base = $facts['kernel'] ? {
    'windows' => 'C:\Users\\',
    'Darwin'  => '/Users/',
    default   => '/home/',
  }

  $real_homedir = $home ? {
    undef  => $title ? {
      'root'  => '/root',
      default => "${default_home_base}${title}",
    },
    default => $home,
  }

  $real_managehome = $facts['kernel'] ? {
    'Darwin' => undef,
    default  => $managehome,
  }

  $real_comment = $comment ? {
    ''      => $name,
    default => $comment,
  }

  $dir_ensure = $ensure ? {
    'present' => 'directory',
    'absent'  => 'absent',
  }

  File {
    ensure  => $ensure,
    owner   => $name,
    group   => $name,
  }

  user { $name:
    ensure           => $ensure,
    allowdupe        => false,
    comment          => $real_comment,
    home             => $real_homedir,
    managehome       => $real_managehome,
    shell            => $shell,
    groups           => $groups,
    membership       => $membership,
    password_max_age => $password_max_age,
    password_min_age => $password_min_age,
  }

  # Manage authorized keys if $authorized_keys_source exists
  if $authorized_keys_source != ''
  or $id_rsa_source != ''
  or $id_rsa_pub_source != ''
  or $known_hosts_source != ''
  or !empty($sshkey_content)
  or !empty($sshkeys_content) {
    file { "${real_homedir}_ssh":
      ensure => $dir_ensure,
      path   => "${real_homedir}/.ssh",
      mode   => $homedir_mode,
    }
  }

  if $authorized_keys_source != '' {
    file { "${real_homedir}/.ssh/authorized_keys2":
      mode   => '0600',
      source => $authorized_keys_source,
    }
  }

  $sshkey_defaults = {
    ensure => present,
    user   => $name,
    'type' => 'ssh-rsa',
  }

  if !empty($sshkey_content) {
    ssh_authorized_key { $sshkey_content['comment']:
      ensure => present,
      user   => $name,
      type   => $sshkey_content['type'],
      key    => $sshkey_content['key'],
    }
  }

  if !empty($sshkeys_content) {
    create_resources(ssh_authorized_key, $sshkeys_content, $sshkey_defaults)
  }

  if $generate_ssh_keypair {
    if $id_rsa_source == '' and $id_rsa_pub_source == '' {
      psick::openssh::keygen { $name:
        comment => $real_comment,
        require => User[$name],
      }
    } else {
      fail('You can set both generate_ssh_keypair to true and any of id_rsa_source or id_rsa_source')
    }
  }

  if $id_rsa_source != '' {
    file { "${real_homedir}/.ssh/id_rsa":
      mode   => '0600',
      source => $id_rsa_source,
    }
  }

  if $id_rsa_pub_source != '' {
    file { "${real_homedir}/.ssh/id_rsa.pub":
      mode   => '0644',
      source => $id_rsa_pub_source,
    }
  }

  if $known_hosts_source != '' {
    file { "${real_homedir}/.ssh/known_hosts":
      mode   => '0600',
      source => $known_hosts_source,
    }
  }

  if $bashprofile_source != '' {
    file { "${real_homedir}/.bash_profile":
      mode   => '0644',
      source => $bashprofile_source,
    }
  }

  if $managehome {
    if $ensure == 'absent' {
      $file_options = {
        ensure  => absent,
        purge   => true,
        force   => true,
        recurse => true,
      }
    } else {
      $file_options = {
        ensure  => directory,
        require => User[$name],
        owner   => $name,
        mode    => undef,
        source  => $homedir_source,
        recurse => $homedir_recurse,
      }
      case $gid {
        'absent','uid': {
          $file_group_option = {
            group => $facts['os']['family'] ? {
              'Suse'  => 'users',
              default => $name,
            },
          }
        }
        default: {
          $file_group_option = {
            group => $gid,
          }
        }
      }
    }
    file { $real_homedir:
      * => $file_options + $file_group_option,
    }
  }

  if $uid != 'absent' {
    User[$name] {
      uid => $uid,
    }
  }

  if $gid != 'absent' {
    if $gid == 'uid' {
      if $uid != 'absent' {
        $real_gid = $uid
      } else {
        #lint:ignore:undef_in_function_assignment
        $real_gid = undef
        #lint:endignore
      }
    } else {
      $real_gid = $gid
    }
    if $real_gid {
      User[$name] {
        gid => $real_gid,
      }
    }
  }

  if $name != 'root' {
    if $uid == 'absent' {
      if $manage_group and ($ensure == 'absent') {
        group { $name:
          ensure => absent,
        }
        case $facts['os']['name'] {
          'OpenBSD': {
            Group[$name] {
              before => User[$name],
            }
          }
          default: {
            Group[$name] {
              require => User[$name],
            }
          }
        }
      }
    } else {
      if $manage_group {
        group { $name:
          ensure    => $ensure,
          allowdupe => false,
        }
        if $real_gid {
          Group[$name] {
            gid => $real_gid,
          }
        }
        if $ensure == 'absent' {
          case $facts['os']['name'] {
            'OpenBSD': {
              Group[$name] {
                before => User[$name],
              }
            }
            default: {
              Group[$name] {
                require => User[$name],
              }
            }
          }
        } else {
          Group[$name] {
            before => User[$name],
          }
        }
      }
    }
  }
  if $ensure == 'present' {
    if $sshkey != 'absent' {
      User[$name] {
        before => Class[$sshkey],
      }
      include $sshkey
    }

    if $password != 'absent' {
      case $facts['os']['name'] {
        'OpenBSD': {
          exec { "setpass ${name}":
            unless  => "grep -q '^${name}:${password}:' /etc/master.passwd",
            command => "usermod -p '${password}' ${name}",
            require => User[$name],
          }
        }
        default: {
          # require ruby::shadow #
          if $password_crypted {
            $real_password = $password
          } else {
            if $password_salt != '' {
              $real_password = mkpasswd($password,$password_salt)
            } else {
              fail('To use unencrypted passwords you have to define a variable \$password_salt to an 8 character salt for passwords!')
            }
          }
          User[$name] {
            password => $real_password,
          }
        }
      }
    }
  }
}