Commit 2b0faffd authored by Pierre Smeyers's avatar Pierre Smeyers
Browse files

feat: support CA certs provided as file

parent 19dc9695
Loading
Loading
Loading
Loading
Loading
+14 −5
Original line number Diff line number Diff line
@@ -290,6 +290,15 @@ stages:
    fi
  }

  function as_content() {
    file_or_content=$1
    if [[ -f "${file_or_content}" ]]; then
      cat "${file_or_content}"
    else
      echo "${file_or_content}"
    fi
  }

  function install_ca_certs() {
    certs=$1
    if [[ -z "$certs" ]]
@@ -298,17 +307,17 @@ stages:
    fi

    # import in system
    if echo "$certs" >> /etc/ssl/certs/ca-certificates.crt
    if as_content "$certs" >> /etc/ssl/certs/ca-certificates.crt
    then
      log_info "CA certificates imported in \\e[33;1m/etc/ssl/certs/ca-certificates.crt\\e[0m"
    fi
    if echo "$certs" >> /etc/ssl/cert.pem
    if as_content "$certs" >> /etc/ssl/cert.pem
    then
      log_info "CA certificates imported in \\e[33;1m/etc/ssl/cert.pem\\e[0m"
    fi

    # configure for npm
    echo "$certs" > /tmp/custom-ca.pem
    as_content "$certs" > /tmp/custom-ca.pem
    export NODE_EXTRA_CA_CERTS=/tmp/custom-ca.pem

    # import in Java keystore (if keytool command found)
@@ -321,12 +330,12 @@ stages:
      if [[ -f "$keystore" ]]
      then
        storepass=${JAVA_KEYSTORE_PASSWORD:-changeit}
        nb_certs=$(echo "$certs" | grep -c 'END CERTIFICATE')
        nb_certs=$(as_content "$certs" | grep -c 'END CERTIFICATE')
        log_info "importing $nb_certs certificates in Java keystore \\e[33;1m$keystore\\e[0m..."
        for idx in $(seq 0 $((nb_certs - 1)))
        do
          # TODO: use keytool option -trustcacerts ?
          if echo "$certs" | awk "n==$idx { print }; /END CERTIFICATE/ { n++ }" | keytool -noprompt -import -alias "imported CA Cert $idx" -keystore "$keystore" -storepass "$storepass"
          if as_content "$certs" | awk "n==$idx { print }; /END CERTIFICATE/ { n++ }" | keytool -noprompt -import -alias "imported CA Cert $idx" -keystore "$keystore" -storepass "$storepass"
          then
            log_info "... CA certificate [$idx] successfully imported"
          else