Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Make data between all-in-one and multi-container setups compatible (#1098)

Leendert de Borst99884b9761c4
diff --git a/.env.example b/.env.example
index dccb3768f..20533aca8 100644
--- a/.env.example
+++ b/.env.example
@@ -26,54 +26,6 @@ HTTPS_PORT=443
SMTP_PORT=25
SMTP_TLS_PORT=587
-# ===========================================
-# DATABASE CONFIGURATION
-# ===========================================
-
-# Set the password for the database user that's automatically created on first container start.
-# Generate using the following command:
-# $ openssl rand -base64 32
-POSTGRES_PASSWORD=
-
-# Note: in order to change the password for an existing installation
-# refer to https://docs.aliasvault.net/misc/dev/database-operations.html
-
-# ===========================================
-# SECURITY TOKENS
-# ===========================================
-
-# Set to a random 32 character string for the JWT keys generated by the API which affect
-# logged inuser session validity.
-# This can be generated using the following command:
-# $ openssl rand -base64 32
-JWT_KEY=
-
-# Set the password for the data protection certificate used by the API and admin.
-# This can be generated using the following command:
-# $ openssl rand -base64 32
-DATA_PROTECTION_CERT_PASS=
-
-# ===========================================
-# ADMIN CONFIGURATION
-# ===========================================
-
-# Set the password for the admin user. This is an encrypted hash that needs
-# to be generated using the `aliasvault-cli` tool. This allows you to login
-# to the admin panel at https://your-hostname/admin.
-#
-# For example:
-# docker run --rm ghcr.io/lanedirt/aliasvault-installcli:latest hash-password "my-password"
-#
-# Then copy the output and paste it into the ADMIN_PASSWORD_HASH variable below.
-# When changing the hash, update the ADMIN_PASSWORD_GENERATED variable to the current date and time
-# and then restart the AliasVault docker containers to apply the changes.
-ADMIN_PASSWORD_HASH=
-
-# Set the date and time the admin password was last generated. When changing the
-# admin password hash manually, make sure to increase this value so the system
-# knows that the password has been changed and should be overwritten with the new hash.
-ADMIN_PASSWORD_GENERATED=2024-01-01T00:00:00Z
-
# ===========================================
# EMAIL SERVER CONFIGURATION
# ===========================================
diff --git a/apps/server/Databases/AliasServerDb/Dockerfile b/apps/server/Databases/AliasServerDb/Dockerfile
index 142a0c440..1007ccc36 100644
--- a/apps/server/Databases/AliasServerDb/Dockerfile
+++ b/apps/server/Databases/AliasServerDb/Dockerfile
@@ -3,4 +3,8 @@ FROM postgres:16-alpine
# Add any custom PostgreSQL configurations if needed
COPY apps/server/Databases/AliasServerDb/postgresql.conf /etc/postgresql/postgresql.conf
+# Define database name and username
+ENV POSTGRES_DB=aliasvault
+ENV POSTGRES_USER=aliasvault
+
CMD ["postgres", "-c", "config_file=/etc/postgresql/postgresql.conf"]
\ No newline at end of file
diff --git a/apps/server/Databases/AliasServerDb/PostgresqlDbContextFactory.cs b/apps/server/Databases/AliasServerDb/PostgresqlDbContextFactory.cs
index 5afbd2276..c701ee7cc 100644
--- a/apps/server/Databases/AliasServerDb/PostgresqlDbContextFactory.cs
+++ b/apps/server/Databases/AliasServerDb/PostgresqlDbContextFactory.cs
@@ -9,6 +9,7 @@ namespace AliasServerDb;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
+using Npgsql;
/// <summary>
/// The PostgreSQL DbContext factory.
@@ -55,8 +56,65 @@ public class PostgresqlDbContextFactory : IAliasServerDbContextFactory
connectionString = _configuration.GetConnectionString("AliasServerDbContext");
}
+ // If running in container override the connection string with the one from the secret file
+ if (IsRunningInContainer())
+ {
+ try
+ {
+ // Parse the existing connection string
+ var builder = new NpgsqlConnectionStringBuilder();
+
+ // Override the password with the one from the secret file
+ builder.Host = "postgres";
+ builder.Database = "aliasvault";
+ builder.Username = "aliasvault";
+ builder.Port = 5432;
+ builder.Password = GetPostgresPasswordFromSecretFile();
+
+ // Build the connection string with the new password
+ connectionString = builder.ConnectionString;
+ }
+ catch (Exception ex)
+ {
+ // Log the error but don't fail - use the original connection string
+ Console.WriteLine($"Warning: Failed to override PostgreSQL password from secret file: {ex.Message}");
+ }
+ }
+
optionsBuilder
.UseNpgsql(connectionString, options => options.CommandTimeout(60))
.UseLazyLoadingProxies();
}
+
+ /// <summary>
+ /// Determines if the application is running in a container.
+ /// </summary>
+ /// <returns>True if running in a container, false otherwise.</returns>
+ private static bool IsRunningInContainer()
+ {
+ return Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER") == "true";
+ }
+
+ /// <summary>
+ /// Gets the PostgreSQL password from the secret file.
+ /// </summary>
+ /// <returns>The PostgreSQL password.</returns>
+ /// <exception cref="KeyNotFoundException">Thrown when the PostgreSQL password cannot be found.</exception>
+ private static string GetPostgresPasswordFromSecretFile()
+ {
+ const string secretsFilePath = "/secrets/postgres_password";
+
+ if (!File.Exists(secretsFilePath))
+ {
+ throw new KeyNotFoundException($"PostgreSQL password file not found at {secretsFilePath}. Container initialization may have failed.");
+ }
+
+ var secretValue = File.ReadAllText(secretsFilePath).Trim();
+ if (string.IsNullOrEmpty(secretValue))
+ {
+ throw new KeyNotFoundException($"PostgreSQL password file at {secretsFilePath} is empty.");
+ }
+
+ return secretValue;
+ }
}
diff --git a/docker-compose.yml b/docker-compose.yml
index 1787e2d77..61916bd28 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -3,12 +3,14 @@ services:
image: ghcr.io/lanedirt/aliasvault-postgres:latest
volumes:
- ./database/postgres:/var/lib/postgresql/data:rw
+ - ./secrets:/secrets:ro
env_file:
- .env
restart: always
environment:
POSTGRES_DB: aliasvault
POSTGRES_USER: aliasvault
+ POSTGRES_PASSWORD_FILE: /secrets/postgres_password
healthcheck:
test: ["CMD-SHELL", "pg_isready -U aliasvault"]
interval: 5s
@@ -34,14 +36,13 @@ services:
- ./database:/database:rw
- ./certificates/app:/certificates/app:rw
- ./logs:/logs:rw
+ - ./secrets:/secrets:ro
restart: always
env_file:
- .env
depends_on:
postgres:
condition: service_healthy
- environment:
- ConnectionStrings__AliasServerDbContext: "Host=postgres;Database=aliasvault;Username=aliasvault;Password=${POSTGRES_PASSWORD}"
admin:
image: ghcr.io/lanedirt/aliasvault-admin:latest
@@ -51,14 +52,13 @@ services:
- ./database:/database:rw
- ./certificates/app:/certificates/app:rw
- ./logs:/logs:rw
+ - ./secrets:/secrets:ro
restart: always
env_file:
- .env
depends_on:
postgres:
condition: service_healthy
- environment:
- ConnectionStrings__AliasServerDbContext: "Host=postgres;Database=aliasvault;Username=aliasvault;Password=${POSTGRES_PASSWORD}"
reverse-proxy:
image: ghcr.io/lanedirt/aliasvault-reverse-proxy:latest
@@ -86,25 +86,23 @@ services:
volumes:
- ./database:/database:rw
- ./logs:/logs:rw
+ - ./secrets:/secrets:ro
restart: always
env_file:
- .env
depends_on:
postgres:
condition: service_healthy
- environment:
- ConnectionStrings__AliasServerDbContext: "Host=postgres;Database=aliasvault;Username=aliasvault;Password=${POSTGRES_PASSWORD}"
task-runner:
image: ghcr.io/lanedirt/aliasvault-task-runner:latest
volumes:
- ./database:/database:rw
- ./logs:/logs:rw
+ - ./secrets:/secrets:ro
restart: always
env_file:
- .env
depends_on:
postgres:
- condition: service_healthy
- environment:
- ConnectionStrings__AliasServerDbContext: "Host=postgres;Database=aliasvault;Username=aliasvault;Password=${POSTGRES_PASSWORD}"
\ No newline at end of file
+ condition: service_healthy
\ No newline at end of file
diff --git a/dockerfiles/all-in-one/s6-scripts/init/script b/dockerfiles/all-in-one/s6-scripts/init/script
index 7c1318e71..aef2ad213 100644
--- a/dockerfiles/all-in-one/s6-scripts/init/script
+++ b/dockerfiles/all-in-one/s6-scripts/init/script
@@ -106,9 +106,9 @@ if [ ! -d "$PGDATA/base" ]; then
# Initialize database as postgres user
log 1 "[init] → Running initdb..."
if [ "$VERBOSITY" -ge 2 ]; then
- su - postgres -c "/usr/lib/postgresql/16/bin/initdb -D $PGDATA --locale=en_US.UTF-8 --encoding=UTF8" 2>&1 | tee /logs/postgres/initdb.log
+ su - postgres -c "/usr/lib/postgresql/16/bin/initdb -D $PGDATA --locale=C.UTF-8 --encoding=UTF8" 2>&1 | tee /logs/postgres/initdb.log
else
- su - postgres -c "/usr/lib/postgresql/16/bin/initdb -D $PGDATA --locale=en_US.UTF-8 --encoding=UTF8" > /logs/postgres/initdb.log 2>&1
+ su - postgres -c "/usr/lib/postgresql/16/bin/initdb -D $PGDATA --locale=C.UTF-8 --encoding=UTF8" > /logs/postgres/initdb.log 2>&1
fi
# Configure PostgreSQL
@@ -152,11 +152,11 @@ if [ ! -d "$PGDATA/base" ]; then
log 0 "[init] → Creating AliasVault database and user..."
if [ "$VERBOSITY" -ge 2 ]; then
su - postgres -c "/usr/lib/postgresql/16/bin/psql -c \"CREATE USER aliasvault WITH PASSWORD '$POSTGRES_PASSWORD'\""
- su - postgres -c "/usr/lib/postgresql/16/bin/psql -c \"CREATE DATABASE aliasvault OWNER aliasvault;\""
+ su - postgres -c "/usr/lib/postgresql/16/bin/psql -c \"CREATE DATABASE aliasvault OWNER aliasvault LOCALE 'C.UTF-8';\""
su - postgres -c "/usr/lib/postgresql/16/bin/psql -c \"GRANT ALL PRIVILEGES ON DATABASE aliasvault TO aliasvault;\""
else
su - postgres -c "/usr/lib/postgresql/16/bin/psql -c \"CREATE USER aliasvault WITH PASSWORD '$POSTGRES_PASSWORD'\"" >/dev/null 2>&1
- su - postgres -c "/usr/lib/postgresql/16/bin/psql -c \"CREATE DATABASE aliasvault OWNER aliasvault;\"" >/dev/null 2>&1
+ su - postgres -c "/usr/lib/postgresql/16/bin/psql -c \"CREATE DATABASE aliasvault OWNER aliasvault LOCALE 'C.UTF-8';\"" >/dev/null 2>&1
su - postgres -c "/usr/lib/postgresql/16/bin/psql -c \"GRANT ALL PRIVILEGES ON DATABASE aliasvault TO aliasvault;\"" >/dev/null 2>&1
fi