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.

384 líneas
11KB

  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. {%- for name, user in pillar.get('users', {}).items()
  6. if user.absent is not defined or not user.absent %}
  7. {%- if user == None -%}
  8. {%- set user = {} -%}
  9. {%- endif -%}
  10. {%- if 'sudouser' in user and user['sudouser'] %}
  11. {%- do used_sudo.append(1) %}
  12. {%- endif %}
  13. {%- if 'google_auth' in user %}
  14. {%- do used_googleauth.append(1) %}
  15. {%- endif %}
  16. {%- endfor %}
  17. {%- if used_sudo or used_googleauth %}
  18. include:
  19. {%- if used_sudo %}
  20. - users.sudo
  21. {%- endif %}
  22. {%- if used_googleauth %}
  23. - users.googleauth
  24. {%- endif %}
  25. {%- endif %}
  26. {% for name, user in pillar.get('users', {}).items()
  27. if user.absent is not defined or not user.absent %}
  28. {%- if user == None -%}
  29. {%- set user = {} -%}
  30. {%- endif -%}
  31. {%- set home = user.get('home', "/home/%s" % name) -%}
  32. {%- if 'prime_group' in user and 'name' in user['prime_group'] %}
  33. {%- set user_group = user.prime_group.name -%}
  34. {%- else -%}
  35. {%- set user_group = name -%}
  36. {%- endif %}
  37. {% for group in user.get('groups', []) %}
  38. users_{{ name }}_{{ group }}_group:
  39. group:
  40. - name: {{ group }}
  41. - present
  42. {% endfor %}
  43. users_{{ name }}_user:
  44. {% if user.get('createhome', True) %}
  45. file.directory:
  46. - name: {{ home }}
  47. - user: {{ name }}
  48. - group: {{ user_group }}
  49. - mode: {{ user.get('user_dir_mode', '0750') }}
  50. - require:
  51. - user: users_{{ name }}_user
  52. - group: {{ user_group }}
  53. {%- endif %}
  54. group.present:
  55. - name: {{ user_group }}
  56. {%- if 'prime_group' in user and 'gid' in user['prime_group'] %}
  57. - gid: {{ user['prime_group']['gid'] }}
  58. {%- elif 'uid' in user %}
  59. - gid: {{ user['uid'] }}
  60. {%- endif %}
  61. user.present:
  62. - name: {{ name }}
  63. - home: {{ home }}
  64. - shell: {{ user.get('shell', users.get('shell', '/bin/bash')) }}
  65. {% if 'uid' in user -%}
  66. - uid: {{ user['uid'] }}
  67. {% endif -%}
  68. {% if 'password' in user -%}
  69. - password: '{{ user['password'] }}'
  70. {% endif -%}
  71. {% if 'enforce_password' in user -%}
  72. - enforce_password: {{ user['enforce_password'] }}
  73. {% endif -%}
  74. {% if user.get('system', False) -%}
  75. - system: True
  76. {% endif -%}
  77. {% if 'prime_group' in user and 'gid' in user['prime_group'] -%}
  78. - gid: {{ user['prime_group']['gid'] }}
  79. {% else -%}
  80. - gid_from_name: True
  81. {% endif -%}
  82. {% if 'fullname' in user %}
  83. - fullname: {{ user['fullname'] }}
  84. {% endif -%}
  85. {% if not user.get('createhome', True) %}
  86. - createhome: False
  87. {% endif %}
  88. {% if 'expire' in user -%}
  89. - expire: {{ user['expire'] }}
  90. {% endif -%}
  91. - remove_groups: {{ user.get('remove_groups', 'False') }}
  92. - groups:
  93. - {{ user_group }}
  94. {% for group in user.get('groups', []) -%}
  95. - {{ group }}
  96. {% endfor %}
  97. - require:
  98. - group: {{ user_group }}
  99. {% for group in user.get('groups', []) -%}
  100. - group: {{ group }}
  101. {% endfor %}
  102. {% if 'ssh_keys' in user or
  103. 'ssh_auth' in user or
  104. 'ssh_auth_file' in user or
  105. 'ssh_auth.absent' in user or
  106. 'ssh_config' in user %}
  107. user_keydir_{{ name }}:
  108. file.directory:
  109. - name: {{ user.get('home', '/home/{0}'.format(name)) }}/.ssh
  110. - user: {{ name }}
  111. - group: {{ user_group }}
  112. - makedirs: True
  113. - mode: 700
  114. - require:
  115. - user: {{ name }}
  116. - group: {{ user_group }}
  117. {%- for group in user.get('groups', []) %}
  118. - group: {{ group }}
  119. {%- endfor %}
  120. {% endif %}
  121. {% if 'ssh_keys' in user %}
  122. {% set key_type = 'id_' + user.get('ssh_key_type', 'rsa') %}
  123. users_user_{{ name }}_private_key:
  124. file.managed:
  125. - name: {{ user.get('home',
  126. '/home/{0}'.format(name)) }}/.ssh/{{ key_type }}
  127. - user: {{ name }}
  128. - group: {{ user_group }}
  129. - mode: 600
  130. - show_diff: False
  131. - contents_pillar: users:{{ name }}:ssh_keys:privkey
  132. - require:
  133. - user: users_{{ name }}_user
  134. {% for group in user.get('groups', []) %}
  135. - group: users_{{ name }}_{{ group }}_group
  136. {% endfor %}
  137. users_user_{{ name }}_public_key:
  138. file.managed:
  139. - name: {{ user.get('home',
  140. '/home/{0}'.format(name)) }}/.ssh/{{ key_type }}.pub
  141. - user: {{ name }}
  142. - group: {{ user_group }}
  143. - mode: 644
  144. - show_diff: False
  145. - contents_pillar: users:{{ name }}:ssh_keys:pubkey
  146. - require:
  147. - user: users_{{ name }}_user
  148. {% for group in user.get('groups', []) %}
  149. - group: users_{{ name }}_{{ group }}_group
  150. {% endfor %}
  151. {% endif %}
  152. {% if 'ssh_auth_file' in user or 'ssh_auth_pillar' in user %}
  153. users_authorized_keys_{{ name }}:
  154. file.managed:
  155. - name: {{ home }}/.ssh/authorized_keys
  156. - user: {{ name }}
  157. - group: {{ name }}
  158. - mode: 600
  159. {% if 'ssh_auth_file' in user %}
  160. - contents: |
  161. {% for auth in user.ssh_auth_file -%}
  162. {{ auth }}
  163. {% endfor -%}
  164. {% else %}
  165. - contents: |
  166. {%- for key_name, pillar_name in user['ssh_auth_pillar'].iteritems() %}
  167. {{ salt['pillar.get'](pillar_name + ':' + key_name + ':pubkey', '') }}
  168. {%- endfor %}
  169. {% endif %}
  170. {% endif %}
  171. {% if 'ssh_auth' in user %}
  172. {% for auth in user['ssh_auth'] %}
  173. users_ssh_auth_{{ name }}_{{ loop.index0 }}:
  174. ssh_auth.present:
  175. - user: {{ name }}
  176. - name: {{ auth }}
  177. - require:
  178. - file: users_{{ name }}_user
  179. - user: users_{{ name }}_user
  180. {% endfor %}
  181. {% endif %}
  182. {% if 'ssh_keys_pillar' in user %}
  183. {% for key_name, pillar_name in user['ssh_keys_pillar'].items() %}
  184. user_ssh_keys_files_{{ name }}_{{ key_name }}_private_key:
  185. file.managed:
  186. - name: {{ user.get('home',
  187. '/home/{0}'.format(name)) }}/.ssh/{{ key_name }}
  188. - user: {{ name }}
  189. - group: {{ user_group }}
  190. - mode: 600
  191. - show_diff: False
  192. - contents_pillar: {{ pillar_name }}:{{ key_name }}:privkey
  193. - require:
  194. - user: users_{{ name }}_user
  195. {% for group in user.get('groups', []) %}
  196. - group: users_{{ name }}_{{ group }}_group
  197. {% endfor %}
  198. user_ssh_keys_files_{{ name }}_{{ key_name }}_public_key:
  199. file.managed:
  200. - name: {{ user.get('home',
  201. '/home/{0}'.format(name)) }}/.ssh/{{ key_name }}.pub
  202. - user: {{ name }}
  203. - group: {{ user_group }}
  204. - mode: 644
  205. - show_diff: False
  206. - contents_pillar: {{ pillar_name }}:{{ key_name }}:pubkey
  207. - require:
  208. - user: users_{{ name }}_user
  209. {% for group in user.get('groups', []) %}
  210. - group: users_{{ name }}_{{ group }}_group
  211. {% endfor %}
  212. {% endfor %}
  213. {% endif %}
  214. {% if 'ssh_auth_sources' in user %}
  215. {% for pubkey_file in user['ssh_auth_sources'] %}
  216. users_ssh_auth_source_{{ name }}_{{ loop.index0 }}:
  217. ssh_auth.present:
  218. - user: {{ name }}
  219. - source: {{ pubkey_file }}
  220. - require:
  221. - file: users_{{ name }}_user
  222. - user: users_{{ name }}_user
  223. {% endfor %}
  224. {% endif %}
  225. {% if 'ssh_auth.absent' in user %}
  226. {% for auth in user['ssh_auth.absent'] %}
  227. users_ssh_auth_delete_{{ name }}_{{ loop.index0 }}:
  228. ssh_auth.absent:
  229. - user: {{ name }}
  230. - name: {{ auth }}
  231. - require:
  232. - file: users_{{ name }}_user
  233. - user: users_{{ name }}_user
  234. {% endfor %}
  235. {% endif %}
  236. {% if 'ssh_config' in user %}
  237. users_ssh_config_{{ name }}:
  238. file.managed:
  239. - name: {{ home }}/.ssh/config
  240. - user: {{ name }}
  241. - group: {{ user_group }}
  242. - mode: 640
  243. - contents: |
  244. # Managed by Saltstack
  245. # Do Not Edit
  246. {% for label, setting in user.ssh_config.items() %}
  247. # {{ label }}
  248. Host {{ setting.get('hostname') }}
  249. {%- for opts in setting.get('options') %}
  250. {{ opts }}
  251. {%- endfor %}
  252. {% endfor -%}
  253. {% endif %}
  254. {% if 'sudouser' in user and user['sudouser'] %}
  255. users_sudoer-{{ name }}:
  256. file.managed:
  257. - name: {{ users.sudoers_dir }}/{{ name }}
  258. - user: root
  259. - group: {{ users.root_group }}
  260. - mode: '0440'
  261. {% if 'sudo_rules' in user or 'sudo_defaults' in user %}
  262. {% if 'sudo_rules' in user %}
  263. {% for rule in user['sudo_rules'] %}
  264. "validate {{ name }} sudo rule {{ loop.index0 }} {{ name }} {{ rule }}":
  265. cmd.run:
  266. - name: 'visudo -cf - <<<"$rule" | { read output; if [[ $output != "stdin: parsed OK" ]] ; then echo $output ; fi }'
  267. - stateful: True
  268. - shell: {{ users.visudo_shell }}
  269. - env:
  270. # Specify the rule via an env var to avoid shell quoting issues.
  271. - rule: "{{ name }} {{ rule }}"
  272. - require_in:
  273. - file: users_{{ users.sudoers_dir }}/{{ name }}
  274. {% endfor %}
  275. {% endif %}
  276. {% if 'sudo_defaults' in user %}
  277. {% for entry in user['sudo_defaults'] %}
  278. "validate {{ name }} sudo Defaults {{ loop.index0 }} {{ name }} {{ entry }}":
  279. cmd.run:
  280. - name: 'visudo -cf - <<<"$rule" | { read output; if [[ $output != "stdin: parsed OK" ]] ; then echo $output ; fi }'
  281. - stateful: True
  282. - shell: {{ users.visudo_shell }}
  283. - env:
  284. # Specify the rule via an env var to avoid shell quoting issues.
  285. - rule: "Defaults:{{ name }} {{ entry }}"
  286. - require_in:
  287. - file: users_{{ users.sudoers_dir }}/{{ name }}
  288. {% endfor %}
  289. {% endif %}
  290. users_{{ users.sudoers_dir }}/{{ name }}:
  291. file.managed:
  292. - name: {{ users.sudoers_dir }}/{{ name }}
  293. - contents: |
  294. {%- if 'sudo_defaults' in user %}
  295. {%- for entry in user['sudo_defaults'] %}
  296. Defaults:{{ name }} {{ entry }}
  297. {%- endfor %}
  298. {%- endif %}
  299. {%- if 'sudo_rules' in user %}
  300. {%- for rule in user['sudo_rules'] %}
  301. {{ name }} {{ rule }}
  302. {%- endfor %}
  303. {%- endif %}
  304. - require:
  305. - file: users_sudoer-defaults
  306. - file: users_sudoer-{{ name }}
  307. {% endif %}
  308. {% else %}
  309. users_{{ users.sudoers_dir }}/{{ name }}:
  310. file.absent:
  311. - name: {{ users.sudoers_dir }}/{{ name }}
  312. {% endif %}
  313. {%- if 'google_auth' in user %}
  314. {%- for svc in user['google_auth'] %}
  315. users_googleauth-{{ svc }}-{{ name }}:
  316. file.managed:
  317. - replace: false
  318. - name: {{ users.googleauth_dir }}/{{ name }}_{{ svc }}
  319. - contents_pillar: 'users:{{ name }}:google_auth:{{ svc }}'
  320. - user: root
  321. - group: {{ users.root_group }}
  322. - mode: 400
  323. - require:
  324. - pkg: users_googleauth-package
  325. {%- endfor %}
  326. {%- endif %}
  327. {% endfor %}
  328. {% for name, user in pillar.get('users', {}).items()
  329. if user.absent is defined and user.absent %}
  330. users_absent_user_{{ name }}:
  331. {% if 'purge' in user or 'force' in user %}
  332. user.absent:
  333. - name: {{ name }}
  334. {% if 'purge' in user %}
  335. - purge: {{ user['purge'] }}
  336. {% endif %}
  337. {% if 'force' in user %}
  338. - force: {{ user['force'] }}
  339. {% endif %}
  340. {% else %}
  341. user.absent:
  342. - name: {{ name }}
  343. {% endif -%}
  344. users_{{ users.sudoers_dir }}/{{ name }}:
  345. file.absent:
  346. - name: {{ users.sudoers_dir }}/{{ name }}
  347. {% endfor %}
  348. {% for user in pillar.get('absent_users', []) %}
  349. users_absent_user_2_{{ user }}:
  350. user.absent
  351. users_2_{{ users.sudoers_dir }}/{{ user }}:
  352. file.absent:
  353. - name: {{ users.sudoers_dir }}/{{ user }}
  354. {% endfor %}
  355. {% for group in pillar.get('absent_groups', []) %}
  356. users_absent_group_{{ group }}:
  357. group.absent:
  358. - name: {{ group }}
  359. {% endfor %}