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.

493 satır
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: {{ 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 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. {% set key_type = 'id_' + user.get('ssh_key_type', 'rsa') %}
  165. users_user_{{ name }}_private_key:
  166. file.managed:
  167. - name: {{ home }}/.ssh/{{ key_type }}
  168. - user: {{ name }}
  169. - group: {{ user_group }}
  170. - mode: 600
  171. - show_diff: False
  172. - contents_pillar: users:{{ name }}:ssh_keys:privkey
  173. - require:
  174. - user: users_{{ name }}_user
  175. {% for group in user.get('groups', []) %}
  176. - group: users_{{ name }}_{{ group }}_group
  177. {% endfor %}
  178. users_user_{{ name }}_public_key:
  179. file.managed:
  180. - name: {{ home }}/.ssh/{{ key_type }}.pub
  181. - user: {{ name }}
  182. - group: {{ user_group }}
  183. - mode: 644
  184. - show_diff: False
  185. - contents_pillar: users:{{ name }}:ssh_keys:pubkey
  186. - require:
  187. - user: users_{{ name }}_user
  188. {% for group in user.get('groups', []) %}
  189. - group: users_{{ name }}_{{ group }}_group
  190. {% endfor %}
  191. {% endif %}
  192. {% if 'ssh_auth_file' in user or 'ssh_auth_pillar' in user %}
  193. users_authorized_keys_{{ name }}:
  194. file.managed:
  195. - name: {{ home }}/.ssh/authorized_keys
  196. - user: {{ name }}
  197. - group: {{ user_group }}
  198. - mode: 600
  199. {% if 'ssh_auth_file' in user %}
  200. - contents: |
  201. {% for auth in user.ssh_auth_file -%}
  202. {{ auth }}
  203. {% endfor -%}
  204. {% else %}
  205. {%- for key_name, pillar_name in user['ssh_auth_pillar'].items() %}
  206. - contents_pillar: {{ pillar_name }}:{{ key_name }}:pubkey
  207. {%- endfor %}
  208. {% endif %}
  209. {% endif %}
  210. {% if 'ssh_auth' in user %}
  211. {% for auth in user['ssh_auth'] %}
  212. users_ssh_auth_{{ name }}_{{ loop.index0 }}:
  213. ssh_auth.present:
  214. - user: {{ name }}
  215. - name: {{ auth }}
  216. - require:
  217. - file: user_keydir_{{ name }}
  218. - user: users_{{ name }}_user
  219. {% endfor %}
  220. {% endif %}
  221. {% if 'ssh_keys_pillar' in user %}
  222. {% for key_name, pillar_name in user['ssh_keys_pillar'].items() %}
  223. user_ssh_keys_files_{{ name }}_{{ key_name }}_private_key:
  224. file.managed:
  225. - name: {{ home }}/.ssh/{{ key_name }}
  226. - user: {{ name }}
  227. - group: {{ user_group }}
  228. - mode: 600
  229. - show_diff: False
  230. - contents_pillar: {{ pillar_name }}:{{ key_name }}:privkey
  231. - require:
  232. - user: users_{{ name }}_user
  233. {% for group in user.get('groups', []) %}
  234. - group: users_{{ name }}_{{ group }}_group
  235. {% endfor %}
  236. user_ssh_keys_files_{{ name }}_{{ key_name }}_public_key:
  237. file.managed:
  238. - name: {{ home }}/.ssh/{{ key_name }}.pub
  239. - user: {{ name }}
  240. - group: {{ user_group }}
  241. - mode: 644
  242. - show_diff: False
  243. - contents_pillar: {{ pillar_name }}:{{ key_name }}:pubkey
  244. - require:
  245. - user: users_{{ name }}_user
  246. {% for group in user.get('groups', []) %}
  247. - group: users_{{ name }}_{{ group }}_group
  248. {% endfor %}
  249. {% endfor %}
  250. {% endif %}
  251. {% if 'ssh_auth_sources' in user %}
  252. {% for pubkey_file in user['ssh_auth_sources'] %}
  253. users_ssh_auth_source_{{ name }}_{{ loop.index0 }}:
  254. ssh_auth.present:
  255. - user: {{ name }}
  256. - source: {{ pubkey_file }}
  257. - require:
  258. - file: users_{{ name }}_user
  259. - user: users_{{ name }}_user
  260. {% endfor %}
  261. {% endif %}
  262. {% if 'ssh_auth.absent' in user %}
  263. {% for auth in user['ssh_auth.absent'] %}
  264. users_ssh_auth_delete_{{ name }}_{{ loop.index0 }}:
  265. ssh_auth.absent:
  266. - user: {{ name }}
  267. - name: {{ auth }}
  268. - require:
  269. - file: users_{{ name }}_user
  270. - user: users_{{ name }}_user
  271. {% endfor %}
  272. {% endif %}
  273. {% if 'ssh_config' in user %}
  274. users_ssh_config_{{ name }}:
  275. file.managed:
  276. - name: {{ home }}/.ssh/config
  277. - user: {{ name }}
  278. - group: {{ user_group }}
  279. - mode: 640
  280. - contents: |
  281. # Managed by Saltstack
  282. # Do Not Edit
  283. {% for label, setting in user.ssh_config.items() %}
  284. # {{ label }}
  285. Host {{ setting.get('hostname') }}
  286. {%- for opts in setting.get('options') %}
  287. {{ opts }}
  288. {%- endfor %}
  289. {% endfor -%}
  290. {% endif %}
  291. {% if 'ssh_known_hosts' in user %}
  292. {% for hostname, host in user['ssh_known_hosts'].items() %}
  293. users_ssh_known_hosts_{{ name }}_{{ loop.index0 }}:
  294. ssh_known_hosts.present:
  295. - user: {{ name }}
  296. - name: {{ hostname }}
  297. {% if 'port' in host %}
  298. - port: {{ host['port'] }}
  299. {% endif -%}
  300. {% if 'fingerprint' in host %}
  301. - fingerprint: {{ host['fingerprint'] }}
  302. {% endif -%}
  303. {% if 'key' in host %}
  304. - key: {{ host['key'] }}
  305. {% endif -%}
  306. {% if 'enc' in host %}
  307. - enc: {{ host['enc'] }}
  308. {% endif -%}
  309. {% if 'hash_hostname' in host %}
  310. - hash_hostname: {{ host['hash_hostname'] }}
  311. {% endif -%}
  312. {% endfor %}
  313. {% endif %}
  314. {% if 'ssh_known_hosts.absent' in user %}
  315. {% for host in user['ssh_known_hosts.absent'] %}
  316. users_ssh_known_hosts_delete_{{ name }}_{{ loop.index0 }}:
  317. ssh_known_hosts.absent:
  318. - user: {{ name }}
  319. - name: {{ host }}
  320. {% endfor %}
  321. {% endif %}
  322. {% if 'sudouser' in user and user['sudouser'] %}
  323. users_sudoer-{{ name }}:
  324. file.managed:
  325. - replace: False
  326. - name: {{ users.sudoers_dir }}/{{ name }}
  327. - user: root
  328. - group: {{ users.root_group }}
  329. - mode: '0440'
  330. {% if 'sudo_rules' in user or 'sudo_defaults' in user %}
  331. #{#%
  332. {% if 'sudo_rules' in user %}
  333. {% for rule in user['sudo_rules'] %}
  334. "validate {{ name }} sudo rule {{ loop.index0 }} {{ name }} {{ rule }}":
  335. cmd.run:
  336. - name: 'visudo -cf - <<<"$rule" | { read output; if [[ $output != "stdin: parsed OK" ]] ; then echo $output ; fi }'
  337. - stateful: True
  338. - shell: {{ users.visudo_shell }}
  339. - env:
  340. # Specify the rule via an env var to avoid shell quoting issues.
  341. - rule: "{{ name }} {{ rule }}"
  342. - require_in:
  343. - file: users_{{ users.sudoers_dir }}/{{ name }}
  344. {% endfor %}
  345. {% endif %}
  346. {% if 'sudo_defaults' in user %}
  347. {% for entry in user['sudo_defaults'] %}
  348. "validate {{ name }} sudo Defaults {{ loop.index0 }} {{ name }} {{ entry }}":
  349. cmd.run:
  350. - name: 'visudo -cf - <<<"$rule" | { read output; if [[ $output != "stdin: parsed OK" ]] ; then echo $output ; fi }'
  351. - stateful: True
  352. - shell: {{ users.visudo_shell }}
  353. - env:
  354. # Specify the rule via an env var to avoid shell quoting issues.
  355. - rule: "Defaults:{{ name }} {{ entry }}"
  356. - require_in:
  357. - file: users_{{ users.sudoers_dir }}/{{ name }}
  358. {% endfor %}
  359. {% endif %}
  360. #%#}
  361. users_{{ users.sudoers_dir }}/{{ name }}:
  362. file.managed:
  363. - replace: True
  364. - name: {{ users.sudoers_dir }}/{{ name }}
  365. - contents: |
  366. {%- if 'sudo_defaults' in user %}
  367. {%- for entry in user['sudo_defaults'] %}
  368. Defaults:{{ name }} {{ entry }}
  369. {%- endfor %}
  370. {%- endif %}
  371. {%- if 'sudo_rules' in user %}
  372. ########################################################################
  373. # File managed by Salt (users-formula).
  374. # Your changes will be overwritten.
  375. ########################################################################
  376. #
  377. {%- for rule in user['sudo_rules'] %}
  378. {{ name }} {{ rule }}
  379. {%- endfor %}
  380. {%- endif %}
  381. - require:
  382. - file: users_sudoer-defaults
  383. - file: users_sudoer-{{ name }}
  384. cmd.wait:
  385. - name: visudo -cf {{ users.sudoers_dir }}/{{ name }} || ( rm -rvf {{ users.sudoers_dir }}/{{ name }}; exit 1 )
  386. - watch:
  387. - file: {{ users.sudoers_dir }}/{{ name }}
  388. {% endif %}
  389. {% else %}
  390. users_{{ users.sudoers_dir }}/{{ name }}:
  391. file.absent:
  392. - name: {{ users.sudoers_dir }}/{{ name }}
  393. {% endif %}
  394. {%- if 'google_auth' in user %}
  395. {%- for svc in user['google_auth'] %}
  396. users_googleauth-{{ svc }}-{{ name }}:
  397. file.managed:
  398. - replace: false
  399. - name: {{ users.googleauth_dir }}/{{ name }}_{{ svc }}
  400. - contents_pillar: 'users:{{ name }}:google_auth:{{ svc }}'
  401. - user: root
  402. - group: {{ users.root_group }}
  403. - mode: 400
  404. - require:
  405. - pkg: users_googleauth-package
  406. {%- endfor %}
  407. {%- endif %}
  408. {% if 'gitconfig' in user %}
  409. {% if not salt['cmd.has_exec']('git') %}
  410. skip_{{ name }}_gitconfig_since_git_not_installed:
  411. test.fail_without_changes:
  412. - name: "Git configuration for user {{ name }} has been skipped because Git is not installed."
  413. {% else %}
  414. {% for key, value in user['gitconfig'].items() %}
  415. users_{{ name }}_user_gitconfig_{{ loop.index0 }}:
  416. {% if grains['saltversioninfo'] >= (2015, 8, 0, 0) %}
  417. git.config_set:
  418. {% else %}
  419. git.config:
  420. {% endif %}
  421. - name: {{ key }}
  422. - value: "{{ value }}"
  423. - user: {{ name }}
  424. {% if grains['saltversioninfo'] >= (2015, 8, 0, 0) %}
  425. - global: True
  426. {% else %}
  427. - is_global: True
  428. {% endif %}
  429. {% endfor %}
  430. {% endif %}
  431. {% endif %}
  432. {% endfor %}
  433. {% for name, user in pillar.get('users', {}).items()
  434. if user.absent is defined and user.absent %}
  435. users_absent_user_{{ name }}:
  436. {% if 'purge' in user or 'force' in user %}
  437. user.absent:
  438. - name: {{ name }}
  439. {% if 'purge' in user %}
  440. - purge: {{ user['purge'] }}
  441. {% endif %}
  442. {% if 'force' in user %}
  443. - force: {{ user['force'] }}
  444. {% endif %}
  445. {% else %}
  446. user.absent:
  447. - name: {{ name }}
  448. {% endif -%}
  449. users_{{ users.sudoers_dir }}/{{ name }}:
  450. file.absent:
  451. - name: {{ users.sudoers_dir }}/{{ name }}
  452. {% endfor %}
  453. {% for user in pillar.get('absent_users', []) %}
  454. users_absent_user_2_{{ user }}:
  455. user.absent:
  456. - name: {{ name }}
  457. users_2_{{ users.sudoers_dir }}/{{ user }}:
  458. file.absent:
  459. - name: {{ users.sudoers_dir }}/{{ user }}
  460. {% endfor %}
  461. {% for group in pillar.get('absent_groups', []) %}
  462. users_absent_group_{{ group }}:
  463. group.absent:
  464. - name: {{ group }}
  465. {% endfor %}