#!/bin/bash

set -x 

BASE=rmoc.reduspaceservices.eu
openssl pkcs12 -export -in fullchain.cer -in ${BASE}.cer -inkey ${BASE}.key -out ${BASE}.p12
openssl pkcs12 -in ${BASE}.p12 -nodes -out ${BASE}.pem



cp ca.cer fullchain.cer /etc/pki/ca-trust/source/anchors

update-ca-trust

rm /etc/pki/ovirt-engine/apache-ca.pem
cp ${BASE}.pem /etc/pki/ovirt-engine/apache-ca.pem

cp ${BASE}.p12 /etc/pki/ovirt-engine/keys/apache.p12

openssl pkcs12 -in  /etc/pki/ovirt-engine/keys/apache.p12 -nocerts -nodes > /etc/pki/ovirt-engine/keys/apache.key.nopass
openssl pkcs12 -in /etc/pki/ovirt-engine/keys/apache.p12 -nokeys > /etc/pki/ovirt-engine/certs/apache.cer
cat fullchain.cer >>/etc/pki/ovirt-engine/certs/apache.cer

cp /etc/pki/ovirt-engine/keys/apache.key.nopass /etc/pki/ovirt-engine/keys/imageio-proxy.key.nopass
cp /etc/pki/ovirt-engine/certs/apache.cer /etc/pki/ovirt-engine/certs/imageio-proxy.cer

cp /etc/pki/ovirt-engine/keys/apache.key.nopass /etc/pki/ovirt-engine/keys/websocket-proxy.key.nopass
cp /etc/pki/ovirt-engine/certs/apache.cer /etc/pki/ovirt-engine/certs/websocket-proxy.cer
cat >/etc/ovirt-engine/engine.conf.d/99-custom-truststore.conf <<EOF
ENGINE_HTTPS_PKI_TRUST_STORE="/etc/pki/java/cacerts"
ENGINE_HTTPS_PKI_TRUST_STORE_PASSWORD=""
EOF

systemctl restart httpd
systemctl restart ovirt-engine
systemctl restart ovirt-imageio-proxy

