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.

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