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.

init.sls 8.2KB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. {{ name }}_{{ group }}_group:
  37. group:
  38. - name: {{ group }}
  39. - present
  40. {% endfor %}
  41. {{ 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. 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. 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: {{ name }}_user
  119. {% for group in user.get('groups', []) %}
  120. - group: {{ name }}_{{ group }}_group
  121. {% endfor %}
  122. 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: {{ name }}_user
  132. {% for group in user.get('groups', []) %}
  133. - group: {{ name }}_{{ group }}_group
  134. {% endfor %}
  135. {% endif %}
  136. {% if 'ssh_auth_file' in user %}
  137. {{ home }}/.ssh/authorized_keys:
  138. file.managed:
  139. - user: {{ name }}
  140. - group: {{ name }}
  141. - mode: 600
  142. - contents: |
  143. {% for auth in user.ssh_auth_file -%}
  144. {{ auth }}
  145. {% endfor -%}
  146. {% endif %}
  147. {% if 'ssh_auth' in user %}
  148. {% for auth in user['ssh_auth'] %}
  149. ssh_auth_{{ name }}_{{ loop.index0 }}:
  150. ssh_auth.present:
  151. - user: {{ name }}
  152. - name: {{ auth }}
  153. - require:
  154. - file: {{ name }}_user
  155. - user: {{ name }}_user
  156. {% endfor %}
  157. {% endif %}
  158. {% if 'ssh_auth_sources' in user %}
  159. {% for pubkey_file in user['ssh_auth_sources'] %}
  160. ssh_auth_source_{{ name }}_{{ loop.index0 }}:
  161. ssh_auth.present:
  162. - user: {{ name }}
  163. - source: {{ pubkey_file }}
  164. - require:
  165. - file: {{ name }}_user
  166. - user: {{ name }}_user
  167. {% endfor %}
  168. {% endif %}
  169. {% if 'ssh_auth.absent' in user %}
  170. {% for auth in user['ssh_auth.absent'] %}
  171. ssh_auth_delete_{{ name }}_{{ loop.index0 }}:
  172. ssh_auth.absent:
  173. - user: {{ name }}
  174. - name: {{ auth }}
  175. - require:
  176. - file: {{ name }}_user
  177. - user: {{ name }}_user
  178. {% endfor %}
  179. {% endif %}
  180. {% if 'sudouser' in user and user['sudouser'] %}
  181. sudoer-{{ name }}:
  182. file.managed:
  183. - name: {{ users.sudoers_dir }}/{{ name }}
  184. - user: root
  185. - group: {{ users.root_group }}
  186. - mode: '0440'
  187. {% if 'sudo_rules' in user or 'sudo_defaults' in user %}
  188. {% if 'sudo_rules' in user %}
  189. {% for rule in user['sudo_rules'] %}
  190. "validate {{ name }} sudo rule {{ loop.index0 }} {{ name }} {{ rule }}":
  191. cmd.run:
  192. - name: 'visudo -cf - <<<"$rule" | { read output; if [[ $output != "stdin: parsed OK" ]] ; then echo $output ; fi }'
  193. - stateful: True
  194. - shell: {{ users.visudo_shell }}
  195. - env:
  196. # Specify the rule via an env var to avoid shell quoting issues.
  197. - rule: "{{ name }} {{ rule }}"
  198. - require_in:
  199. - file: {{ users.sudoers_dir }}/{{ name }}
  200. {% endfor %}
  201. {% endif %}
  202. {% if 'sudo_defaults' in user %}
  203. {% for entry in user['sudo_defaults'] %}
  204. "validate {{ name }} sudo Defaults {{ loop.index0 }} {{ name }} {{ entry }}":
  205. cmd.run:
  206. - name: 'visudo -cf - <<<"$rule" | { read output; if [[ $output != "stdin: parsed OK" ]] ; then echo $output ; fi }'
  207. - stateful: True
  208. - shell: {{ users.visudo_shell }}
  209. - env:
  210. # Specify the rule via an env var to avoid shell quoting issues.
  211. - rule: "Defaults:{{ name }} {{ entry }}"
  212. - require_in:
  213. - file: {{ users.sudoers_dir }}/{{ name }}
  214. {% endfor %}
  215. {% endif %}
  216. {{ users.sudoers_dir }}/{{ name }}:
  217. file.managed:
  218. - contents: |
  219. {%- if 'sudo_defaults' in user %}
  220. {%- for entry in user['sudo_defaults'] %}
  221. Defaults:{{ name }} {{ entry }}
  222. {%- endfor %}
  223. {%- endif %}
  224. {%- if 'sudo_rules' in user %}
  225. {%- for rule in user['sudo_rules'] %}
  226. {{ name }} {{ rule }}
  227. {%- endfor %}
  228. {%- endif %}
  229. - require:
  230. - file: sudoer-defaults
  231. - file: sudoer-{{ name }}
  232. {% endif %}
  233. {% else %}
  234. {{ users.sudoers_dir }}/{{ name }}:
  235. file.absent:
  236. - name: {{ users.sudoers_dir }}/{{ name }}
  237. {% endif %}
  238. {%- if 'google_auth' in user %}
  239. {%- for svc in user['google_auth'] %}
  240. googleauth-{{ svc }}-{{ name }}:
  241. file.managed:
  242. - replace: false
  243. - name: {{ users.googleauth_dir }}/{{ name }}_{{ svc }}
  244. - contents_pillar: 'users:{{ name }}:google_auth:{{ svc }}'
  245. - user: root
  246. - group: {{ users.root_group }}
  247. - mode: 600
  248. - require:
  249. - pkg: googleauth-package
  250. {%- endfor %}
  251. {%- endif %}
  252. {% endfor %}
  253. {% for name, user in pillar.get('users', {}).items() if user.absent is defined and user.absent %}
  254. {{ name }}:
  255. {% if 'purge' in user or 'force' in user %}
  256. user.absent:
  257. {% if 'purge' in user %}
  258. - purge: {{ user['purge'] }}
  259. {% endif %}
  260. {% if 'force' in user %}
  261. - force: {{ user['force'] }}
  262. {% endif %}
  263. {% else %}
  264. user.absent
  265. {% endif -%}
  266. {{ users.sudoers_dir }}/{{ name }}:
  267. file.absent:
  268. - name: {{ users.sudoers_dir }}/{{ name }}
  269. {% endfor %}
  270. {% for user in pillar.get('absent_users', []) %}
  271. {{ user }}:
  272. user.absent
  273. {{ users.sudoers_dir }}/{{ user }}:
  274. file.absent:
  275. - name: {{ users.sudoers_dir }}/{{ user }}
  276. {% endfor %}
  277. {% for group in pillar.get('absent_groups', []) %}
  278. {{ group }}:
  279. group.absent
  280. {% endfor %}