Saltstack Official Users Formula
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

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