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.

482 lines
14KB

  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. {% set used_user_files = [] %}
  6. {%- for name, user in pillar.get('users', {}).items()
  7. if user.absent is not defined or not user.absent %}
  8. {%- if user == None -%}
  9. {%- set user = {} -%}
  10. {%- endif -%}
  11. {%- if 'sudouser' in user and user['sudouser'] %}
  12. {%- do used_sudo.append(1) %}
  13. {%- endif %}
  14. {%- if 'google_auth' in user %}
  15. {%- do used_googleauth.append(1) %}
  16. {%- endif %}
  17. {%- if salt['pillar.get']('users:' ~ name ~ ':user_files:enabled', False) %}
  18. {%- do used_user_files.append(1) %}
  19. {%- endif %}
  20. {%- endfor %}
  21. {%- if used_sudo or used_googleauth or used_user_files %}
  22. include:
  23. {%- if used_sudo %}
  24. - users.sudo
  25. {%- endif %}
  26. {%- if used_googleauth %}
  27. - users.googleauth
  28. {%- endif %}
  29. {%- if used_user_files %}
  30. - users.user_files
  31. {%- endif %}
  32. {%- endif %}
  33. {% for name, user in pillar.get('users', {}).items()
  34. if user.absent is not defined or not user.absent %}
  35. {%- if user == None -%}
  36. {%- set user = {} -%}
  37. {%- endif -%}
  38. {%- set home = user.get('home', "/home/%s" % name) -%}
  39. {%- if 'prime_group' in user and 'name' in user['prime_group'] %}
  40. {%- set user_group = user.prime_group.name -%}
  41. {%- else -%}
  42. {%- set user_group = name -%}
  43. {%- endif %}
  44. {% for group in user.get('groups', []) %}
  45. users_{{ name }}_{{ group }}_group:
  46. group.present:
  47. - name: {{ group }}
  48. {% if group == 'sudo' %}
  49. - system: True
  50. {% endif %}
  51. {% endfor %}
  52. users_{{ name }}_user:
  53. {% if user.get('createhome', True) %}
  54. file.directory:
  55. - name: {{ home }}
  56. - user: {{ name }}
  57. - group: {{ user_group }}
  58. - mode: {{ user.get('user_dir_mode', '0750') }}
  59. - require:
  60. - user: users_{{ name }}_user
  61. - group: {{ user_group }}
  62. {%- endif %}
  63. group.present:
  64. - name: {{ user_group }}
  65. {%- if 'prime_group' in user and 'gid' in user['prime_group'] %}
  66. - gid: {{ user['prime_group']['gid'] }}
  67. {%- elif 'uid' in user %}
  68. - gid: {{ user['uid'] }}
  69. {%- endif %}
  70. {% if 'system' in user and user['system'] %}
  71. - system: True
  72. {% endif %}
  73. user.present:
  74. - name: {{ name }}
  75. - home: {{ home }}
  76. - shell: {{ user.get('shell', users.get('shell', '/bin/bash')) }}
  77. {% if 'uid' in user -%}
  78. - uid: {{ user['uid'] }}
  79. {% endif -%}
  80. {% if 'password' in user -%}
  81. - password: '{{ user['password'] }}'
  82. {% endif -%}
  83. {% if user.get('empty_password') -%}
  84. - empty_password: {{ user.get('empty_password') }}
  85. {% endif -%}
  86. {% if 'enforce_password' in user -%}
  87. - enforce_password: {{ user['enforce_password'] }}
  88. {% endif -%}
  89. {% if user.get('system', False) -%}
  90. - system: True
  91. {% endif -%}
  92. {% if 'prime_group' in user and 'gid' in user['prime_group'] -%}
  93. - gid: {{ user['prime_group']['gid'] }}
  94. {% else -%}
  95. - gid_from_name: True
  96. {% endif -%}
  97. {% if 'fullname' in user %}
  98. - fullname: {{ user['fullname'] }}
  99. {% endif -%}
  100. {% if 'roomnumber' in user %}
  101. - roomnumber: {{ user['roomnumber'] }}
  102. {% endif %}
  103. {% if 'workphone' in user %}
  104. - workphone: {{ user['workphone'] }}
  105. {% endif %}
  106. {% if 'homephone' in user %}
  107. - homephone: {{ user['workphone'] }}
  108. {% endif %}
  109. {% if not user.get('createhome', True) %}
  110. - createhome: False
  111. {% endif %}
  112. {% if 'expire' in user -%}
  113. {% if grains['kernel'].endswith('BSD') and
  114. user['expire'] < 157766400 %}
  115. {# 157762800s since epoch equals 01 Jan 1975 00:00:00 UTC #}
  116. - expire: {{ user['expire'] * 86400 }}
  117. {% elif grains['kernel'] == 'Linux' and
  118. user['expire'] > 84006 %}
  119. {# 2932896 days since epoch equals 9999-12-31 #}
  120. - expire: {{ (user['expire'] / 86400) | int}}
  121. {% else %}
  122. - expire: {{ user['expire'] }}
  123. {% endif %}
  124. {% endif -%}
  125. - remove_groups: {{ user.get('remove_groups', 'False') }}
  126. - groups:
  127. - {{ user_group }}
  128. {% for group in user.get('groups', []) -%}
  129. - {{ group }}
  130. {% endfor %}
  131. - require:
  132. - group: {{ user_group }}
  133. {% for group in user.get('groups', []) -%}
  134. - group: {{ group }}
  135. {% endfor %}
  136. {% if 'ssh_keys' in user or
  137. 'ssh_auth' in user or
  138. 'ssh_auth_file' in user or
  139. 'ssh_auth_pillar' in user or
  140. 'ssh_auth.absent' in user or
  141. 'ssh_config' in user %}
  142. user_keydir_{{ name }}:
  143. file.directory:
  144. - name: {{ user.get('home', '/home/{0}'.format(name)) }}/.ssh
  145. - user: {{ name }}
  146. - group: {{ user_group }}
  147. - makedirs: True
  148. - mode: 700
  149. - require:
  150. - user: {{ name }}
  151. - group: {{ user_group }}
  152. {%- for group in user.get('groups', []) %}
  153. - group: {{ group }}
  154. {%- endfor %}
  155. {% endif %}
  156. {% if 'ssh_keys' in user %}
  157. {% set key_type = 'id_' + user.get('ssh_key_type', 'rsa') %}
  158. users_user_{{ name }}_private_key:
  159. file.managed:
  160. - name: {{ user.get('home',
  161. '/home/{0}'.format(name)) }}/.ssh/{{ key_type }}
  162. - user: {{ name }}
  163. - group: {{ user_group }}
  164. - mode: 600
  165. - show_diff: False
  166. - contents_pillar: users:{{ name }}:ssh_keys:privkey
  167. - require:
  168. - user: users_{{ name }}_user
  169. {% for group in user.get('groups', []) %}
  170. - group: users_{{ name }}_{{ group }}_group
  171. {% endfor %}
  172. users_user_{{ name }}_public_key:
  173. file.managed:
  174. - name: {{ user.get('home',
  175. '/home/{0}'.format(name)) }}/.ssh/{{ key_type }}.pub
  176. - user: {{ name }}
  177. - group: {{ user_group }}
  178. - mode: 644
  179. - show_diff: False
  180. - contents_pillar: users:{{ name }}:ssh_keys:pubkey
  181. - require:
  182. - user: users_{{ name }}_user
  183. {% for group in user.get('groups', []) %}
  184. - group: users_{{ name }}_{{ group }}_group
  185. {% endfor %}
  186. {% endif %}
  187. {% if 'ssh_auth_file' in user or 'ssh_auth_pillar' in user %}
  188. users_authorized_keys_{{ name }}:
  189. file.managed:
  190. - name: {{ home }}/.ssh/authorized_keys
  191. - user: {{ name }}
  192. - group: {{ user_group }}
  193. - mode: 600
  194. {% if 'ssh_auth_file' in user %}
  195. - contents: |
  196. {% for auth in user.ssh_auth_file -%}
  197. {{ auth }}
  198. {% endfor -%}
  199. {% else %}
  200. {%- for key_name, pillar_name in user['ssh_auth_pillar'].items() %}
  201. - contents_pillar: {{ pillar_name }}:{{ key_name }}:pubkey
  202. {%- endfor %}
  203. {% endif %}
  204. {% endif %}
  205. {% if 'ssh_auth' in user %}
  206. {% for auth in user['ssh_auth'] %}
  207. users_ssh_auth_{{ name }}_{{ loop.index0 }}:
  208. ssh_auth.present:
  209. - user: {{ name }}
  210. - name: {{ auth }}
  211. - require:
  212. - file: user_keydir_{{ name }}
  213. - user: users_{{ name }}_user
  214. {% endfor %}
  215. {% endif %}
  216. {% if 'ssh_keys_pillar' in user %}
  217. {% for key_name, pillar_name in user['ssh_keys_pillar'].items() %}
  218. user_ssh_keys_files_{{ name }}_{{ key_name }}_private_key:
  219. file.managed:
  220. - name: {{ user.get('home',
  221. '/home/{0}'.format(name)) }}/.ssh/{{ key_name }}
  222. - user: {{ name }}
  223. - group: {{ user_group }}
  224. - mode: 600
  225. - show_diff: False
  226. - contents_pillar: {{ pillar_name }}:{{ key_name }}:privkey
  227. - require:
  228. - user: users_{{ name }}_user
  229. {% for group in user.get('groups', []) %}
  230. - group: users_{{ name }}_{{ group }}_group
  231. {% endfor %}
  232. user_ssh_keys_files_{{ name }}_{{ key_name }}_public_key:
  233. file.managed:
  234. - name: {{ user.get('home',
  235. '/home/{0}'.format(name)) }}/.ssh/{{ key_name }}.pub
  236. - user: {{ name }}
  237. - group: {{ user_group }}
  238. - mode: 644
  239. - show_diff: False
  240. - contents_pillar: {{ pillar_name }}:{{ key_name }}:pubkey
  241. - require:
  242. - user: users_{{ name }}_user
  243. {% for group in user.get('groups', []) %}
  244. - group: users_{{ name }}_{{ group }}_group
  245. {% endfor %}
  246. {% endfor %}
  247. {% endif %}
  248. {% if 'ssh_auth_sources' in user %}
  249. {% for pubkey_file in user['ssh_auth_sources'] %}
  250. users_ssh_auth_source_{{ name }}_{{ loop.index0 }}:
  251. ssh_auth.present:
  252. - user: {{ name }}
  253. - source: {{ pubkey_file }}
  254. - require:
  255. - file: users_{{ name }}_user
  256. - user: users_{{ name }}_user
  257. {% endfor %}
  258. {% endif %}
  259. {% if 'ssh_auth.absent' in user %}
  260. {% for auth in user['ssh_auth.absent'] %}
  261. users_ssh_auth_delete_{{ name }}_{{ loop.index0 }}:
  262. ssh_auth.absent:
  263. - user: {{ name }}
  264. - name: {{ auth }}
  265. - require:
  266. - file: users_{{ name }}_user
  267. - user: users_{{ name }}_user
  268. {% endfor %}
  269. {% endif %}
  270. {% if 'ssh_config' in user %}
  271. users_ssh_config_{{ name }}:
  272. file.managed:
  273. - name: {{ home }}/.ssh/config
  274. - user: {{ name }}
  275. - group: {{ user_group }}
  276. - mode: 640
  277. - contents: |
  278. # Managed by Saltstack
  279. # Do Not Edit
  280. {% for label, setting in user.ssh_config.items() %}
  281. # {{ label }}
  282. Host {{ setting.get('hostname') }}
  283. {%- for opts in setting.get('options') %}
  284. {{ opts }}
  285. {%- endfor %}
  286. {% endfor -%}
  287. {% endif %}
  288. {% if 'ssh_known_hosts' in user %}
  289. {% for hostname, host in user['ssh_known_hosts'].items() %}
  290. users_ssh_known_hosts_{{ name }}_{{ loop.index0 }}:
  291. ssh_known_hosts.present:
  292. - user: {{ name }}
  293. - name: {{ hostname }}
  294. {% if 'port' in host %}
  295. - port: {{ host['port'] }}
  296. {% endif -%}
  297. {% if 'fingerprint' in host %}
  298. - fingerprint: {{ host['fingerprint'] }}
  299. {% endif -%}
  300. {% if 'key' in host %}
  301. - key: {{ host['key'] }}
  302. {% endif -%}
  303. {% if 'enc' in host %}
  304. - enc: {{ host['enc'] }}
  305. {% endif -%}
  306. {% if 'hash_hostname' in host %}
  307. - hash_hostname: {{ host['hash_hostname'] }}
  308. {% endif -%}
  309. {% endfor %}
  310. {% endif %}
  311. {% if 'ssh_known_hosts.absent' in user %}
  312. {% for host in user['ssh_known_hosts.absent'] %}
  313. users_ssh_known_hosts_delete_{{ name }}_{{ loop.index0 }}:
  314. ssh_known_hosts.absent:
  315. - user: {{ name }}
  316. - name: {{ host }}
  317. {% endfor %}
  318. {% endif %}
  319. {% if 'sudouser' in user and user['sudouser'] %}
  320. users_sudoer-{{ name }}:
  321. file.managed:
  322. - name: {{ users.sudoers_dir }}/{{ name }}
  323. - user: root
  324. - group: {{ users.root_group }}
  325. - mode: '0440'
  326. {% if 'sudo_rules' in user or 'sudo_defaults' in user %}
  327. #{#%
  328. {% if 'sudo_rules' in user %}
  329. {% for rule in user['sudo_rules'] %}
  330. "validate {{ name }} sudo rule {{ loop.index0 }} {{ name }} {{ rule }}":
  331. cmd.run:
  332. - name: 'visudo -cf - <<<"$rule" | { read output; if [[ $output != "stdin: parsed OK" ]] ; then echo $output ; fi }'
  333. - stateful: True
  334. - shell: {{ users.visudo_shell }}
  335. - env:
  336. # Specify the rule via an env var to avoid shell quoting issues.
  337. - rule: "{{ name }} {{ rule }}"
  338. - require_in:
  339. - file: users_{{ users.sudoers_dir }}/{{ name }}
  340. {% endfor %}
  341. {% endif %}
  342. {% if 'sudo_defaults' in user %}
  343. {% for entry in user['sudo_defaults'] %}
  344. "validate {{ name }} sudo Defaults {{ loop.index0 }} {{ name }} {{ entry }}":
  345. cmd.run:
  346. - name: 'visudo -cf - <<<"$rule" | { read output; if [[ $output != "stdin: parsed OK" ]] ; then echo $output ; fi }'
  347. - stateful: True
  348. - shell: {{ users.visudo_shell }}
  349. - env:
  350. # Specify the rule via an env var to avoid shell quoting issues.
  351. - rule: "Defaults:{{ name }} {{ entry }}"
  352. - require_in:
  353. - file: users_{{ users.sudoers_dir }}/{{ name }}
  354. {% endfor %}
  355. {% endif %}
  356. #%#}
  357. users_{{ users.sudoers_dir }}/{{ name }}:
  358. file.managed:
  359. - name: {{ users.sudoers_dir }}/{{ name }}
  360. - contents: |
  361. {%- if 'sudo_defaults' in user %}
  362. {%- for entry in user['sudo_defaults'] %}
  363. Defaults:{{ name }} {{ entry }}
  364. {%- endfor %}
  365. {%- endif %}
  366. {%- if 'sudo_rules' in user %}
  367. {%- for rule in user['sudo_rules'] %}
  368. {{ name }} {{ rule }}
  369. {%- endfor %}
  370. {%- endif %}
  371. - require:
  372. - file: users_sudoer-defaults
  373. - file: users_sudoer-{{ name }}
  374. cmd.wait:
  375. - name: visudo -cf {{ users.sudoers_dir }}/{{ name }} || ( rm -rvf {{ users.sudoers_dir }}/{{ name }}; exit 1 )
  376. - watch:
  377. - file: {{ users.sudoers_dir }}/{{ name }}
  378. {% endif %}
  379. {% else %}
  380. users_{{ users.sudoers_dir }}/{{ name }}:
  381. file.absent:
  382. - name: {{ users.sudoers_dir }}/{{ name }}
  383. {% endif %}
  384. {%- if 'google_auth' in user %}
  385. {%- for svc in user['google_auth'] %}
  386. users_googleauth-{{ svc }}-{{ name }}:
  387. file.managed:
  388. - replace: false
  389. - name: {{ users.googleauth_dir }}/{{ name }}_{{ svc }}
  390. - contents_pillar: 'users:{{ name }}:google_auth:{{ svc }}'
  391. - user: root
  392. - group: {{ users.root_group }}
  393. - mode: 400
  394. - require:
  395. - pkg: users_googleauth-package
  396. {%- endfor %}
  397. {%- endif %}
  398. {% if 'gitconfig' in user %}
  399. {% if not salt['cmd.has_exec']('git') %}
  400. skip_{{ name }}_gitconfig_since_git_not_installed:
  401. test.fail_without_changes:
  402. - name: "Git configuration for user {{ name }} has been skipped because Git is not installed."
  403. {% else %}
  404. {% for key, value in user['gitconfig'].items() %}
  405. users_{{ name }}_user_gitconfig_{{ loop.index0 }}:
  406. {% if grains['saltversioninfo'] >= (2015, 8, 0, 0) %}
  407. git.config_set:
  408. {% else %}
  409. git.config:
  410. {% endif %}
  411. - name: {{ key }}
  412. - value: "{{ value }}"
  413. - user: {{ name }}
  414. {% if grains['saltversioninfo'] >= (2015, 8, 0, 0) %}
  415. - global: True
  416. {% else %}
  417. - is_global: True
  418. {% endif %}
  419. {% endfor %}
  420. {% endif %}
  421. {% endif %}
  422. {% endfor %}
  423. {% for name, user in pillar.get('users', {}).items()
  424. if user.absent is defined and user.absent %}
  425. users_absent_user_{{ name }}:
  426. {% if 'purge' in user or 'force' in user %}
  427. user.absent:
  428. - name: {{ name }}
  429. {% if 'purge' in user %}
  430. - purge: {{ user['purge'] }}
  431. {% endif %}
  432. {% if 'force' in user %}
  433. - force: {{ user['force'] }}
  434. {% endif %}
  435. {% else %}
  436. user.absent:
  437. - name: {{ name }}
  438. {% endif -%}
  439. users_{{ users.sudoers_dir }}/{{ name }}:
  440. file.absent:
  441. - name: {{ users.sudoers_dir }}/{{ name }}
  442. {% endfor %}
  443. {% for user in pillar.get('absent_users', []) %}
  444. users_absent_user_2_{{ user }}:
  445. user.absent
  446. users_2_{{ users.sudoers_dir }}/{{ user }}:
  447. file.absent:
  448. - name: {{ users.sudoers_dir }}/{{ user }}
  449. {% endfor %}
  450. {% for group in pillar.get('absent_groups', []) %}
  451. users_absent_group_{{ group }}:
  452. group.absent:
  453. - name: {{ group }}
  454. {% endfor %}