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

481 lines
17 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-07 22:31:00 +02:00
using System.Diagnostics;
using System.IO;
2017-08-19 15:33:14 +02:00
using System.Net.Http;
using System.Reflection;
2017-08-07 22:31:00 +02:00
using System.Runtime.InteropServices;
namespace Setup
{
public class Program
{
private static string[] _args = null;
private static IDictionary<string, string> _parameters = null;
2017-08-19 15:33:14 +02:00
private static string _outputDir = "/etc/bitwarden";
2017-08-07 22:31:00 +02:00
private static string _domain = null;
2017-08-08 03:16:56 +02:00
private static string _url = null;
private static string _identityCertPassword = null;
2017-08-07 22:31:00 +02:00
private static bool _ssl = false;
2017-08-08 20:35:31 +02:00
private static bool _selfSignedSsl = false;
2017-08-07 22:31:00 +02:00
private static bool _letsEncrypt = false;
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 bool _push = false;
2017-08-07 22:31:00 +02:00
public static void Main(string[] args)
{
_args = args;
_parameters = ParseParameters();
if(_parameters.ContainsKey("install"))
{
Install();
}
else if(_parameters.ContainsKey("update"))
{
Update();
}
else
{
Console.WriteLine("No top-level command detected. Exiting...");
}
}
2017-08-07 22:31:00 +02:00
private static void Install()
{
_outputDir = _parameters.ContainsKey("out") ?
2017-08-19 15:33:14 +02:00
_parameters["out"].ToLowerInvariant() : _outputDir;
2017-08-08 03:16:56 +02:00
_domain = _parameters.ContainsKey("domain") ?
_parameters["domain"].ToLowerInvariant() : "localhost";
_letsEncrypt = _parameters.ContainsKey("letsencrypt") ?
_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-08-08 20:35:31 +02:00
_ssl = _letsEncrypt;
if(!_letsEncrypt)
{
Console.Write("(!) Do you have a SSL certificate to use? (y/n): ");
2017-08-08 20:35:31 +02:00
_ssl = Console.ReadLine().ToLowerInvariant() == "y";
if(_ssl)
{
Console.WriteLine("Make sure 'certificate.crt' and 'private.key' are provided in the " +
"appropriate directory (see setup instructions).");
}
}
_identityCertPassword = Helpers.SecureRandomString(32, alpha: true, numeric: true);
MakeCerts();
2017-08-08 20:35:31 +02:00
_url = _ssl ? $"https://{_domain}" : $"http://{_domain}";
2017-08-07 22:31:00 +02:00
BuildNginxConfig();
2017-08-11 14:57:31 +02:00
Console.Write("(!) Do you want to use push notifications? (y/n): ");
2017-08-11 14:57:31 +02:00
_push = Console.ReadLine().ToLowerInvariant() == "y";
2017-08-07 22:31:00 +02:00
BuildEnvironmentFiles();
2017-08-08 03:16:56 +02:00
BuildAppSettingsFiles();
2017-08-21 15:39:12 +02:00
BuildAppId();
2017-08-07 22:31:00 +02:00
}
private static void Update()
{
if(_parameters.ContainsKey("db"))
{
MigrateDatabase();
}
}
private static void MigrateDatabase()
{
Console.WriteLine("Migrating database.");
var dbPass = Helpers.GetDatabasePasswordFronEnvFile();
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-19 15:56:55 +02:00
Console.Write("(!) Enter your installation id (get it at https://bitwarden.com/install): ");
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();
2017-08-19 21:33:28 +02:00
// validate all installations for now. remove later.
return true;
2017-08-19 15:33:14 +02:00
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;
}
}
private static void MakeCerts()
2017-08-07 22:31:00 +02:00
{
2017-08-08 20:35:31 +02:00
if(!_ssl)
{
Console.Write("(!) Do you want to generate a self-signed SSL certificate? (y/n): ");
2017-08-08 20:35:31 +02:00
if(Console.ReadLine().ToLowerInvariant() == "y")
{
Directory.CreateDirectory($"/bitwarden/ssl/self/{_domain}/");
Console.WriteLine("Generating self signed SSL certificate.");
_ssl = _selfSignedSsl = true;
Exec("openssl req -x509 -newkey rsa:4096 -sha256 -nodes -days 365 " +
$"-keyout /bitwarden/ssl/self/{_domain}/private.key " +
$"-out /bitwarden/ssl/self/{_domain}/certificate.crt " +
$"-subj \"/C=US/ST=New York/L=New York/O=8bit Solutions LLC/OU=bitwarden/CN={_domain}\"");
}
}
if(_letsEncrypt)
{
Directory.CreateDirectory($"/bitwarden/letsencrypt/live/{_domain}/");
Exec($"openssl dhparam -out /bitwarden/letsencrypt/live/{_domain}/dhparam.pem 2048");
}
Console.WriteLine("Generating key for IdentityServer.");
2017-08-07 22:31:00 +02:00
Directory.CreateDirectory("/bitwarden/identity/");
Exec("openssl req -x509 -newkey rsa:4096 -sha256 -nodes -keyout identity.key " +
2017-08-08 03:16:56 +02:00
"-out identity.crt -subj \"/CN=bitwarden IdentityServer\" -days 10950");
Exec("openssl pkcs12 -export -out /bitwarden/identity/identity.pfx -inkey identity.key " +
$"-in identity.crt -certfile identity.crt -passout pass:{_identityCertPassword}");
2017-08-07 22:31:00 +02:00
}
private static void BuildNginxConfig()
{
Directory.CreateDirectory("/bitwarden/nginx/");
var sslCiphers = "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:" +
2017-08-08 03:16:56 +02:00
"DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:" +
"ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:" +
"ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:" +
"AES256-SHA:AES128-SHA:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4:@STRENGTH";
2017-08-07 22:31:00 +02:00
2017-08-08 20:35:31 +02:00
var dh = _letsEncrypt;
if(_ssl && !_selfSignedSsl && !_letsEncrypt)
{
Console.Write("(!) Use Diffie Hellman ephemeral parameters for SSL (requires dhparam.pem)? (y/n): ");
2017-08-08 20:35:31 +02:00
dh = Console.ReadLine().ToLowerInvariant() == "y";
}
var trusted = _letsEncrypt;
if(_ssl && !_selfSignedSsl && !_letsEncrypt)
{
Console.Write("(!) Is this a trusted SSL certificate (requires ca.crt)? (y/n): ");
2017-08-08 20:35:31 +02:00
trusted = Console.ReadLine().ToLowerInvariant() == "y";
}
var sslPath = _letsEncrypt ? $"/etc/letsencrypt/live/{_domain}" :
_selfSignedSsl ? $"/etc/ssl/self/{_domain}" : $"/etc/ssl/{_domain}";
var certFile = _letsEncrypt ? "fullchain.pem" : "certificate.crt";
var keyFile = _letsEncrypt ? "privkey.pem" : "private.key";
var caFile = _letsEncrypt ? "fullchain.pem" : "ca.crt";
2017-08-07 22:31:00 +02:00
2017-08-08 20:35:31 +02:00
Console.WriteLine("Building nginx config.");
2017-08-07 22:31:00 +02:00
using(var sw = File.CreateText("/bitwarden/nginx/default.conf"))
{
sw.WriteLine($@"server {{
2017-08-24 04:13:50 +02:00
listen 80 default_server;
listen [::]:80 default_server;
server_name {_domain};");
2017-08-07 22:31:00 +02:00
if(_ssl)
{
2017-08-24 04:13:50 +02:00
sw.WriteLine($@" return 301 https://$server_name$request_uri;
2017-08-07 22:31:00 +02:00
}}
server {{
2017-08-24 04:13:50 +02:00
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name {_domain};
2017-08-07 22:31:00 +02:00
2017-08-24 04:13:50 +02:00
ssl_certificate {sslPath}/{certFile};
ssl_certificate_key {sslPath}/{keyFile};
ssl_session_timeout 30m;
ssl_session_cache shared:SSL:20m;
ssl_session_tickets off;");
2017-08-07 22:31:00 +02:00
if(dh)
{
sw.WriteLine($@"
2017-08-24 04:13:50 +02:00
# Diffie-Hellman parameter for DHE ciphersuites, recommended 2048 bits
ssl_dhparam {sslPath}/dhparam.pem;");
2017-08-07 22:31:00 +02:00
}
sw.WriteLine($@"
2017-08-24 04:13:50 +02:00
# SSL protocols TLS v1~TLSv1.2 are allowed. Disabed SSLv3
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
# Disabled insecure ciphers suite. For example, MD5, DES, RC4, PSK
ssl_ciphers ""{sslCiphers}"";
# enables server-side protection from BEAST attacks
ssl_prefer_server_ciphers on;");
2017-08-07 22:31:00 +02:00
if(trusted)
{
sw.WriteLine($@"
2017-08-24 04:13:50 +02:00
# OCSP Stapling ---
# fetch OCSP records from URL in ssl_certificate and cache them
ssl_stapling on;
ssl_stapling_verify on;
2017-08-07 22:31:00 +02:00
2017-08-24 04:13:50 +02:00
## verify chain of trust of OCSP response using Root CA and Intermediate certs
ssl_trusted_certificate {sslPath}/{caFile};
2017-08-07 22:31:00 +02:00
2017-08-24 04:13:50 +02:00
resolver 8.8.8.8 8.8.4.4 208.67.222.222 208.67.220.220 valid=300s;
2017-08-07 22:31:00 +02:00
2017-08-24 04:13:50 +02:00
# This will enforce HTTP browsing into HTTPS and avoid ssl stripping attack. 6 months age
add_header Strict-Transport-Security max-age=15768000;");
}
2017-08-08 20:35:31 +02:00
}
2017-08-07 22:31:00 +02:00
2017-08-08 20:35:31 +02:00
sw.WriteLine($@"
2017-08-24 04:13:50 +02:00
# X-Frame-Options is to prevent from clickJacking attack
add_header X-Frame-Options SAMEORIGIN;
2017-08-07 22:31:00 +02:00
2017-08-24 04:13:50 +02:00
# disable content-type sniffing on some browsers.
add_header X-Content-Type-Options nosniff;
2017-08-07 22:31:00 +02:00
2017-08-24 04:13:50 +02:00
# This header enables the Cross-site scripting (XSS) filter
add_header X-XSS-Protection ""1; mode=block"";
2017-08-07 22:31:00 +02:00
2017-08-24 04:13:50 +02:00
# This header controls what referrer information is shared
add_header Referrer-Policy same-origin;
2017-08-08 06:37:05 +02:00
2017-08-24 04:13:50 +02:00
# Content-Security-Policy is set via meta tag on the website so it is not included here");
2017-08-07 22:31:00 +02:00
sw.WriteLine($@"
2017-08-24 04:13:50 +02:00
location / {{
proxy_pass http://web/;
}}
location = /app-id.json {{
proxy_pass http://web/app-id.json;
proxy_hide_header Content-Type;
add_header Content-Type $fido_content_type;
}}
location /attachments/ {{
proxy_pass http://attachments/;
}}
location /api/ {{
proxy_pass http://api/;
}}
location /identity/ {{
proxy_pass http://identity/;
}}
2017-08-07 22:31:00 +02:00
}}");
}
}
private static void BuildEnvironmentFiles()
{
2017-08-08 20:35:31 +02:00
Console.WriteLine("Building docker environment override files.");
2017-08-20 04:09:00 +02:00
Directory.CreateDirectory("/bitwarden/env/");
2017-08-19 15:33:14 +02:00
var dbPass = Helpers.SecureRandomString(32);
var dbConnectionString = Helpers.MakeSqlConnectionString("mssql", "vault", "sa", dbPass);
2017-08-07 22:31:00 +02:00
2017-08-20 04:09:00 +02:00
using(var sw = File.CreateText("/bitwarden/env/global.override.env"))
2017-08-07 22:31:00 +02:00
{
2017-08-17 22:45:27 +02:00
sw.Write($@"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={_outputDir}/core/attachments
globalSettings__attachment__baseUrl={_url}/attachments
globalSettings__dataProtection__directory={_outputDir}/core/aspnet-dataprotection
globalSettings__logDirectory={_outputDir}/core/logs
globalSettings__licenseDirectory={_outputDir}/core/licenses
globalSettings__duo__aKey={Helpers.SecureRandomString(64, alpha: true, numeric: true)}
globalSettings__installation__id={_installationId}
globalSettings__installation__key={_installationKey}
globalSettings__yubico__clientId=REPLACE
2017-08-21 22:44:46 +02:00
globalSettings__yubico__key=REPLACE
globalSettings__mail__smtp__host=REPLACE
globalSettings__mail__smtp__username=REPLACE
globalSettings__mail__smtp__password=REPLACE
globalSettings__mail__smtp__ssl=true
2017-08-22 04:36:31 +02:00
globalSettings__mail__smtp__port=587");
2017-08-11 14:57:31 +02:00
if(!_push)
{
sw.Write(@"
2017-08-17 22:45:27 +02:00
globalSettings__pushRelayBaseUri=REPLACE");
2017-08-11 14:57:31 +02:00
}
2017-08-07 22:31:00 +02:00
}
2017-08-20 04:09:00 +02:00
using(var sw = File.CreateText("/bitwarden/env/mssql.override.env"))
2017-08-07 22:31:00 +02:00
{
sw.Write($@"ACCEPT_EULA=Y
MSSQL_PID=Express
SA_PASSWORD={dbPass}");
}
}
2017-08-08 03:16:56 +02:00
private static void BuildAppSettingsFiles()
{
2017-08-08 20:35:31 +02:00
Console.WriteLine("Building app settings.");
2017-08-08 03:16:56 +02:00
Directory.CreateDirectory("/bitwarden/web/");
using(var sw = File.CreateText("/bitwarden/web/settings.js"))
{
sw.Write($@"var bitwardenAppSettings = {{
apiUri: ""{_url}/api"",
identityUri: ""{_url}/identity"",
stripeKey: null,
braintreeKey: null,
2017-08-14 19:06:44 +02:00
whitelistDomains: [""{_domain}""],
selfHosted: true
2017-08-08 03:16:56 +02:00
}};");
}
}
2017-08-08 18:29:59 +02:00
private static void BuildAppId()
{
2017-08-08 20:35:31 +02:00
Console.WriteLine("Building FIDO U2F app id.");
2017-08-08 18:29:59 +02:00
Directory.CreateDirectory("/bitwarden/web/");
using(var sw = File.CreateText("/bitwarden/web/app-id.json"))
{
sw.Write($@"{{
""trustedFacets"": [
{{
""version"": {{
""major"": 1,
""minor"": 0
}},
""ids"": [
""{_url}"",
""ios:bundle-id:com.8bit.bitwarden"",
""android:apk-key-hash:dUGFzUzf3lmHSLBDBIv+WaFyZMI""
]
}}
]
}}");
}
}
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;
}
private static string Exec(string cmd)
{
var process = new Process
{
StartInfo = new ProcessStartInfo
{
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden
}
};
if(!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
var escapedArgs = cmd.Replace("\"", "\\\"");
process.StartInfo.FileName = "/bin/bash";
process.StartInfo.Arguments = $"-c \"{escapedArgs}\"";
}
else
{
process.StartInfo.FileName = "powershell";
process.StartInfo.Arguments = cmd;
}
process.Start();
string result = process.StandardOutput.ReadToEnd();
process.WaitForExit();
return result;
}
}
}