|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Imports the Cloudflare WARP Gateway Root CA certificate into all Java installations. |
| 4 | +# This is necessary because Cloudflare Zero Trust WARP acts as a man-in-the-middle proxy, |
| 5 | +# intercepting SSL connections with its own certificate. Without importing this certificate, |
| 6 | +# Gradle and other Java tools fail with SSL handshake errors when downloading dependencies. |
| 7 | +# This script automatically finds all JDKs, stops Gradle daemons, and imports the certificate. |
| 8 | + |
| 9 | +set -eu |
| 10 | + |
| 11 | +# Get the directory where this script lives |
| 12 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 13 | +readonly SCRIPT_DIR |
| 14 | + |
| 15 | +source "${SCRIPT_DIR}/shellUtils.sh" |
| 16 | + |
| 17 | +# Certificate is in the Expensidev repo (two levels up from scripts directory) |
| 18 | +EXPENSIDEV_DIR="$(dirname "$(dirname "${SCRIPT_DIR}")")" |
| 19 | +readonly EXPENSIDEV_DIR |
| 20 | +readonly CERT_FILE="${EXPENSIDEV_DIR}/config/ssl/cloudflare-ca.pem" |
| 21 | + |
| 22 | +# Global constants |
| 23 | +readonly CERT_ALIAS="cloudflare-gateway-root" |
| 24 | +readonly KEYSTORE_PASSWORD="${JAVA_KEYSTORE_PASSWORD:-changeit}" |
| 25 | + |
| 26 | +# Global variables |
| 27 | +JAVA_HOMES="" |
| 28 | +JAVA_HOME_PATH="" |
| 29 | +KEYSTORE="" |
| 30 | +SUCCESS_COUNT=0 |
| 31 | +FAIL_COUNT=0 |
| 32 | + |
| 33 | +# Function to set the KEYSTORE global variable for the current JAVA_HOME_PATH |
| 34 | +# Returns 0 if found, 1 if not found |
| 35 | +function get_keystore_path() { |
| 36 | + if [[ -f "${JAVA_HOME_PATH}/lib/security/cacerts" ]]; then |
| 37 | + KEYSTORE="${JAVA_HOME_PATH}/lib/security/cacerts" |
| 38 | + return 0 |
| 39 | + elif [[ -f "${JAVA_HOME_PATH}/jre/lib/security/cacerts" ]]; then |
| 40 | + KEYSTORE="${JAVA_HOME_PATH}/jre/lib/security/cacerts" |
| 41 | + return 0 |
| 42 | + fi |
| 43 | + return 1 |
| 44 | +} |
| 45 | + |
| 46 | +# Function to check if Cloudflare WARP certificate is imported into default JDK |
| 47 | +# Returns 0 if certificate is imported, 1 otherwise |
| 48 | +function is_cloudflare_cert_imported() { |
| 49 | + if ! command -v keytool &>/dev/null; then |
| 50 | + return 1 |
| 51 | + fi |
| 52 | + |
| 53 | + JAVA_HOME_PATH=$(/usr/libexec/java_home 2>/dev/null) || return 1 |
| 54 | + |
| 55 | + if ! get_keystore_path; then |
| 56 | + return 1 |
| 57 | + fi |
| 58 | + |
| 59 | + keytool -list -keystore "${KEYSTORE}" \ |
| 60 | + -storepass "${KEYSTORE_PASSWORD}" -alias "${CERT_ALIAS}" &>/dev/null |
| 61 | +} |
| 62 | + |
| 63 | +function stop_gradle_daemons() { |
| 64 | + if pgrep -f "GradleDaemon" &>/dev/null; then |
| 65 | + pkill -f "GradleDaemon" &>/dev/null || true |
| 66 | + success "Gradle daemons stopped" |
| 67 | + else |
| 68 | + echo "No Gradle daemons running" |
| 69 | + fi |
| 70 | +} |
| 71 | + |
| 72 | +function find_java_installations() { |
| 73 | + JAVA_HOMES=$(/usr/libexec/java_home -V 2>&1 | grep -E "^\s+[0-9]" | awk '{print $NF}') |
| 74 | + |
| 75 | + if [[ -z "${JAVA_HOMES}" ]]; then |
| 76 | + error "No Java installations found" |
| 77 | + exit 1 |
| 78 | + fi |
| 79 | + |
| 80 | + local JDK_COUNT |
| 81 | + JDK_COUNT=$(echo "${JAVA_HOMES}" | wc -l | tr -d ' ') |
| 82 | + success "Found ${JDK_COUNT} Java installation(s)" |
| 83 | +} |
| 84 | + |
| 85 | +function import_certificates() { |
| 86 | + while IFS= read -r JAVA_HOME_PATH; do |
| 87 | + if [[ -z "${JAVA_HOME_PATH}" ]]; then |
| 88 | + continue |
| 89 | + fi |
| 90 | + |
| 91 | + # Get Java version for display |
| 92 | + local VERSION |
| 93 | + VERSION=$("${JAVA_HOME_PATH}/bin/java" -version 2>&1 | head -n 1 | awk -F'"' '{print $2}') |
| 94 | + echo |
| 95 | + echo "Processing: Java ${VERSION}" |
| 96 | + echo " Location: ${JAVA_HOME_PATH}" |
| 97 | + |
| 98 | + # Get keystore location |
| 99 | + if ! get_keystore_path; then |
| 100 | + error "Could not find cacerts file\n Tried: ${JAVA_HOME_PATH}/lib/security/cacerts\n Tried: ${JAVA_HOME_PATH}/jre/lib/security/cacerts" |
| 101 | + ((FAIL_COUNT++)) |
| 102 | + continue |
| 103 | + fi |
| 104 | + |
| 105 | + # Check if certificate already exists |
| 106 | + if keytool -list -keystore "${KEYSTORE}" -storepass "${KEYSTORE_PASSWORD}" \ |
| 107 | + -alias "${CERT_ALIAS}" &>/dev/null; then |
| 108 | + echo " Certificate already exists, removing old one..." |
| 109 | + keytool -delete -keystore "${KEYSTORE}" -alias "${CERT_ALIAS}" \ |
| 110 | + -storepass "${KEYSTORE_PASSWORD}" \ |
| 111 | + &>/dev/null || true |
| 112 | + fi |
| 113 | + |
| 114 | + # Import the certificate |
| 115 | + if keytool -import -trustcacerts -keystore "${KEYSTORE}" \ |
| 116 | + -alias "${CERT_ALIAS}" \ |
| 117 | + -file "${CERT_FILE}" \ |
| 118 | + -storepass "${KEYSTORE_PASSWORD}" \ |
| 119 | + -noprompt &>/dev/null; then |
| 120 | + success "Certificate imported successfully" |
| 121 | + ((SUCCESS_COUNT++)) |
| 122 | + else |
| 123 | + error "Failed to import certificate" |
| 124 | + ((FAIL_COUNT++)) |
| 125 | + fi |
| 126 | + done <<< "${JAVA_HOMES}" |
| 127 | +} |
| 128 | + |
| 129 | +function print_summary() { |
| 130 | + echo |
| 131 | + success "Successfully imported: ${SUCCESS_COUNT}" |
| 132 | + if [[ "${FAIL_COUNT}" -gt 0 ]]; then |
| 133 | + error "Failed: ${FAIL_COUNT}" |
| 134 | + fi |
| 135 | + |
| 136 | + echo |
| 137 | + if [[ "${SUCCESS_COUNT}" -gt 0 ]]; then |
| 138 | + success "Certificate import complete!" |
| 139 | + echo |
| 140 | + echo "The Cloudflare WARP certificate has been imported into your" |
| 141 | + echo "Java keystores. You can now build Android apps with WARP enabled." |
| 142 | + echo |
| 143 | + info "Gradle daemon will automatically use the updated certificates when it restarts on your next build." |
| 144 | + exit 0 |
| 145 | + else |
| 146 | + error "No certificates were imported successfully" |
| 147 | + exit 1 |
| 148 | + fi |
| 149 | +} |
| 150 | + |
| 151 | +function main() { |
| 152 | + # Early exit if certs are already imported |
| 153 | + if is_cloudflare_cert_imported; then |
| 154 | + success "Cloudflare certificates are already imported into JDK." |
| 155 | + exit 0 |
| 156 | + fi |
| 157 | + |
| 158 | + # At this point, certs need to be imported |
| 159 | + # If not running with sudo, re-exec with sudo (will prompt for password) |
| 160 | + if [[ "${EUID}" -ne 0 ]]; then |
| 161 | + info "Importing certificates requires sudo access." |
| 162 | + exec sudo bash "${BASH_SOURCE[0]}" "$@" |
| 163 | + fi |
| 164 | + |
| 165 | + # At this point, we're running with sudo and certs need to be imported |
| 166 | + title "Cloudflare WARP Certificate Importer" |
| 167 | + |
| 168 | + # Verify certificate file exists |
| 169 | + if [[ ! -f "${CERT_FILE}" ]]; then |
| 170 | + error "Cloudflare certificate not found at: ${CERT_FILE}\nMake sure you have the Expensidev repository cloned and that App is a child of Expensidev.\nAlternatively, manually follow this stackoverflow: https://stackoverflowteams.com/c/expensify/questions/18506" |
| 171 | + exit 1 |
| 172 | + fi |
| 173 | + |
| 174 | + title "Step 1: Stopping Gradle daemons" |
| 175 | + stop_gradle_daemons |
| 176 | + |
| 177 | + title "Step 2: Finding all Java installations" |
| 178 | + find_java_installations |
| 179 | + |
| 180 | + title "Step 3: Importing certificate into Java keystores" |
| 181 | + import_certificates |
| 182 | + |
| 183 | + print_summary |
| 184 | +} |
| 185 | + |
| 186 | +main "$@" |
0 commit comments