Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Improve self-signed SSL cert generation to take into account HOSTNAME env var (#1181)
Leendert de Borst835b350d53f0
diff --git a/.gitignore b/.gitignoreindex 9468d6088..f9c85e684 100644--- a/.gitignore+++ b/.gitignore@@ -404,6 +404,7 @@ certificates/**/*.crtcertificates/**/*.keycertificates/**/*.pfxcertificates/**/*.pem+certificates/**/.hostname_markercertificates/letsencrypt/**# Secretsdiff --git a/apps/server/entrypoint.sh b/apps/server/entrypoint.shindex eb2a53f6b..235fa6ea9 100644--- a/apps/server/entrypoint.sh+++ b/apps/server/entrypoint.sh@@ -14,17 +14,56 @@ elsecp /etc/nginx/nginx-443.conf /etc/nginx/nginx.conffi-# Generate self-signed SSL certificate if not exists-if [ ! -f /etc/nginx/ssl/cert.pem ] || [ ! -f /etc/nginx/ssl/key.pem ]; then+# Function to check if certificate needs regeneration+needs_cert_regeneration() {+ # If cert doesn't exist, need to generate+ if [ ! -f /etc/nginx/ssl/cert.pem ] || [ ! -f /etc/nginx/ssl/key.pem ]; then+ return 0+ fi++ # Check if we have a hostname marker file+ if [ -f /etc/nginx/ssl/.hostname_marker ]; then+ STORED_HOSTNAME=$(cat /etc/nginx/ssl/.hostname_marker)+ if [ "$STORED_HOSTNAME" != "${HOSTNAME:-localhost}" ]; then+ echo "Hostname changed from '$STORED_HOSTNAME' to '${HOSTNAME:-localhost}', regenerating certificate..."+ return 0+ fi+ else+ # No marker file, regenerate to be safe+ return 0+ fi++ return 1+}++# Generate self-signed SSL certificate if not exists or hostname changed+if needs_cert_regeneration; thenecho "Generating new SSL certificate (10 years validity)..."- openssl req -x509 -nodes -days 3650 -newkey rsa:2048 \- -keyout /etc/nginx/ssl/key.pem \- -out /etc/nginx/ssl/cert.pem \- -subj "/C=US/ST=State/L=City/O=Organization/CN=localhost"++ HOSTNAME_VALUE="${HOSTNAME:-localhost}"++ if [ "$HOSTNAME_VALUE" = "localhost" ] || [ -z "$HOSTNAME_VALUE" ]; then+ # Default localhost-only configuration+ openssl req -x509 -nodes -days 3650 -newkey rsa:2048 \+ -keyout /etc/nginx/ssl/key.pem \+ -out /etc/nginx/ssl/cert.pem \+ -subj "/C=US/ST=State/L=City/O=Organization/CN=localhost"+ else+ # Generate certificate with the hostname and include localhost as SAN+ openssl req -x509 -nodes -days 3650 -newkey rsa:2048 \+ -keyout /etc/nginx/ssl/key.pem \+ -out /etc/nginx/ssl/cert.pem \+ -subj "/C=US/ST=State/L=City/O=AliasVault/CN=${HOSTNAME_VALUE}" \+ -addext "subjectAltName=DNS:${HOSTNAME_VALUE},DNS:localhost,IP:127.0.0.1"+ fi# Set proper permissionschmod 644 /etc/nginx/ssl/cert.pemchmod 600 /etc/nginx/ssl/key.pem++ # Store current hostname for change detection+ echo "${HOSTNAME:-localhost}" > /etc/nginx/ssl/.hostname_marker+ chmod 644 /etc/nginx/ssl/.hostname_markerfi# Create the appropriate SSL configuration based on LETSENCRYPT_ENABLEDdiff --git a/dockerfiles/all-in-one/s6-scripts/init/script b/dockerfiles/all-in-one/s6-scripts/init/scriptindex 6e028ffaf..9684f715f 100644--- a/dockerfiles/all-in-one/s6-scripts/init/script+++ b/dockerfiles/all-in-one/s6-scripts/init/script@@ -154,25 +154,75 @@ elsechmod 700 /database/postgresfi-# Generate SSL certificates if needed-if [ ! -f /certificates/ssl/cert.pem ] || [ ! -f /certificates/ssl/key.pem ]; then+# Function to check if certificate needs regeneration+needs_cert_regeneration() {+ # If cert doesn't exist, need to generate+ if [ ! -f /certificates/ssl/cert.pem ] || [ ! -f /certificates/ssl/key.pem ]; then+ return 0+ fi++ # Check if we have a hostname marker file+ if [ -f /certificates/ssl/.hostname_marker ]; then+ STORED_HOSTNAME=$(cat /certificates/ssl/.hostname_marker)+ if [ "$STORED_HOSTNAME" != "${HOSTNAME:-localhost}" ]; then+ log 0 "[init] Hostname changed from '$STORED_HOSTNAME' to '${HOSTNAME:-localhost}', regenerating certificate..."+ return 0+ fi+ else+ # No marker file, regenerate to be safe+ return 0+ fi++ return 1+}++# Generate SSL certificates if needed or hostname changed+if needs_cert_regeneration; thenlog 0 ""log 0 "[init] Generating SSL certificates..."mkdir -p /certificates/ssl- if [ "$VERBOSITY" -ge 2 ]; then- openssl req -x509 -nodes -days 3650 -newkey rsa:2048 \- -keyout /certificates/ssl/key.pem \- -out /certificates/ssl/cert.pem \- -subj "/C=US/ST=State/L=City/O=AliasVault/CN=${HOSTNAME:-localhost}"++ HOSTNAME_VALUE="${HOSTNAME:-localhost}"++ if [ "$HOSTNAME_VALUE" = "localhost" ] || [ -z "$HOSTNAME_VALUE" ]; then+ # Default localhost-only configuration+ if [ "$VERBOSITY" -ge 2 ]; then+ openssl req -x509 -nodes -days 3650 -newkey rsa:2048 \+ -keyout /certificates/ssl/key.pem \+ -out /certificates/ssl/cert.pem \+ -subj "/C=US/ST=State/L=City/O=AliasVault/CN=localhost"+ else+ openssl req -x509 -nodes -days 3650 -newkey rsa:2048 \+ -keyout /certificates/ssl/key.pem \+ -out /certificates/ssl/cert.pem \+ -subj "/C=US/ST=State/L=City/O=AliasVault/CN=localhost" \+ >/dev/null 2>&1+ fielse- openssl req -x509 -nodes -days 3650 -newkey rsa:2048 \- -keyout /certificates/ssl/key.pem \- -out /certificates/ssl/cert.pem \- -subj "/C=US/ST=State/L=City/O=AliasVault/CN=${HOSTNAME:-localhost}" \- >/dev/null 2>&1+ # Generate certificate with the hostname and include localhost as SAN+ if [ "$VERBOSITY" -ge 2 ]; then+ openssl req -x509 -nodes -days 3650 -newkey rsa:2048 \+ -keyout /certificates/ssl/key.pem \+ -out /certificates/ssl/cert.pem \+ -subj "/C=US/ST=State/L=City/O=AliasVault/CN=${HOSTNAME_VALUE}" \+ -addext "subjectAltName=DNS:${HOSTNAME_VALUE},DNS:localhost,IP:127.0.0.1"+ else+ openssl req -x509 -nodes -days 3650 -newkey rsa:2048 \+ -keyout /certificates/ssl/key.pem \+ -out /certificates/ssl/cert.pem \+ -subj "/C=US/ST=State/L=City/O=AliasVault/CN=${HOSTNAME_VALUE}" \+ -addext "subjectAltName=DNS:${HOSTNAME_VALUE},DNS:localhost,IP:127.0.0.1" \+ >/dev/null 2>&1+ fifi+chmod 600 /certificates/ssl/key.pemchmod 644 /certificates/ssl/cert.pem++ # Store current hostname for change detection+ echo "${HOSTNAME:-localhost}" > /certificates/ssl/.hostname_marker+ chmod 644 /certificates/ssl/.hostname_marker+log 0 "[init] Self-signed SSL certificates generated"elselog 0 "[init] ✅ Self-signed SSL certificates already exist"diff --git a/install.sh b/install.shindex 3b2a03c38..45ae63596 100755--- a/install.sh+++ b/install.sh@@ -871,9 +871,9 @@ main() {if [ $? -eq 0 ]; thenprintf "${CYAN}> Restarting admin container...${NC}\n"if [ "$VERBOSE" = true ]; then- $(get_docker_compose_command) up -d --force-recreate admin+ eval "$(get_docker_compose_command) up -d --force-recreate admin"else- $(get_docker_compose_command) up -d --force-recreate admin > /dev/null 2>&1+ eval "$(get_docker_compose_command) up -d --force-recreate admin" > /dev/null 2>&1fiprint_password_reset_messagefi@@ -1142,7 +1142,7 @@ create_env_file() {populate_hostname() {if ! grep -q "^HOSTNAME=" "$ENV_FILE" || [ -z "$(grep "^HOSTNAME=" "$ENV_FILE" | cut -d '=' -f2)" ]; thenwhile true; do- read -p "Enter the (public) hostname where this AliasVault server can be accessed from (e.g. aliasvault.net): " USER_HOSTNAME+ read -p "Enter the hostname where this AliasVault server can be accessed (e.g. aliasvault.example.com): " USER_HOSTNAMEif [ -n "$USER_HOSTNAME" ]; thenHOSTNAME="$USER_HOSTNAME"break@@ -1583,13 +1583,13 @@ recreate_docker_containers() {if [ "$VERBOSE" = true ]; thenprintf "${CYAN}ℹ (Re)creating Docker containers...${NC}\n"printf "\b${NC}\n"- if ! $(get_docker_compose_command) up -d --force-recreate; then+ if ! eval "$(get_docker_compose_command) up -d --force-recreate"; thenlog_error "Failed to recreate Docker containers"exit 1fielse(- $(get_docker_compose_command) up -d --force-recreate > /tmp/docker_recreate.log 2>&1 &+ eval "$(get_docker_compose_command) up -d --force-recreate" > /tmp/docker_recreate.log 2>&1 &RECREATE_PID=$!show_spinner $RECREATE_PID "Recreating Docker containers "wait $RECREATE_PID@@ -1793,13 +1793,13 @@ handle_build() {if [ "$VERBOSE" = true ]; thenprintf "${CYAN}ℹ Building Docker Compose stack...${NC}\n"printf "\b${NC}\n"- if ! $(get_docker_compose_command) build; then+ if ! eval "$(get_docker_compose_command) build"; thenlog_error "Failed to build Docker Compose stack"exit 1fielse(- $(get_docker_compose_command) build > install_compose_build_output.log 2>&1 &+ eval "$(get_docker_compose_command) build" > install_compose_build_output.log 2>&1 &BUILD_PID=$!show_spinner $BUILD_PID "Building Docker Compose stack "wait $BUILD_PID@@ -1936,7 +1936,7 @@ handle_ssl_configuration() {printf "Hostname: ${CYAN}${CURRENT_HOSTNAME}${NC} (change via: ./install.sh configure-hostname)\n"printf "\n"printf "Choose an option:\n"- printf "1) Use Let's Encrypt certificate (recommended)\n"+ printf "1) Use Let's Encrypt certificate (recommended for public domains)\n"printf "2) Use self-signed certificate\n"printf "3) Cancel\n"printf "\n"@@ -2192,11 +2192,11 @@ configure_letsencrypt() {# Restart only the reverse proxy with new configuration so it loads the new certificateprintf "${CYAN}> Restarting reverse proxy with Let's Encrypt configuration...${NC}\n"- $(get_docker_compose_command) up -d reverse-proxy --force-recreate+ eval "$(get_docker_compose_command) up -d reverse-proxy --force-recreate"# Starting certbot container to renew certificates automaticallyprintf "${CYAN}> Starting new certbot container to renew certificates automatically...${NC}\n"- $(get_docker_compose_command) up -d certbot+ eval "$(get_docker_compose_command) up -d certbot"# Print success messageprintf "\n"@@ -2210,17 +2210,27 @@ generate_self_signed_cert() {# Disable Let's Encryptupdate_env_var "LETSENCRYPT_ENABLED" "false"+ # Get current hostname from .env+ HOSTNAME_VALUE=$(grep "^HOSTNAME=" "$ENV_FILE" | cut -d '=' -f2)++ if [ -n "$HOSTNAME_VALUE" ] && [ "$HOSTNAME_VALUE" != "localhost" ]; then+ printf "${CYAN}> Using configured hostname: ${HOSTNAME_VALUE}${NC}\n"+ printf "${CYAN}> The certificate will include:${NC}\n"+ printf " ${GREEN}Primary CN:${NC} ${HOSTNAME_VALUE}\n"+ printf " ${GREEN}Alternative Names:${NC} localhost, 127.0.0.1\n\n"+ fi+# Stop existing containersprintf "${CYAN}> Stopping existing containers...${NC}\n"docker compose down- # Remove existing certificates- rm -f ./certificates/ssl/cert.pem ./certificates/ssl/key.pem+ # Remove existing certificates and hostname marker+ rm -f ./certificates/ssl/cert.pem ./certificates/ssl/key.pem ./certificates/ssl/.hostname_marker# Remove Let's Encrypt directoriesrm -rf ./certificates/letsencrypt- # Start containers (which will generate new self-signed certs)+ # Start containers (which will generate new self-signed cert with hostname)printf "${CYAN}> Restarting services...${NC}\n"docker compose up -d@@ -2232,7 +2242,7 @@ generate_self_signed_cert() {# New functions to handle container lifecycle:handle_start() {printf "${CYAN}> Starting AliasVault containers...${NC}\n"- $(get_docker_compose_command) up -d+ eval "$(get_docker_compose_command) up -d"printf "${GREEN}> AliasVault containers started successfully.${NC}\n"}@@ -2243,14 +2253,14 @@ handle_stop() {exit 0fi- $(get_docker_compose_command) down+ eval "$(get_docker_compose_command) down"printf "${GREEN}> AliasVault containers stopped successfully.${NC}\n"}handle_restart() {printf "\n${CYAN}> Restarting AliasVault containers...${NC}\n"- $(get_docker_compose_command) down- $(get_docker_compose_command) up -d+ eval "$(get_docker_compose_command) down"+ eval "$(get_docker_compose_command) up -d"printf "${GREEN}> AliasVault containers restarted successfully.${NC}\n"}@@ -2943,8 +2953,7 @@ handle_hostname_configuration() {fiprintf "The hostname is the domain name where your AliasVault server will be accessible.\n"- printf "A valid hostname is required for Let's Encrypt SSL certificate generation.\n"- printf "The hostname must be a real domain that points to this server (not localhost).\n"+ printf "This hostname will be used for both Let's Encrypt and self-signed certificates.\n"printf "\n"# Get current hostname@@ -2954,7 +2963,7 @@ handle_hostname_configuration() {# Ask for new hostnamewhile true; do- read -p "Enter new hostname (e.g. aliasvault.net): " NEW_HOSTNAME+ read -p "Enter new hostname (e.g. aliasvault.example.com): " NEW_HOSTNAMEif [ -n "$NEW_HOSTNAME" ]; thenbreakelse@@ -2962,9 +2971,36 @@ handle_hostname_configuration() {fidone+ # Check if hostname changed+ HOSTNAME_CHANGED=false+ if [ "$CURRENT_HOSTNAME" != "$NEW_HOSTNAME" ]; then+ HOSTNAME_CHANGED=true+ fi+# Update the hostnameupdate_env_var "HOSTNAME" "$NEW_HOSTNAME"+ # If using self-signed cert and hostname changed, offer to regenerate+ if [ "$HOSTNAME_CHANGED" = true ]; then+ LETSENCRYPT_ENABLED=$(grep "^LETSENCRYPT_ENABLED=" "$ENV_FILE" | cut -d '=' -f2)+ if [ "$LETSENCRYPT_ENABLED" != "true" ]; then+ printf "\n${YELLOW}Hostname changed. The self-signed certificate needs to be regenerated.${NC}\n"+ read -p "Regenerate certificate now? (y/n): " REGEN_CERT+ if [ "$REGEN_CERT" = "y" ] || [ "$REGEN_CERT" = "Y" ]; then+ # Remove the hostname marker to force regeneration+ rm -f ./certificates/ssl/.hostname_marker++ printf "\n${YELLOW}Restarting services to regenerate certificate...${NC}\n"+ handle_restart+ else+ printf "${YELLOW}Please restart services manually to apply the new certificate.${NC}\n"+ fi+ else+ printf "\n${YELLOW}Note: You're using Let's Encrypt. Make sure the new hostname has proper DNS records.${NC}\n"+ printf "${YELLOW}You may need to reconfigure Let's Encrypt for the new hostname.${NC}\n"+ fi+ fi+printf "\n"print_success_box "Hostname updated successfully to ${NEW_HOSTNAME}!"}