0
0
mirror of https://github.com/saltstack-formulas/users-formula.git synced 2024-11-28 07:18:23 +01:00
users-formula/users/init.sls

343 lines
9.7 KiB
Plaintext
Raw Normal View History

# vim: sts=2 ts=2 sw=2 et ai
{% from "users/map.jinja" import users with context %}
{% set used_sudo = [] %}
{% set used_googleauth = [] %}
{%- for name, user in pillar.get('users', {}).items() if user.absent is not defined or not user.absent %}
{%- if user == None -%}
{%- set user = {} -%}
{%- endif -%}
{%- if 'sudouser' in user and user['sudouser'] %}
{%- do used_sudo.append(1) %}
{%- endif %}
{%- if 'google_auth' in user %}
{%- do used_googleauth.append(1) %}
{%- endif %}
{%- endfor %}
{%- if used_sudo or used_googleauth %}
include:
{%- if used_sudo %}
- users.sudo
{%- endif %}
{%- if used_googleauth %}
- users.googleauth
{%- endif %}
{%- endif %}
2013-06-19 21:53:46 +02:00
{% for name, user in pillar.get('users', {}).items() if user.absent is not defined or not user.absent %}
{%- if user == None -%}
{%- set user = {} -%}
{%- endif -%}
{%- set home = user.get('home', "/home/%s" % name) -%}
{%- if 'prime_group' in user and 'name' in user['prime_group'] %}
{%- set user_group = user.prime_group.name -%}
{%- else -%}
{%- set user_group = name -%}
{%- endif %}
2013-06-19 21:53:46 +02:00
{% for group in user.get('groups', []) %}
users_{{ name }}_{{ group }}_group:
2013-06-19 21:53:46 +02:00
group:
- name: {{ group }}
- present
{% endfor %}
users_{{ name }}_user:
{% if user.get('createhome', True) %}
2013-06-19 21:53:46 +02:00
file.directory:
- name: {{ home }}
- user: {{ name }}
- group: {{ user_group }}
- mode: {{ user.get('user_dir_mode', '0750') }}
2013-06-19 21:53:46 +02:00
- require:
- user: {{ name }}
- group: {{ user_group }}
{%- endif %}
2013-06-19 21:53:46 +02:00
group.present:
- name: {{ user_group }}
{%- if 'prime_group' in user and 'gid' in user['prime_group'] %}
- gid: {{ user['prime_group']['gid'] }}
{%- elif 'uid' in user %}
- gid: {{ user['uid'] }}
{%- endif %}
2013-06-19 21:53:46 +02:00
user.present:
- name: {{ name }}
- home: {{ home }}
- shell: {{ user.get('shell', users.get('shell', '/bin/bash')) }}
2013-06-19 21:53:46 +02:00
{% if 'uid' in user -%}
- uid: {{ user['uid'] }}
{% endif -%}
2014-04-25 04:45:04 +02:00
{% if 'password' in user -%}
- password: '{{ user['password'] }}'
2014-04-25 04:45:04 +02:00
{% endif -%}
{% if 'prime_group' in user and 'gid' in user['prime_group'] -%}
- gid: {{ user['prime_group']['gid'] }}
{% else -%}
2013-06-19 21:53:46 +02:00
- gid_from_name: True
{% endif -%}
2013-06-19 21:53:46 +02:00
{% if 'fullname' in user %}
- fullname: {{ user['fullname'] }}
{% endif -%}
{% if not user.get('createhome', True) %}
- createhome: False
{% endif %}
2014-12-22 14:22:46 +01:00
{% if 'expire' in user -%}
- expire: {{ user['expire'] }}
{% endif -%}
- remove_groups: {{ user.get('remove_groups', 'False') }}
2013-06-19 21:53:46 +02:00
- groups:
- {{ user_group }}
{% for group in user.get('groups', []) -%}
- {{ group }}
2013-06-19 21:53:46 +02:00
{% endfor %}
- require:
- group: {{ user_group }}
{% for group in user.get('groups', []) -%}
- group: {{ group }}
2013-06-19 21:53:46 +02:00
{% endfor %}
users_user_keydir_{{ name }}:
2013-06-19 21:53:46 +02:00
file.directory:
- name: {{ user.get('home', '/home/{0}'.format(name)) }}/.ssh
- user: {{ name }}
- group: {{ user_group }}
2013-06-19 21:53:46 +02:00
- makedirs: True
2014-02-14 14:13:17 +01:00
- mode: 700
2013-06-19 21:53:46 +02:00
- require:
- user: {{ name }}
- group: {{ user_group }}
{%- for group in user.get('groups', []) %}
2013-06-19 21:53:46 +02:00
- group: {{ group }}
{%- endfor %}
2013-06-19 21:53:46 +02:00
{% if 'ssh_keys' in user %}
{% set key_type = 'id_' + user.get('ssh_key_type', 'rsa') %}
users_user_{{ name }}_private_key:
2013-06-19 21:53:46 +02:00
file.managed:
- name: {{ user.get('home', '/home/{0}'.format(name)) }}/.ssh/{{ key_type }}
2013-06-19 21:53:46 +02:00
- user: {{ name }}
- group: {{ user_group }}
2013-06-19 21:53:46 +02:00
- mode: 600
- show_diff: False
- contents_pillar: users:{{ name }}:ssh_keys:privkey
2013-06-19 21:53:46 +02:00
- require:
- user: users_{{ name }}_user
2013-06-19 21:53:46 +02:00
{% for group in user.get('groups', []) %}
- group: users_{{ name }}_{{ group }}_group
2013-06-19 21:53:46 +02:00
{% endfor %}
users_user_{{ name }}_public_key:
2013-06-19 21:53:46 +02:00
file.managed:
- name: {{ user.get('home', '/home/{0}'.format(name)) }}/.ssh/{{ key_type }}.pub
2013-06-19 21:53:46 +02:00
- user: {{ name }}
- group: {{ user_group }}
2013-06-19 21:53:46 +02:00
- mode: 644
- show_diff: False
2014-03-30 17:00:17 +02:00
- contents_pillar: users:{{ name }}:ssh_keys:pubkey
2013-06-19 21:53:46 +02:00
- require:
- user: users_{{ name }}_user
2013-06-19 21:53:46 +02:00
{% for group in user.get('groups', []) %}
- group: users_{{ name }}_{{ group }}_group
2013-06-19 21:53:46 +02:00
{% endfor %}
{% endif %}
{% if 'ssh_auth_file' in user %}
users_authorized_keys_{{ name }}:
file.managed:
- name: {{ home }}/.ssh/authorized_keys
- user: {{ name }}
- group: {{ name }}
- mode: 600
- contents: |
{% for auth in user.ssh_auth_file -%}
{{ auth }}
{% endfor -%}
{% endif %}
2013-06-19 21:53:46 +02:00
{% if 'ssh_auth' in user %}
{% for auth in user['ssh_auth'] %}
users_ssh_auth_{{ name }}_{{ loop.index0 }}:
2013-06-19 21:53:46 +02:00
ssh_auth.present:
- user: {{ name }}
- name: {{ auth }}
- require:
- file: users_{{ name }}_user
- user: users_{{ name }}_user
2013-06-19 21:53:46 +02:00
{% endfor %}
{% endif %}
{% if 'ssh_keys_pillar' in user %}
{% for key_name, pillar_name in user['ssh_keys_pillar'].iteritems() %}
users_ssh_keys_files_{{ name }}_{{ key_name }}_pub:
file.managed:
- name: {{ user.get('home', '/home/{0}'.format(name)) }}/.ssh/{{ key_name
}}.pub
- contents: |
{{ pillar[pillar_name][key_name]['pubkey'] }}
users_ssh_keys_files_{{ name }}_{{ key_name }}_priv:
file.managed:
- name: {{ user.get('home', '/home/{0}'.format(name)) }}/.ssh/{{ key_name
}}
- contents: |
{{ pillar[pillar_name][key_name]['privkey'] | indent(8) }}
{% endfor %}
{% endif %}
{% if 'ssh_auth_sources' in user %}
{% for pubkey_file in user['ssh_auth_sources'] %}
users_ssh_auth_source_{{ name }}_{{ loop.index0 }}:
ssh_auth.present:
- user: {{ name }}
- source: {{ pubkey_file }}
- require:
- file: users_{{ name }}_user
- user: users_{{ name }}_user
{% endfor %}
{% endif %}
{% if 'ssh_auth.absent' in user %}
{% for auth in user['ssh_auth.absent'] %}
users_ssh_auth_delete_{{ name }}_{{ loop.index0 }}:
ssh_auth.absent:
- user: {{ name }}
- name: {{ auth }}
- require:
- file: users_{{ name }}_user
- user: users_{{ name }}_user
{% endfor %}
{% endif %}
2013-06-19 21:53:46 +02:00
{% if 'ssh_config' in user %}
users_ssh_config_{{ name }}:
file.managed:
- name: {{ home }}/.ssh/config
- user: {{ name }}
- group: {{ user_group }}
- mode: 640
- contents: |
# Managed by Saltstack
2015-05-16 09:56:20 +02:00
# Do Not Edit
{% for label, setting in user.ssh_config.items() %}
# {{ label }}
Host {{ setting.get('hostname') }}
{%- for opts in setting.get('options') %}
{{ opts }}
{%- endfor %}
{% endfor -%}
{% endif %}
{% if 'sudouser' in user and user['sudouser'] %}
users_sudoer-{{ name }}:
file.managed:
2014-10-25 04:57:44 +02:00
- name: {{ users.sudoers_dir }}/{{ name }}
- user: root
- group: {{ users.root_group }}
- mode: '0440'
{% if 'sudo_rules' in user or 'sudo_defaults' in user %}
{% if 'sudo_rules' in user %}
{% for rule in user['sudo_rules'] %}
"validate {{ name }} sudo rule {{ loop.index0 }} {{ name }} {{ rule }}":
cmd.run:
- name: 'visudo -cf - <<<"$rule" | { read output; if [[ $output != "stdin: parsed OK" ]] ; then echo $output ; fi }'
- stateful: True
- shell: {{ users.visudo_shell }}
- env:
# Specify the rule via an env var to avoid shell quoting issues.
- rule: "{{ name }} {{ rule }}"
- require_in:
- file: users_{{ users.sudoers_dir }}/{{ name }}
{% endfor %}
{% endif %}
{% if 'sudo_defaults' in user %}
{% for entry in user['sudo_defaults'] %}
"validate {{ name }} sudo Defaults {{ loop.index0 }} {{ name }} {{ entry }}":
cmd.run:
- name: 'visudo -cf - <<<"$rule" | { read output; if [[ $output != "stdin: parsed OK" ]] ; then echo $output ; fi }'
- stateful: True
- shell: {{ users.visudo_shell }}
- env:
# Specify the rule via an env var to avoid shell quoting issues.
- rule: "Defaults:{{ name }} {{ entry }}"
- require_in:
- file: users_{{ users.sudoers_dir }}/{{ name }}
{% endfor %}
{% endif %}
users_{{ users.sudoers_dir }}/{{ name }}:
file.managed:
- name: {{ users.sudoers_dir }}/{{ name }}
- contents: |
{%- if 'sudo_defaults' in user %}
{%- for entry in user['sudo_defaults'] %}
Defaults:{{ name }} {{ entry }}
{%- endfor %}
{%- endif %}
{%- if 'sudo_rules' in user %}
{%- for rule in user['sudo_rules'] %}
{{ name }} {{ rule }}
{%- endfor %}
{%- endif %}
- require:
- file: users_sudoer-defaults
- file: users_sudoer-{{ name }}
{% endif %}
{% else %}
users_{{ users.sudoers_dir }}/{{ name }}:
file.absent:
2014-10-25 04:57:44 +02:00
- name: {{ users.sudoers_dir }}/{{ name }}
2013-06-19 21:53:46 +02:00
{% endif %}
{%- if 'google_auth' in user %}
{%- for svc in user['google_auth'] %}
users_googleauth-{{ svc }}-{{ name }}:
file.managed:
- replace: false
- name: {{ users.googleauth_dir }}/{{ name }}_{{ svc }}
- contents_pillar: 'users:{{ name }}:google_auth:{{ svc }}'
- user: root
- group: {{ users.root_group }}
- mode: 600
- require:
- pkg: users_googleauth-package
{%- endfor %}
{%- endif %}
2013-06-19 21:53:46 +02:00
{% endfor %}
{% for name, user in pillar.get('users', {}).items() if user.absent is defined and user.absent %}
users_absent_user_{{ name }}:
{% if 'purge' in user or 'force' in user %}
user.absent:
- name: {{ name }}
{% if 'purge' in user %}
- purge: {{ user['purge'] }}
{% endif %}
{% if 'force' in user %}
- force: {{ user['force'] }}
{% endif %}
{% else %}
user.absent:
- name: {{ name }}
{% endif -%}
users_{{ users.sudoers_dir }}/{{ name }}:
file.absent:
2014-10-25 04:57:44 +02:00
- name: {{ users.sudoers_dir }}/{{ name }}
{% endfor %}
2013-06-19 21:53:46 +02:00
{% for user in pillar.get('absent_users', []) %}
users_absent_user_2_{{ user }}:
2013-06-19 21:53:46 +02:00
user.absent
users_2_{{ users.sudoers_dir }}/{{ user }}:
file.absent:
2014-10-25 04:57:44 +02:00
- name: {{ users.sudoers_dir }}/{{ user }}
2013-06-19 21:53:46 +02:00
{% endfor %}
{% for group in pillar.get('absent_groups', []) %}
users_absent_group_{{ group }}:
group.absent:
- name: {{ group }}
{% endfor %}