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.

461 lines
13KB

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