23b6feae6b
The server MSI's wizard is now Welcome > Folders > Database > Certificate > Server Address > Ready, after StifleR's flow: - Folders: install, data, log and backup folders with Browse (defaults follow a changed install folder), Enable Windows Long Paths while they are off. - Database: SQLite, PostgreSQL (host, port, database, SSL mode, user, password) or SQL Server (server\instance, port, database, Windows or SQL login, Encrypt, Trust Server Certificate), Create If Missing, and Test Connection with the server's own error; Next needs a passing test or Continue Without a Successful Test. - Server Address: HTTPS port and an HTTP redirect port, Listen On all or one of this computer's addresses, NATS and object store ports, public URL, firewall rules; a port in use names the program holding it. Every choice is a public property (DATAFOLDER LOGFOLDER BACKUPFOLDER ENABLELONGPATHS DBPROVIDER DBSERVER DBPORT DBNAME DBAUTH DBUSER DBPASSWORD DBSSLMODE DBENCRYPT DBTRUSTCERT DBCREATE DBSKIPTEST HTTPREDIRECTPORT OBJECTSTOREPORT PUBLICSERVERURL FIREWALL), checked the same way for /qn before anything changes. The custom actions test PostgreSQL with their own protocol client (SCRAM-SHA-256, MD5, SSL) and SQL Server with System.Data.SqlClient, create the database when the account may (and give the service's Windows login db_owner), write appsettings.json in place, keep the password DPAPI-protected and out of the log, restrict the file and new folders to SYSTEM and Administrators, create long folders through \\?\, and keep netsh firewall rules for the ports in effect. Upgrades find the install folder again, keep the configuration and change the database only when the command line gives another; the choices but the password are remembered for a reinstall. The banner and Welcome/Finish artwork are drawn from the product icon by Generate-Icons.ps1. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
233 lines
9.1 KiB
C#
233 lines
9.1 KiB
C#
using System.Text.Json;
|
|
using ExecuteR.Installer.CustomActions;
|
|
using Xunit;
|
|
|
|
namespace Shared.Tests;
|
|
|
|
/// <summary>The server MSI's edit of appsettings.json (JsonSettingsFile) and the keys it writes (ServerConfiguration).</summary>
|
|
public sealed class InstallerSettingsFileTests
|
|
{
|
|
/// <summary>The shape `setup` writes: flat string keys, two-space indent.</summary>
|
|
private const string Generated = """
|
|
{
|
|
"Urls": "https://0.0.0.0:9080",
|
|
"DATA_PATH": "C:/Program Files/ExecuteR/Server/data",
|
|
"NATS_SIDECAR": "1",
|
|
"PUBLIC_SERVER_URL": ""
|
|
}
|
|
""";
|
|
|
|
[Fact]
|
|
public void ReadsTopLevelStrings()
|
|
{
|
|
var file = new JsonSettingsFile(Generated);
|
|
|
|
Assert.Equal("https://0.0.0.0:9080", file.Get("Urls"));
|
|
Assert.Equal("", file.Get("PUBLIC_SERVER_URL"));
|
|
Assert.Null(file.Get("DATABASE_PROVIDER"));
|
|
}
|
|
|
|
[Fact]
|
|
public void ReplacesAValueInPlaceAndLeavesTheRestAsItWas()
|
|
{
|
|
var file = new JsonSettingsFile(Generated);
|
|
|
|
Assert.True(file.Set("PUBLIC_SERVER_URL", "https://executer.example.com"));
|
|
|
|
Assert.Equal(Generated.Replace("\"PUBLIC_SERVER_URL\": \"\"", "\"PUBLIC_SERVER_URL\": \"https://executer.example.com\""), file.Text);
|
|
Assert.False(file.Set("PUBLIC_SERVER_URL", "https://executer.example.com"));
|
|
}
|
|
|
|
[Fact]
|
|
public void AppendsANewKeyAsTheLastMemberWithTheFilesIndent()
|
|
{
|
|
var file = new JsonSettingsFile(Generated);
|
|
|
|
Assert.True(file.Set("DATABASE_PROVIDER", "postgres"));
|
|
|
|
using var document = JsonDocument.Parse(file.Text);
|
|
Assert.Equal("postgres", document.RootElement.GetProperty("DATABASE_PROVIDER").GetString());
|
|
Assert.Contains("\"PUBLIC_SERVER_URL\": \"\",\n \"DATABASE_PROVIDER\": \"postgres\"", file.Text.ReplaceLineEndings("\n"));
|
|
}
|
|
|
|
[Fact]
|
|
public void KeepsWhatAnAdministratorAdded()
|
|
{
|
|
const string edited = """
|
|
{
|
|
// Kept by hand.
|
|
"Logging": { "LogLevel": { "Default": "Warning" } },
|
|
"Urls": "https://0.0.0.0:9080", /* the listener */
|
|
"Array": [ "a", { "b": "}" } ],
|
|
"Number": 5,
|
|
}
|
|
""";
|
|
var file = new JsonSettingsFile(edited);
|
|
|
|
file.Set("Urls", "https://10.0.0.5:9443");
|
|
file.Set("LOG_DIRECTORY", "D:/Logs");
|
|
|
|
Assert.Contains("// Kept by hand.", file.Text);
|
|
Assert.Contains("/* the listener */", file.Text);
|
|
Assert.Contains("\"Logging\": { \"LogLevel\": { \"Default\": \"Warning\" } }", file.Text);
|
|
using var document = JsonDocument.Parse(file.Text, new JsonDocumentOptions { CommentHandling = JsonCommentHandling.Skip, AllowTrailingCommas = true });
|
|
Assert.Equal("https://10.0.0.5:9443", document.RootElement.GetProperty("Urls").GetString());
|
|
Assert.Equal("D:/Logs", document.RootElement.GetProperty("LOG_DIRECTORY").GetString());
|
|
Assert.Equal(5, document.RootElement.GetProperty("Number").GetInt32());
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("Host=db;Password=\"a;b\"")]
|
|
[InlineData("C:\\Data\\with \"quotes\"\tand\nlines")]
|
|
[InlineData("ünïcødé \u2028 <&>")]
|
|
public void WritesAnyValueSoJsonReadsItBack(string value)
|
|
{
|
|
var file = new JsonSettingsFile(Generated);
|
|
|
|
file.Set("DATABASE_CONNECTION_STRING", value);
|
|
|
|
using var document = JsonDocument.Parse(file.Text);
|
|
Assert.Equal(value, document.RootElement.GetProperty("DATABASE_CONNECTION_STRING").GetString());
|
|
Assert.Equal(value, new JsonSettingsFile(file.Text).Get("DATABASE_CONNECTION_STRING"));
|
|
}
|
|
|
|
[Fact]
|
|
public void AnEmptyObjectTakesItsFirstMember()
|
|
{
|
|
var file = new JsonSettingsFile("{}");
|
|
|
|
file.Set("A", "1");
|
|
|
|
using var document = JsonDocument.Parse(file.Text);
|
|
Assert.Equal("1", document.RootElement.GetProperty("A").GetString());
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("")]
|
|
[InlineData("[]")]
|
|
[InlineData("{ \"a\": }")]
|
|
[InlineData("{ \"a\": \"b\" ")]
|
|
public void RefusesWhatIsNotAJsonObject(string text)
|
|
{
|
|
Assert.Throws<FormatException>(() => new JsonSettingsFile(text));
|
|
}
|
|
|
|
[Fact]
|
|
public void TheDatabaseKeysNeverCarryThePassword()
|
|
{
|
|
var database = new DatabaseSettings
|
|
{
|
|
Provider = DatabaseSettings.PostgreSql,
|
|
Server = "db.example.com",
|
|
Port = "5432",
|
|
Name = "executer",
|
|
User = "executer",
|
|
Password = "s3cret;pw",
|
|
};
|
|
|
|
var keys = ServerConfiguration.DatabaseKeys(database, "dpapi:AAAA").ToDictionary(pair => pair.Key, pair => pair.Value);
|
|
|
|
Assert.Equal("postgres", keys["DATABASE_PROVIDER"]);
|
|
Assert.DoesNotContain("s3cret", keys["DATABASE_CONNECTION_STRING"]);
|
|
Assert.Equal("dpapi:AAAA", keys["DATABASE_PASSWORD"]);
|
|
}
|
|
|
|
[Fact]
|
|
public void NoNewPasswordKeepsTheOneInEffect()
|
|
{
|
|
var database = new DatabaseSettings { Provider = DatabaseSettings.PostgreSql, Server = "db", Port = "5432", Name = "x", User = "u" };
|
|
|
|
var keys = ServerConfiguration.DatabaseKeys(database, protectedPassword: null);
|
|
|
|
Assert.DoesNotContain(keys, pair => pair.Key == "DATABASE_PASSWORD");
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(DatabaseSettings.Sqlite, "")]
|
|
[InlineData(DatabaseSettings.SqlServer, DatabaseSettings.WindowsAuthentication)]
|
|
public void SqliteAndWindowsSignInClearThePassword(string provider, string authentication)
|
|
{
|
|
var database = new DatabaseSettings { Provider = provider, Server = "sql01", Name = "ExecuteR", Authentication = authentication };
|
|
|
|
var keys = ServerConfiguration.DatabaseKeys(database, "dpapi:AAAA").ToDictionary(pair => pair.Key, pair => pair.Value);
|
|
|
|
Assert.Equal("", keys["DATABASE_PASSWORD"]);
|
|
}
|
|
|
|
[Fact]
|
|
public void TheDatabaseDiffersOnlyWhenTheCommandLineGivesAnother()
|
|
{
|
|
var database = new DatabaseSettings
|
|
{
|
|
Provider = DatabaseSettings.SqlServer,
|
|
Server = "sql01",
|
|
Name = "ExecuteR",
|
|
Authentication = DatabaseSettings.WindowsAuthentication,
|
|
};
|
|
var file = new JsonSettingsFile(Generated);
|
|
Assert.True(ServerConfiguration.DatabaseDiffers(file, database));
|
|
|
|
foreach (var key in ServerConfiguration.DatabaseKeys(database, null))
|
|
{
|
|
file.Set(key.Key, key.Value);
|
|
}
|
|
|
|
Assert.False(ServerConfiguration.DatabaseDiffers(file, database));
|
|
database.Name = "Other";
|
|
Assert.True(ServerConfiguration.DatabaseDiffers(file, database));
|
|
Assert.True(ServerConfiguration.DatabaseDiffers(file, new DatabaseSettings { Provider = DatabaseSettings.Sqlite }));
|
|
Assert.False(ServerConfiguration.DatabaseDiffers(new JsonSettingsFile(Generated), new DatabaseSettings { Provider = DatabaseSettings.Sqlite }));
|
|
}
|
|
|
|
[Fact]
|
|
public void FoldersAreWrittenAsSetupWritesPaths()
|
|
{
|
|
var keys = ServerConfiguration.InstallKeys(@"D:\ExecuteR\Logs\", @"\\backup\share\ExecuteR\", " https://x.example.com ", "80", "9199")
|
|
.ToDictionary(pair => pair.Key, pair => pair.Value);
|
|
|
|
Assert.Equal("D:/ExecuteR/Logs", keys["LOG_DIRECTORY"]);
|
|
Assert.Equal("//backup/share/ExecuteR", keys["DATABASE_BACKUP_PATH"]);
|
|
Assert.Equal("https://x.example.com", keys["PUBLIC_SERVER_URL"]);
|
|
Assert.Equal("80", keys["HTTP_REDIRECT_PORT"]);
|
|
Assert.Equal("9199", keys["S3_EMBEDDED_PORT"]);
|
|
}
|
|
|
|
[Fact]
|
|
public void FirewallRulesFollowThePortsInEffect()
|
|
{
|
|
const string loopbackNats = "websocket {\n listen: 127.0.0.1:9222\n no_tls: true\n}";
|
|
const string exposedNats = "websocket {\n listen: 0.0.0.0:9333\n tls {\n cert_file: \"x\"\n }\n}";
|
|
|
|
var rules = ServerConfiguration.FirewallRules("https://0.0.0.0:9443", "80", loopbackNats, @"C:\S\ExecuteR-Server.exe", @"C:\S\nats\nats-server.exe");
|
|
|
|
Assert.Equal(new[] { 9443, 80 }, rules.Select(rule => rule.Port));
|
|
Assert.All(rules, rule => Assert.Equal(@"C:\S\ExecuteR-Server.exe", rule.Program));
|
|
|
|
var exposed = ServerConfiguration.FirewallRules("http://0.0.0.0:9080", "80", exposedNats, "s", "n");
|
|
Assert.Equal(new[] { 9080, 9333 }, exposed.Select(rule => rule.Port));
|
|
Assert.Equal("n", exposed[1].Program);
|
|
Assert.All(exposed, rule => Assert.Contains(rule.Name, ServerConfiguration.FirewallRuleNames));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("https://0.0.0.0:9080", new[] { 9080 })]
|
|
[InlineData("https://[::]:9443;http://[::]:80", new[] { 9443, 80 })]
|
|
[InlineData("https://server", new[] { 443 })]
|
|
[InlineData("", new int[0])]
|
|
public void ReadsThePortsOfAUrlsValue(string urls, int[] ports)
|
|
{
|
|
Assert.Equal(ports, ServerConfiguration.UrlPorts(urls));
|
|
}
|
|
|
|
[Fact]
|
|
public void ReadsTheWebSocketListenOfNatsConf()
|
|
{
|
|
var listen = ServerConfiguration.NatsWebSocketListen("listen: 127.0.0.1:4222\nwebsocket {\n listen: [::]:9222\n no_tls: true\n}");
|
|
|
|
Assert.NotNull(listen);
|
|
Assert.Equal("::", listen!.Value.Key);
|
|
Assert.Equal(9222, listen.Value.Value);
|
|
Assert.Null(ServerConfiguration.NatsWebSocketListen("listen: 127.0.0.1:4222"));
|
|
}
|
|
}
|