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.

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