mirror of
https://github.com/bitwarden/server.git
synced 2025-04-06 21:48:12 -05:00
build environment files from setup
This commit is contained in:
parent
49bd3bcf35
commit
f4142a1a1d
@ -1,10 +1,14 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
|
||||||
namespace Bit.Setup
|
namespace Bit.Setup
|
||||||
{
|
{
|
||||||
public class EnvironmentFileBuilder
|
public class EnvironmentFileBuilder
|
||||||
{
|
{
|
||||||
|
private IDictionary<string, string> _globalValues;
|
||||||
|
private IDictionary<string, string> _mssqlValues;
|
||||||
|
|
||||||
public string Url { get; set; }
|
public string Url { get; set; }
|
||||||
public string Domain { get; set; }
|
public string Domain { get; set; }
|
||||||
public string IdentityCertPassword { get; set; }
|
public string IdentityCertPassword { get; set; }
|
||||||
@ -14,50 +18,143 @@ namespace Bit.Setup
|
|||||||
public string DatabasePassword { get; set; }
|
public string DatabasePassword { get; set; }
|
||||||
public string OutputDirectory { get; set; }
|
public string OutputDirectory { get; set; }
|
||||||
|
|
||||||
public void Build()
|
public void BuildForInstaller()
|
||||||
{
|
{
|
||||||
Console.WriteLine("Building docker environment override files.");
|
|
||||||
Directory.CreateDirectory("/bitwarden/env/");
|
Directory.CreateDirectory("/bitwarden/env/");
|
||||||
var dbConnectionString = Helpers.MakeSqlConnectionString("mssql", "vault", "sa", DatabasePassword);
|
Init();
|
||||||
|
Build();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void BuildForUpdater()
|
||||||
|
{
|
||||||
|
Init();
|
||||||
|
LoadExistingValues(_globalValues, "/bitwarden/env/global.override.env");
|
||||||
|
LoadExistingValues(_mssqlValues, "/bitwarden/env/mssql.override.env");
|
||||||
|
Build();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void Init()
|
||||||
|
{
|
||||||
|
var dbConnectionString = Helpers.MakeSqlConnectionString("mssql", "vault", "sa", DatabasePassword);
|
||||||
|
_globalValues = new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
["globalSettings__baseServiceUri__vault"] = Url,
|
||||||
|
["globalSettings__baseServiceUri__api"] = $"{Url}/api",
|
||||||
|
["globalSettings__baseServiceUri__identity"] = $"{Url}/identity",
|
||||||
|
["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}/core/logs",
|
||||||
|
["globalSettings__licenseDirectory"] = $"{OutputDirectory}/core/licenses",
|
||||||
|
["globalSettings__duo__aKey"] = $"{Helpers.SecureRandomString(64, alpha: true, numeric: true)}",
|
||||||
|
["globalSettings__installation__id"] = InstallationId?.ToString(),
|
||||||
|
["globalSettings__installation__key"] = InstallationKey,
|
||||||
|
["globalSettings__yubico__clientId"] = "REPLACE",
|
||||||
|
["globalSettings__yubico__key"] = "REPLACE",
|
||||||
|
["globalSettings__mail__replyToEmail"] = $"no-reply@{Domain}",
|
||||||
|
["globalSettings__mail__smtp__host"] = "REPLACE",
|
||||||
|
["globalSettings__mail__smtp__username"] = "REPLACE",
|
||||||
|
["globalSettings__mail__smtp__password"] = "REPLACE",
|
||||||
|
["globalSettings__mail__smtp__ssl"] = "true",
|
||||||
|
["globalSettings__mail__smtp__port"] = "587",
|
||||||
|
["globalSettings__mail__smtp__useDefaultCredentials"] = "false",
|
||||||
|
["globalSettings__disableUserRegistration"] = "false",
|
||||||
|
};
|
||||||
|
|
||||||
|
if(!Push)
|
||||||
|
{
|
||||||
|
_globalValues.Add("globalSettings__pushRelayBaseUri", "REPLACE");
|
||||||
|
}
|
||||||
|
|
||||||
|
_mssqlValues = new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
["ACCEPT_EULA"] = "Y",
|
||||||
|
["MSSQL_PID"] = "Express",
|
||||||
|
["SA_PASSWORD"] = DatabasePassword,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private void LoadExistingValues(IDictionary<string, string> _values, string file)
|
||||||
|
{
|
||||||
|
if(!File.Exists(file))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var fileLines = File.ReadAllLines(file);
|
||||||
|
foreach(var line in fileLines)
|
||||||
|
{
|
||||||
|
if(!line.Contains("="))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var value = string.Empty;
|
||||||
|
var lineParts = line.Split("=", 2);
|
||||||
|
if(lineParts.Length < 1)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(lineParts.Length > 1)
|
||||||
|
{
|
||||||
|
value = lineParts[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
if(_values.ContainsKey(lineParts[0]))
|
||||||
|
{
|
||||||
|
_values[lineParts[0]] = value;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_values.Add(lineParts[0], value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void Build()
|
||||||
|
{
|
||||||
|
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__internalIdentity=http://identity
|
||||||
|
globalSettings__pushRelayBaseUri=https://push.bitwarden.com
|
||||||
|
globalSettings__installation__identityUri=https://identity.bitwarden.com
|
||||||
|
");
|
||||||
|
}
|
||||||
|
|
||||||
|
using(var sw = File.CreateText("/bitwarden/docker/mssql.env"))
|
||||||
|
{
|
||||||
|
sw.Write($@"ACCEPT_EULA=Y
|
||||||
|
MSSQL_PID=Express
|
||||||
|
SA_PASSWORD=SECRET
|
||||||
|
");
|
||||||
|
}
|
||||||
|
|
||||||
|
Console.WriteLine("Building docker environment override files.");
|
||||||
|
Directory.CreateDirectory(" /bitwarden/env/");
|
||||||
using(var sw = File.CreateText("/bitwarden/env/global.override.env"))
|
using(var sw = File.CreateText("/bitwarden/env/global.override.env"))
|
||||||
{
|
{
|
||||||
sw.Write($@"globalSettings__baseServiceUri__vault={Url}
|
foreach(var item in _globalValues)
|
||||||
globalSettings__baseServiceUri__api={Url}/api
|
|
||||||
globalSettings__baseServiceUri__identity={Url}/identity
|
|
||||||
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}/core/logs
|
|
||||||
globalSettings__licenseDirectory={OutputDirectory}/core/licenses
|
|
||||||
globalSettings__duo__aKey={Helpers.SecureRandomString(64, alpha: true, numeric: true)}
|
|
||||||
globalSettings__installation__id={InstallationId}
|
|
||||||
globalSettings__installation__key={InstallationKey}
|
|
||||||
globalSettings__yubico__clientId=REPLACE
|
|
||||||
globalSettings__yubico__key=REPLACE
|
|
||||||
globalSettings__mail__replyToEmail=no-reply@{Domain}
|
|
||||||
globalSettings__mail__smtp__host=REPLACE
|
|
||||||
globalSettings__mail__smtp__username=REPLACE
|
|
||||||
globalSettings__mail__smtp__password=REPLACE
|
|
||||||
globalSettings__mail__smtp__ssl=true
|
|
||||||
globalSettings__mail__smtp__port=587
|
|
||||||
globalSettings__mail__smtp__useDefaultCredentials=false
|
|
||||||
globalSettings__disableUserRegistration=false");
|
|
||||||
|
|
||||||
if(!Push)
|
|
||||||
{
|
{
|
||||||
sw.Write(@"
|
sw.WriteLine($"{item.Key}={item.Value}");
|
||||||
globalSettings__pushRelayBaseUri=REPLACE");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
using(var sw = File.CreateText("/bitwarden/env/mssql.override.env"))
|
using(var sw = File.CreateText("/bitwarden/env/mssql.override.env"))
|
||||||
{
|
{
|
||||||
sw.Write($@"ACCEPT_EULA=Y
|
foreach(var item in _mssqlValues)
|
||||||
MSSQL_PID=Express
|
{
|
||||||
SA_PASSWORD={DatabasePassword}");
|
sw.WriteLine($"{item.Key}={item.Value}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -117,7 +117,7 @@ namespace Bit.Setup
|
|||||||
Push = push,
|
Push = push,
|
||||||
Url = url
|
Url = url
|
||||||
};
|
};
|
||||||
environmentFileBuilder.Build();
|
environmentFileBuilder.BuildForInstaller();
|
||||||
|
|
||||||
var appSettingsBuilder = new AppSettingsBuilder(url, domain);
|
var appSettingsBuilder = new AppSettingsBuilder(url, domain);
|
||||||
appSettingsBuilder.Build();
|
appSettingsBuilder.Build();
|
||||||
@ -250,6 +250,9 @@ namespace Bit.Setup
|
|||||||
|
|
||||||
private static void RebuildConfigs()
|
private static void RebuildConfigs()
|
||||||
{
|
{
|
||||||
|
var environmentFileBuilder = new EnvironmentFileBuilder();
|
||||||
|
environmentFileBuilder.BuildForUpdater();
|
||||||
|
|
||||||
var url = Helpers.GetValueFronEnvFile("global", "globalSettings__baseServiceUri__vault");
|
var url = Helpers.GetValueFronEnvFile("global", "globalSettings__baseServiceUri__vault");
|
||||||
if(!Uri.TryCreate(url, UriKind.Absolute, out Uri uri))
|
if(!Uri.TryCreate(url, UriKind.Absolute, out Uri uri))
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user