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.

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