2022-06-30 01:46:41 +02:00
|
|
|
|
namespace Bit.Setup
|
2017-10-24 04:45:59 +02:00
|
|
|
|
{
|
|
|
|
|
public class NginxConfigBuilder
|
|
|
|
|
{
|
2018-03-29 19:43:52 +02:00
|
|
|
|
private const string ConfFile = "/bitwarden/nginx/default.conf";
|
2017-10-24 04:45:59 +02:00
|
|
|
|
|
2018-08-30 17:35:44 +02:00
|
|
|
|
private readonly Context _context;
|
2017-10-24 04:45:59 +02:00
|
|
|
|
|
2018-08-30 17:35:44 +02:00
|
|
|
|
public NginxConfigBuilder(Context context)
|
2017-10-24 04:45:59 +02:00
|
|
|
|
{
|
2018-08-30 17:35:44 +02:00
|
|
|
|
_context = context;
|
2017-10-24 04:45:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void BuildForInstaller()
|
|
|
|
|
{
|
2018-08-30 17:35:44 +02:00
|
|
|
|
var model = new TemplateModel(_context);
|
2020-03-27 19:36:37 +01:00
|
|
|
|
if (model.Ssl && !_context.Config.SslManagedLetsEncrypt)
|
2018-08-30 17:35:44 +02:00
|
|
|
|
{
|
|
|
|
|
var sslPath = _context.Install.SelfSignedCert ?
|
|
|
|
|
$"/etc/ssl/self/{model.Domain}" : $"/etc/ssl/{model.Domain}";
|
|
|
|
|
_context.Config.SslCertificatePath = model.CertificatePath =
|
|
|
|
|
string.Concat(sslPath, "/", "certificate.crt");
|
|
|
|
|
_context.Config.SslKeyPath = model.KeyPath =
|
|
|
|
|
string.Concat(sslPath, "/", "private.key");
|
2020-03-27 19:36:37 +01:00
|
|
|
|
if (_context.Install.Trusted)
|
2018-08-30 17:35:44 +02:00
|
|
|
|
{
|
|
|
|
|
_context.Config.SslCaPath = model.CaPath =
|
|
|
|
|
string.Concat(sslPath, "/", "ca.crt");
|
|
|
|
|
}
|
2020-03-27 19:36:37 +01:00
|
|
|
|
if (_context.Install.DiffieHellman)
|
2018-08-30 17:35:44 +02:00
|
|
|
|
{
|
|
|
|
|
_context.Config.SslDiffieHellmanPath = model.DiffieHellmanPath =
|
|
|
|
|
string.Concat(sslPath, "/", "dhparam.pem");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Build(model);
|
2017-10-24 04:45:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void BuildForUpdater()
|
|
|
|
|
{
|
2018-08-30 17:35:44 +02:00
|
|
|
|
var model = new TemplateModel(_context);
|
|
|
|
|
Build(model);
|
2017-10-24 04:45:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-30 17:35:44 +02:00
|
|
|
|
private void Build(TemplateModel model)
|
2017-10-24 04:45:59 +02:00
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory("/bitwarden/nginx/");
|
2019-03-12 15:26:14 +01:00
|
|
|
|
Helpers.WriteLine(_context, "Building nginx config.");
|
2020-03-27 19:36:37 +01:00
|
|
|
|
if (!_context.Config.GenerateNginxConfig)
|
2018-08-30 17:35:44 +02:00
|
|
|
|
{
|
2019-03-12 15:26:14 +01:00
|
|
|
|
Helpers.WriteLine(_context, "...skipped");
|
2018-08-30 17:35:44 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var template = Helpers.ReadTemplate("NginxConfig");
|
2020-03-27 19:36:37 +01:00
|
|
|
|
using (var sw = File.CreateText(ConfFile))
|
2017-10-24 04:45:59 +02:00
|
|
|
|
{
|
2018-08-30 17:35:44 +02:00
|
|
|
|
sw.WriteLine(template(model));
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-24 04:45:59 +02:00
|
|
|
|
|
2018-08-30 17:35:44 +02:00
|
|
|
|
public class TemplateModel
|
|
|
|
|
{
|
|
|
|
|
public TemplateModel() { }
|
|
|
|
|
|
|
|
|
|
public TemplateModel(Context context)
|
|
|
|
|
{
|
2021-08-13 15:52:26 +02:00
|
|
|
|
Captcha = context.Config.Captcha;
|
2018-08-30 17:35:44 +02:00
|
|
|
|
Ssl = context.Config.Ssl;
|
2021-11-16 18:52:02 +01:00
|
|
|
|
EnableKeyConnector = context.Config.EnableKeyConnector;
|
2022-07-14 21:58:48 +02:00
|
|
|
|
EnableScim = context.Config.EnableScim;
|
2018-08-30 17:35:44 +02:00
|
|
|
|
Domain = context.Config.Domain;
|
|
|
|
|
Url = context.Config.Url;
|
2019-04-26 18:26:54 +02:00
|
|
|
|
RealIps = context.Config.RealIps;
|
2020-12-18 16:58:35 +01:00
|
|
|
|
ContentSecurityPolicy = string.Format(context.Config.NginxHeaderContentSecurityPolicy, Domain);
|
2017-10-24 04:45:59 +02:00
|
|
|
|
|
2020-03-27 19:36:37 +01:00
|
|
|
|
if (Ssl)
|
2017-10-24 04:45:59 +02:00
|
|
|
|
{
|
2020-03-27 19:36:37 +01:00
|
|
|
|
if (context.Config.SslManagedLetsEncrypt)
|
2017-10-24 04:45:59 +02:00
|
|
|
|
{
|
2018-08-30 17:35:44 +02:00
|
|
|
|
var sslPath = $"/etc/letsencrypt/live/{Domain}";
|
|
|
|
|
CertificatePath = CaPath = string.Concat(sslPath, "/", "fullchain.pem");
|
|
|
|
|
KeyPath = string.Concat(sslPath, "/", "privkey.pem");
|
|
|
|
|
DiffieHellmanPath = string.Concat(sslPath, "/", "dhparam.pem");
|
2017-10-24 04:45:59 +02:00
|
|
|
|
}
|
2018-08-30 17:35:44 +02:00
|
|
|
|
else
|
2017-10-24 04:45:59 +02:00
|
|
|
|
{
|
2018-08-30 17:35:44 +02:00
|
|
|
|
CertificatePath = context.Config.SslCertificatePath;
|
|
|
|
|
KeyPath = context.Config.SslKeyPath;
|
|
|
|
|
CaPath = context.Config.SslCaPath;
|
|
|
|
|
DiffieHellmanPath = context.Config.SslDiffieHellmanPath;
|
2017-10-24 04:45:59 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-31 18:11:44 +02:00
|
|
|
|
|
2020-03-27 19:36:37 +01:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(context.Config.SslCiphersuites))
|
2018-08-31 18:11:44 +02:00
|
|
|
|
{
|
|
|
|
|
SslCiphers = context.Config.SslCiphersuites;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
SslCiphers = "ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:" +
|
|
|
|
|
"ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:" +
|
|
|
|
|
"ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:" +
|
|
|
|
|
"ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256";
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-27 19:36:37 +01:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(context.Config.SslVersions))
|
2018-08-31 18:11:44 +02:00
|
|
|
|
{
|
|
|
|
|
SslProtocols = context.Config.SslVersions;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
SslProtocols = "TLSv1.2";
|
|
|
|
|
}
|
2017-10-24 04:45:59 +02:00
|
|
|
|
}
|
2018-08-30 17:35:44 +02:00
|
|
|
|
|
2021-08-13 15:52:26 +02:00
|
|
|
|
public bool Captcha { get; set; }
|
2018-08-30 17:35:44 +02:00
|
|
|
|
public bool Ssl { get; set; }
|
2021-11-16 18:52:02 +01:00
|
|
|
|
public bool EnableKeyConnector { get; set; }
|
2022-07-14 21:58:48 +02:00
|
|
|
|
public bool EnableScim { get; set; }
|
2018-08-30 17:35:44 +02:00
|
|
|
|
public string Domain { get; set; }
|
|
|
|
|
public string Url { get; set; }
|
|
|
|
|
public string CertificatePath { get; set; }
|
|
|
|
|
public string KeyPath { get; set; }
|
|
|
|
|
public string CaPath { get; set; }
|
|
|
|
|
public string DiffieHellmanPath { get; set; }
|
2018-08-31 18:11:44 +02:00
|
|
|
|
public string SslCiphers { get; set; }
|
|
|
|
|
public string SslProtocols { get; set; }
|
2020-12-18 16:58:35 +01:00
|
|
|
|
public string ContentSecurityPolicy { get; set; }
|
2019-04-26 18:26:54 +02:00
|
|
|
|
public List<string> RealIps { get; set; }
|
2017-10-24 04:45:59 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|