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.

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