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.

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