Saltstack Official Users Formula
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

458 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. {% if 'hash_hostname' in host %}
  289. - hash_hostname: {{ host['hash_hostname'] }}
  290. {% endif -%}
  291. {% endfor %}
  292. {% endif %}
  293. {% if 'ssh_known_hosts.absent' in user %}
  294. {% for host in user['ssh_known_hosts.absent'] %}
  295. users_ssh_known_hosts_delete_{{ name }}_{{ loop.index0 }}:
  296. ssh_known_hosts.absent:
  297. - user: {{ name }}
  298. - name: {{ host }}
  299. {% endfor %}
  300. {% endif %}
  301. {% if 'sudouser' in user and user['sudouser'] %}
  302. users_sudoer-{{ name }}:
  303. file.managed:
  304. - name: {{ users.sudoers_dir }}/{{ name }}
  305. - user: root
  306. - group: {{ users.root_group }}
  307. - mode: '0440'
  308. {% if 'sudo_rules' in user or 'sudo_defaults' in user %}
  309. #{#%
  310. {% if 'sudo_rules' in user %}
  311. {% for rule in user['sudo_rules'] %}
  312. "validate {{ name }} sudo rule {{ loop.index0 }} {{ name }} {{ rule }}":
  313. cmd.run:
  314. - name: 'visudo -cf - <<<"$rule" | { read output; if [[ $output != "stdin: parsed OK" ]] ; then echo $output ; fi }'
  315. - stateful: True
  316. - shell: {{ users.visudo_shell }}
  317. - env:
  318. # Specify the rule via an env var to avoid shell quoting issues.
  319. - rule: "{{ name }} {{ rule }}"
  320. - require_in:
  321. - file: users_{{ users.sudoers_dir }}/{{ name }}
  322. {% endfor %}
  323. {% endif %}
  324. {% if 'sudo_defaults' in user %}
  325. {% for entry in user['sudo_defaults'] %}
  326. "validate {{ name }} sudo Defaults {{ loop.index0 }} {{ name }} {{ entry }}":
  327. cmd.run:
  328. - name: 'visudo -cf - <<<"$rule" | { read output; if [[ $output != "stdin: parsed OK" ]] ; then echo $output ; fi }'
  329. - stateful: True
  330. - shell: {{ users.visudo_shell }}
  331. - env:
  332. # Specify the rule via an env var to avoid shell quoting issues.
  333. - rule: "Defaults:{{ name }} {{ entry }}"
  334. - require_in:
  335. - file: users_{{ users.sudoers_dir }}/{{ name }}
  336. {% endfor %}
  337. {% endif %}
  338. #%#}
  339. users_{{ users.sudoers_dir }}/{{ name }}:
  340. file.managed:
  341. - name: {{ users.sudoers_dir }}/{{ name }}
  342. - contents: |
  343. {%- if 'sudo_defaults' in user %}
  344. {%- for entry in user['sudo_defaults'] %}
  345. Defaults:{{ name }} {{ entry }}
  346. {%- endfor %}
  347. {%- endif %}
  348. {%- if 'sudo_rules' in user %}
  349. {%- for rule in user['sudo_rules'] %}
  350. {{ name }} {{ rule }}
  351. {%- endfor %}
  352. {%- endif %}
  353. - require:
  354. - file: users_sudoer-defaults
  355. - file: users_sudoer-{{ name }}
  356. cmd.wait:
  357. - name: visudo -cf {{ users.sudoers_dir }}/{{ name }} || ( rm -rvf {{ users.sudoers_dir }}/{{ name }}; exit 1 )
  358. - watch:
  359. - file: {{ users.sudoers_dir }}/{{ name }}
  360. {% endif %}
  361. {% else %}
  362. users_{{ users.sudoers_dir }}/{{ name }}:
  363. file.absent:
  364. - name: {{ users.sudoers_dir }}/{{ name }}
  365. {% endif %}
  366. {%- if 'google_auth' in user %}
  367. {%- for svc in user['google_auth'] %}
  368. users_googleauth-{{ svc }}-{{ name }}:
  369. file.managed:
  370. - replace: false
  371. - name: {{ users.googleauth_dir }}/{{ name }}_{{ svc }}
  372. - contents_pillar: 'users:{{ name }}:google_auth:{{ svc }}'
  373. - user: root
  374. - group: {{ users.root_group }}
  375. - mode: 400
  376. - require:
  377. - pkg: users_googleauth-package
  378. {%- endfor %}
  379. {%- endif %}
  380. {% if 'gitconfig' in user %}
  381. {% for key, value in user['gitconfig'].items() %}
  382. users_{{ name }}_user_gitconfig_{{ loop.index0 }}:
  383. {% if grains['saltversioninfo'] >= (2015, 8, 0, 0) %}
  384. git.config_set:
  385. {% else %}
  386. git.config:
  387. {% endif %}
  388. - name: {{ key }}
  389. - value: "{{ value }}"
  390. - user: {{ name }}
  391. {% if grains['saltversioninfo'] >= (2015, 8, 0, 0) %}
  392. - global: True
  393. {% else %}
  394. - is_global: True
  395. {% endif %}
  396. {% endfor %}
  397. {% endif %}
  398. {% endfor %}
  399. {% for name, user in pillar.get('users', {}).items()
  400. if user.absent is defined and user.absent %}
  401. users_absent_user_{{ name }}:
  402. {% if 'purge' in user or 'force' in user %}
  403. user.absent:
  404. - name: {{ name }}
  405. {% if 'purge' in user %}
  406. - purge: {{ user['purge'] }}
  407. {% endif %}
  408. {% if 'force' in user %}
  409. - force: {{ user['force'] }}
  410. {% endif %}
  411. {% else %}
  412. user.absent:
  413. - name: {{ name }}
  414. {% endif -%}
  415. users_{{ users.sudoers_dir }}/{{ name }}:
  416. file.absent:
  417. - name: {{ users.sudoers_dir }}/{{ name }}
  418. {% endfor %}
  419. {% for user in pillar.get('absent_users', []) %}
  420. users_absent_user_2_{{ user }}:
  421. user.absent
  422. users_2_{{ users.sudoers_dir }}/{{ user }}:
  423. file.absent:
  424. - name: {{ users.sudoers_dir }}/{{ user }}
  425. {% endfor %}
  426. {% for group in pillar.get('absent_groups', []) %}
  427. users_absent_group_{{ group }}:
  428. group.absent:
  429. - name: {{ group }}
  430. {% endfor %}