• Stars
    star
    138
  • Rank 264,508 (Top 6 %)
  • Language
    Ruby
  • License
    Apache License 2.0
  • Created over 12 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Development repository for the users cookbook

users Cookbook

Cookbook Version CI State OpenCollective OpenCollective License

Manages OS users and groups (optionally from databags).

Scope

This cookbook is concerned with the management of OS users and groups (optionally from databags). It also manages the distribution of ssh keys to a user's home directory.

Maintainers

This cookbook is maintained by the Sous Chefs. The Sous Chefs are a community of Chef cookbook maintainers working together to maintain important cookbooks. If you’d like to know more please visit sous-chefs.org or come chat with us on the Chef Community Slack in #sous-chefs.

Requirements

If you are upgrading from a version < 6.0.0 please see upgrading.md

Platforms

The following platforms have been tested with Test Kitchen:

  • Debian / Ubuntu derivatives
  • RHEL and derivatives
  • Fedora
  • openSUSE / SUSE Linux Enterprises
  • FreeBSD / OpenBSD
  • macOS
  • AIX

Chef

  • Chef 12.7+

Cookbooks

  • none

Usage

To use the resource users_manage, make sure to add the dependency on the users cookbook by the following line to your wrapper cookbook's metadata.rb:

depends 'users'

or to pin to a specific version of the users cookbook, in this case any version of 6.X:

depends 'users', '~> 6'

Then in a recipe use the user_manage resource to add all users in the defined group to the system:

users_variable = [{
  id: 'databag_test_user',
  ssh_keys: "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAklOUpkDHrfHY17SbrmTIpNLTGK9Tjom/BWDSU\nGPl+nafzlHDTYW7hdI4yZ5ew18JH4JW9jbhUFrviQzM7xlELEVf4h9lFX5QVkbPppSwg0cda3\nPbv7kOdJ/MTyBlWXFCR+HAo3FXRitBqxiX1nKhXpHAZsMciLq8V6RjsNAQwdsdMFvSlVK/7XA\nt3FaoJoAsncM1Q9x5+3V0Ww68/eIFmb1zuUFljQJKprrX88XypNDvjYNby6vw/Pb0rwert/En\nmZ+AW4OZPnTPI89ZPmVMLuayrD2cE86Z/il8b+gw3r3+1nKatmIkjn2so1d01QraTlMqVSsbx\nNrRFi9wrf+M7Q== [email protected]",
  groups: [ 'GROUPNAME' ],
}]

users_manage 'GROUPNAME' do
  group_id GROUPID
  action [:create]
  users users_variable
end

Example:

users_manage 'testgroup' do
  group_id 3000
  action [:create]
  users node['users']['array_of_users']
end

Note: The users property needs to be given an Array of Hashes that contains one user per hash. This can be done by passing a data bag like the example below or from any other source.

Databag Definition

This is an alternative to the attribute definition as mentioned below.

You could for instance create a databag called users. You then create a subdatabag for each user object.

A sample user object in a users databag would look like:

{
  "id": "test_user",
  "password": "$1$5cE1rI/9$4p0fomh9U4kAI23qUlZVv/",
  "ssh_keys": [
    "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAklOUpkDHrfHY17SbrmTIpNLTGK9Tjom/BWDSU\nGPl+nafzlHDTYW7hdI4yZ5ew18JH4JW9jbhUFrviQzM7xlELEVf4h9lFX5QVkbPppSwg0cda3\nPbv7kOdJ/MTyBlWXFCR+HAo3FXRitBqxiX1nKhXpHAZsMciLq8V6RjsNAQwdsdMFvSlVK/7XA\nt3FaoJoAsncM1Q9x5+3V0Ww68/eIFmb1zuUFljQJKprrX88XypNDvjYNby6vw/Pb0rwert/En\nmZ+AW4OZPnTPI89ZPmVMLuayrD2cE86Z/il8b+gw3r3+1nKatmIkjn2so1d01QraTlMqVSsbx\nNrRFi9wrf+M7Q== [email protected]",
    "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAklOUpkDHrfHY17SbrmTIpNLTGK9Tjom/BWDSU\nGPl+nafzlHDTYW7hdI4yZ5ew18JH4JW9jbhUFrviQzM7xlELEVf4h9lFX5QVkbPppSwg0cda3\nPbv7kOdJ/MTyBlWXFCR+HAo3FXRitBqxiX1nKhXpHAZsMciLq8V6RjsNAQwdsdMFvSlVK/7XA\nt3FaoJoAsncM1Q9x5+3V0Ww68/eIFmb1zuUFljQJKprrX88XypNDvjYNby6vw/Pb0rwert/En\nmZ+AW4OZPnTPI89ZPmVMLuayrD2cE86Z/il8b+gw3r3+1nKatmIkjn2so1d01QraTlMqVSsbx\nNQCPO0ZZEa1== [email protected]"
  ],
  "groups": [ "testgroup", "nfsgroup" ],
  "uid": 9001,
  "shell": "\/bin\/bash",
  "comment": "Test User"
}

A sample user to remove from a system would like like:

{
  "id": "mwaddams",
  "action": "remove",
  "groups": [ "testgroup", "nfsgroup" ]
}

Attributes Definition

This is an alternative to the data bag definition as mentioned above.

Consider having a cookbook called usermanagement where you include this users cookbook.

You could then set the attributes like this:

default['usermanagement']['users'] = [
  {
    id: 'test_user',
    password: '$1$5cE1rI/9$4p0fomh9U4kAI23qUlZVv/',
    ssh_keys: [
      "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAklOUpkDHrfHY17SbrmTIpNLTGK9Tjom/BWDSU\nGPl+nafzlHDTYW7hdI4yZ5ew18JH4JW9jbhUFrviQzM7xlELEVf4h9lFX5QVkbPppSwg0cda3\nPbv7kOdJ/MTyBlWXFCR+HAo3FXRitBqxiX1nKhXpHAZsMciLq8V6RjsNAQwdsdMFvSlVK/7XA\nt3FaoJoAsncM1Q9x5+3V0Ww68/eIFmb1zuUFljQJKprrX88XypNDvjYNby6vw/Pb0rwert/En\nmZ+AW4OZPnTPI89ZPmVMLuayrD2cE86Z/il8b+gw3r3+1nKatmIkjn2so1d01QraTlMqVSsbx\nNrRFi9wrf+M7Q== [email protected]",
      "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAklOUpkDHrfHY17SbrmTIpNLTGK9Tjom/BWDSU\nGPl+nafzlHDTYW7hdI4yZ5ew18JH4JW9jbhUFrviQzM7xlELEVf4h9lFX5QVkbPppSwg0cda3\nPbv7kOdJ/MTyBlWXFCR+HAo3FXRitBqxiX1nKhXpHAZsMciLq8V6RjsNAQwdsdMFvSlVK/7XA\nt3FaoJoAsncM1Q9x5+3V0Ww68/eIFmb1zuUFljQJKprrX88XypNDvjYNby6vw/Pb0rwert/En\nmZ+AW4OZPnTPI89ZPmVMLuayrD2cE86Z/il8b+gw3r3+1nKatmIkjn2so1d01QraTlMqVSsbx\nNQCPO0ZZEa1== [email protected]"
    ],
    groups: %w(testgroup nfsgroup),
    uid: 9001,
    shell: '/bin/bash',
    comment: 'Test User'
  },
  {
     id: 'mwaddams',
     action: 'remove',
     groups: %w(testgroup nfsgroup)
  }
]

User Key Definitions

  • id: String specifies the username, as well as the data bag object id.
  • password: String specifies the user's password.
  • ssh_keys: Array an array of authorized keys that will be managed by Chef to the user's home directory in $HOME/.ssh/authorized_keys. A key can include an https endpoint that returns a line separated list of keys such as https://github.com/$GITHUB_USERNAME.keys this will retrieve all the keys and add it to the array and can be used with static keys as well as dynamic ones.
  • groups: Array an array of groups that the user will be added to
  • uid: Integer a unique identifier for the user
  • shell: String the user's shell
  • comment:String the GECOS field, generally the User's full name.

Other potential fields (optional):

  • home: String User's home directory. If not assigned, will be set based on platform and username.
  • manage_home: True, False Creates/removes the home directory. Defaults to false.
  • homedir_mode: String, Integer Modifies a user's home directory permissions.
  • no_user_group: True, False Specifies if the user needs to get a group with the name of the users. Defaults to false.
  • action: String Supported actions are one's supported by the user resource. If not specified, the default action is create.
  • ssh_private_key: String manages user's private key generally ~/.ssh/id_*
  • ssh_public_key: String manages user's public key generally ~/.ssh/id_*.pub
  • authorized_keys_file: String a nonstandard location for the authorized_keys file
  • gid: String, Integer Specifies the primary group of a user by the gid number or the group name. If gid is an integer and no primary_group is specified than the gid will be assigned to the username group, if applicable. The group will be created if it doesn't exist.
  • primary_group: String To be used in combination with the gid field when the gid is an integer. Specifying the group name prevents errors where the user is created before their primary group.
  • system: True, False Specifies if a user is a system account. See the -r option of useradd.

Resources Overview

users_manage

The users_manage resource manages users and groups based off the users property or of a data bag search and the specified action(s).

Examples

Creates the sysadmin group and users defined in the users databag.

# Get the users from the data bag
users_from_databag = search('users', '*:*')

users_manage 'sysadmin' do
  group_id 2300
  action [:create]
  users users_from_databag
end

Creates the testgroup group, and users defined in the test_home_dir attribute.

users_manage 'testgroup' do
  group_id 3000
  action [:create]
  users node['test_home_dir']
end

Creates the nfsgroup group, and users defined in the test_home_dir local variable and does not manage nfs home directories.

users_manage 'nfsgroup' do
  group_id 4000
  action [:create]
  users test_home_dir
  manage_nfs_home_dirs false
end

Parameters

  • users Array This is the source of the users. It needs to be an array of hashes, where each hash represents its own user. You can use data bags, attributes or something different here.
  • group_name String name of the group to create, defaults to resource name
  • group_id Integer numeric id of the group to create, default is to allow the OS to pick next
  • cookbook String name of the cookbook that the authorized_keys template should be found in
  • manage_nfs_home_dirs Boolean whether to manage nfs home directories.
  • system True, False Specifies if a group is a system group. See the -r option of groupadd.

Reminder users_manage module will only create the user as long as the user's group (groups array) in the attribute or databag includes the users_manage's group name.

Recipe Overview

Recipes are not directly used. Please include the users_manage resource directly in your cookbook.

Data bag Overview

Reminder You do not have to use data bags, you can also pass the users directly to the resource from a different source as explained above.

Reminder Data bags generally should not be stored in cookbooks, but in a policy repo within your organization. Data bags are useful across cookbooks, not just for a single cookbook.

Use knife to create a data bag for users.

knife data bag create users

Create a user in the data_bag/users/ directory.

An optional password hash can be specified that will be used as the user's password.

The hash can be generated with the following command.

openssl passwd -1 "plaintextpassword"

Note: The ssh_keys attribute below can be either a String or an Array. However, we are recommending the use of an Array.

{
  "id": "bofh",
  "ssh_keys": "ssh-rsa AAAAB3Nz...yhCw== bofh"
}
{
  "id": "bofh",
  "password": "$1$d...HgH0",
  "ssh_keys": [
    "ssh-rsa AAA123...xyz== foo",
    "ssh-rsa AAA456...uvw== bar"
  ],
  "groups": [ "sysadmin", "dba", "devops" ],
  "uid": 2001,
  "shell": "\/bin\/bash",
  "comment": "BOFH"
}

You can pass any action listed in the user resource for Chef via the "action" option. For Example:

Lock a user, johndoe1.

knife data bag edit users johndoe1

And then change the action to "lock":

{
  "id": "johndoe1",
  "groups": ["sysadmin", "dba", "devops"],
  "uid": 2002,
  "action": "lock", // <--
  "comment": "User violated access policy"
}

Remove a user, johndoe1.

knife data bag edit users johndoe1

And then change the action to "remove":

{
  "id": "johndoe1",
  "groups": [ "sysadmin", "dba", "devops" ],
  "uid": 2002,
  "action": "remove", // <--
  "comment": "User quit, retired, or fired."
}
  • Note only user bags with the "action : remove" and a search-able "group" attribute will be purged by the :remove action.
  • As of v2.0.3 you can use the force parameter within the user data bag object for users with action remove. As per user docs this may leave the system in an inconsistent state. For example, a user account will be removed even if the user is logged in. A user's home directory will be removed, even if that directory is shared by multiple users.

If you have different requirements, for example:

  • You want to search a different data bag specific to a role such as mail. You may change the data_bag searched.

    data_bag `mail`
  • You want to search for a different group attribute named postmaster. You may change the search_group attribute. This attribute defaults to the resource name.

    search_group `postmaster`
  • You want to add the users to a security group other than the lightweight resource name. You may change the group_name attribute. This attribute also defaults to the resource name.

    group_name `wheel`

Putting these requirements together our recipe might look like this:

users_manage "postmaster" do
  data_bag "mail"
  group_name "wheel"
  group_id 10
end

Knife supports reading data bags from a file and automatically looks in a directory called +data_bags+ in the current directory. The "bag" should be a directory with JSON files of each item. For the above:

$ mkdir data_bags/users
$EDITOR data_bags/users/bofh.json

Paste the user's public SSH key into the ssh_keys value. Also make sure the uid is unique, and if you're not using bash, that the shell is installed.

The Apache cookbook can set up authentication using OpenIDs, which is set up using the openid key here. See the Chef Software 'apache2' cookbook for more information about this.

Contributors

This project exists thanks to all the people who contribute.

Backers

Thank you to all our backers!

https://opencollective.com/sous-chefs#backers

Sponsors

Support this project by becoming a sponsor. Your logo will show up here with a link to your website.

https://opencollective.com/sous-chefs/sponsor/0/website https://opencollective.com/sous-chefs/sponsor/1/website https://opencollective.com/sous-chefs/sponsor/2/website https://opencollective.com/sous-chefs/sponsor/3/website https://opencollective.com/sous-chefs/sponsor/4/website https://opencollective.com/sous-chefs/sponsor/5/website https://opencollective.com/sous-chefs/sponsor/6/website https://opencollective.com/sous-chefs/sponsor/7/website https://opencollective.com/sous-chefs/sponsor/8/website https://opencollective.com/sous-chefs/sponsor/9/website

More Repositories

1

docker

Development repository for the docker cookbook
Ruby
1,348
star
2

elasticsearch

Development repository for the elasticsearch cookbook
Ruby
882
star
3

aws

Development repository for the aws cookbook
Ruby
594
star
4

nginx

Development repository for the nginx cookbook
Ruby
551
star
5

rvm

Development repository for the rvm cookbook
Ruby
516
star
6

php

Development repository for the php cookbook
HTML
445
star
7

jenkins

Development repository for the jenkins cookbook
Ruby
424
star
8

java

Development repository for the java cookbook
Ruby
397
star
9

postgresql

Development repository for the postgresql cookbook
Ruby
357
star
10

mysql

Development repository for the mysql cookbook
Ruby
336
star
11

ruby_rbenv

Development repository for the ruby_rbenv cookbook
Ruby
332
star
12

redisio

Development repository for the redisio cookbook
HTML
297
star
13

apache2

Development repository for the apache2 cookbook
Ruby
282
star
14

nodejs

Development repository for the nodejs cookbook
Ruby
228
star
15

apt

Development repository for the apt cookbook
Ruby
203
star
16

consul

Development repository for the consul cookbook
Ruby
191
star
17

haproxy

Development repository for the haproxy cookbook
Ruby
156
star
18

graphite

Development repository for the graphite cookbook
Ruby
154
star
19

homebrew

Development repository for the homebrew cookbook
Ruby
151
star
20

nagios

Development repository for the nagios cookbook
Ruby
125
star
21

ruby_build

Development repository for the ruby_build cookbook
Ruby
123
star
22

git

Development repository for the git cookbook
Ruby
123
star
23

logrotate

Development repository for the logrotate cookbook
Ruby
122
star
24

percona

Development repository for the percona cookbook
Ruby
117
star
25

openssh

Development repository for the openssh cookbook
Ruby
114
star
26

powershell

Development repository for the powershell cookbook
Ruby
110
star
27

postfix

Development repository for the postfix cookbook
Ruby
103
star
28

tomcat

Development repository for the tomcat cookbook
Ruby
99
star
29

openvpn

Development repository for the openvpn cookbook
Ruby
98
star
30

ark

Development repository for the ark cookbook
Ruby
98
star
31

line

Development repository for the line cookbook
Ruby
97
star
32

firewall

Development repository for the firewall cookbook
Ruby
95
star
33

yum

Development repository for the yum cookbook
Ruby
95
star
34

kafka

Development repository for the kafka cookbook
Ruby
91
star
35

erlang

Development repository for the erlang cookbook
Ruby
87
star
36

sublimechef

A Sublime Text 2 Package for authoring Chef related files
84
star
37

iis

Development repository for the iis cookbook
Ruby
82
star
38

etcd

Development repository for the etcd cookbook
Ruby
79
star
39

cron

Development repository for the cron cookbook
Ruby
77
star
40

grafana

Development repository for the grafana cookbook
Ruby
76
star
41

chef-splunk

Development repository for the chef-splunk cookbook
Ruby
76
star
42

sc-mongodb

Development repository for the sc-mongodb cookbook
Ruby
75
star
43

certificate

Development repository for the certificate cookbook
Ruby
73
star
44

ntp

Development repository for the ntp cookbook
Ruby
68
star
45

rsyslog

Development repository for the rsyslog cookbook
Ruby
65
star
46

sql_server

Development repository for the sql_server cookbook
Ruby
63
star
47

fail2ban

Development repository for the fail2ban cookbook
Ruby
58
star
48

selinux

Development repository for the selinux cookbook
Ruby
58
star
49

windows_ad

Development repository for the windows_ad cookbook
Ruby
58
star
50

vagrant

Development repository for the vagrant cookbook
Ruby
57
star
51

varnish

Development repository for the varnish cookbook
Ruby
56
star
52

lvm

Development repository for the lvm cookbook
Ruby
56
star
53

perl

Development repository for the perl cookbook
Ruby
52
star
54

golang

Development repository for the golang cookbook
Ruby
50
star
55

memcached

Development repository for the memcached cookbook
Ruby
49
star
56

mariadb

Development repository for the mariadb cookbook
Ruby
48
star
57

hashicorp-vault

Development repository for the hashicorp-vault cookbook
Ruby
46
star
58

rundeck

Development repository for the rundeck cookbook
Ruby
46
star
59

ossec

Development repository for the ossec cookbook
Ruby
44
star
60

ufw

Development repository for the ufw cookbook
Ruby
44
star
61

confluence

Development repository for the confluence cookbook
Ruby
43
star
62

openldap

Development repository for the openldap cookbook
Ruby
42
star
63

nfs

Development repository for the nfs cookbook
Ruby
40
star
64

kubernetes

Development repository for the kubernetes cookbook
Ruby
39
star
65

vim

Development repository for the vim cookbook
Ruby
38
star
66

maven

Development repository for the maven cookbook
Ruby
36
star
67

bind

Development repository for the bind cookbook
Ruby
36
star
68

passenger_apache2

Development repository for the passenger_apache2 cookbook
Ruby
36
star
69

keepalived

Development repository for the keepalived cookbook
Ruby
33
star
70

aptly

Development repository for the aptly cookbook
Ruby
31
star
71

samba

Development repository for the samba cookbook
Ruby
30
star
72

resolver

Development repository for the resolver cookbook
Ruby
28
star
73

squid

Development repository for the squid cookbook
Ruby
28
star
74

snort

Development repository for the snort cookbook
Ruby
27
star
75

pyenv

Development repository for the pyenv cookbook
Ruby
27
star
76

freebsd

Development repository for the freebsd cookbook
Ruby
27
star
77

dhcp

Development repository for the dhcp cookbook
Ruby
27
star
78

nrpe

Development repository for the nrpe cookbook
Ruby
25
star
79

rsync

Development repository for the rsync cookbook
Ruby
24
star
80

github

Development repository for the github cookbook
Ruby
24
star
81

filesystem

Development repository for the filesystem cookbook
Ruby
24
star
82

yum-epel

Development repository for the yum-epel cookbook
Ruby
23
star
83

network_interfaces

Development repository for the network_interfaces cookbook
Ruby
23
star
84

drbd

Development repository for the drbd cookbook
Ruby
19
star
85

djbdns

Development repository for the djbdns cookbook
Ruby
19
star
86

sssd_ldap

Development repository for the sssd_ldap cookbook
Ruby
18
star
87

dpkg_autostart

Development repository for the dpkg_autostart cookbook
Ruby
18
star
88

packagecloud

Development repository for the packagecloud cookbook
Ruby
17
star
89

webpi

Development repository for the webpi cookbook
Ruby
17
star
90

gems

Development repository for the gems cookbook
Ruby
17
star
91

language-chef

Development repository for the language-chef plugin for the Atom text editor
JavaScript
17
star
92

elixir

Development repository for the elixir cookbook
Ruby
17
star
93

htpasswd

Development repository for the htpasswd cookbook
Ruby
15
star
94

stunnel

Development repository for the stunnel cookbook
Ruby
14
star
95

apparmor

Development repository for the apparmor cookbook
Ruby
14
star
96

smartmontools

Development repository for the smartmontools cookbook
HTML
13
star
97

transmission

Development repository for the transmission cookbook
Ruby
13
star
98

gpg

Development repository for the gpg cookbook
Ruby
12
star
99

wix

Development repository for the wix cookbook
Ruby
12
star
100

tftp

Development repository for the tftp cookbook
Ruby
11
star