mirror of
https://github.com/bitwarden/server.git
synced 2025-06-30 15:42:48 -05:00
convert setup to use config.yml
This commit is contained in:
@ -1,62 +1,95 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace Bit.Setup
|
||||
{
|
||||
public class EnvironmentFileBuilder
|
||||
{
|
||||
private readonly Context _context;
|
||||
|
||||
private IDictionary<string, string> _globalValues;
|
||||
private IDictionary<string, string> _mssqlValues;
|
||||
private IDictionary<string, string> _globalOverrideValues;
|
||||
private IDictionary<string, string> _mssqlOverrideValues;
|
||||
|
||||
public string Url { get; set; } = "https://localhost";
|
||||
public string Domain { get; set; } = "localhost";
|
||||
public string IdentityCertPassword { get; set; } = "REPLACE";
|
||||
public Guid? InstallationId { get; set; }
|
||||
public string InstallationKey { get; set; }
|
||||
public bool Push { get; set; }
|
||||
public string DatabasePassword { get; set; } = "REPLACE";
|
||||
public string OutputDirectory { get; set; } = ".";
|
||||
public EnvironmentFileBuilder(Context context)
|
||||
{
|
||||
_context = context;
|
||||
_globalValues = new Dictionary<string, string>
|
||||
{
|
||||
["ASPNETCORE_ENVIRONMENT"] = "Production",
|
||||
["globalSettings__selfHosted"] = "true",
|
||||
["globalSettings__baseServiceUri__vault"] = "http://localhost",
|
||||
["globalSettings__baseServiceUri__api"] = "http://localhost/api",
|
||||
["globalSettings__baseServiceUri__identity"] = "http://localhost/identity",
|
||||
["globalSettings__baseServiceUri__admin"] = "http://localhost/admin",
|
||||
["globalSettings__baseServiceUri__notifications"] = "http://localhost/notifications",
|
||||
["globalSettings__baseServiceUri__internalNotifications"] = "http://notifications:5000",
|
||||
["globalSettings__baseServiceUri__internalAdmin"] = "http://admin:5000",
|
||||
["globalSettings__baseServiceUri__internalIdentity"] = "http://identity:5000",
|
||||
["globalSettings__baseServiceUri__internalApi"] = "http://api:5000",
|
||||
["globalSettings__baseServiceUri__internalVault"] = "http://web:5000",
|
||||
["globalSettings__pushRelayBaseUri"] = "https://push.bitwarden.com",
|
||||
["globalSettings__installation__identityUri"] = "https://identity.bitwarden.com",
|
||||
};
|
||||
_mssqlValues = new Dictionary<string, string>
|
||||
{
|
||||
["ACCEPT_EULA"] = "Y",
|
||||
["MSSQL_PID"] = "Express",
|
||||
["SA_PASSWORD"] = "SECRET",
|
||||
};
|
||||
}
|
||||
|
||||
public void BuildForInstaller()
|
||||
{
|
||||
Directory.CreateDirectory("/bitwarden/env/");
|
||||
Init(true);
|
||||
Init();
|
||||
Build();
|
||||
}
|
||||
|
||||
public void BuildForUpdater()
|
||||
{
|
||||
Init(false);
|
||||
LoadExistingValues(_globalValues, "/bitwarden/env/global.override.env");
|
||||
LoadExistingValues(_mssqlValues, "/bitwarden/env/mssql.override.env");
|
||||
Init();
|
||||
LoadExistingValues(_globalOverrideValues, "/bitwarden/env/global.override.env");
|
||||
LoadExistingValues(_mssqlOverrideValues, "/bitwarden/env/mssql.override.env");
|
||||
|
||||
if(_context.Config.PushNotifications &&
|
||||
_globalOverrideValues.ContainsKey("globalSettings__pushRelayBaseUri") &&
|
||||
_globalOverrideValues["globalSettings__pushRelayBaseUri"] == "REPLACE")
|
||||
{
|
||||
_globalOverrideValues.Remove("globalSettings__pushRelayBaseUri");
|
||||
}
|
||||
|
||||
Build();
|
||||
}
|
||||
|
||||
private void Init(bool forInstall)
|
||||
private void Init()
|
||||
{
|
||||
var dbConnectionString = Helpers.MakeSqlConnectionString("mssql", "vault", "sa", DatabasePassword);
|
||||
_globalValues = new Dictionary<string, string>
|
||||
var dbPassword = Helpers.SecureRandomString(32);
|
||||
var dbConnectionString = Helpers.MakeSqlConnectionString("mssql", "vault", "sa", dbPassword);
|
||||
_globalOverrideValues = new Dictionary<string, string>
|
||||
{
|
||||
["globalSettings__baseServiceUri__vault"] = Url,
|
||||
["globalSettings__baseServiceUri__api"] = $"{Url}/api",
|
||||
["globalSettings__baseServiceUri__identity"] = $"{Url}/identity",
|
||||
["globalSettings__baseServiceUri__admin"] = $"{Url}/admin",
|
||||
["globalSettings__baseServiceUri__notifications"] = $"{Url}/notifications",
|
||||
["globalSettings__sqlServer__connectionString"] = $"\"{ dbConnectionString }\"",
|
||||
["globalSettings__identityServer__certificatePassword"] = IdentityCertPassword,
|
||||
["globalSettings__attachment__baseDirectory"] = $"{OutputDirectory}/core/attachments",
|
||||
["globalSettings__attachment__baseUrl"] = $"{Url}/attachments",
|
||||
["globalSettings__dataProtection__directory"] = $"{OutputDirectory}/core/aspnet-dataprotection",
|
||||
["globalSettings__logDirectory"] = $"{OutputDirectory}/logs",
|
||||
["globalSettings__licenseDirectory"] = $"{OutputDirectory}/core/licenses",
|
||||
["globalSettings__baseServiceUri__vault"] = _context.Config.Url,
|
||||
["globalSettings__baseServiceUri__api"] = $"{_context.Config.Url}/api",
|
||||
["globalSettings__baseServiceUri__identity"] = $"{_context.Config.Url}/identity",
|
||||
["globalSettings__baseServiceUri__admin"] = $"{_context.Config.Url}/admin",
|
||||
["globalSettings__baseServiceUri__notifications"] = $"{_context.Config.Url}/notifications",
|
||||
["globalSettings__sqlServer__connectionString"] = $"\"{dbConnectionString}\"",
|
||||
["globalSettings__identityServer__certificatePassword"] = _context.Install?.IdentityCertPassword,
|
||||
["globalSettings__attachment__baseDirectory"] = $"{_context.OutputDir}/core/attachments",
|
||||
["globalSettings__attachment__baseUrl"] = $"{_context.Config.Url}/attachments",
|
||||
["globalSettings__dataProtection__directory"] = $"{_context.OutputDir}/core/aspnet-dataprotection",
|
||||
["globalSettings__logDirectory"] = $"{_context.OutputDir}/logs",
|
||||
["globalSettings__licenseDirectory"] = $"{_context.OutputDir}/core/licenses",
|
||||
["globalSettings__internalIdentityKey"] = Helpers.SecureRandomString(64, alpha: true, numeric: true),
|
||||
["globalSettings__duo__aKey"] = Helpers.SecureRandomString(64, alpha: true, numeric: true),
|
||||
["globalSettings__installation__id"] = InstallationId?.ToString(),
|
||||
["globalSettings__installation__key"] = InstallationKey,
|
||||
["globalSettings__installation__id"] = _context.Install?.InstallationId.ToString(),
|
||||
["globalSettings__installation__key"] = _context.Install?.InstallationKey,
|
||||
["globalSettings__yubico__clientId"] = "REPLACE",
|
||||
["globalSettings__yubico__key"] = "REPLACE",
|
||||
["globalSettings__mail__replyToEmail"] = $"no-reply@{Domain}",
|
||||
["globalSettings__mail__replyToEmail"] = $"no-reply@{_context.Config.Domain}",
|
||||
["globalSettings__mail__smtp__host"] = "REPLACE",
|
||||
["globalSettings__mail__smtp__username"] = "REPLACE",
|
||||
["globalSettings__mail__smtp__password"] = "REPLACE",
|
||||
@ -67,16 +100,16 @@ namespace Bit.Setup
|
||||
["adminSettings__admins"] = string.Empty,
|
||||
};
|
||||
|
||||
if(forInstall && !Push)
|
||||
if(!_context.Config.PushNotifications)
|
||||
{
|
||||
_globalValues.Add("globalSettings__pushRelayBaseUri", "REPLACE");
|
||||
_globalOverrideValues.Add("globalSettings__pushRelayBaseUri", "REPLACE");
|
||||
}
|
||||
|
||||
_mssqlValues = new Dictionary<string, string>
|
||||
_mssqlOverrideValues = new Dictionary<string, string>
|
||||
{
|
||||
["ACCEPT_EULA"] = "Y",
|
||||
["MSSQL_PID"] = "Express",
|
||||
["SA_PASSWORD"] = DatabasePassword,
|
||||
["SA_PASSWORD"] = dbPassword,
|
||||
};
|
||||
}
|
||||
|
||||
@ -120,59 +153,34 @@ namespace Bit.Setup
|
||||
|
||||
private void Build()
|
||||
{
|
||||
var template = Helpers.ReadTemplate("EnvironmentFile");
|
||||
|
||||
Console.WriteLine("Building docker environment files.");
|
||||
Directory.CreateDirectory("/bitwarden/docker/");
|
||||
using(var sw = File.CreateText("/bitwarden/docker/global.env"))
|
||||
{
|
||||
sw.Write($@"ASPNETCORE_ENVIRONMENT=Production
|
||||
globalSettings__selfHosted=true
|
||||
globalSettings__baseServiceUri__vault=http://localhost
|
||||
globalSettings__baseServiceUri__api=http://localhost/api
|
||||
globalSettings__baseServiceUri__identity=http://localhost/identity
|
||||
globalSettings__baseServiceUri__admin=http://localhost/admin
|
||||
globalSettings__baseServiceUri__notifications=http://localhost/notifications
|
||||
globalSettings__baseServiceUri__internalNotifications=http://notifications:5000
|
||||
globalSettings__baseServiceUri__internalAdmin=http://admin:5000
|
||||
globalSettings__baseServiceUri__internalIdentity=http://identity:5000
|
||||
globalSettings__baseServiceUri__internalApi=http://api:5000
|
||||
globalSettings__baseServiceUri__internalVault=http://web:5000
|
||||
globalSettings__pushRelayBaseUri=https://push.bitwarden.com
|
||||
globalSettings__installation__identityUri=https://identity.bitwarden.com
|
||||
");
|
||||
sw.Write(template(new TemplateModel(_globalValues)));
|
||||
}
|
||||
|
||||
Helpers.Exec("chmod 600 /bitwarden/docker/global.env");
|
||||
|
||||
using(var sw = File.CreateText("/bitwarden/docker/mssql.env"))
|
||||
{
|
||||
sw.Write($@"ACCEPT_EULA=Y
|
||||
MSSQL_PID=Express
|
||||
SA_PASSWORD=SECRET
|
||||
");
|
||||
sw.Write(template(new TemplateModel(_mssqlValues)));
|
||||
}
|
||||
|
||||
Helpers.Exec("chmod 600 /bitwarden/docker/mssql.env");
|
||||
|
||||
Console.WriteLine("Building docker environment override files.");
|
||||
Directory.CreateDirectory(" /bitwarden/env/");
|
||||
Directory.CreateDirectory("/bitwarden/env/");
|
||||
using(var sw = File.CreateText("/bitwarden/env/global.override.env"))
|
||||
{
|
||||
foreach(var item in _globalValues)
|
||||
{
|
||||
sw.WriteLine($"{item.Key}={item.Value}");
|
||||
}
|
||||
sw.Write(template(new TemplateModel(_globalOverrideValues)));
|
||||
}
|
||||
|
||||
Helpers.Exec("chmod 600 /bitwarden/env/global.override.env");
|
||||
|
||||
using(var sw = File.CreateText("/bitwarden/env/mssql.override.env"))
|
||||
{
|
||||
foreach(var item in _mssqlValues)
|
||||
{
|
||||
sw.WriteLine($"{item.Key}={item.Value}");
|
||||
}
|
||||
sw.Write(template(new TemplateModel(_mssqlOverrideValues)));
|
||||
}
|
||||
|
||||
Helpers.Exec("chmod 600 /bitwarden/env/mssql.override.env");
|
||||
|
||||
// Empty uid env file. Only used on Linux hosts.
|
||||
@ -181,5 +189,21 @@ SA_PASSWORD=SECRET
|
||||
using(var sw = File.CreateText("/bitwarden/env/uid.env")) { }
|
||||
}
|
||||
}
|
||||
|
||||
public class TemplateModel
|
||||
{
|
||||
public TemplateModel(IEnumerable<KeyValuePair<string, string>> variables)
|
||||
{
|
||||
Variables = variables.Select(v => new Kvp { Key = v.Key, Value = v.Value });
|
||||
}
|
||||
|
||||
public IEnumerable<Kvp> Variables { get; set; }
|
||||
|
||||
public class Kvp
|
||||
{
|
||||
public string Key { get; set; }
|
||||
public string Value { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user