################################################################################
# BSD LICENSE
#
# Copyright(c) 2021-2023 Intel Corporation. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
#   * Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#   * Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in
#     the documentation and/or other materials provided with the
#     distribution.
#   * Neither the name of Intel Corporation nor the names of its
#     contributors may be used to endorse or promote products derived
#     from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
################################################################################

---
- name: Verify that 'cert_dir' variable is defined
  assert:
    that: cert_dir is defined
    fail_msg: >-
      Please provide cert_dir variable using --extra-vars 'cert_dir=<CERT_DIR>'
      where CERT_DIR points to directory with AppQoS mTLS certificates. Please
      read README.md for more info about AppQoS certificates

- name: Check provided 'cert_dir={{ cert_dir }}' with stat
  stat:
    path: "{{ cert_dir }}"
  register: stat_cert_dir

- name: Verify that 'cert_dir={{ cert_dir }}' exists
  assert:
    that: stat_cert_dir.stat.exists
    fail_msg: >-
      Provided 'cert_dir={{ cert_dir }}' does not exists

- name: Verify that 'cert_dir={{ cert_dir }}' is a directory
  assert:
    that: stat_cert_dir.stat.isdir
    fail_msg: >-
      Provided 'cert_dir={{ cert_dir }}' is not a directory

- name: Get stat for files in  'cert_dir={{ cert_dir }}'
  stat:
    path: "{{ cert_dir }}/{{ item }}"
  register: stat_cert_dir_content_items
  with_items: "{{ appqos_ca_required_content }}"

- name: Check if all needed cert/key files exists in 'cert_dir={{ cert_dir }}'
  assert:
    that: item.stat.exists
    fail_msg: >-
      AppQoS cert_dir={{ cert_dir }} should contain files:
      {{ appqos_ca_required_content }}
  with_items: "{{ stat_cert_dir_content_items.results }}"

- name: Copy certificates and key
  copy:
    src: "{{ cert_dir }}/{{ item }}"
    dest: "{{ appqos_ca_dir }}/"
    mode: preserve
    owner: "{{ ansible_user_id }}"
  with_items: "{{ appqos_ca_required_content }}"

- name: Set limited permissions to AppQoS key
  file:
    path: "{{ appqos_ca_dir }}/{{ appqos_key_file }}"
    mode: '0400'
