main.yml 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. # Version horrors. FQMN will choke here. Must use short name.
  56. - name: Dump the policies
  57. shell:
  58. cmd: "./scripts/dump-policies.sh > policyexport"
  59. chdir: "{{ ansible_facts['user_dir'] }}"
  60. creates: "{{ ansible_facts['user_dir'] }}/policyexport"
  61. # Version horrors. FQMN will choke here. Must use short name.
  62. - name: Fix the policies
  63. command:
  64. cmd: ./scripts/fix-policies.sh
  65. chdir: "{{ ansible_facts['user_dir'] }}"
  66. - name: Clean up token symlink
  67. file:
  68. path: "{{ ansible_facts['user_dir'] }}/roxctl-token"
  69. state: absent
  70. when:
  71. - symlink_token is defined
  72. - symlink_token.changed
  73. - name: Clean up policy symlink
  74. file:
  75. path: "{{ ansible_facts['user_dir'] }}/policyexport"
  76. state: absent
  77. when:
  78. - symlink_policies is defined
  79. - symlink_policies.changed
  80. # Get a list of policies:
  81. # curl -ks -H "Authorization: Bearer $(cat roxctl-token)" -XGET https://central-rhacs.apps.ocp4.example.com/v1/policies | jq -r '[ .policies[].id ]'
  82. #
  83. # Dump the policies in that list:
  84. # curl -ks -H "Authorization: Bearer $(cat roxctl-token)" -XPOST -d "{ \"policyIds\": $(cat policyids) }" https://central-rhacs.apps.ocp4.example.com/v1/policies/export
  85. #
  86. # Match a policy by name and print its ID:
  87. # jq '.policies[] | select(.name | test("(?i)docker cis 4\\.1")) | .id'
  88. #
  89. # Fix a policy and return the fixed list:
  90. # 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)'
  91. #
  92. # Extract one policy and fix it (along with its name):
  93. # 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)"'
  94. ...