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.

45 lines
1.8 KiB

  1. ---
  2. - name: Rotate Tokens | Get default token name
  3. shell: "{{ bin_dir }}/kubectl get secrets -o custom-columns=name:{.metadata.name} --no-headers | grep -m1 default-token"
  4. register: default_token
  5. changed_when: false
  6. - name: Rotate Tokens | Get default token data
  7. command: "{{ bin_dir }}/kubectl get secrets {{ default_token.stdout }} -ojson"
  8. register: default_token_data
  9. changed_when: false
  10. - name: Rotate Tokens | Test if default certificate is expired
  11. uri:
  12. url: https://{{ kube_apiserver_ip }}/api/v1/nodes
  13. method: GET
  14. return_content: no
  15. validate_certs: no
  16. headers:
  17. Authorization: "Bearer {{ (default_token_data.stdout|from_json)['data']['token']|b64decode }}"
  18. register: check_secret
  19. failed_when: false
  20. - name: Rotate Tokens | Determine if certificate is expired
  21. set_fact:
  22. needs_rotation: '{{ check_secret.status not in [200, 403] }}'
  23. # FIXME(mattymo): Exclude built in secrets that were automatically rotated,
  24. # instead of filtering manually
  25. - name: Rotate Tokens | Get all serviceaccount tokens to expire
  26. shell: >-
  27. {{ bin_dir }}/kubectl get secrets --all-namespaces
  28. -o 'jsonpath={range .items[*]}{"\n"}{.metadata.namespace}{" "}{.metadata.name}{" "}{.type}{end}'
  29. | grep kubernetes.io/service-account-token
  30. | egrep 'default-token|kube-proxy|kube-dns|dnsmasq|netchecker|weave|calico|canal|flannel|dashboard|cluster-proportional-autoscaler|efk|tiller'
  31. register: tokens_to_delete
  32. when: needs_rotation
  33. - name: Rotate Tokens | Delete expired tokens
  34. command: "{{ bin_dir }}/kubectl delete secrets -n {{ item.split(' ')[0] }} {{ item.split(' ')[1] }}"
  35. with_items: "{{ tokens_to_delete.stdout_lines }}"
  36. when: needs_rotation
  37. - name: Rotate Tokens | Delete pods in system namespace
  38. command: "{{ bin_dir }}/kubectl delete pods -n {{ system_namespace }} --all"
  39. when: needs_rotation