1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-22 12:15:36 +01:00
bitwarden-server/util/Setup/Program.cs

305 lines
11 KiB
C#
Raw Normal View History

using DbUp;
2017-08-19 15:33:14 +02:00
using Newtonsoft.Json;
using System;
2017-08-07 22:31:00 +02:00
using System.Collections.Generic;
2017-08-21 15:57:09 +02:00
using System.Data.SqlClient;
2017-08-19 15:33:14 +02:00
using System.Net.Http;
using System.Reflection;
2017-08-07 22:31:00 +02:00
2017-09-08 17:45:20 +02:00
namespace Bit.Setup
2017-08-07 22:31:00 +02:00
{
public class Program
{
private static string[] _args = null;
private static IDictionary<string, string> _parameters = null;
2017-08-19 15:56:55 +02:00
private static Guid? _installationId = null;
2017-08-11 14:57:31 +02:00
private static string _installationKey = null;
private static string _hostOs = "win";
2017-11-07 16:54:00 +01:00
private static string _coreVersion = "latest";
private static string _webVersion = "latest";
2017-08-07 22:31:00 +02:00
public static void Main(string[] args)
{
_args = args;
_parameters = ParseParameters();
if(_parameters.ContainsKey("os"))
{
_hostOs = _parameters["os"];
}
2017-11-07 16:54:00 +01:00
if(_parameters.ContainsKey("corev"))
{
_coreVersion = _parameters["corev"];
}
if(_parameters.ContainsKey("webv"))
{
_webVersion = _parameters["webv"];
}
if(_parameters.ContainsKey("install"))
{
Install();
}
else if(_parameters.ContainsKey("update"))
{
Update();
}
2017-08-24 17:16:01 +02:00
else if(_parameters.ContainsKey("printenv"))
{
PrintEnvironment();
}
else
{
Console.WriteLine("No top-level command detected. Exiting...");
}
}
2017-08-07 22:31:00 +02:00
private static void Install()
{
2017-10-24 04:45:59 +02:00
var outputDir = _parameters.ContainsKey("out") ?
_parameters["out"].ToLowerInvariant() : "/etc/bitwarden";
2017-10-24 14:45:48 +02:00
var domain = _parameters.ContainsKey("domain") ?
2017-08-08 03:16:56 +02:00
_parameters["domain"].ToLowerInvariant() : "localhost";
2017-10-24 14:45:48 +02:00
var letsEncrypt = _parameters.ContainsKey("letsencrypt") ?
2017-08-08 03:16:56 +02:00
_parameters["letsencrypt"].ToLowerInvariant() == "y" : false;
2017-08-19 15:33:14 +02:00
if(!ValidateInstallation())
{
return;
}
2017-08-07 22:31:00 +02:00
2017-10-24 14:45:48 +02:00
var ssl = letsEncrypt;
if(!letsEncrypt)
2017-08-08 20:35:31 +02:00
{
Console.Write("(!) Do you have a SSL certificate to use? (y/n): ");
2017-10-24 14:45:48 +02:00
ssl = Console.ReadLine().ToLowerInvariant() == "y";
2017-08-08 20:35:31 +02:00
2017-10-24 14:45:48 +02:00
if(ssl)
2017-08-08 20:35:31 +02:00
{
Console.WriteLine("Make sure 'certificate.crt' and 'private.key' are provided in the " +
"appropriate directory (see setup instructions).");
}
}
2017-10-24 14:45:48 +02:00
var identityCertPassword = Helpers.SecureRandomString(32, alpha: true, numeric: true);
var certBuilder = new CertBuilder(domain, identityCertPassword, letsEncrypt, ssl);
var selfSignedSsl = certBuilder.BuildForInstall();
ssl = certBuilder.Ssl; // Ssl prop can get flipped during the build
2017-08-08 20:35:31 +02:00
2017-10-24 14:45:48 +02:00
var url = ssl ? $"https://{domain}" : $"http://{domain}";
var nginxBuilder = new NginxConfigBuilder(domain, ssl, selfSignedSsl, letsEncrypt);
2017-10-24 04:45:59 +02:00
nginxBuilder.BuildForInstaller();
2017-08-11 14:57:31 +02:00
Console.Write("(!) Do you want to use the default HTTP (80) and HTTPS (443) ports? (y/n): ");
var defaultPorts = Console.ReadLine().ToLowerInvariant() == "y";
int httpPort = default(int), httpsPort = default(int);
if(!defaultPorts)
{
Console.Write("(!) HTTP port: ");
if(int.TryParse(Console.ReadLine().ToLowerInvariant().Trim(), out httpPort))
{
Console.Write("(!) HTTPS port: ");
if(int.TryParse(Console.ReadLine().ToLowerInvariant().Trim(), out httpsPort))
{
url += (":" + httpsPort);
}
else
{
Console.WriteLine("Invalid HTTPS port.");
}
}
else
{
Console.WriteLine("Invalid HTTP port.");
}
}
Console.Write("(!) Do you want to use push notifications? (y/n): ");
2017-10-24 04:45:59 +02:00
var push = Console.ReadLine().ToLowerInvariant() == "y";
2017-08-11 14:57:31 +02:00
2017-10-24 04:45:59 +02:00
var environmentFileBuilder = new EnvironmentFileBuilder
{
DatabasePassword = Helpers.SecureRandomString(32),
2017-10-24 14:45:48 +02:00
Domain = domain,
IdentityCertPassword = identityCertPassword,
2017-10-24 04:45:59 +02:00
InstallationId = _installationId,
InstallationKey = _installationKey,
OutputDirectory = outputDir,
Push = push,
Url = url
};
2017-11-07 04:55:15 +01:00
environmentFileBuilder.BuildForInstaller();
2017-10-24 04:45:59 +02:00
2017-10-24 14:45:48 +02:00
var appSettingsBuilder = new AppSettingsBuilder(url, domain);
2017-10-24 04:45:59 +02:00
appSettingsBuilder.Build();
var appIdBuilder = new AppIdBuilder(url);
appIdBuilder.Build();
2017-11-07 16:54:00 +01:00
var dockerComposeBuilder = new DockerComposeBuilder(_hostOs, _webVersion, _coreVersion);
dockerComposeBuilder.BuildForInstaller(httpPort, httpsPort);
2017-08-07 22:31:00 +02:00
}
private static void Update()
{
if(_parameters.ContainsKey("db"))
{
MigrateDatabase();
}
2017-10-24 04:45:59 +02:00
else
{
RebuildConfigs();
}
}
2017-08-24 17:16:01 +02:00
private static void PrintEnvironment()
{
var vaultUrl = Helpers.GetValueFronEnvFile("global", "globalSettings__baseServiceUri__vault");
2017-08-24 17:35:16 +02:00
Console.WriteLine("\nbitwarden is up and running!");
Console.WriteLine("===================================================");
2017-08-24 19:28:04 +02:00
Console.WriteLine("\n- visit {0}", vaultUrl);
Console.Write("- to update, run ");
if(_hostOs == "win")
2017-08-24 17:16:01 +02:00
{
2017-08-24 17:35:16 +02:00
Console.Write("'.\\bitwarden.ps1 -update'");
2017-08-24 17:16:01 +02:00
}
else
{
2017-08-24 17:35:16 +02:00
Console.Write("'./bitwarden.sh update'");
2017-08-24 17:16:01 +02:00
}
2017-08-24 17:35:16 +02:00
Console.WriteLine("\n");
2017-08-24 17:16:01 +02:00
}
private static void MigrateDatabase()
{
Console.WriteLine("Migrating database.");
2017-08-24 17:16:01 +02:00
var dbPass = Helpers.GetValueFronEnvFile("mssql", "SA_PASSWORD");
2017-08-21 15:57:09 +02:00
var masterConnectionString = Helpers.MakeSqlConnectionString("mssql", "master", "sa", dbPass ?? string.Empty);
var vaultConnectionString = Helpers.MakeSqlConnectionString("mssql", "vault", "sa", dbPass ?? string.Empty);
using(var connection = new SqlConnection(masterConnectionString))
{
var command = new SqlCommand(
"IF ((SELECT COUNT(1) FROM sys.databases WHERE [name] = 'vault') = 0) CREATE DATABASE [vault];",
connection);
command.Connection.Open();
command.ExecuteNonQuery();
}
var upgrader = DeployChanges.To
2017-08-21 15:57:09 +02:00
.SqlDatabase(vaultConnectionString)
.JournalToSqlTable("dbo", "Migration")
.WithScriptsAndCodeEmbeddedInAssembly(Assembly.GetExecutingAssembly(),
s => s.Contains($".DbScripts.") && !s.Contains(".Archive."))
.WithTransaction()
.WithExecutionTimeout(new TimeSpan(0, 5, 0))
.LogToConsole()
.Build();
var result = upgrader.PerformUpgrade();
if(result.Successful)
{
Console.WriteLine("Migration successful.");
}
else
{
Console.WriteLine("Migration failed.");
}
}
2017-08-19 15:33:14 +02:00
private static bool ValidateInstallation()
{
2017-08-25 18:43:16 +02:00
Console.Write("(!) Enter your installation id (get it at https://bitwarden.com/host): ");
2017-08-19 15:56:55 +02:00
var installationId = Console.ReadLine();
2017-08-19 15:33:14 +02:00
Guid installationidGuid;
2017-08-19 15:56:55 +02:00
if(!Guid.TryParse(installationId.Trim(), out installationidGuid))
2017-08-19 15:33:14 +02:00
{
Console.WriteLine("Invalid installation id.");
return false;
}
2017-08-19 15:56:55 +02:00
_installationId = installationidGuid;
2017-08-19 15:33:14 +02:00
Console.Write("(!) Enter your installation key: ");
_installationKey = Console.ReadLine();
try
{
var response = new HttpClient().GetAsync("https://api.bitwarden.com/installations/" + _installationId)
.GetAwaiter().GetResult();
if(!response.IsSuccessStatusCode)
{
if(response.StatusCode == System.Net.HttpStatusCode.NotFound)
{
Console.WriteLine("Invalid installation id.");
}
else
{
Console.WriteLine("Unable to validate installation id.");
}
return false;
}
var resultString = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
var result = JsonConvert.DeserializeObject<dynamic>(resultString);
if(!(bool)result.Enabled)
{
Console.WriteLine("Installation id has been disabled.");
return false;
}
return true;
}
catch
{
Console.WriteLine("Unable to validate installation id. Problem contacting bitwarden server.");
return false;
}
}
2017-10-24 04:45:59 +02:00
private static void RebuildConfigs()
2017-08-07 22:31:00 +02:00
{
2017-11-07 04:55:15 +01:00
var environmentFileBuilder = new EnvironmentFileBuilder();
environmentFileBuilder.BuildForUpdater();
2017-10-24 04:45:59 +02:00
var url = Helpers.GetValueFronEnvFile("global", "globalSettings__baseServiceUri__vault");
if(!Uri.TryCreate(url, UriKind.Absolute, out Uri uri))
2017-08-07 22:31:00 +02:00
{
2017-10-24 04:45:59 +02:00
Console.WriteLine("Unable to determine existing installation url.");
return;
2017-08-07 22:31:00 +02:00
}
2017-10-24 04:45:59 +02:00
var domain = uri.Host;
2017-08-07 22:31:00 +02:00
2017-10-24 04:45:59 +02:00
var nginxBuilder = new NginxConfigBuilder(domain);
nginxBuilder.BuildForUpdater();
2017-08-07 22:31:00 +02:00
2017-10-24 04:45:59 +02:00
var appSettingsBuilder = new AppSettingsBuilder(url, domain);
appSettingsBuilder.Build();
2017-08-08 03:16:56 +02:00
2017-10-24 04:45:59 +02:00
var appIdBuilder = new AppIdBuilder(url);
appIdBuilder.Build();
2017-11-07 16:54:00 +01:00
var dockerComposeBuilder = new DockerComposeBuilder(_hostOs, _webVersion, _coreVersion);
dockerComposeBuilder.BuildForUpdater();
2017-08-08 18:29:59 +02:00
}
2017-08-07 22:31:00 +02:00
private static IDictionary<string, string> ParseParameters()
{
var dict = new Dictionary<string, string>();
for(var i = 0; i < _args.Length; i = i + 2)
{
if(!_args[i].StartsWith("-"))
{
continue;
}
dict.Add(_args[i].Substring(1), _args[i + 1]);
}
return dict;
}
}
}