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ů.

502 lines
14KB

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