Saltstack Official Users Formula
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

init.sls 15KB

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