Saltstack Official Users Formula
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

366 lines
10KB

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