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

383 lines
14 KiB
C#
Raw Normal View History

2017-08-07 22:31:00 +02:00
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
namespace Setup
{
public class Program
{
private static string[] _args = null;
private static IDictionary<string, string> _parameters = null;
private static string _outputDir = null;
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-11 14:57:31 +02:00
private static string _installationId = null;
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();
_outputDir = _parameters.ContainsKey("out") ?
_parameters["out"].ToLowerInvariant() : "/etc/bitwarden";
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;
_ssl = _letsEncrypt || (_parameters.ContainsKey("ssl") ?
_parameters["ssl"].ToLowerInvariant() == "y" : false);
2017-08-07 22:31:00 +02:00
2017-08-08 20:35:31 +02:00
_ssl = _letsEncrypt;
if(!_letsEncrypt)
{
Console.Write("Are you using your own SSL certificate? (y/n): ");
_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("Installation ID: ");
_installationId = Console.ReadLine().ToLowerInvariant();
Console.Write("Installation key: ");
_installationKey = Console.ReadLine().ToLowerInvariant();
Console.Write("Do you want to use push notifications? (y/n): ");
_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-07 22:31:00 +02:00
}
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): ");
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}/");
Console.WriteLine("Generating DH ephemeral parameter.");
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): ");
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): ");
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 {{
listen 80 default_server;
listen [::]:80 default_server;
server_name {_domain};");
if(_ssl)
{
sw.WriteLine($@" return 301 https://$server_name$request_uri;
}}
server {{
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name {_domain};
2017-08-08 20:35:31 +02:00
ssl_certificate {sslPath}/{certFile};
ssl_certificate_key {sslPath}/{keyFile};
2017-08-07 22:31:00 +02:00
ssl_session_timeout 30m;
ssl_session_cache shared:SSL:20m;
ssl_session_tickets off;");
if(dh)
{
sw.WriteLine($@"
# Diffie-Hellman parameter for DHE ciphersuites, recommended 2048 bits
2017-08-08 20:35:31 +02:00
ssl_dhparam {sslPath}/dhparam.pem;");
2017-08-07 22:31:00 +02:00
}
sw.WriteLine($@"
# 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;");
if(trusted)
{
sw.WriteLine($@"
# OCSP Stapling ---
# fetch OCSP records from URL in ssl_certificate and cache them
ssl_stapling on;
ssl_stapling_verify on;
## verify chain of trust of OCSP response using Root CA and Intermediate certs
2017-08-08 20:35:31 +02:00
ssl_trusted_certificate {sslPath}/{caFile};
2017-08-07 22:31:00 +02:00
resolver 8.8.8.8 8.8.4.4 208.67.222.222 208.67.220.220 valid=300s;");
}
sw.WriteLine($@"
2017-08-08 20:35:31 +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-07 22:31:00 +02:00
2017-08-08 20:35:31 +02:00
sw.WriteLine($@"
2017-08-07 22:31:00 +02:00
# X-Frame-Options is to prevent from clickJacking attack
2017-08-08 06:37:05 +02:00
add_header X-Frame-Options SAMEORIGIN;
2017-08-07 22:31:00 +02:00
# disable content-type sniffing on some browsers.
add_header X-Content-Type-Options nosniff;
# This header enables the Cross-site scripting (XSS) filter
add_header X-XSS-Protection ""1; mode=block"";
2017-08-08 06:37:05 +02:00
# This header controls what referrer information is shared
add_header Referrer-Policy same-origin;
# 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-07 23:08:41 +02:00
location / {{
proxy_pass http://web/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Url-Scheme $scheme;
proxy_redirect off;
}}
2017-08-08 18:29:59 +02:00
location = /app-id.json {{
proxy_pass http://web/app-id.json;
proxy_hide_header Content-Type;
add_header Content-Type $fido_content_type;
proxy_redirect off;
}}
location /attachments/ {{
proxy_pass http://attachments/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Url-Scheme $scheme;
proxy_redirect off;
}}
2017-08-07 22:31:00 +02:00
location /api/ {{
proxy_pass http://api/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Url-Scheme $scheme;
proxy_redirect off;
}}
location /identity/ {{
proxy_pass http://identity/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Url-Scheme $scheme;
proxy_redirect off;
}}
}}");
}
}
private static void BuildEnvironmentFiles()
{
2017-08-08 20:35:31 +02:00
Console.WriteLine("Building docker environment override files.");
Directory.CreateDirectory("/bitwarden/docker/");
2017-08-07 22:31:00 +02:00
var dbPass = _parameters.ContainsKey("db_pass") ? _parameters["db_pass"].ToLowerInvariant() : "REPLACE";
var dbConnectionString = "Server=tcp:mssql,1433;Initial Catalog=vault;Persist Security Info=False;User ID=sa;" +
$"Password={dbPass};MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=True;" +
"Connection Timeout=30;";
2017-08-08 03:16:56 +02:00
using(var sw = File.CreateText("/bitwarden/docker/global.override.env"))
2017-08-07 22:31:00 +02:00
{
2017-08-08 03:16:56 +02:00
sw.Write($@"globalSettings:baseServiceUri:vault={_url}
globalSettings:baseServiceUri:api={_url}/api
globalSettings:baseServiceUri:identity={_url}/identity
2017-08-07 22:31:00 +02:00
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
2017-08-07 22:31:00 +02:00
globalSettings:duo:aKey={Helpers.SecureRandomString(32, alpha: true, numeric: true)}
2017-08-11 14:57:31 +02:00
globalSettings:installation:id={_installationId}
globalSettings:installation:key={_installationKey}
2017-08-07 22:31:00 +02:00
globalSettings:yubico:clientId=REPLACE
2017-08-11 14:57:31 +02:00
globalSettings:yubico:key=REPLACE");
if(!_push)
{
sw.Write("globalSettings:pushRelayBaseUri=REPLACE");
}
2017-08-07 22:31:00 +02:00
}
2017-08-08 03:16:56 +02:00
using(var sw = File.CreateText("/bitwarden/docker/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-08 03:16:56 +02:00
whitelistDomains: [""{_domain}""]
}};");
}
}
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;
}
}
}