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.

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