Skip to content

Commit

Permalink
Merge pull request #556 from lanedirt/555-use-relative-url-to-access-…
Browse files Browse the repository at this point in the history
…api-instead-of-requiring-hostname-in-env

Simplify installation by using relative url for API instead of requiring hostname in env file
  • Loading branch information
lanedirt authored Jan 22, 2025
2 parents 7e7a8b0 + a941ffa commit 2d9b6f3
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 34 deletions.
36 changes: 26 additions & 10 deletions docs/installation/advanced/manual-setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,17 @@ If you prefer to manually set up AliasVault, this README provides step-by-step i
HOSTNAME=localhost
```

4. **Generate and set JWT_KEY**
4. **Set default ports**

Update the .env file with the ports you want to use for the AliasVault components. The values defined here are used by the docker-compose.yml file.
```bash
HTTP_PORT=80
HTTPS_PORT=443
SMTP_PORT=25
SMTP_TLS_PORT=587
```

5. **Generate and set JWT_KEY**

Generate a random 32-char string for JWT token generation:
```bash
Expand All @@ -49,7 +59,7 @@ If you prefer to manually set up AliasVault, this README provides step-by-step i
JWT_KEY=your_generated_key_here
```

5. **Generate and set DATA_PROTECTION_CERT_PASS**
6. **Generate and set DATA_PROTECTION_CERT_PASS**

Generate a random password for the data protection certificate:
```bash
Expand All @@ -61,7 +71,7 @@ If you prefer to manually set up AliasVault, this README provides step-by-step i
DATA_PROTECTION_CERT_PASS=your_generated_password_here
```

6. **Configure PostgreSQL Settings**
7. **Configure PostgreSQL Settings**

Set the following PostgreSQL-related variables in your .env file:
```bash
Expand All @@ -75,7 +85,7 @@ If you prefer to manually set up AliasVault, this README provides step-by-step i
POSTGRES_PASSWORD=$(openssl rand -base64 32)
```

7. **Set PRIVATE_EMAIL_DOMAINS**
8. **Set PRIVATE_EMAIL_DOMAINS**

Update the .env file with allowed email domains. Use DISABLED.TLD to disable email support:
```bash
Expand All @@ -86,14 +96,14 @@ If you prefer to manually set up AliasVault, this README provides step-by-step i
PRIVATE_EMAIL_DOMAINS=DISABLED.TLD
```

8. **Set SUPPORT_EMAIL (Optional)**
9. **Set SUPPORT_EMAIL (Optional)**

Add a support email address if desired:
```bash
SUPPORT_EMAIL=support@yourdomain.com
```

9. **Generate admin password**
10. **Generate admin password**

Build the Docker image for password hashing:
```bash
Expand All @@ -111,19 +121,25 @@ If you prefer to manually set up AliasVault, this README provides step-by-step i
ADMIN_PASSWORD_GENERATED=2024-01-01T00:00:00Z
```

10. **Build and start Docker containers**
11. **Optional configuration**
Enable or disable public registration of new users:
```bash
PUBLIC_REGISTRATION_ENABLED=false
```

12. **Build and start Docker containers**

Build the Docker Compose stack:
```bash
docker compose build
docker compose -f docker-compose.yml -f docker-compose.build.yml build
```

Start the Docker Compose stack:
```bash
docker compose up -d
docker compose -f docker-compose.yml -f docker-compose.build.yml up -d
```

11. **Access AliasVault**
13. **Access AliasVault**

AliasVault should now be running. You can access it at:

Expand Down
71 changes: 59 additions & 12 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ show_usage() {
printf " uninstall Uninstall AliasVault\n"
printf " update Update AliasVault to the latest version\n"
printf " update-installer Check and update install.sh script if newer version available\n"
printf " configure-hostname Configure the hostname where AliasVault can be accessed from\n"
printf " configure-ssl Configure SSL certificates (Let's Encrypt or self-signed)\n"
printf " configure-email Configure email domains for receiving emails\n"
printf " configure-registration Configure new account registration (enable or disable)\n"
Expand Down Expand Up @@ -116,6 +117,10 @@ parse_args() {
COMMAND="reset-password"
shift
;;
configure-hostname|hostname)
COMMAND="configure-hostname"
shift
;;
configure-ssl|ssl)
COMMAND="configure-ssl"
shift
Expand Down Expand Up @@ -246,6 +251,9 @@ main() {
"configure-registration")
handle_registration_configuration
;;
"configure-hostname")
handle_hostname_configuration
;;
"start")
handle_start
;;
Expand Down Expand Up @@ -406,20 +414,25 @@ create_env_file() {
fi
}

# Environment setup functions
populate_hostname() {
printf "${CYAN}> Checking HOSTNAME...${NC}\n"
if ! grep -q "^HOSTNAME=" "$ENV_FILE" || [ -z "$(grep "^HOSTNAME=" "$ENV_FILE" | cut -d '=' -f2)" ]; then
DEFAULT_HOSTNAME="localhost"
read -p "Enter the hostname where AliasVault will be hosted (press Enter for default: $DEFAULT_HOSTNAME): " USER_HOSTNAME
HOSTNAME=${USER_HOSTNAME:-$DEFAULT_HOSTNAME}
while true; do
read -p "Enter the (public) hostname where this AliasVault instance can be accessed from (e.g. aliasvault.net): " USER_HOSTNAME
if [ -n "$USER_HOSTNAME" ]; then
HOSTNAME="$USER_HOSTNAME"
break
else
printf "${YELLOW}> Hostname cannot be empty. Please enter a valid hostname.${NC}\n"
fi
done
update_env_var "HOSTNAME" "$HOSTNAME"
else
HOSTNAME=$(grep "^HOSTNAME=" "$ENV_FILE" | cut -d '=' -f2)
printf " ${GREEN}> HOSTNAME already exists.${NC}\n"
fi
}

# Environment setup functions
populate_jwt_key() {
printf "${CYAN}> Checking JWT_KEY...${NC}\n"
if ! grep -q "^JWT_KEY=" "$ENV_FILE" || [ -z "$(grep "^JWT_KEY=" "$ENV_FILE" | cut -d '=' -f2)" ]; then
Expand Down Expand Up @@ -490,7 +503,7 @@ set_smtp_tls_enabled() {
set_support_email() {
printf "${CYAN}> Checking SUPPORT_EMAIL...${NC}\n"
if ! grep -q "^SUPPORT_EMAIL=" "$ENV_FILE"; then
read -p "Enter support email address (optional, press Enter to skip): " SUPPORT_EMAIL
read -p "Enter server admin support email address that is shown on contact page (optional, press Enter to skip): " SUPPORT_EMAIL
update_env_var "SUPPORT_EMAIL" "$SUPPORT_EMAIL"
else
printf " ${GREEN}> SUPPORT_EMAIL already exists.${NC}\n"
Expand Down Expand Up @@ -613,13 +626,13 @@ print_success_message() {
printf "${CYAN}To configure the server, login to the admin panel:${NC}\n"
printf "\n"
if [ -n "$PASSWORD" ]; then
printf "Admin Panel: https://${HOSTNAME}/admin\n"
printf "Admin Panel: https://localhost/admin\n"
printf "Username: admin\n"
printf "Password: $PASSWORD\n"
printf "\n"
printf "${YELLOW}(!) Caution: Make sure to backup the above credentials in a safe place, they won't be shown again!${NC}\n"
else
printf "Admin Panel: https://${HOSTNAME}/admin\n"
printf "Admin Panel: https://localhost/admin\n"
printf "Username: admin\n"
printf "Password: (Previously set. Use ./install.sh reset-password to generate new one.)\n"
fi
Expand All @@ -628,7 +641,7 @@ print_success_message() {
printf "\n"
printf "${CYAN}In order to start using AliasVault, log into the client website:${NC}\n"
printf "\n"
printf "Client Website: https://${HOSTNAME}/\n"
printf "Client Website: https://localhost/\n"
printf "\n"
printf "${MAGENTA}=========================================================${NC}\n"
}
Expand Down Expand Up @@ -808,7 +821,6 @@ handle_build() {

# Initialize environment with proper error handling
create_env_file || { printf "${RED}> Failed to create .env file${NC}\n"; exit 1; }
populate_hostname || { printf "${RED}> Failed to set hostname${NC}\n"; exit 1; }
set_support_email || { printf "${RED}> Failed to set support email${NC}\n"; exit 1; }
populate_jwt_key || { printf "${RED}> Failed to set JWT key${NC}\n"; exit 1; }
populate_data_protection_cert_pass || { printf "${RED}> Failed to set certificate password${NC}\n"; exit 1; }
Expand Down Expand Up @@ -947,6 +959,8 @@ handle_ssl_configuration() {
exit 1
fi

populate_hostname || { printf "${RED}> Failed to set hostname${NC}\n"; exit 1; }

# Get the current hostname and SSL config from .env
CURRENT_HOSTNAME=$(grep "^HOSTNAME=" "$ENV_FILE" | cut -d '=' -f2)
LETSENCRYPT_ENABLED=$(grep "^LETSENCRYPT_ENABLED=" "$ENV_FILE" | cut -d '=' -f2)
Expand All @@ -970,7 +984,7 @@ handle_ssl_configuration() {
printf "Currently using: ${YELLOW}Self-signed certificates${NC}\n"
fi

printf "Current hostname: ${CYAN}${CURRENT_HOSTNAME}${NC}\n"
printf "Current hostname: ${CYAN}${CURRENT_HOSTNAME}${NC} (To change this, run: ./install.sh configure-hostname)\n"
printf "\n"
printf "SSL Options:\n"
printf "1) Activate and/or request new Let's Encrypt certificate (recommended for production)\n"
Expand Down Expand Up @@ -1526,7 +1540,6 @@ handle_install_version() {

# Initialize environment
create_env_file || { printf "${RED}> Failed to create .env file${NC}\n"; exit 1; }
populate_hostname || { printf "${RED}> Failed to set hostname${NC}\n"; exit 1; }
set_support_email || { printf "${RED}> Failed to set support email${NC}\n"; exit 1; }
populate_jwt_key || { printf "${RED}> Failed to set JWT key${NC}\n"; exit 1; }
populate_data_protection_cert_pass || { printf "${RED}> Failed to set certificate password${NC}\n"; exit 1; }
Expand Down Expand Up @@ -1987,4 +2000,38 @@ handle_db_import() {
fi
}

# Function to handle hostname configuration
handle_hostname_configuration() {
printf "${YELLOW}+++ Hostname Configuration +++${NC}\n"
printf "\n"

# Check if AliasVault is installed
if [ ! -f "docker-compose.yml" ]; then
printf "${RED}Error: AliasVault must be installed first.${NC}\n"
exit 1
fi

# Get current hostname
CURRENT_HOSTNAME=$(grep "^HOSTNAME=" "$ENV_FILE" | cut -d '=' -f2)
printf "${CYAN}Removing current hostname ${CURRENT_HOSTNAME}${NC}...\n"
printf "\n"

# Force hostname to be empty so populate_hostname will ask for a new one
sed -i.bak "/^HOSTNAME=/d" "$ENV_FILE" && rm -f "$ENV_FILE.bak"

# Reuse existing hostname population logic
populate_hostname

if [ $? -eq 0 ]; then
printf "New hostname: ${CYAN}${HOSTNAME}${NC}\n"
printf "\n"
printf "${MAGENTA}=========================================================${NC}\n"
else
printf "${RED}> Failed to update hostname. Please try again.${NC}\n"
printf "\n"
printf "${MAGENTA}=========================================================${NC}\n"
exit 1
fi
}

main "$@"
9 changes: 3 additions & 6 deletions src/AliasVault.Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@

var config = new Config();
builder.Configuration.Bind(config);
if (string.IsNullOrEmpty(config.ApiUrl))
{
throw new KeyNotFoundException("ApiUrl is not set in the configuration.");
}

if (config.PrivateEmailDomains == null || config.PrivateEmailDomains.Count == 0)
{
Expand Down Expand Up @@ -56,8 +52,9 @@
var httpClient = httpClientFactory.CreateClient("AliasVault.Api");
var apiConfig = sp.GetRequiredService<Config>();

// Ensure the API URL ends with a forward slash
var baseUrl = apiConfig.ApiUrl.TrimEnd('/') + "/";
// If API URL is not set, use the current base URL and append "/api" which is the default for the Docker setup.
// If API URL override is set (used e.g. in dev), then ensure the API URL ends with a forward slash.
var baseUrl = string.IsNullOrEmpty(apiConfig.ApiUrl) ? builder.HostEnvironment.BaseAddress + "api/" : apiConfig.ApiUrl.TrimEnd('/') + "/";
httpClient.BaseAddress = new Uri(baseUrl);
return httpClient;
});
Expand Down
11 changes: 5 additions & 6 deletions src/AliasVault.Client/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#!/bin/sh
# Set the default hostname for localhost debugging
DEFAULT_HOSTNAME="localhost"
# Set the default values
DEFAULT_PRIVATE_EMAIL_DOMAINS="localmail.tld"
DEFAULT_SUPPORT_EMAIL=""

# Use the provided HOSTNAME environment variable if it exists, otherwise use the default
HOSTNAME=${HOSTNAME:-$DEFAULT_HOSTNAME}
# Use the provided environment variables if they exist, otherwise use defaults
PRIVATE_EMAIL_DOMAINS=${PRIVATE_EMAIL_DOMAINS:-$DEFAULT_PRIVATE_EMAIL_DOMAINS}
SUPPORT_EMAIL=${SUPPORT_EMAIL:-$DEFAULT_SUPPORT_EMAIL}

Expand All @@ -25,8 +23,9 @@ if [ ! -f /etc/nginx/ssl/nginx.crt ] || [ ! -f /etc/nginx/ssl/nginx.key ]; then
chmod 600 /etc/nginx/ssl/nginx.key
fi

# Replace the default URL with the actual API URL constructed from hostname
sed -i "s|http://localhost:5092|https://${HOSTNAME}/api|g" /usr/share/nginx/html/appsettings.json
# Remove the default API URL as it's only used for local dev/debugging.
# The app will use a relative URL instead (base url + "/api/" which is the default for the Docker setup).
sed -i "s|\"ApiUrl\": \"http://localhost:5092\",||g" /usr/share/nginx/html/appsettings.json

# Convert comma-separated list to JSON array
json_array=$(echo $PRIVATE_EMAIL_DOMAINS | awk '{split($0,a,","); printf "["; for(i=1;i<=length(a);i++) {printf "\"%s\"", a[i]; if(i<length(a)) printf ","} printf "]"}')
Expand Down

0 comments on commit 2d9b6f3

Please sign in to comment.