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.

279 líneas
7.4KB

  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 %}
  188. {% for rule in user['sudo_rules'] %}
  189. "validate {{ name }} sudo rule {{ loop.index0 }} {{ name }} {{ rule }}":
  190. cmd.run:
  191. - name: 'visudo -cf - <<<"$rule" | { read output; if [[ $output != "stdin: parsed OK" ]] ; then echo $output ; fi }'
  192. - stateful: True
  193. - shell: {{ users.visudo_shell }}
  194. - env:
  195. # Specify the rule via an env var to avoid shell quoting issues.
  196. - rule: "{{ name }} {{ rule }}"
  197. - require_in:
  198. - file: {{ users.sudoers_dir }}/{{ name }}
  199. {% endfor %}
  200. {{ users.sudoers_dir }}/{{ name }}:
  201. file.managed:
  202. - contents: |
  203. {%- for rule in user['sudo_rules'] %}
  204. {{ name }} {{ rule }}
  205. {%- endfor %}
  206. - require:
  207. - file: sudoer-defaults
  208. - file: sudoer-{{ name }}
  209. {% endif %}
  210. {% else %}
  211. {{ users.sudoers_dir }}/{{ name }}:
  212. file.absent:
  213. - name: {{ users.sudoers_dir }}/{{ name }}
  214. {% endif %}
  215. {%- if 'google_auth' in user %}
  216. {%- for svc in user['google_auth'] %}
  217. googleauth-{{ svc }}-{{ name }}:
  218. file.managed:
  219. - replace: false
  220. - name: {{ users.googleauth_dir }}/{{ name }}_{{ svc }}
  221. - contents_pillar: 'users:{{ name }}:google_auth:{{ svc }}'
  222. - user: root
  223. - group: {{ users.root_group }}
  224. - mode: 600
  225. - require:
  226. - pkg: googleauth-package
  227. {%- endfor %}
  228. {%- endif %}
  229. {% endfor %}
  230. {% for name, user in pillar.get('users', {}).items() if user.absent is defined and user.absent %}
  231. {{ name }}:
  232. {% if 'purge' in user or 'force' in user %}
  233. user.absent:
  234. {% if 'purge' in user %}
  235. - purge: {{ user['purge'] }}
  236. {% endif %}
  237. {% if 'force' in user %}
  238. - force: {{ user['force'] }}
  239. {% endif %}
  240. {% else %}
  241. user.absent
  242. {% endif -%}
  243. {{ users.sudoers_dir }}/{{ name }}:
  244. file.absent:
  245. - name: {{ users.sudoers_dir }}/{{ name }}
  246. {% endfor %}
  247. {% for user in pillar.get('absent_users', []) %}
  248. {{ user }}:
  249. user.absent
  250. {{ users.sudoers_dir }}/{{ user }}:
  251. file.absent:
  252. - name: {{ users.sudoers_dir }}/{{ user }}
  253. {% endfor %}
  254. {% for group in pillar.get('absent_groups', []) %}
  255. {{ group }}:
  256. group.absent
  257. {% endfor %}