Saltstack Official Users Formula
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

256 lines
6.8KB

  1. # vim: sts=2 ts=2 sw=2 et ai
  2. {% from "users/map.jinja" import users with context %}
  3. {% set used_sudo = [] %}
  4. {% set used_googleauth = [] %}
  5. {%- for name, user in pillar.get('users', {}).items() if user.absent is not defined or not user.absent %}
  6. {%- if user == None -%}
  7. {%- set user = {} -%}
  8. {%- endif -%}
  9. {%- if 'sudouser' in user and user['sudouser'] %}
  10. {%- do used_sudo.append(1) %}
  11. {%- endif %}
  12. {%- if 'google_auth' in user %}
  13. {%- do used_googleauth.append(1) %}
  14. {%- endif %}
  15. {%- endfor %}
  16. {%- if used_sudo or used_googleauth %}
  17. include:
  18. {%- if used_sudo %}
  19. - users.sudo
  20. {%- endif %}
  21. {%- if used_googleauth %}
  22. - users.googleauth
  23. {%- endif %}
  24. {%- endif %}
  25. {% for name, user in pillar.get('users', {}).items() if user.absent is not defined or not user.absent %}
  26. {%- if user == None -%}
  27. {%- set user = {} -%}
  28. {%- endif -%}
  29. {%- set home = user.get('home', "/home/%s" % name) -%}
  30. {%- if 'prime_group' in user and 'name' in user['prime_group'] %}
  31. {%- set user_group = user.prime_group.name -%}
  32. {%- else -%}
  33. {%- set user_group = name -%}
  34. {%- endif %}
  35. {% for group in user.get('groups', []) %}
  36. {{ name }}_{{ group }}_group:
  37. group:
  38. - name: {{ group }}
  39. - present
  40. {% endfor %}
  41. {{ name }}_user:
  42. {% if user.get('createhome', True) %}
  43. file.directory:
  44. - name: {{ home }}
  45. - user: {{ name }}
  46. - group: {{ user_group }}
  47. - mode: {{ user.get('user_dir_mode', '0750') }}
  48. - require:
  49. - user: {{ name }}
  50. - group: {{ user_group }}
  51. {%- endif %}
  52. group.present:
  53. - name: {{ user_group }}
  54. {%- if 'prime_group' in user and 'gid' in user['prime_group'] %}
  55. - gid: {{ user['prime_group']['gid'] }}
  56. {%- elif 'uid' in user %}
  57. - gid: {{ user['uid'] }}
  58. {%- endif %}
  59. user.present:
  60. - name: {{ name }}
  61. - home: {{ home }}
  62. - shell: {{ user.get('shell', users.get('shell', '/bin/bash')) }}
  63. {% if 'uid' in user -%}
  64. - uid: {{ user['uid'] }}
  65. {% endif -%}
  66. {% if 'password' in user -%}
  67. - password: '{{ user['password'] }}'
  68. {% endif -%}
  69. {% if 'prime_group' in user and 'gid' in user['prime_group'] -%}
  70. - gid: {{ user['prime_group']['gid'] }}
  71. {% else -%}
  72. - gid_from_name: True
  73. {% endif -%}
  74. {% if 'fullname' in user %}
  75. - fullname: {{ user['fullname'] }}
  76. {% endif -%}
  77. {% if not user.get('createhome', True) %}
  78. - createhome: False
  79. {% endif %}
  80. {% if 'expire' in user -%}
  81. - expire: {{ user['expire'] }}
  82. {% endif -%}
  83. - remove_groups: {{ user.get('remove_groups', 'False') }}
  84. - groups:
  85. - {{ user_group }}
  86. {% for group in user.get('groups', []) -%}
  87. - {{ group }}
  88. {% endfor %}
  89. - require:
  90. - group: {{ user_group }}
  91. {% for group in user.get('groups', []) -%}
  92. - group: {{ group }}
  93. {% endfor %}
  94. user_keydir_{{ name }}:
  95. file.directory:
  96. - name: {{ user.get('home', '/home/{0}'.format(name)) }}/.ssh
  97. - user: {{ name }}
  98. - group: {{ user_group }}
  99. - makedirs: True
  100. - mode: 700
  101. - require:
  102. - user: {{ name }}
  103. - group: {{ user_group }}
  104. {%- for group in user.get('groups', []) %}
  105. - group: {{ group }}
  106. {%- endfor %}
  107. {% if 'ssh_keys' in user %}
  108. {% set key_type = 'id_' + user.get('ssh_key_type', 'rsa') %}
  109. user_{{ name }}_private_key:
  110. file.managed:
  111. - name: {{ user.get('home', '/home/{0}'.format(name)) }}/.ssh/{{ key_type }}
  112. - user: {{ name }}
  113. - group: {{ user_group }}
  114. - mode: 600
  115. - show_diff: False
  116. - contents_pillar: users:{{ name }}:ssh_keys:privkey
  117. - require:
  118. - user: {{ name }}_user
  119. {% for group in user.get('groups', []) %}
  120. - group: {{ name }}_{{ group }}_group
  121. {% endfor %}
  122. user_{{ name }}_public_key:
  123. file.managed:
  124. - name: {{ user.get('home', '/home/{0}'.format(name)) }}/.ssh/{{ key_type }}.pub
  125. - user: {{ name }}
  126. - group: {{ user_group }}
  127. - mode: 644
  128. - show_diff: False
  129. - contents_pillar: users:{{ name }}:ssh_keys:pubkey
  130. - require:
  131. - user: {{ name }}_user
  132. {% for group in user.get('groups', []) %}
  133. - group: {{ name }}_{{ group }}_group
  134. {% endfor %}
  135. {% endif %}
  136. {% if 'ssh_auth' in user %}
  137. {% for auth in user['ssh_auth'] %}
  138. ssh_auth_{{ name }}_{{ loop.index0 }}:
  139. ssh_auth.present:
  140. - user: {{ name }}
  141. - name: {{ auth }}
  142. - require:
  143. - file: {{ name }}_user
  144. - user: {{ name }}_user
  145. {% endfor %}
  146. {% endif %}
  147. {% if 'ssh_auth.absent' in user %}
  148. {% for auth in user['ssh_auth.absent'] %}
  149. ssh_auth_delete_{{ name }}_{{ loop.index0 }}:
  150. ssh_auth.absent:
  151. - user: {{ name }}
  152. - name: {{ auth }}
  153. - require:
  154. - file: {{ name }}_user
  155. - user: {{ name }}_user
  156. {% endfor %}
  157. {% endif %}
  158. {% if 'sudouser' in user and user['sudouser'] %}
  159. sudoer-{{ name }}:
  160. file.managed:
  161. - name: {{ users.sudoers_dir }}/{{ name }}
  162. - user: root
  163. - group: {{ users.root_group }}
  164. - mode: '0440'
  165. {% if 'sudo_rules' in user %}
  166. {% for rule in user['sudo_rules'] %}
  167. "validate {{ name }} sudo rule {{ loop.index0 }} {{ name }} {{ rule }}":
  168. cmd.run:
  169. - name: 'visudo -cf - <<<"$rule" | { read output; if [[ $output != "stdin: parsed OK" ]] ; then echo $output ; fi }'
  170. - stateful: True
  171. - shell: {{ users.visudo_shell }}
  172. - env:
  173. # Specify the rule via an env var to avoid shell quoting issues.
  174. - rule: "{{ name }} {{ rule }}"
  175. - require_in:
  176. - file: {{ users.sudoers_dir }}/{{ name }}
  177. {% endfor %}
  178. {{ users.sudoers_dir }}/{{ name }}:
  179. file.managed:
  180. - contents: |
  181. {%- for rule in user['sudo_rules'] %}
  182. {{ name }} {{ rule }}
  183. {%- endfor %}
  184. - require:
  185. - file: sudoer-defaults
  186. - file: sudoer-{{ name }}
  187. {% endif %}
  188. {% else %}
  189. {{ users.sudoers_dir }}/{{ name }}:
  190. file.absent:
  191. - name: {{ users.sudoers_dir }}/{{ name }}
  192. {% endif %}
  193. {%- if 'google_auth' in user %}
  194. {%- for svc in user['google_auth'] %}
  195. googleauth-{{ svc }}-{{ name }}:
  196. file.managed:
  197. - replace: false
  198. - name: {{ users.googleauth_dir }}/{{ name }}_{{ svc }}
  199. - contents_pillar: 'users:{{ name }}:google_auth:{{ svc }}'
  200. - user: root
  201. - group: {{ users.root_group }}
  202. - mode: 600
  203. - require:
  204. - pkg: googleauth-package
  205. {%- endfor %}
  206. {%- endif %}
  207. {% endfor %}
  208. {% for name, user in pillar.get('users', {}).items() if user.absent is defined and user.absent %}
  209. {{ name }}:
  210. {% if 'purge' in user or 'force' in user %}
  211. user.absent:
  212. {% if 'purge' in user %}
  213. - purge: {{ user['purge'] }}
  214. {% endif %}
  215. {% if 'force' in user %}
  216. - force: {{ user['force'] }}
  217. {% endif %}
  218. {% else %}
  219. user.absent
  220. {% endif -%}
  221. {{ users.sudoers_dir }}/{{ name }}:
  222. file.absent:
  223. - name: {{ users.sudoers_dir }}/{{ name }}
  224. {% endfor %}
  225. {% for user in pillar.get('absent_users', []) %}
  226. {{ user }}:
  227. user.absent
  228. {{ users.sudoers_dir }}/{{ user }}:
  229. file.absent:
  230. - name: {{ users.sudoers_dir }}/{{ user }}
  231. {% endfor %}
  232. {% for group in pillar.get('absent_groups', []) %}
  233. {{ group }}:
  234. group.absent
  235. {% endfor %}