main.yml 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. ---
  2. - name: Check for presence of roxctl-token
  3. stat:
  4. path: "{{ ansible_facts['user_dir'] }}/roxctl-token"
  5. register: user_token_file
  6. - name: Alternatively, fall back to api-token
  7. stat:
  8. path: "{{ ansible_facts['user_dir'] }}/api-token"
  9. register: auto_token_file
  10. - assert:
  11. that: user_token_file.stat.exists or auto_token_file.stat.exists
  12. fail_msg: "ERROR: No roxctl-token file found."
  13. success_msg: "OK, proceeding with token from roxctl-token."
  14. - name: Symlink api-token to roxctl-token if latter is missing
  15. file:
  16. path: "{{ ansible_facts['user_dir'] }}/roxctl-token"
  17. src: "{{ ansible_facts['user_dir'] }}/api-token"
  18. state: link
  19. when: not user_token_file.stat.exists
  20. register: symlink_token
  21. - name: Ensure the scripts/ directory is there
  22. file:
  23. path: "{{ ansible_facts['user_dir'] }}/scripts"
  24. state: directory
  25. owner: "{{ ansible_user }}"
  26. group: "{{ ansible_user }}"
  27. mode: 0755
  28. - name: Ensure the two scripts are there
  29. copy:
  30. src: files/{{ item }}
  31. dest: "{{ ansible_facts['user_dir'] }}/scripts/{{ item }}"
  32. owner: "{{ ansible_user }}"
  33. group: "{{ ansible_user }}"
  34. mode: 0755
  35. loop:
  36. - dump-policies.sh
  37. - fix-policies.sh
  38. - name: Does policyexport exist?
  39. stat:
  40. path: "{{ ansible_facts['user_dir'] }}/policyexport"
  41. register: policy_export
  42. - name: Does api-policies exist?
  43. stat:
  44. path: "{{ ansible_facts['user_dir'] }}/api-policies"
  45. register: policy_backup
  46. - name: Fall back to api-policies if one exists
  47. file:
  48. path: "{{ ansible_facts['user_dir'] }}/policyexport"
  49. src: "{{ ansible_facts['user_dir'] }}/api-policies"
  50. state: link
  51. when:
  52. - not policy_export.stat.exists
  53. - policy_backup.stat.exists
  54. register: symlink_policies
  55. - name: Dump the policies
  56. ansible.builtin.shell:
  57. chdir: "{{ ansible_facts['user_dir'] }}"
  58. cmd: ./scripts/dump-policies.sh > policyexport
  59. creates: "{{ ansible_facts['user_dir'] }}/policyexport"
  60. - name: Fix the policies
  61. ansible.builtin.command:
  62. chdir: "{{ ansible_facts['user_dir'] }}"
  63. cmd: ./scripts/fix-policies.sh
  64. - name: Clean up token symlink
  65. file:
  66. path: "{{ ansible_facts['user_dir'] }}/roxctl-token"
  67. state: absent
  68. when: symlink_token is defined
  69. - name: Clean up policy symlink
  70. file:
  71. path: "{{ ansible_facts['user_dir'] }}/policyexport"
  72. state: absent
  73. when: symlink_policies is defined
  74. # Get a list of policies:
  75. # curl -ks -H "Authorization: Bearer $(cat roxctl-token)" -XGET https://central-rhacs.apps.ocp4.example.com/v1/policies | jq -r '[ .policies[].id ]'
  76. #
  77. # Dump the policies in that list:
  78. # curl -ks -H "Authorization: Bearer $(cat roxctl-token)" -XPOST -d "{ \"policyIds\": $(cat policyids) }" https://central-rhacs.apps.ocp4.example.com/v1/policies/export
  79. #
  80. # Match a policy by name and print its ID:
  81. # jq '.policies[] | select(.name | test("(?i)docker cis 4\\.1")) | .id'
  82. #
  83. # Fix a policy and return the fixed list:
  84. # jq '.policies |= map(if .name | test("(?i)docker cis 4\\.1") then .exclusions = [ { "name": "Skip system namespaces", "deployment": { "name": "", "scope": { "cluster": "", "namespace": "^kube-.*|^openshift-.*|^istio-.*|^rhacs$|^stackrox$", "label": null } }, "image": null, "expiration": null } ] else . end)'
  85. #
  86. # Extract one policy and fix it (along with its name):
  87. # jq '.policies[] | select(.name | test("(?i)docker cis 4\\.1")) | .exclusions |= [ { "name": "Skip system namespaces", "deployment": { "name": "", "scope": { "cluster": "", "namespace": "^kube-.*|^openshift-.*|^istio-.*|^rhacs$|^stackrox$", "label": null } }, "image": null, "expiration": null } ] | .name |= . + " (non-system)"'
  88. ...