Saltstack Official Users Formula
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

349 rindas
9.9KB

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