2017-10-24 04:45:59 +02:00
|
|
|
|
using System;
|
2017-11-07 04:55:15 +01:00
|
|
|
|
using System.Collections.Generic;
|
2017-10-24 04:45:59 +02:00
|
|
|
|
using System.IO;
|
2018-08-30 17:35:44 +02:00
|
|
|
|
using System.Linq;
|
2017-10-24 04:45:59 +02:00
|
|
|
|
|
|
|
|
|
namespace Bit.Setup
|
|
|
|
|
{
|
|
|
|
|
public class EnvironmentFileBuilder
|
|
|
|
|
{
|
2018-08-30 17:35:44 +02:00
|
|
|
|
private readonly Context _context;
|
|
|
|
|
|
2017-11-07 04:55:15 +01:00
|
|
|
|
private IDictionary<string, string> _globalValues;
|
|
|
|
|
private IDictionary<string, string> _mssqlValues;
|
2018-08-30 17:35:44 +02:00
|
|
|
|
private IDictionary<string, string> _globalOverrideValues;
|
|
|
|
|
private IDictionary<string, string> _mssqlOverrideValues;
|
2017-11-07 04:55:15 +01:00
|
|
|
|
|
2018-08-30 17:35:44 +02:00
|
|
|
|
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",
|
|
|
|
|
};
|
|
|
|
|
}
|
2017-10-24 04:45:59 +02:00
|
|
|
|
|
2017-11-07 04:55:15 +01:00
|
|
|
|
public void BuildForInstaller()
|
2017-10-24 04:45:59 +02:00
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory("/bitwarden/env/");
|
2018-08-30 17:35:44 +02:00
|
|
|
|
Init();
|
2017-11-07 04:55:15 +01:00
|
|
|
|
Build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void BuildForUpdater()
|
|
|
|
|
{
|
2018-08-30 17:35:44 +02:00
|
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-07 04:55:15 +01:00
|
|
|
|
Build();
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-30 17:35:44 +02:00
|
|
|
|
private void Init()
|
2017-11-07 04:55:15 +01:00
|
|
|
|
{
|
2018-08-30 17:35:44 +02:00
|
|
|
|
var dbPassword = Helpers.SecureRandomString(32);
|
|
|
|
|
var dbConnectionString = Helpers.MakeSqlConnectionString("mssql", "vault", "sa", dbPassword);
|
|
|
|
|
_globalOverrideValues = new Dictionary<string, string>
|
2017-11-07 04:55:15 +01:00
|
|
|
|
{
|
2018-08-30 17:35:44 +02:00
|
|
|
|
["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",
|
2018-08-16 00:43:26 +02:00
|
|
|
|
["globalSettings__internalIdentityKey"] = Helpers.SecureRandomString(64, alpha: true, numeric: true),
|
|
|
|
|
["globalSettings__duo__aKey"] = Helpers.SecureRandomString(64, alpha: true, numeric: true),
|
2018-08-30 17:35:44 +02:00
|
|
|
|
["globalSettings__installation__id"] = _context.Install?.InstallationId.ToString(),
|
|
|
|
|
["globalSettings__installation__key"] = _context.Install?.InstallationKey,
|
2017-11-07 04:55:15 +01:00
|
|
|
|
["globalSettings__yubico__clientId"] = "REPLACE",
|
|
|
|
|
["globalSettings__yubico__key"] = "REPLACE",
|
2018-08-30 17:35:44 +02:00
|
|
|
|
["globalSettings__mail__replyToEmail"] = $"no-reply@{_context.Config.Domain}",
|
2017-11-07 04:55:15 +01:00
|
|
|
|
["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",
|
2018-03-28 16:45:30 +02:00
|
|
|
|
["adminSettings__admins"] = string.Empty,
|
2017-11-07 04:55:15 +01:00
|
|
|
|
};
|
2017-10-24 04:45:59 +02:00
|
|
|
|
|
2018-08-30 17:35:44 +02:00
|
|
|
|
if(!_context.Config.PushNotifications)
|
2017-11-07 04:55:15 +01:00
|
|
|
|
{
|
2018-08-30 17:35:44 +02:00
|
|
|
|
_globalOverrideValues.Add("globalSettings__pushRelayBaseUri", "REPLACE");
|
2017-11-07 04:55:15 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-30 17:35:44 +02:00
|
|
|
|
_mssqlOverrideValues = new Dictionary<string, string>
|
2017-10-24 04:45:59 +02:00
|
|
|
|
{
|
2017-11-07 04:55:15 +01:00
|
|
|
|
["ACCEPT_EULA"] = "Y",
|
|
|
|
|
["MSSQL_PID"] = "Express",
|
2018-08-30 17:35:44 +02:00
|
|
|
|
["SA_PASSWORD"] = dbPassword,
|
2017-11-07 04:55:15 +01:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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]))
|
2017-10-24 04:45:59 +02:00
|
|
|
|
{
|
2017-11-07 04:55:15 +01:00
|
|
|
|
_values[lineParts[0]] = value;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_values.Add(lineParts[0], value);
|
2017-10-24 04:45:59 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-07 04:55:15 +01:00
|
|
|
|
}
|
2017-10-24 04:45:59 +02:00
|
|
|
|
|
2017-11-07 04:55:15 +01:00
|
|
|
|
private void Build()
|
|
|
|
|
{
|
2018-08-30 17:35:44 +02:00
|
|
|
|
var template = Helpers.ReadTemplate("EnvironmentFile");
|
|
|
|
|
|
2017-11-07 04:55:15 +01:00
|
|
|
|
Console.WriteLine("Building docker environment files.");
|
|
|
|
|
Directory.CreateDirectory("/bitwarden/docker/");
|
|
|
|
|
using(var sw = File.CreateText("/bitwarden/docker/global.env"))
|
|
|
|
|
{
|
2018-08-30 17:35:44 +02:00
|
|
|
|
sw.Write(template(new TemplateModel(_globalValues)));
|
2017-11-07 04:55:15 +01:00
|
|
|
|
}
|
2017-12-21 04:31:30 +01:00
|
|
|
|
Helpers.Exec("chmod 600 /bitwarden/docker/global.env");
|
|
|
|
|
|
2017-11-07 04:55:15 +01:00
|
|
|
|
using(var sw = File.CreateText("/bitwarden/docker/mssql.env"))
|
2017-10-24 04:45:59 +02:00
|
|
|
|
{
|
2018-08-30 17:35:44 +02:00
|
|
|
|
sw.Write(template(new TemplateModel(_mssqlValues)));
|
2017-11-07 04:55:15 +01:00
|
|
|
|
}
|
2017-12-21 04:31:30 +01:00
|
|
|
|
Helpers.Exec("chmod 600 /bitwarden/docker/mssql.env");
|
|
|
|
|
|
2017-11-07 04:55:15 +01:00
|
|
|
|
Console.WriteLine("Building docker environment override files.");
|
2018-08-30 17:35:44 +02:00
|
|
|
|
Directory.CreateDirectory("/bitwarden/env/");
|
2017-11-07 04:55:15 +01:00
|
|
|
|
using(var sw = File.CreateText("/bitwarden/env/global.override.env"))
|
|
|
|
|
{
|
2018-08-30 17:35:44 +02:00
|
|
|
|
sw.Write(template(new TemplateModel(_globalOverrideValues)));
|
2017-11-07 04:55:15 +01:00
|
|
|
|
}
|
2017-12-21 04:31:30 +01:00
|
|
|
|
Helpers.Exec("chmod 600 /bitwarden/env/global.override.env");
|
|
|
|
|
|
2017-11-07 04:55:15 +01:00
|
|
|
|
using(var sw = File.CreateText("/bitwarden/env/mssql.override.env"))
|
|
|
|
|
{
|
2018-08-30 17:35:44 +02:00
|
|
|
|
sw.Write(template(new TemplateModel(_mssqlOverrideValues)));
|
2017-10-24 04:45:59 +02:00
|
|
|
|
}
|
2017-12-21 04:31:30 +01:00
|
|
|
|
Helpers.Exec("chmod 600 /bitwarden/env/mssql.override.env");
|
2018-03-27 21:23:02 +02:00
|
|
|
|
|
|
|
|
|
// Empty uid env file. Only used on Linux hosts.
|
2018-05-31 18:05:26 +02:00
|
|
|
|
if(!File.Exists("/bitwarden/env/uid.env"))
|
|
|
|
|
{
|
|
|
|
|
using(var sw = File.CreateText("/bitwarden/env/uid.env")) { }
|
|
|
|
|
}
|
2017-10-24 04:45:59 +02:00
|
|
|
|
}
|
2018-08-30 17:35:44 +02:00
|
|
|
|
|
|
|
|
|
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; }
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-24 04:45:59 +02:00
|
|
|
|
}
|
|
|
|
|
}
|