From b8da7d8763cfbfa5248a890a0a91f09e2adcbed4 Mon Sep 17 00:00:00 2001 From: Bastian Eicher Date: Mon, 18 Oct 2021 16:08:43 +0200 Subject: [PATCH] Added support for embedded command-line args and GnuPG keys to Bootstrapper --- src/Bootstrap.WinForms/App.config | 16 +++++++--- src/Bootstrap/App.config | 16 +++++++--- src/Bootstrap/BootstrapProcess.cs | 52 ++++++++++++++++++++++++++++--- src/Bootstrap/EmbeddedConfig.cs | 52 ++++++++++++++++++++----------- src/Bootstrap/EmbeddedConfig.txt | 2 ++ 5 files changed, 105 insertions(+), 33 deletions(-) diff --git a/src/Bootstrap.WinForms/App.config b/src/Bootstrap.WinForms/App.config index d8b2cfc8d5..7927244271 100644 --- a/src/Bootstrap.WinForms/App.config +++ b/src/Bootstrap.WinForms/App.config @@ -16,14 +16,20 @@ - + + + + - - + + + + + - - + + diff --git a/src/Bootstrap/App.config b/src/Bootstrap/App.config index 6599a869b4..d47d362075 100644 --- a/src/Bootstrap/App.config +++ b/src/Bootstrap/App.config @@ -13,14 +13,20 @@ - + + + + - - + + + + + - - + + diff --git a/src/Bootstrap/BootstrapProcess.cs b/src/Bootstrap/BootstrapProcess.cs index 0f4e2c372b..81006c2f97 100644 --- a/src/Bootstrap/BootstrapProcess.cs +++ b/src/Bootstrap/BootstrapProcess.cs @@ -20,6 +20,7 @@ using ZeroInstall.Services.Feeds; using ZeroInstall.Store.Configuration; using ZeroInstall.Store.Implementations; +using ZeroInstall.Store.Trust; namespace ZeroInstall { @@ -179,6 +180,8 @@ public ExitCode Execute(IEnumerable args) // NOTE: This must be done before parsing command-line options, since they may manipulate Config Config.Save(); + TrustKeys(); + _targetArgs.AddRange(GetEmbeddedArgs()); _targetArgs.AddRange(_options.Parse(args)); @@ -204,16 +207,55 @@ public ExitCode Execute(IEnumerable args) return (ExitCode)startInfo.Run(); } + /// + /// Adds keys for Zero Install (and optionally an app) to the . + /// + private static void TrustKeys() + { + try + { + var trust = TrustDB.Load(); + trust.TrustKey("88C8A1F375928691D7365C0259AA3927C24E4E1E", new Domain("apps.0install.net")); + if (!string.IsNullOrEmpty(EmbeddedConfig.Instance.AppFingerprint) && EmbeddedConfig.Instance.AppUri != null) + trust.TrustKey(EmbeddedConfig.Instance.AppFingerprint, new Domain(EmbeddedConfig.Instance.AppUri.Host)); + trust.Save(); + } + #region Error handling + catch (Exception ex) + { + Log.Error(ex); + } + #endregion + } + /// /// Gets implicit command-line arguments based on the , if any. /// private static IEnumerable GetEmbeddedArgs() - => EmbeddedConfig.Instance.AppMode switch + { + var config = EmbeddedConfig.Instance; + if (config.AppUri == null) yield break; + + switch (config.AppMode) { - BootstrapMode.Run => new[] {"run", EmbeddedConfig.Instance.AppUri.ToStringRfc()}, - BootstrapMode.Integrate => new[] {"integrate", EmbeddedConfig.Instance.AppUri.ToStringRfc()}, - _ => new string[0] - }; + case BootstrapMode.Run: + yield return "run"; + break; + case BootstrapMode.Integrate: + yield return "integrate"; + break; + default: + yield break; + } + + yield return config.AppUri.ToStringRfc(); + + if (!string.IsNullOrEmpty(config.AppArgs)) + { + foreach (string arg in WindowsUtils.SplitArgs(config.AppArgs)) + yield return arg; + } + } /// /// Handles arguments passed to the target that are also applicable to the bootstrapper. diff --git a/src/Bootstrap/EmbeddedConfig.cs b/src/Bootstrap/EmbeddedConfig.cs index cae81cd1a9..971a9d06e0 100644 --- a/src/Bootstrap/EmbeddedConfig.cs +++ b/src/Bootstrap/EmbeddedConfig.cs @@ -1,7 +1,6 @@ // Copyright Bastian Eicher et al. // Licensed under the GNU Lesser Public License -using System; using System.Configuration; using NanoByte.Common; using NanoByte.Common.Streams; @@ -24,46 +23,63 @@ public class EmbeddedConfig { /// /// The name of the target application to bootstrap. - /// Only relevant if is not . /// - public string AppName { get; } + public string? AppName { get; } /// /// The feed URI of the target application to bootstrap. - /// Only relevant if is not . /// - public FeedUri AppUri { get; } + public FeedUri? AppUri { get; } /// /// The application bootstrapping mode to use. /// public BootstrapMode AppMode { get; } + /// + /// Additional command-line arguments to pass to the application (if is ) or 0install-win integrate (if is ). + /// + public string? AppArgs { get; } + + /// + /// The GnuPG key fingerprint to trust for signing the application's feed. + /// + public string? AppFingerprint { get; } + /// /// Loads the embedded configuration. /// private EmbeddedConfig() { - var lines = typeof(EmbeddedConfig).GetEmbeddedString("EmbeddedConfig.txt").SplitMultilineText(); + string[] lines = typeof(EmbeddedConfig).GetEmbeddedString("EmbeddedConfig.txt").SplitMultilineText(); - try + string? ReadConfig(string key, int lineNumber, string placeholder) { - AppUri = new(ConfigurationManager.AppSettings["app_uri"] ?? lines[0].TrimEnd()); - Log.Info("Embedded config: AppUri: " + AppUri); + string setting = ConfigurationManager.AppSettings[key]; + if (!string.IsNullOrEmpty(setting)) + { + Log.Info($"AppSettings config: {key}: {setting}"); + return setting; + } - AppName = ConfigurationManager.AppSettings["app_name"] ?? lines[1].TrimEnd(); - Log.Info("Embedded config: AppName: " + AppName); + string line = lines[lineNumber].TrimEnd(); + if (!string.IsNullOrEmpty(line) && !(line.Contains(placeholder) && line.StartsWith("--") && line.EndsWith("--"))) + { + Log.Info($"Embedded config: {key}: {line}"); + return line; + } - AppMode = GetAppMode(ConfigurationManager.AppSettings["app_mode"] ?? lines[2].TrimEnd()); - Log.Info("Embedded config: AppMode: " + AppMode); - } - catch (UriFormatException) - { - // No (valid) feed URI set + return null; } + + AppUri = ReadConfig("app_uri", lineNumber: 0, nameof(AppUri))?.To(x => new FeedUri(x)); + AppName = ReadConfig("app_name", lineNumber: 1, placeholder: nameof(AppName)); + AppMode = GetAppMode(ReadConfig("app_mode", lineNumber: 2, placeholder: nameof(AppMode))); + AppArgs = ReadConfig("app_args", lineNumber: 3, placeholder: nameof(AppArgs)); + AppFingerprint = ReadConfig("app_fingerprint", lineNumber: 4, placeholder: nameof(AppFingerprint)); } - private static BootstrapMode GetAppMode(string value) + private static BootstrapMode GetAppMode(string? value) => value switch { "run" => BootstrapMode.Run, diff --git a/src/Bootstrap/EmbeddedConfig.txt b/src/Bootstrap/EmbeddedConfig.txt index 18a1bca305..175f79ced3 100644 --- a/src/Bootstrap/EmbeddedConfig.txt +++ b/src/Bootstrap/EmbeddedConfig.txt @@ -1,3 +1,5 @@ --------------------------------------------------------------------------------AppUri-------------------------------------------------------------------------------- ----------------------------------------AppName---------------------------------------- --------------------AppMode-------------------- +----------------------------------------AppArgs---------------------------------------- +-------------AppFingerprint-------------