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 6.9KB

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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 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. user_keydir_{{ name }}:
  98. file.directory:
  99. - name: {{ user.get('home', '/home/{0}'.format(name)) }}/.ssh
  100. - user: {{ name }}
  101. - group: {{ user_group }}
  102. - makedirs: True
  103. - mode: 700
  104. - require:
  105. - user: {{ name }}
  106. - group: {{ user_group }}
  107. {%- for group in user.get('groups', []) %}
  108. - group: {{ group }}
  109. {%- endfor %}
  110. {% if 'ssh_keys' in user %}
  111. {% set key_type = 'id_' + user.get('ssh_key_type', 'rsa') %}
  112. user_{{ name }}_private_key:
  113. file.managed:
  114. - name: {{ user.get('home', '/home/{0}'.format(name)) }}/.ssh/{{ key_type }}
  115. - user: {{ name }}
  116. - group: {{ user_group }}
  117. - mode: 600
  118. - show_diff: False
  119. - contents_pillar: users:{{ name }}:ssh_keys:privkey
  120. - require:
  121. - user: {{ name }}_user
  122. {% for group in user.get('groups', []) %}
  123. - group: {{ name }}_{{ group }}_group
  124. {% endfor %}
  125. user_{{ name }}_public_key:
  126. file.managed:
  127. - name: {{ user.get('home', '/home/{0}'.format(name)) }}/.ssh/{{ key_type }}.pub
  128. - user: {{ name }}
  129. - group: {{ user_group }}
  130. - mode: 644
  131. - show_diff: False
  132. - contents_pillar: users:{{ name }}:ssh_keys:pubkey
  133. - require:
  134. - user: {{ name }}_user
  135. {% for group in user.get('groups', []) %}
  136. - group: {{ name }}_{{ group }}_group
  137. {% endfor %}
  138. {% endif %}
  139. {% if 'ssh_auth' in user %}
  140. {% for auth in user['ssh_auth'] %}
  141. ssh_auth_{{ name }}_{{ loop.index0 }}:
  142. ssh_auth.present:
  143. - user: {{ name }}
  144. - name: {{ auth }}
  145. - require:
  146. - file: {{ name }}_user
  147. - user: {{ name }}_user
  148. {% endfor %}
  149. {% endif %}
  150. {% if 'ssh_auth.absent' in user %}
  151. {% for auth in user['ssh_auth.absent'] %}
  152. ssh_auth_delete_{{ name }}_{{ loop.index0 }}:
  153. ssh_auth.absent:
  154. - user: {{ name }}
  155. - name: {{ auth }}
  156. - require:
  157. - file: {{ name }}_user
  158. - user: {{ name }}_user
  159. {% endfor %}
  160. {% endif %}
  161. {% if 'sudouser' in user and user['sudouser'] %}
  162. sudoer-{{ name }}:
  163. file.managed:
  164. - name: {{ users.sudoers_dir }}/{{ name }}
  165. - user: root
  166. - group: {{ users.root_group }}
  167. - mode: '0440'
  168. {% if 'sudo_rules' in user %}
  169. {% for rule in user['sudo_rules'] %}
  170. "validate {{ name }} sudo rule {{ loop.index0 }} {{ name }} {{ rule }}":
  171. cmd.run:
  172. - name: 'visudo -cf - <<<"$rule" | { read output; if [[ $output != "stdin: parsed OK" ]] ; then echo $output ; fi }'
  173. - stateful: True
  174. - shell: {{ users.visudo_shell }}
  175. - env:
  176. # Specify the rule via an env var to avoid shell quoting issues.
  177. - rule: "{{ name }} {{ rule }}"
  178. - require_in:
  179. - file: {{ users.sudoers_dir }}/{{ name }}
  180. {% endfor %}
  181. {{ users.sudoers_dir }}/{{ name }}:
  182. file.managed:
  183. - contents: |
  184. {%- for rule in user['sudo_rules'] %}
  185. {{ name }} {{ rule }}
  186. {%- endfor %}
  187. - require:
  188. - file: sudoer-defaults
  189. - file: sudoer-{{ name }}
  190. {% endif %}
  191. {% else %}
  192. {{ users.sudoers_dir }}/{{ name }}:
  193. file.absent:
  194. - name: {{ users.sudoers_dir }}/{{ name }}
  195. {% endif %}
  196. {%- if 'google_auth' in user %}
  197. {%- for svc in user['google_auth'] %}
  198. googleauth-{{ svc }}-{{ name }}:
  199. file.managed:
  200. - replace: false
  201. - name: {{ users.googleauth_dir }}/{{ name }}_{{ svc }}
  202. - contents_pillar: 'users:{{ name }}:google_auth:{{ svc }}'
  203. - user: root
  204. - group: {{ users.root_group }}
  205. - mode: 600
  206. - require:
  207. - pkg: googleauth-package
  208. {%- endfor %}
  209. {%- endif %}
  210. {% endfor %}
  211. {% for name, user in pillar.get('users', {}).items() if user.absent is defined and user.absent %}
  212. {{ name }}:
  213. {% if 'purge' in user or 'force' in user %}
  214. user.absent:
  215. {% if 'purge' in user %}
  216. - purge: {{ user['purge'] }}
  217. {% endif %}
  218. {% if 'force' in user %}
  219. - force: {{ user['force'] }}
  220. {% endif %}
  221. {% else %}
  222. user.absent
  223. {% endif -%}
  224. {{ users.sudoers_dir }}/{{ name }}:
  225. file.absent:
  226. - name: {{ users.sudoers_dir }}/{{ name }}
  227. {% endfor %}
  228. {% for user in pillar.get('absent_users', []) %}
  229. {{ user }}:
  230. user.absent
  231. {{ users.sudoers_dir }}/{{ user }}:
  232. file.absent:
  233. - name: {{ users.sudoers_dir }}/{{ user }}
  234. {% endfor %}
  235. {% for group in pillar.get('absent_groups', []) %}
  236. {{ group }}:
  237. group.absent
  238. {% endfor %}