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.

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