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.

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