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.

575 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. - require:
  193. - user: {{ name }}
  194. - group: {{ user_group }}
  195. {%- for group in user.get('groups', []) %}
  196. - group: {{ group }}
  197. {%- endfor %}
  198. {% endif %}
  199. {% if 'ssh_keys' in user %}
  200. {% for _key in user.ssh_keys.keys() %}
  201. {% if _key == 'privkey' %}
  202. {% set key_name = 'id_' + user.get('ssh_key_type', 'rsa') %}
  203. {% elif _key == 'pubkey' %}
  204. {% set key_name = 'id_' + user.get('ssh_key_type', 'rsa') + '.pub' %}
  205. {% else %}
  206. {% set key_name = _key %}
  207. {% endif %}
  208. users_{{ name }}_{{ key_name }}_key:
  209. file.managed:
  210. - name: {{ home }}/.ssh/{{ key_name }}
  211. - user: {{ name }}
  212. - group: {{ user_group }}
  213. {% if key_name.endswith(".pub") %}
  214. - mode: 644
  215. {% else %}
  216. - mode: 600
  217. {% endif %}
  218. - show_diff: False
  219. {%- set key_value = salt['pillar.get']('users:'+name+':ssh_keys:'+_key) %}
  220. {%- if 'salt://' in key_value[:7] %}
  221. - source: {{ key_value }}
  222. {%- else %}
  223. - contents_pillar: users:{{ name }}:ssh_keys:{{ _key }}
  224. {%- endif %}
  225. - require:
  226. - user: users_{{ name }}_user
  227. {% for group in user.get('groups', []) %}
  228. - group: users_{{ name }}_{{ group }}_group
  229. {% endfor %}
  230. {% endfor %}
  231. {% endif %}
  232. {% if 'ssh_auth_file' in user or 'ssh_auth_pillar' in user %}
  233. users_authorized_keys_{{ name }}:
  234. file.managed:
  235. - name: {{ home }}/.ssh/authorized_keys
  236. - user: {{ name }}
  237. - group: {{ user_group }}
  238. - mode: 600
  239. {% if 'ssh_auth_file' in user %}
  240. - contents: |
  241. {% for auth in user.ssh_auth_file -%}
  242. {{ auth }}
  243. {% endfor -%}
  244. {% else %}
  245. - contents: |
  246. {%- for key_name, pillar_name in user['ssh_auth_pillar'].items() %}
  247. {{ salt['pillar.get'](pillar_name + ':' + key_name + ':pubkey', '') }}
  248. {%- endfor %}
  249. {% endif %}
  250. {% endif %}
  251. {% if 'ssh_auth' in user %}
  252. {% for auth in user['ssh_auth'] %}
  253. users_ssh_auth_{{ name }}_{{ loop.index0 }}:
  254. ssh_auth.present:
  255. - user: {{ name }}
  256. - name: {{ auth }}
  257. - require:
  258. - file: user_keydir_{{ name }}
  259. - user: users_{{ name }}_user
  260. {% endfor %}
  261. {% endif %}
  262. {% if 'ssh_keys_pillar' in user %}
  263. {% for key_name, pillar_name in user['ssh_keys_pillar'].items() %}
  264. user_ssh_keys_files_{{ name }}_{{ key_name }}_private_key:
  265. file.managed:
  266. - name: {{ home }}/.ssh/{{ key_name }}
  267. - user: {{ name }}
  268. - group: {{ user_group }}
  269. - mode: 600
  270. - show_diff: False
  271. - contents_pillar: {{ pillar_name }}:{{ key_name }}:privkey
  272. - require:
  273. - user: users_{{ name }}_user
  274. {% for group in user.get('groups', []) %}
  275. - group: users_{{ name }}_{{ group }}_group
  276. {% endfor %}
  277. user_ssh_keys_files_{{ name }}_{{ key_name }}_public_key:
  278. file.managed:
  279. - name: {{ home }}/.ssh/{{ key_name }}.pub
  280. - user: {{ name }}
  281. - group: {{ user_group }}
  282. - mode: 644
  283. - show_diff: False
  284. - contents_pillar: {{ pillar_name }}:{{ key_name }}:pubkey
  285. - require:
  286. - user: users_{{ name }}_user
  287. {% for group in user.get('groups', []) %}
  288. - group: users_{{ name }}_{{ group }}_group
  289. {% endfor %}
  290. {% endfor %}
  291. {% endif %}
  292. {% if 'ssh_auth_sources' in user %}
  293. {% for pubkey_file in user['ssh_auth_sources'] %}
  294. users_ssh_auth_source_{{ name }}_{{ loop.index0 }}:
  295. ssh_auth.present:
  296. - user: {{ name }}
  297. - source: {{ pubkey_file }}
  298. - require:
  299. - file: users_{{ name }}_user
  300. - user: users_{{ name }}_user
  301. {% endfor %}
  302. {% endif %}
  303. {% if 'ssh_auth_sources.absent' in user %}
  304. {% for pubkey_file in user['ssh_auth_sources.absent'] %}
  305. users_ssh_auth_source_delete_{{ name }}_{{ loop.index0 }}:
  306. ssh_auth.absent:
  307. - user: {{ name }}
  308. - source: {{ pubkey_file }}
  309. - require:
  310. - file: users_{{ name }}_user
  311. - user: users_{{ name }}_user
  312. {% endfor %}
  313. {% endif %}
  314. {% if 'ssh_auth.absent' in user %}
  315. {% for auth in user['ssh_auth.absent'] %}
  316. users_ssh_auth_delete_{{ name }}_{{ loop.index0 }}:
  317. ssh_auth.absent:
  318. - user: {{ name }}
  319. - name: {{ auth }}
  320. - require:
  321. - file: users_{{ name }}_user
  322. - user: users_{{ name }}_user
  323. {% endfor %}
  324. {% endif %}
  325. {% if 'ssh_config' in user %}
  326. users_ssh_config_{{ name }}:
  327. file.managed:
  328. - name: {{ home }}/.ssh/config
  329. - user: {{ name }}
  330. - group: {{ user_group }}
  331. - mode: 640
  332. - contents: |
  333. # Managed by Saltstack
  334. # Do Not Edit
  335. {% for label, setting in user.ssh_config.items() %}
  336. # {{ label }}
  337. Host {{ setting.get('hostname') }}
  338. {%- for opts in setting.get('options') %}
  339. {{ opts }}
  340. {%- endfor %}
  341. {% endfor -%}
  342. {% endif %}
  343. {% if 'ssh_known_hosts' in user %}
  344. {% for hostname, host in user['ssh_known_hosts'].items() %}
  345. users_ssh_known_hosts_{{ name }}_{{ loop.index0 }}:
  346. ssh_known_hosts.present:
  347. - user: {{ name }}
  348. - name: {{ hostname }}
  349. {% if 'port' in host %}
  350. - port: {{ host['port'] }}
  351. {% endif -%}
  352. {% if 'fingerprint' in host %}
  353. - fingerprint: {{ host['fingerprint'] }}
  354. {% endif -%}
  355. {% if 'key' in host %}
  356. - key: {{ host['key'] }}
  357. {% endif -%}
  358. {% if 'enc' in host %}
  359. - enc: {{ host['enc'] }}
  360. {% endif -%}
  361. {% if 'hash_known_hosts' in host %}
  362. - hash_known_hosts: {{ host['hash_known_hosts'] }}
  363. {% endif -%}
  364. {% if 'timeout' in host %}
  365. - timeout: {{ host['timeout'] }}
  366. {% endif -%}
  367. {% if 'fingerprint_hash_type' in host %}
  368. - fingerprint_hash_type: {{ host['fingerprint_hash_type'] }}
  369. {% endif -%}
  370. {% endfor %}
  371. {% endif %}
  372. {% if 'ssh_known_hosts.absent' in user %}
  373. {% for host in user['ssh_known_hosts.absent'] %}
  374. users_ssh_known_hosts_delete_{{ name }}_{{ loop.index0 }}:
  375. ssh_known_hosts.absent:
  376. - user: {{ name }}
  377. - name: {{ host }}
  378. {% endfor %}
  379. {% endif %}
  380. {% endif %}
  381. {% set sudoers_d_filename = name|replace('.','_') %}
  382. {% if 'sudouser' in user and user['sudouser'] %}
  383. users_sudoer-{{ name }}:
  384. file.managed:
  385. - replace: False
  386. - name: {{ users.sudoers_dir }}/{{ sudoers_d_filename }}
  387. - user: root
  388. - group: {{ users.root_group }}
  389. - mode: '0440'
  390. {% if 'sudo_rules' in user or 'sudo_defaults' in user %}
  391. #{#%
  392. {% if 'sudo_rules' in user %}
  393. {% for rule in user['sudo_rules'] %}
  394. "validate {{ name }} sudo rule {{ loop.index0 }} {{ name }} {{ rule }}":
  395. cmd.run:
  396. - name: 'visudo -cf - <<<"$rule" | { read output; if [[ $output != "stdin: parsed OK" ]] ; then echo $output ; fi }'
  397. - stateful: True
  398. - shell: {{ users.visudo_shell }}
  399. - env:
  400. # Specify the rule via an env var to avoid shell quoting issues.
  401. - rule: "{{ name }} {{ rule }}"
  402. - require_in:
  403. - file: users_{{ users.sudoers_dir }}/{{ name }}
  404. {% endfor %}
  405. {% endif %}
  406. {% if 'sudo_defaults' in user %}
  407. {% for entry in user['sudo_defaults'] %}
  408. "validate {{ name }} sudo Defaults {{ loop.index0 }} {{ name }} {{ entry }}":
  409. cmd.run:
  410. - name: 'visudo -cf - <<<"$rule" | { read output; if [[ $output != "stdin: parsed OK" ]] ; then echo $output ; fi }'
  411. - stateful: True
  412. - shell: {{ users.visudo_shell }}
  413. - env:
  414. # Specify the rule via an env var to avoid shell quoting issues.
  415. - rule: "Defaults:{{ name }} {{ entry }}"
  416. - require_in:
  417. - file: users_{{ users.sudoers_dir }}/{{ name }}
  418. {% endfor %}
  419. {% endif %}
  420. #%#}
  421. users_{{ users.sudoers_dir }}/{{ name }}:
  422. file.managed:
  423. - replace: True
  424. - name: {{ users.sudoers_dir }}/{{ sudoers_d_filename }}
  425. - contents: |
  426. {%- if 'sudo_defaults' in user %}
  427. {%- for entry in user['sudo_defaults'] %}
  428. Defaults:{{ name }} {{ entry }}
  429. {%- endfor %}
  430. {%- endif %}
  431. {%- if 'sudo_rules' in user %}
  432. ########################################################################
  433. # File managed by Salt (users-formula).
  434. # Your changes will be overwritten.
  435. ########################################################################
  436. #
  437. {%- for rule in user['sudo_rules'] %}
  438. {{ name }} {{ rule }}
  439. {%- endfor %}
  440. {%- endif %}
  441. - require:
  442. - file: users_sudoer-defaults
  443. - file: users_sudoer-{{ name }}
  444. cmd.wait:
  445. - name: visudo -cf {{ users.sudoers_dir }}/{{ sudoers_d_filename }} || ( rm -rvf {{ users.sudoers_dir }}/{{ sudoers_d_filename }}; exit 1 )
  446. - watch:
  447. - file: {{ users.sudoers_dir }}/{{ sudoers_d_filename }}
  448. {% endif %}
  449. {% else %}
  450. users_{{ users.sudoers_dir }}/{{ sudoers_d_filename }}:
  451. file.absent:
  452. - name: {{ users.sudoers_dir }}/{{ sudoers_d_filename }}
  453. {% endif %}
  454. {%- if 'google_auth' in user %}
  455. {%- for svc in user['google_auth'] %}
  456. users_googleauth-{{ svc }}-{{ name }}:
  457. file.managed:
  458. - replace: false
  459. - name: {{ users.googleauth_dir }}/{{ name }}_{{ svc }}
  460. - contents_pillar: 'users:{{ name }}:google_auth:{{ svc }}'
  461. - user: root
  462. - group: {{ users.root_group }}
  463. - mode: 400
  464. - require:
  465. - pkg: users_googleauth-package
  466. {%- endfor %}
  467. {%- endif %}
  468. # this doesn't work (Salt bug), therefore need to run state.apply twice
  469. #include:
  470. # - users
  471. #
  472. #git:
  473. # pkg.installed:
  474. # - require_in:
  475. # - sls: users
  476. #
  477. {% if salt['cmd.has_exec']('git') %}
  478. {% if 'gitconfig' in user %}
  479. {% for key, value in user['gitconfig'].items() %}
  480. users_{{ name }}_user_gitconfig_{{ loop.index0 }}:
  481. {% if grains['saltversioninfo'] >= [2015, 8, 0, 0] %}
  482. git.config_set:
  483. {% else %}
  484. git.config:
  485. {% endif %}
  486. - name: {{ key }}
  487. - value: "{{ value }}"
  488. - user: {{ name }}
  489. {% if grains['saltversioninfo'] >= [2015, 8, 0, 0] %}
  490. - global: True
  491. {% else %}
  492. - is_global: True
  493. {% endif %}
  494. {% endfor %}
  495. {% endif %}
  496. {% if 'gitconfig.absent' in user and grains['saltversioninfo'] >= [2015, 8, 0, 0] %}
  497. {% for key in user.get('gitconfig.absent') %}
  498. users_{{ name }}_user_gitconfig_absent_{{ key }}:
  499. git.config_unset:
  500. - name: '{{ key }}'
  501. - user: {{ name }}
  502. - global: True
  503. - all: True
  504. {% endfor %}
  505. {% endif %}
  506. {% endif %}
  507. {% endfor %}
  508. {% for name, user in pillar.get('users', {}).items()
  509. if user.absent is defined and user.absent %}
  510. users_absent_user_{{ name }}:
  511. {% if 'purge' in user or 'force' in user %}
  512. user.absent:
  513. - name: {{ name }}
  514. {% if 'purge' in user %}
  515. - purge: {{ user['purge'] }}
  516. {% endif %}
  517. {% if 'force' in user %}
  518. - force: {{ user['force'] }}
  519. {% endif %}
  520. {% else %}
  521. user.absent:
  522. - name: {{ name }}
  523. {% endif -%}
  524. users_{{ users.sudoers_dir }}/{{ name }}:
  525. file.absent:
  526. - name: {{ users.sudoers_dir }}/{{ name }}
  527. {% endfor %}
  528. {% for user in pillar.get('absent_users', []) %}
  529. users_absent_user_2_{{ user }}:
  530. user.absent:
  531. - name: {{ user }}
  532. users_2_{{ users.sudoers_dir }}/{{ user }}:
  533. file.absent:
  534. - name: {{ users.sudoers_dir }}/{{ user }}
  535. {% endfor %}
  536. {% for group in pillar.get('absent_groups', []) %}
  537. users_absent_group_{{ group }}:
  538. group.absent:
  539. - name: {{ group }}
  540. {% endfor %}