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.

591 lines
17KB

  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 group, setting in salt['pillar.get']('groups', {}).items() %}
  7. users_group_{{ setting.get('state', "present") }}_{{ group }}:
  8. group.{{ setting.get('state', "present") }}:
  9. - name: {{ group }}
  10. {%- if setting.get('gid') %}
  11. - gid: {{setting.get('gid') }}
  12. {%- endif %}
  13. - system: {{ setting.get('system',"False") }}
  14. {% endfor %}
  15. {%- for name, user in pillar.get('users', {}).items()
  16. if user.absent is not defined or not user.absent %}
  17. {%- if user == None -%}
  18. {%- set user = {} -%}
  19. {%- endif -%}
  20. {%- if 'sudoonly' in user and user['sudoonly'] %}
  21. {%- set _dummy=user.update({'sudouser': True}) %}
  22. {%- endif %}
  23. {%- if 'sudouser' in user and user['sudouser'] %}
  24. {%- do used_sudo.append(1) %}
  25. {%- endif %}
  26. {%- if 'google_auth' in user %}
  27. {%- do used_googleauth.append(1) %}
  28. {%- endif %}
  29. {%- if salt['pillar.get']('users:' ~ name ~ ':user_files:enabled', False) %}
  30. {%- do used_user_files.append(1) %}
  31. {%- endif %}
  32. {%- endfor %}
  33. {%- if used_sudo or used_googleauth or used_user_files %}
  34. include:
  35. {%- if used_sudo %}
  36. - users.sudo
  37. {%- endif %}
  38. {%- if used_googleauth %}
  39. - users.googleauth
  40. {%- endif %}
  41. {%- if used_user_files %}
  42. - users.user_files
  43. {%- endif %}
  44. {%- endif %}
  45. {% for name, user in pillar.get('users', {}).items()
  46. if user.absent is not defined or not user.absent %}
  47. {%- if user == None -%}
  48. {%- set user = {} -%}
  49. {%- endif -%}
  50. {%- set current = salt.user.info(name) -%}
  51. {%- set home = user.get('home', current.get('home', "/home/%s" % name)) -%}
  52. {%- set createhome = user.get('createhome') -%}
  53. {%- if 'prime_group' in user and 'name' in user['prime_group'] %}
  54. {%- set user_group = user.prime_group.name -%}
  55. {%- else -%}
  56. {%- set user_group = name -%}
  57. {%- endif %}
  58. {%- if not ( 'sudoonly' in user and user['sudoonly'] ) %}
  59. {% for group in user.get('groups', []) %}
  60. users_{{ name }}_{{ group }}_group:
  61. group.present:
  62. - name: {{ group }}
  63. {% if group == 'sudo' %}
  64. - system: True
  65. {% endif %}
  66. {% endfor %}
  67. {# in case home subfolder doesn't exist, create it before the user exists #}
  68. {% if createhome -%}
  69. users_{{ name }}_user_prereq:
  70. file.directory:
  71. - name: {{ salt['file.dirname'](home) }}
  72. - makedirs: True
  73. - prereq:
  74. - user: users_{{ name }}_user
  75. {%- endif %}
  76. users_{{ name }}_user:
  77. {% if createhome -%}
  78. file.directory:
  79. - name: {{ home }}
  80. - user: {{ user.get('homedir_owner', name) }}
  81. - group: {{ user.get('homedir_group', user_group) }}
  82. - mode: {{ user.get('user_dir_mode', '0750') }}
  83. - makedirs: True
  84. - require:
  85. - user: users_{{ name }}_user
  86. - group: {{ user_group }}
  87. {%- endif %}
  88. group.present:
  89. - name: {{ user_group }}
  90. {%- if 'prime_group' in user and 'gid' in user['prime_group'] %}
  91. - gid: {{ user['prime_group']['gid'] }}
  92. {%- elif 'uid' in user %}
  93. - gid: {{ user['uid'] }}
  94. {%- endif %}
  95. {% if 'system' in user and user['system'] %}
  96. - system: True
  97. {% endif %}
  98. user.present:
  99. - name: {{ name }}
  100. - home: {{ home }}
  101. - shell: {{ user.get('shell', current.get('shell', users.get('shell', '/bin/bash'))) }}
  102. {% if 'uid' in user -%}
  103. - uid: {{ user['uid'] }}
  104. {% endif -%}
  105. {% if 'password' in user -%}
  106. - password: '{{ user['password'] }}'
  107. {% endif -%}
  108. {% if user.get('empty_password') -%}
  109. - empty_password: {{ user.get('empty_password') }}
  110. {% endif -%}
  111. {% if 'enforce_password' in user -%}
  112. - enforce_password: {{ user['enforce_password'] }}
  113. {% endif -%}
  114. {% if 'hash_password' in user -%}
  115. - hash_password: {{ user['hash_password'] }}
  116. {% endif -%}
  117. {% if user.get('system', False) -%}
  118. - system: True
  119. {% endif -%}
  120. {% if 'prime_group' in user and 'gid' in user['prime_group'] -%}
  121. - gid: {{ user['prime_group']['gid'] }}
  122. {% elif 'prime_group' in user and 'name' in user['prime_group'] %}
  123. - gid: {{ user['prime_group']['name'] }}
  124. {% else -%}
  125. - gid_from_name: True
  126. {% endif -%}
  127. {% if 'fullname' in user %}
  128. - fullname: {{ user['fullname'] }}
  129. {% endif -%}
  130. {% if 'roomnumber' in user %}
  131. - roomnumber: {{ user['roomnumber'] }}
  132. {% endif %}
  133. {% if 'workphone' in user %}
  134. - workphone: {{ user['workphone'] }}
  135. {% endif %}
  136. {% if 'homephone' in user %}
  137. - homephone: {{ user['homephone'] }}
  138. {% endif %}
  139. - createhome: {{ createhome }}
  140. {% if not user.get('unique', True) %}
  141. - unique: False
  142. {% endif %}
  143. {%- if grains['saltversioninfo'] >= [2018, 3, 1] %}
  144. - allow_gid_change: {{ users.allow_gid_change if 'allow_gid_change' not in user else user['allow_gid_change'] }}
  145. {%- endif %}
  146. {% if 'expire' in user -%}
  147. {% if grains['kernel'].endswith('BSD') and
  148. user['expire'] < 157766400 %}
  149. {# 157762800s since epoch equals 01 Jan 1975 00:00:00 UTC #}
  150. - expire: {{ user['expire'] * 86400 }}
  151. {% elif grains['kernel'] == 'Linux' and
  152. user['expire'] > 84006 %}
  153. {# 2932896 days since epoch equals 9999-12-31 #}
  154. - expire: {{ (user['expire'] / 86400) | int}}
  155. {% else %}
  156. - expire: {{ user['expire'] }}
  157. {% endif %}
  158. {% endif -%}
  159. {% if 'mindays' in user %}
  160. - mindays: {{ user.get('mindays', None) }}
  161. {% endif %}
  162. {% if 'maxdays' in user %}
  163. - maxdays: {{ user.get('maxdays', None) }}
  164. {% endif %}
  165. {% if 'inactdays' in user %}
  166. - inactdays: {{ user.get('inactdays', None) }}
  167. {% endif %}
  168. {% if 'warndays' in user %}
  169. - warndays: {{ user.get('warndays', None) }}
  170. {% endif %}
  171. - remove_groups: {{ user.get('remove_groups', 'False') }}
  172. - groups:
  173. - {{ user_group }}
  174. {% for group in user.get('groups', []) -%}
  175. - {{ group }}
  176. {% endfor %}
  177. {% if 'optional_groups' in user %}
  178. - optional_groups:
  179. {% for optional_group in user['optional_groups'] -%}
  180. - {{optional_group}}
  181. {% endfor %}
  182. {% endif %}
  183. - require:
  184. - group: {{ user_group }}
  185. {% for group in user.get('groups', []) -%}
  186. - group: {{ group }}
  187. {% endfor %}
  188. {% if 'ssh_keys' in user or
  189. 'ssh_auth' in user or
  190. 'ssh_auth_file' in user or
  191. 'ssh_auth_pillar' in user or
  192. 'ssh_auth.absent' in user or
  193. 'ssh_config' in user %}
  194. user_keydir_{{ name }}:
  195. file.directory:
  196. - name: {{ home }}/.ssh
  197. - user: {{ name }}
  198. - group: {{ user_group }}
  199. - makedirs: True
  200. - mode: 700
  201. - dir_mode: 700
  202. - require:
  203. - user: {{ name }}
  204. - group: {{ user_group }}
  205. {%- for group in user.get('groups', []) %}
  206. - group: {{ group }}
  207. {%- endfor %}
  208. {% endif %}
  209. {% if 'ssh_keys' in user %}
  210. {% for _key in user.ssh_keys.keys() %}
  211. {% if _key == 'privkey' %}
  212. {% set key_name = 'id_' + user.get('ssh_key_type', 'rsa') %}
  213. {% elif _key == 'pubkey' %}
  214. {% set key_name = 'id_' + user.get('ssh_key_type', 'rsa') + '.pub' %}
  215. {% else %}
  216. {% set key_name = _key %}
  217. {% endif %}
  218. users_{{ name }}_{{ key_name }}_key:
  219. file.managed:
  220. - name: {{ home }}/.ssh/{{ key_name }}
  221. - user: {{ name }}
  222. - group: {{ user_group }}
  223. {% if key_name.endswith(".pub") %}
  224. - mode: 644
  225. {% else %}
  226. - mode: 600
  227. {% endif %}
  228. - show_diff: False
  229. {%- set key_value = salt['pillar.get']('users:'+name+':ssh_keys:'+_key) %}
  230. {%- if 'salt://' in key_value[:7] %}
  231. - source: {{ key_value }}
  232. {%- else %}
  233. - contents_pillar: users:{{ name }}:ssh_keys:{{ _key }}
  234. {%- endif %}
  235. - require:
  236. - user: users_{{ name }}_user
  237. {% for group in user.get('groups', []) %}
  238. - group: users_{{ name }}_{{ group }}_group
  239. {% endfor %}
  240. {% endfor %}
  241. {% endif %}
  242. {% if 'ssh_auth_file' in user or 'ssh_auth_pillar' in user %}
  243. users_authorized_keys_{{ name }}:
  244. file.managed:
  245. - name: {{ home }}/.ssh/authorized_keys
  246. - user: {{ name }}
  247. - group: {{ user_group }}
  248. - mode: 600
  249. {% if 'ssh_auth_file' in user %}
  250. - contents: |
  251. {% for auth in user.ssh_auth_file -%}
  252. {{ auth }}
  253. {% endfor -%}
  254. {% else %}
  255. - contents: |
  256. {%- for key_name, pillar_name in user['ssh_auth_pillar'].items() %}
  257. {{ salt['pillar.get'](pillar_name + ':' + key_name + ':pubkey', '') }}
  258. {%- endfor %}
  259. {% endif %}
  260. {% endif %}
  261. {% if 'ssh_auth' in user %}
  262. {% for auth in user['ssh_auth'] %}
  263. users_ssh_auth_{{ name }}_{{ loop.index0 }}:
  264. ssh_auth.present:
  265. - user: {{ name }}
  266. - name: {{ auth }}
  267. - require:
  268. - file: user_keydir_{{ name }}
  269. - user: users_{{ name }}_user
  270. {% endfor %}
  271. {% endif %}
  272. {% if 'ssh_keys_pillar' in user %}
  273. {% for key_name, pillar_name in user['ssh_keys_pillar'].items() %}
  274. user_ssh_keys_files_{{ name }}_{{ key_name }}_private_key:
  275. file.managed:
  276. - name: {{ home }}/.ssh/{{ key_name }}
  277. - user: {{ name }}
  278. - group: {{ user_group }}
  279. - mode: 600
  280. - show_diff: False
  281. - contents_pillar: {{ pillar_name }}:{{ key_name }}:privkey
  282. - require:
  283. - user: users_{{ name }}_user
  284. {% for group in user.get('groups', []) %}
  285. - group: users_{{ name }}_{{ group }}_group
  286. {% endfor %}
  287. user_ssh_keys_files_{{ name }}_{{ key_name }}_public_key:
  288. file.managed:
  289. - name: {{ home }}/.ssh/{{ key_name }}.pub
  290. - user: {{ name }}
  291. - group: {{ user_group }}
  292. - mode: 644
  293. - show_diff: False
  294. - contents_pillar: {{ pillar_name }}:{{ key_name }}:pubkey
  295. - require:
  296. - user: users_{{ name }}_user
  297. {% for group in user.get('groups', []) %}
  298. - group: users_{{ name }}_{{ group }}_group
  299. {% endfor %}
  300. {% endfor %}
  301. {% endif %}
  302. {% if 'ssh_auth_sources' in user %}
  303. {% for pubkey_file in user['ssh_auth_sources'] %}
  304. users_ssh_auth_source_{{ name }}_{{ loop.index0 }}:
  305. ssh_auth.present:
  306. - user: {{ name }}
  307. - source: {{ pubkey_file }}
  308. - require:
  309. {% if createhome -%}
  310. - file: users_{{ name }}_user
  311. {% endif -%}
  312. - user: users_{{ name }}_user
  313. {% endfor %}
  314. {% endif %}
  315. {% if 'ssh_auth_sources.absent' in user %}
  316. {% for pubkey_file in user['ssh_auth_sources.absent'] %}
  317. users_ssh_auth_source_delete_{{ name }}_{{ loop.index0 }}:
  318. ssh_auth.absent:
  319. - user: {{ name }}
  320. - source: {{ pubkey_file }}
  321. - require:
  322. {% if createhome -%}
  323. - file: users_{{ name }}_user
  324. {% endif -%}
  325. - user: users_{{ name }}_user
  326. {% endfor %}
  327. {% endif %}
  328. {% if 'ssh_auth.absent' in user %}
  329. {% for auth in user['ssh_auth.absent'] %}
  330. users_ssh_auth_delete_{{ name }}_{{ loop.index0 }}:
  331. ssh_auth.absent:
  332. - user: {{ name }}
  333. - name: {{ auth }}
  334. - require:
  335. {% if createhome -%}
  336. - file: users_{{ name }}_user
  337. {% endif -%}
  338. - user: users_{{ name }}_user
  339. {% endfor %}
  340. {% endif %}
  341. {% if 'ssh_config' in user %}
  342. users_ssh_config_{{ name }}:
  343. file.managed:
  344. - name: {{ home }}/.ssh/config
  345. - user: {{ name }}
  346. - group: {{ user_group }}
  347. - mode: 640
  348. - contents: |
  349. # Managed by Saltstack
  350. # Do Not Edit
  351. {% for label, setting in user.ssh_config.items() %}
  352. # {{ label }}
  353. Host {{ setting.get('hostname') }}
  354. {%- for opts in setting.get('options') %}
  355. {{ opts }}
  356. {%- endfor %}
  357. {% endfor -%}
  358. {% endif %}
  359. {% if 'ssh_known_hosts' in user %}
  360. {% for hostname, host in user['ssh_known_hosts'].items() %}
  361. users_ssh_known_hosts_{{ name }}_{{ loop.index0 }}:
  362. ssh_known_hosts.present:
  363. - user: {{ name }}
  364. - name: {{ hostname }}
  365. {% if 'port' in host %}
  366. - port: {{ host['port'] }}
  367. {% endif -%}
  368. {% if 'fingerprint' in host %}
  369. - fingerprint: {{ host['fingerprint'] }}
  370. {% endif -%}
  371. {% if 'key' in host %}
  372. - key: {{ host['key'] }}
  373. {% endif -%}
  374. {% if 'enc' in host %}
  375. - enc: {{ host['enc'] }}
  376. {% endif -%}
  377. {% if 'hash_known_hosts' in host %}
  378. - hash_known_hosts: {{ host['hash_known_hosts'] }}
  379. {% endif -%}
  380. {% if 'timeout' in host %}
  381. - timeout: {{ host['timeout'] }}
  382. {% endif -%}
  383. {% if 'fingerprint_hash_type' in host %}
  384. - fingerprint_hash_type: {{ host['fingerprint_hash_type'] }}
  385. {% endif -%}
  386. {% endfor %}
  387. {% endif %}
  388. {% if 'ssh_known_hosts.absent' in user %}
  389. {% for host in user['ssh_known_hosts.absent'] %}
  390. users_ssh_known_hosts_delete_{{ name }}_{{ loop.index0 }}:
  391. ssh_known_hosts.absent:
  392. - user: {{ name }}
  393. - name: {{ host }}
  394. {% endfor %}
  395. {% endif %}
  396. {% endif %}
  397. {% set sudoers_d_filename = name|replace('.','_') %}
  398. {% if 'sudouser' in user and user['sudouser'] %}
  399. users_sudoer-{{ name }}:
  400. file.managed:
  401. - replace: False
  402. - name: {{ users.sudoers_dir }}/{{ sudoers_d_filename }}
  403. - user: root
  404. - group: {{ users.root_group }}
  405. - mode: '0440'
  406. {% if 'sudo_rules' in user or 'sudo_defaults' in user %}
  407. #{#%
  408. {% if 'sudo_rules' in user %}
  409. {% for rule in user['sudo_rules'] %}
  410. "validate {{ name }} sudo rule {{ loop.index0 }} {{ name }} {{ rule }}":
  411. cmd.run:
  412. - name: 'visudo -cf - <<<"$rule" | { read output; if [[ $output != "stdin: parsed OK" ]] ; then echo $output ; fi }'
  413. - stateful: True
  414. - shell: {{ users.visudo_shell }}
  415. - env:
  416. # Specify the rule via an env var to avoid shell quoting issues.
  417. - rule: "{{ name }} {{ rule }}"
  418. - require_in:
  419. - file: users_{{ users.sudoers_dir }}/{{ name }}
  420. {% endfor %}
  421. {% endif %}
  422. {% if 'sudo_defaults' in user %}
  423. {% for entry in user['sudo_defaults'] %}
  424. "validate {{ name }} sudo Defaults {{ loop.index0 }} {{ name }} {{ entry }}":
  425. cmd.run:
  426. - name: 'visudo -cf - <<<"$rule" | { read output; if [[ $output != "stdin: parsed OK" ]] ; then echo $output ; fi }'
  427. - stateful: True
  428. - shell: {{ users.visudo_shell }}
  429. - env:
  430. # Specify the rule via an env var to avoid shell quoting issues.
  431. - rule: "Defaults:{{ name }} {{ entry }}"
  432. - require_in:
  433. - file: users_{{ users.sudoers_dir }}/{{ name }}
  434. {% endfor %}
  435. {% endif %}
  436. #%#}
  437. users_{{ users.sudoers_dir }}/{{ name }}:
  438. file.managed:
  439. - replace: True
  440. - name: {{ users.sudoers_dir }}/{{ sudoers_d_filename }}
  441. - contents: |
  442. {%- if 'sudo_defaults' in user %}
  443. {%- for entry in user['sudo_defaults'] %}
  444. Defaults:{{ name }} {{ entry }}
  445. {%- endfor %}
  446. {%- endif %}
  447. {%- if 'sudo_rules' in user %}
  448. ########################################################################
  449. # File managed by Salt (users-formula).
  450. # Your changes will be overwritten.
  451. ########################################################################
  452. #
  453. {%- for rule in user['sudo_rules'] %}
  454. {{ name }} {{ rule }}
  455. {%- endfor %}
  456. {%- endif %}
  457. - require:
  458. - file: users_sudoer-defaults
  459. - file: users_sudoer-{{ name }}
  460. cmd.wait:
  461. - name: visudo -cf {{ users.sudoers_dir }}/{{ sudoers_d_filename }} || ( rm -rvf {{ users.sudoers_dir }}/{{ sudoers_d_filename }}; exit 1 )
  462. - watch:
  463. - file: {{ users.sudoers_dir }}/{{ sudoers_d_filename }}
  464. {% endif %}
  465. {% else %}
  466. users_{{ users.sudoers_dir }}/{{ sudoers_d_filename }}:
  467. file.absent:
  468. - name: {{ users.sudoers_dir }}/{{ sudoers_d_filename }}
  469. {% endif %}
  470. {%- if 'google_auth' in user %}
  471. {%- for svc in user['google_auth'] %}
  472. users_googleauth-{{ svc }}-{{ name }}:
  473. file.managed:
  474. - replace: false
  475. - name: {{ users.googleauth_dir }}/{{ name }}_{{ svc }}
  476. - contents_pillar: 'users:{{ name }}:google_auth:{{ svc }}'
  477. - user: root
  478. - group: {{ users.root_group }}
  479. - mode: 400
  480. - require:
  481. - pkg: users_googleauth-package
  482. {%- endfor %}
  483. {%- endif %}
  484. # this doesn't work (Salt bug), therefore need to run state.apply twice
  485. #include:
  486. # - users
  487. #
  488. #git:
  489. # pkg.installed:
  490. # - require_in:
  491. # - sls: users
  492. #
  493. {% if salt['cmd.has_exec']('git') %}
  494. {% if 'gitconfig' in user %}
  495. {% for key, value in user['gitconfig'].items() %}
  496. users_{{ name }}_user_gitconfig_{{ loop.index0 }}:
  497. {% if grains['saltversioninfo'] >= [2015, 8, 0, 0] %}
  498. git.config_set:
  499. {% else %}
  500. git.config:
  501. {% endif %}
  502. - name: {{ key }}
  503. - value: "{{ value }}"
  504. - user: {{ name }}
  505. {% if grains['saltversioninfo'] >= [2015, 8, 0, 0] %}
  506. - global: True
  507. {% else %}
  508. - is_global: True
  509. {% endif %}
  510. {% endfor %}
  511. {% endif %}
  512. {% if 'gitconfig.absent' in user and grains['saltversioninfo'] >= [2015, 8, 0, 0] %}
  513. {% for key in user.get('gitconfig.absent') %}
  514. users_{{ name }}_user_gitconfig_absent_{{ key }}:
  515. git.config_unset:
  516. - name: '{{ key }}'
  517. - user: {{ name }}
  518. - global: True
  519. - all: True
  520. {% endfor %}
  521. {% endif %}
  522. {% endif %}
  523. {% endfor %}
  524. {% for name, user in pillar.get('users', {}).items()
  525. if user.absent is defined and user.absent %}
  526. users_absent_user_{{ name }}:
  527. {% if 'purge' in user or 'force' in user %}
  528. user.absent:
  529. - name: {{ name }}
  530. {% if 'purge' in user %}
  531. - purge: {{ user['purge'] }}
  532. {% endif %}
  533. {% if 'force' in user %}
  534. - force: {{ user['force'] }}
  535. {% endif %}
  536. {% else %}
  537. user.absent:
  538. - name: {{ name }}
  539. {% endif -%}
  540. users_{{ users.sudoers_dir }}/{{ name }}:
  541. file.absent:
  542. - name: {{ users.sudoers_dir }}/{{ name }}
  543. {% endfor %}
  544. {% for user in pillar.get('absent_users', []) %}
  545. users_absent_user_2_{{ user }}:
  546. user.absent:
  547. - name: {{ user }}
  548. users_2_{{ users.sudoers_dir }}/{{ user }}:
  549. file.absent:
  550. - name: {{ users.sudoers_dir }}/{{ user }}
  551. {% endfor %}
  552. {% for group in pillar.get('absent_groups', []) %}
  553. users_absent_group_{{ group }}:
  554. group.absent:
  555. - name: {{ group }}
  556. {% endfor %}