Saltstack Official Users Formula
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

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