Saltstack Official Users Formula
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

489 líneas
14KB

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