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.

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