Saltstack Official Users Formula
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

524 lines
15KB

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