1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-29 13:25:17 +01:00
bitwarden-server/util/Setup/Context.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

156 lines
6.4 KiB
C#
Raw Normal View History

2018-08-30 17:35:44 +02:00
using System;
using System.Collections.Generic;
using System.IO;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
namespace Bit.Setup
{
public class Context
{
private const string ConfigPath = "/bitwarden/config.yml";
public string[] Args { get; set; }
2019-03-12 15:26:14 +01:00
public bool Quiet { get; set; }
2019-03-15 14:28:39 +01:00
public bool Stub { get; set; }
2018-08-30 17:35:44 +02:00
public IDictionary<string, string> Parameters { get; set; }
public string OutputDir { get; set; } = "/etc/bitwarden";
public string HostOS { get; set; } = "win";
public string CoreVersion { get; set; } = "latest";
public string WebVersion { get; set; } = "latest";
public Installation Install { get; set; } = new Installation();
public Configuration Config { get; set; } = new Configuration();
2019-03-12 15:26:14 +01:00
public bool PrintToScreen()
{
return !Quiet || Parameters.ContainsKey("install");
}
2018-08-30 17:35:44 +02:00
public void LoadConfiguration()
{
if (!File.Exists(ConfigPath))
2018-08-30 17:35:44 +02:00
{
2019-03-12 15:26:14 +01:00
Helpers.WriteLine(this, "No existing `config.yml` detected. Let's generate one.");
2018-08-30 22:40:06 +02:00
2018-08-30 17:35:44 +02:00
// Looks like updating from older version. Try to create config file.
2018-10-18 17:41:49 +02:00
var url = Helpers.GetValueFromEnvFile("global", "globalSettings__baseServiceUri__vault");
if (!Uri.TryCreate(url, UriKind.Absolute, out var uri))
2018-08-30 17:35:44 +02:00
{
2019-03-12 15:26:14 +01:00
Helpers.WriteLine(this, "Unable to determine existing installation url.");
2018-08-30 17:35:44 +02:00
return;
}
Config.Url = url;
2018-10-18 17:41:49 +02:00
var push = Helpers.GetValueFromEnvFile("global", "globalSettings__pushRelayBaseUri");
2018-08-30 17:35:44 +02:00
Config.PushNotifications = push != "REPLACE";
var composeFile = "/bitwarden/docker/docker-compose.yml";
if (File.Exists(composeFile))
2018-08-30 17:35:44 +02:00
{
var fileLines = File.ReadAllLines(composeFile);
foreach (var line in fileLines)
2018-08-30 17:35:44 +02:00
{
if (!line.StartsWith("# Parameter:"))
2018-08-30 17:35:44 +02:00
{
continue;
}
var paramParts = line.Split("=");
if (paramParts.Length < 2)
2018-08-30 17:35:44 +02:00
{
continue;
}
if (paramParts[0] == "# Parameter:MssqlDataDockerVolume" &&
2018-08-30 17:35:44 +02:00
bool.TryParse(paramParts[1], out var mssqlDataDockerVolume))
{
Config.DatabaseDockerVolume = mssqlDataDockerVolume;
continue;
}
if (paramParts[0] == "# Parameter:HttpPort" && int.TryParse(paramParts[1], out var httpPort))
2018-08-30 17:35:44 +02:00
{
Config.HttpPort = httpPort == 0 ? null : httpPort.ToString();
continue;
}
if (paramParts[0] == "# Parameter:HttpsPort" && int.TryParse(paramParts[1], out var httpsPort))
2018-08-30 17:35:44 +02:00
{
Config.HttpsPort = httpsPort == 0 ? null : httpsPort.ToString();
continue;
}
}
}
var nginxFile = "/bitwarden/nginx/default.conf";
if (File.Exists(nginxFile))
2018-08-30 17:35:44 +02:00
{
var confContent = File.ReadAllText(nginxFile);
var selfSigned = confContent.Contains("/etc/ssl/self/");
Config.Ssl = confContent.Contains("ssl http2;");
Config.SslManagedLetsEncrypt = !selfSigned && confContent.Contains("/etc/letsencrypt/live/");
var diffieHellman = confContent.Contains("/dhparam.pem;");
var trusted = confContent.Contains("ssl_trusted_certificate ");
if (Config.SslManagedLetsEncrypt)
2018-08-30 17:35:44 +02:00
{
Config.Ssl = true;
}
else if (Config.Ssl)
2018-08-30 17:35:44 +02:00
{
var sslPath = selfSigned ? $"/etc/ssl/self/{Config.Domain}" : $"/etc/ssl/{Config.Domain}";
Config.SslCertificatePath = string.Concat(sslPath, "/", "certificate.crt");
Config.SslKeyPath = string.Concat(sslPath, "/", "private.key");
if (trusted)
2018-08-30 17:35:44 +02:00
{
Config.SslCaPath = string.Concat(sslPath, "/", "ca.crt");
}
if (diffieHellman)
2018-08-30 17:35:44 +02:00
{
Config.SslDiffieHellmanPath = string.Concat(sslPath, "/", "dhparam.pem");
}
}
}
SaveConfiguration();
}
var configText = File.ReadAllText(ConfigPath);
var deserializer = new DeserializerBuilder()
2020-01-13 15:33:12 +01:00
.WithNamingConvention(UnderscoredNamingConvention.Instance)
2018-08-30 17:35:44 +02:00
.Build();
Config = deserializer.Deserialize<Configuration>(configText);
}
public void SaveConfiguration()
{
if (Config == null)
2018-08-30 17:35:44 +02:00
{
throw new Exception("Config is null.");
}
var serializer = new SerializerBuilder()
2020-01-13 15:33:12 +01:00
.WithNamingConvention(UnderscoredNamingConvention.Instance)
2018-08-30 17:35:44 +02:00
.WithTypeInspector(inner => new CommentGatheringTypeInspector(inner))
.WithEmissionPhaseObjectGraphVisitor(args => new CommentsObjectGraphVisitor(args.InnerVisitor))
.Build();
var yaml = serializer.Serialize(Config);
Directory.CreateDirectory("/bitwarden/");
using (var sw = File.CreateText(ConfigPath))
2018-08-30 17:35:44 +02:00
{
sw.Write(yaml);
}
}
public class Installation
{
public Guid InstallationId { get; set; }
public string InstallationKey { get; set; }
public bool DiffieHellman { get; set; }
public bool Trusted { get; set; }
public bool SelfSignedCert { get; set; }
public string IdentityCertPassword { get; set; }
public string Domain { get; set; }
public string Database { get; set; }
2018-08-30 17:35:44 +02:00
}
}
}