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.

324 lines
9.2KB

  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. users_{{ name }}_{{ group }}_group:
  37. group:
  38. - name: {{ group }}
  39. - present
  40. {% endfor %}
  41. users_{{ 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. users_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. users_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: users_{{ name }}_user
  119. {% for group in user.get('groups', []) %}
  120. - group: users_{{ name }}_{{ group }}_group
  121. {% endfor %}
  122. users_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: users_{{ name }}_user
  132. {% for group in user.get('groups', []) %}
  133. - group: users_{{ name }}_{{ group }}_group
  134. {% endfor %}
  135. {% endif %}
  136. {% if 'ssh_auth_file' in user %}
  137. users_authorized_keys_{{ name }}:
  138. file.managed:
  139. - name: {{ home }}/.ssh/authorized_keys
  140. - user: {{ name }}
  141. - group: {{ name }}
  142. - mode: 600
  143. - contents: |
  144. {% for auth in user.ssh_auth_file -%}
  145. {{ auth }}
  146. {% endfor -%}
  147. {% endif %}
  148. {% if 'ssh_auth' in user %}
  149. {% for auth in user['ssh_auth'] %}
  150. users_ssh_auth_{{ name }}_{{ loop.index0 }}:
  151. ssh_auth.present:
  152. - user: {{ name }}
  153. - name: {{ auth }}
  154. - require:
  155. - file: users_{{ name }}_user
  156. - user: users_{{ name }}_user
  157. {% endfor %}
  158. {% endif %}
  159. {% if 'ssh_keys_pillar' in user %}
  160. {% for key_name, pillar_name in user['ssh_keys_pillar'].iteritems() %}
  161. users_ssh_keys_files_{{ name }}_{{ key_name }}_pub:
  162. file.managed:
  163. - name: {{ user.get('home', '/home/{0}'.format(name)) }}/.ssh/{{ key_name
  164. }}.pub
  165. - contents: |
  166. {{ pillar[pillar_name][key_name]['pubkey'] }}
  167. users_ssh_keys_files_{{ name }}_{{ key_name }}_priv:
  168. file.managed:
  169. - name: {{ user.get('home', '/home/{0}'.format(name)) }}/.ssh/{{ key_name
  170. }}
  171. - contents: |
  172. {{ pillar[pillar_name][key_name]['privkey'] | indent(8) }}
  173. {% endfor %}
  174. {% endif %}
  175. {% if 'ssh_auth_sources' in user %}
  176. {% for pubkey_file in user['ssh_auth_sources'] %}
  177. users_ssh_auth_source_{{ name }}_{{ loop.index0 }}:
  178. ssh_auth.present:
  179. - user: {{ name }}
  180. - source: {{ pubkey_file }}
  181. - require:
  182. - file: users_{{ name }}_user
  183. - user: users_{{ name }}_user
  184. {% endfor %}
  185. {% endif %}
  186. {% if 'ssh_auth.absent' in user %}
  187. {% for auth in user['ssh_auth.absent'] %}
  188. users_ssh_auth_delete_{{ name }}_{{ loop.index0 }}:
  189. ssh_auth.absent:
  190. - user: {{ name }}
  191. - name: {{ auth }}
  192. - require:
  193. - file: users_{{ name }}_user
  194. - user: users_{{ name }}_user
  195. {% endfor %}
  196. {% endif %}
  197. {% if 'sudouser' in user and user['sudouser'] %}
  198. users_sudoer-{{ name }}:
  199. file.managed:
  200. - name: {{ users.sudoers_dir }}/{{ name }}
  201. - user: root
  202. - group: {{ users.root_group }}
  203. - mode: '0440'
  204. {% if 'sudo_rules' in user or 'sudo_defaults' in user %}
  205. {% if 'sudo_rules' in user %}
  206. {% for rule in user['sudo_rules'] %}
  207. "validate {{ name }} sudo rule {{ loop.index0 }} {{ name }} {{ rule }}":
  208. cmd.run:
  209. - name: 'visudo -cf - <<<"$rule" | { read output; if [[ $output != "stdin: parsed OK" ]] ; then echo $output ; fi }'
  210. - stateful: True
  211. - shell: {{ users.visudo_shell }}
  212. - env:
  213. # Specify the rule via an env var to avoid shell quoting issues.
  214. - rule: "{{ name }} {{ rule }}"
  215. - require_in:
  216. - file: users_{{ users.sudoers_dir }}/{{ name }}
  217. {% endfor %}
  218. {% endif %}
  219. {% if 'sudo_defaults' in user %}
  220. {% for entry in user['sudo_defaults'] %}
  221. "validate {{ name }} sudo Defaults {{ loop.index0 }} {{ name }} {{ entry }}":
  222. cmd.run:
  223. - name: 'visudo -cf - <<<"$rule" | { read output; if [[ $output != "stdin: parsed OK" ]] ; then echo $output ; fi }'
  224. - stateful: True
  225. - shell: {{ users.visudo_shell }}
  226. - env:
  227. # Specify the rule via an env var to avoid shell quoting issues.
  228. - rule: "Defaults:{{ name }} {{ entry }}"
  229. - require_in:
  230. - file: users_{{ users.sudoers_dir }}/{{ name }}
  231. {% endfor %}
  232. {% endif %}
  233. users_{{ users.sudoers_dir }}/{{ name }}:
  234. file.managed:
  235. - name: {{ users.sudoers_dir }}/{{ name }}
  236. - contents: |
  237. {%- if 'sudo_defaults' in user %}
  238. {%- for entry in user['sudo_defaults'] %}
  239. Defaults:{{ name }} {{ entry }}
  240. {%- endfor %}
  241. {%- endif %}
  242. {%- if 'sudo_rules' in user %}
  243. {%- for rule in user['sudo_rules'] %}
  244. {{ name }} {{ rule }}
  245. {%- endfor %}
  246. {%- endif %}
  247. - require:
  248. - file: users_sudoer-defaults
  249. - file: users_sudoer-{{ name }}
  250. {% endif %}
  251. {% else %}
  252. users_{{ users.sudoers_dir }}/{{ name }}:
  253. file.absent:
  254. - name: {{ users.sudoers_dir }}/{{ name }}
  255. {% endif %}
  256. {%- if 'google_auth' in user %}
  257. {%- for svc in user['google_auth'] %}
  258. users_googleauth-{{ svc }}-{{ name }}:
  259. file.managed:
  260. - replace: false
  261. - name: {{ users.googleauth_dir }}/{{ name }}_{{ svc }}
  262. - contents_pillar: 'users:{{ name }}:google_auth:{{ svc }}'
  263. - user: root
  264. - group: {{ users.root_group }}
  265. - mode: 600
  266. - require:
  267. - pkg: users_googleauth-package
  268. {%- endfor %}
  269. {%- endif %}
  270. {% endfor %}
  271. {% for name, user in pillar.get('users', {}).items() if user.absent is defined and user.absent %}
  272. users_absent_user_{{ name }}:
  273. {% if 'purge' in user or 'force' in user %}
  274. user.absent:
  275. - name: {{ name }}
  276. {% if 'purge' in user %}
  277. - purge: {{ user['purge'] }}
  278. {% endif %}
  279. {% if 'force' in user %}
  280. - force: {{ user['force'] }}
  281. {% endif %}
  282. {% else %}
  283. user.absent:
  284. - name: {{ name }}
  285. {% endif -%}
  286. users_{{ users.sudoers_dir }}/{{ name }}:
  287. file.absent:
  288. - name: {{ users.sudoers_dir }}/{{ name }}
  289. {% endfor %}
  290. {% for user in pillar.get('absent_users', []) %}
  291. users_absent_user_2_{{ user }}:
  292. user.absent
  293. users_2_{{ users.sudoers_dir }}/{{ user }}:
  294. file.absent:
  295. - name: {{ users.sudoers_dir }}/{{ user }}
  296. {% endfor %}
  297. {% for group in pillar.get('absent_groups', []) %}
  298. users_absent_group_{{ group }}:
  299. group.absent:
  300. - name: {{ group }}
  301. {% endfor %}