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.

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