---
- name: Retrieving the cert to use when generating secrets for the logging components
  slurp:
    src: "{{ generated_certs_dir }}/{{ item.file }}"
  register: key_pairs
  with_items:
    - { name: "ca_file", file: "ca.crt" }
    - { name: "kibana_internal_key", file: "kibana-internal.key"}
    - { name: "kibana_internal_cert", file: "kibana-internal.crt"}
    - { name: "server_tls", file: "server-tls.json"}
    - { name: "session_secret", file: "session_secret" }
    - { name: "oauth_secret", file: "oauth_secret" }

# create Kibana secret
- name: Set Kibana secret
  oc_secret:
    state: present
    name: "logging-kibana"
    namespace: "{{ openshift_logging_namespace }}"
    files:
      - name: ca
        path: "{{ generated_certs_dir }}/ca.crt"
      - name: key
        path: "{{ generated_certs_dir }}/system.logging.kibana.key"
      - name: cert
        path: "{{ generated_certs_dir }}/system.logging.kibana.crt"

# create Kibana-proxy secret
- name: Set Kibana Proxy secret
  oc_secret:
    state: present
    name: "logging-kibana-proxy"
    namespace: "{{ openshift_logging_namespace }}"
    # TODO: when possible to have both files and contents for oc_secret use this
    #files:
    #- name: server-key
    #  path: "{{ generated_certs_dir }}/kibana-internal.key"
    #- name: server-cert
    #  path: "{{ generated_certs_dir }}/kibana-internal.crt"
    #- name: server-tls.json
    #  path: "{{ generated_certs_dir }}/server-tls.json"
    contents:
      - path: oauth-secret
        data: "{{ key_pairs | entry_from_named_pair('oauth_secret') | b64decode }}"
      - path: session-secret
        data: "{{ key_pairs | entry_from_named_pair('session_secret') | b64decode }}"
      - path: server-key
        data: "{{ key_pairs | entry_from_named_pair('kibana_internal_key') | b64decode }}"
      - path: server-cert
        data: "{{ key_pairs | entry_from_named_pair('kibana_internal_cert') | b64decode }}"
      - path: server-tls.json
        data: "{{ key_pairs | entry_from_named_pair('server_tls') | b64decode }}"
