Update install.sh to generate postgresql credentials (#190)

This commit is contained in:
Leendert de Borst
2024-12-23 13:57:01 +01:00
parent 78a872a67d
commit 9735df0436
8 changed files with 94 additions and 15 deletions

4
.gitignore vendored
View File

@@ -272,6 +272,10 @@ ServiceFabricBackup/
*.sqlite-shm
*.sqlite-wal
# SQL files
*.sql
*.sql.gz
# Business Intelligence projects
*.rdl.data
*.bim.layout

View File

@@ -52,7 +52,7 @@ This method uses pre-built Docker images and works on minimal hardware specifica
- Linux VM with root access (Ubuntu or RHEL based distros recommended)
- 1 vCPU
- 512MB RAM
- 1GB RAM
- 16GB disk space
- Docker installed

View File

@@ -42,9 +42,3 @@ services:
dockerfile: Dockerfile.postgres
ports:
- "5432:5432"
volumes:
- ./database/postgres:/var/lib/postgresql/data:rw
environment:
POSTGRES_DB: aliasvault
POSTGRES_USER: aliasvault
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}

View File

@@ -92,8 +92,6 @@ services:
image: ghcr.io/lanedirt/aliasvault-postgres:latest
volumes:
- ./database/postgres:/var/lib/postgresql/data:rw
environment:
POSTGRES_DB: aliasvault
POSTGRES_USER: aliasvault
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
env_file:
- .env
restart: always

View File

@@ -20,7 +20,7 @@ To get AliasVault up and running quickly, run the install script to pull pre-bui
### Hardware requirements
- Linux VM with root access (Ubuntu or RHEL based distros recommended)
- 1 vCPU
- 512MB RAM
- 1GB RAM
- 16GB disk space
- Docker installed

View File

@@ -0,0 +1,51 @@
---
layout: default
title: PostgreSQL Commands
parent: Development
grand_parent: Miscellaneous
nav_order: 1
---
# PostgreSQL Commands
## Backup database to file
To backup the database to a file, you can use the following command:
```bash
docker compose exec postgres pg_dump -U aliasvault aliasvault | gzip > aliasvault.sql.gz
```
## Import database from file
To drop the existing database and restore the database from a file, you can use the following command:
{: .warning }
Executing this command will drop the existing database and restore the database from the file. Make sure to have a backup of the existing database before running this command.
```bash
docker compose exec postgres psql -U aliasvault postgres -c "DROP DATABASE aliasvault;" && \
docker compose exec postgres psql -U aliasvault postgres -c "CREATE DATABASE aliasvault;" && \
gunzip < aliasvault.sql.gz | docker compose exec -iT postgres psql -U aliasvault aliasvault
```
## Change master password
By default during initial installation the PostgreSQL master password is set to a random string that is
stored in the `.env` file with the `POSTGRES_PASSWORD` variable.
If you wish to change the master password, you can do so by running the following command:
1. Open a terminal and navigate to the root of the AliasVault repository.
2. Run the following command to connect to the PostgreSQL container:
```bash
docker compose exec -it postgres psql -U aliasvault -d aliasvault
```
3. Once connected to the database, you can change the master password by running the following command:
```sql
ALTER USER aliasvault WITH PASSWORD 'new_password';
```
4. Press Enter to confirm the changes.
5. Exit the PostgreSQL shell by running `\q`.
6. Manually update the `.env` file variable `POSTGRES_PASSWORD` with the new password.
7. Restart the AliasVault containers by running the following command:
```bash
docker compose restart
```

View File

@@ -338,6 +338,17 @@ populate_data_protection_cert_pass() {
fi
}
populate_postgres_password() {
printf "${CYAN}> Checking POSTGRES_PASSWORD...${NC}\n"
if ! grep -q "^POSTGRES_PASSWORD=" "$ENV_FILE" || [ -z "$(grep "^POSTGRES_PASSWORD=" "$ENV_FILE" | cut -d '=' -f2)" ]; then
# Generate a strong random password with 32 characters
POSTGRES_PASS=$(openssl rand -base64 32)
update_env_var "POSTGRES_PASSWORD" "$POSTGRES_PASS"
else
printf " ${GREEN}> POSTGRES_PASSWORD already exists.${NC}\n"
fi
}
set_private_email_domains() {
printf "${CYAN}> Checking PRIVATE_EMAIL_DOMAINS...${NC}\n"
if ! grep -q "^PRIVATE_EMAIL_DOMAINS=" "$ENV_FILE" || [ -z "$(grep "^PRIVATE_EMAIL_DOMAINS=" "$ENV_FILE" | cut -d '=' -f2)" ]; then
@@ -683,6 +694,7 @@ handle_build() {
populate_hostname || { printf "${RED}> Failed to set hostname${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; }
populate_postgres_password || { printf "${RED}> Failed to set PostgreSQL password${NC}\n"; exit 1; }
set_private_email_domains || { printf "${RED}> Failed to set email domains${NC}\n"; exit 1; }
set_smtp_tls_enabled || { printf "${RED}> Failed to set SMTP TLS${NC}\n"; exit 1; }
set_support_email || { printf "${RED}> Failed to set support email${NC}\n"; exit 1; }
@@ -1365,6 +1377,7 @@ handle_install_version() {
populate_hostname || { printf "${RED}> Failed to set hostname${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; }
populate_postgres_password || { printf "${RED}> Failed to set PostgreSQL password${NC}\n"; exit 1; }
set_private_email_domains || { printf "${RED}> Failed to set email domains${NC}\n"; exit 1; }
set_smtp_tls_enabled || { printf "${RED}> Failed to set SMTP TLS${NC}\n"; exit 1; }
set_support_email || { printf "${RED}> Failed to set support email${NC}\n"; exit 1; }

View File

@@ -23,9 +23,28 @@ public static class DatabaseConfiguration
/// <returns>The IServiceCollection for method chaining.</returns>
public static IServiceCollection AddAliasVaultDatabaseConfiguration(this IServiceCollection services, IConfiguration configuration)
{
var dbProvider = configuration.GetValue<string>("DatabaseProvider")?.ToLower() ?? "sqlite";
// Check for environment variable first, then fall back to configuration
var connectionString = Environment.GetEnvironmentVariable("ConnectionStrings__AliasServerDbContext");
var dbProvider = Environment.GetEnvironmentVariable("DatabaseProvider")?.ToLower()
?? configuration.GetValue<string>("DatabaseProvider")?.ToLower()
?? "postgresql";
// Add custom DbContextFactory registration which supports multiple database providers.
// Create a new configuration if we have an environment-provided connection string
if (!string.IsNullOrEmpty(connectionString))
{
var configDictionary = new Dictionary<string, string?>
{
["ConnectionStrings:AliasServerDbContext"] = connectionString,
};
var configurationBuilder = new ConfigurationBuilder()
.AddConfiguration(configuration)
.AddInMemoryCollection(configDictionary);
configuration = configurationBuilder.Build();
}
// Add custom DbContextFactory registration which supports multiple database providers
switch (dbProvider)
{
case "postgresql":
@@ -41,7 +60,7 @@ public static class DatabaseConfiguration
services.AddDbContextFactory<AliasServerDbContext>((sp, options) =>
{
var factory = sp.GetRequiredService<IAliasServerDbContextFactory>();
factory.ConfigureDbContextOptions(options); // Let the factory configure the options directly
factory.ConfigureDbContextOptions(options);
});
// Add scoped DbContext registration based on the factory