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.

343 líneas
9.7KB

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