/// <summary>
 /// Creates a new service provider.
 /// </summary>
 /// <param name="handler">A callback object used when the the user needs to be asked questions or informed about download and IO tasks.</param>
 public ServiceProvider(ITaskHandler handler)
 {
     Handler                     = handler ?? throw new ArgumentNullException(nameof(handler));
     _feedCache                  = new(() => FeedCaches.Default(OpenPgp));
     _trustManager               = new(() => new(TrustDB.LoadSafe(), Config, OpenPgp, FeedCache, Handler));
     _feedManager                = new(() => new(Config, FeedCache, TrustManager, Handler));
     _catalogManager             = new(() => new(TrustManager, Handler));
     _selectionCandidateProvider = new(() => new(Config, FeedManager, ImplementationStore, PackageManager));
     _solver                     = new(() =>
     {
         var backtrackingSolver = new BacktrackingSolver(SelectionCandidateProvider);
         if (Config.ExternalSolverUri == null)
         {
             return(backtrackingSolver);
         }
         else
         {
             var externalSolver = new ExternalSolver(backtrackingSolver, SelectionsManager, Fetcher, Executor, FeedManager, Handler, Config.ExternalSolverUri);
             return(new FallbackSolver(backtrackingSolver, externalSolver));
         }
     });
     _fetcher           = new(() => new(Config, ImplementationStore, Handler));
     _executor          = new(() => new(ImplementationStore));
     _selectionsManager = new(() => new(FeedManager, ImplementationStore, PackageManager));
 }
Example #2
0
        /// <inheritdoc />
        public override ExitCode Execute()
        {
            var trustDB = TrustDB.Load();

            switch (AdditionalArgs.Count)
            {
            case 0:
                if (Handler.IsGui)
                {
                    Config.InitialTab = ConfigTab.Trust;
                    Handler.Output(Resources.Configuration, Config);
                }
                else
                {
                    Handler.Output(Resources.TrustedKeys, trustDB.Keys);
                }
                return(ExitCode.OK);

            case 1:
                string fingerprint = AdditionalArgs[0];
                Handler.Output(
                    string.Format(Resources.TrustedForDomains, fingerprint),
                    trustDB.Keys.FirstOrDefault(x => x.Fingerprint == fingerprint)?.Domains ?? new());
                return(ExitCode.OK);

            default:
                throw new InvalidOperationException();
            }
        }
Example #3
0
        /// <summary>
        /// Creates a new trust manager.
        /// </summary>
        /// <param name="config">User settings controlling network behaviour, solving, etc.</param>
        /// <param name="openPgp">The OpenPGP-compatible system used to validate the signatures.</param>
        /// <param name="trustDB">A database of OpenPGP signature fingerprints the users trusts to sign <see cref="Feed"/>s coming from specific domains.</param>
        /// <param name="feedCache">Provides access to a cache of <see cref="Feed"/>s that were downloaded via HTTP(S).</param>
        /// <param name="handler">A callback object used when the the user needs to be asked questions.</param>
        public TrustManager([NotNull] Config config, [NotNull] IOpenPgp openPgp, [NotNull] TrustDB trustDB, [NotNull] IFeedCache feedCache, [NotNull] ITaskHandler handler)
        {
            #region Sanity checks
            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }
            if (openPgp == null)
            {
                throw new ArgumentNullException(nameof(openPgp));
            }
            if (trustDB == null)
            {
                throw new ArgumentNullException(nameof(trustDB));
            }
            if (feedCache == null)
            {
                throw new ArgumentNullException(nameof(feedCache));
            }
            if (handler == null)
            {
                throw new ArgumentNullException(nameof(handler));
            }
            #endregion

            _config    = config;
            _openPgp   = openPgp;
            _trustDB   = trustDB;
            _feedCache = feedCache;
            _handler   = handler;
        }
 /// <summary>
 /// Creates a new trust manager.
 /// </summary>
 /// <param name="config">User settings controlling network behaviour, solving, etc.</param>
 /// <param name="openPgp">The OpenPGP-compatible system used to validate the signatures.</param>
 /// <param name="trustDB">A database of OpenPGP signature fingerprints the users trusts to sign <see cref="Feed"/>s coming from specific domains.</param>
 /// <param name="feedCache">Provides access to a cache of <see cref="Feed"/>s that were downloaded via HTTP(S).</param>
 /// <param name="handler">A callback object used when the the user needs to be asked questions.</param>
 public TrustManager(Config config, IOpenPgp openPgp, TrustDB trustDB, IFeedCache feedCache, ITaskHandler handler)
 {
     _config    = config ?? throw new ArgumentNullException(nameof(config));
     _openPgp   = openPgp ?? throw new ArgumentNullException(nameof(openPgp));
     _trustDB   = trustDB ?? throw new ArgumentNullException(nameof(trustDB));
     _feedCache = feedCache ?? throw new ArgumentNullException(nameof(feedCache));
     _handler   = handler ?? throw new ArgumentNullException(nameof(handler));
 }
Example #5
0
        public void AddKeyForDomain()
        {
            RunAndAssert(null, ExitCode.OK,
                         "abc", "example.com");

            TrustDB.LoadSafe()
            .Should().Be(new TrustDB().TrustKey("abc", new("example.com")));
        }
Example #6
0
        public void ListForKey()
        {
            var trust = new TrustDB()
                        .TrustKey("abc", new("example.com"))
                        .TrustKey("abc", new("example2.com"));

            trust.Save(TrustDB.DefaultLocation);

            RunAndAssert(trust.Keys[0].Domains, ExitCode.OK, "abc");
        }
        /// <summary>
        /// Creates a <see cref="TrustDB"/> from <see cref="TrustNode"/>s.
        /// </summary>
        public static TrustDB ToTrustDB([NotNull] this IEnumerable<TrustNode> nodes)
        {
            #region Sanity checks
            if (nodes == null) throw new ArgumentNullException("nodes");
            #endregion

            var trustDB = new TrustDB();
            foreach (var node in nodes)
                trustDB.TrustKey(node.Fingerprint, node.Domain);
            return trustDB;
        }
Example #8
0
        public void TestAddRemoveTrust()
        {
            var trust = new TrustDB();
            Assert.IsFalse(trust.IsTrusted("abc", new Domain("domain")));

            trust.TrustKey("abc", new Domain("domain"));
            CollectionAssert.AreEqual(new[] {new Key {Fingerprint = "abc", Domains = {new Domain("domain")}}}, trust.Keys);
            Assert.IsTrue(trust.IsTrusted("abc", new Domain("domain")));

            trust.UntrustKey("abc", new Domain("domain"));
            Assert.IsFalse(trust.IsTrusted("abc", new Domain("domain")));
        }
Example #9
0
        public void TestAddRemoveTrust()
        {
            var trust = new TrustDB();
            trust.IsTrusted("abc", new Domain("domain")).Should().BeFalse();

            trust.TrustKey("abc", new Domain("domain"));
            trust.Keys.Should().Equal(new Key {Fingerprint = "abc", Domains = {new Domain("domain")}});
            trust.IsTrusted("abc", new Domain("domain")).Should().BeTrue();

            trust.UntrustKey("abc", new Domain("domain"));
            trust.IsTrusted("abc", new Domain("domain")).Should().BeFalse();
        }
Example #10
0
        public void RemoveKeyForDomain()
        {
            new TrustDB()
            .TrustKey("abc", new("example.com"))
            .TrustKey("abc", new("example2.com"))
            .Save(TrustDB.DefaultLocation);

            RunAndAssert(null, ExitCode.OK,
                         "abc", "example.com");

            TrustDB.LoadSafe()
            .Should().Be(new TrustDB().TrustKey("abc", new("example2.com")));
        }
Example #11
0
 private bool TrustNew(TrustDB trustDB, FeedUri uri, ValidSignature signature, Domain domain)
 {
     if (AskKeyApproval(uri, signature, domain))
     {
         trustDB.TrustKey(signature.Fingerprint, domain);
         trustDB.Save();
         return(true);
     }
     else
     {
         return(false);
     }
 }
Example #12
0
        /// <summary>
        /// Creates a <see cref="TrustDB"/> from <see cref="TrustNode"/>s.
        /// </summary>
        public static TrustDB ToTrustDB(this IEnumerable <TrustNode> nodes)
        {
            #region Sanity checks
            if (nodes == null)
            {
                throw new ArgumentNullException(nameof(nodes));
            }
            #endregion

            var trustDB = new TrustDB();
            foreach (var node in nodes)
            {
                trustDB.TrustKey(node.Fingerprint, node.Domain);
            }
            return(trustDB);
        }
Example #13
0
        /// <summary>
        /// Creates a new trust manager.
        /// </summary>
        /// <param name="config">User settings controlling network behaviour, solving, etc.</param>
        /// <param name="openPgp">The OpenPGP-compatible system used to validate the signatures.</param>
        /// <param name="trustDB">A database of OpenPGP signature fingerprints the users trusts to sign <see cref="Feed"/>s coming from specific domains.</param>
        /// <param name="feedCache">Provides access to a cache of <see cref="Feed"/>s that were downloaded via HTTP(S).</param>
        /// <param name="handler">A callback object used when the the user needs to be asked questions.</param>
        public TrustManager([NotNull] Config config, [NotNull] IOpenPgp openPgp, [NotNull] TrustDB trustDB, [NotNull] IFeedCache feedCache, [NotNull] ITaskHandler handler)
        {
            #region Sanity checks
            if (config == null) throw new ArgumentNullException("config");
            if (openPgp == null) throw new ArgumentNullException("openPgp");
            if (trustDB == null) throw new ArgumentNullException("trustDB");
            if (feedCache == null) throw new ArgumentNullException("feedCache");
            if (handler == null) throw new ArgumentNullException("handler");
            #endregion

            _config = config;
            _openPgp = openPgp;
            _trustDB = trustDB;
            _feedCache = feedCache;
            _handler = handler;
        }
Example #14
0
        /// <inheritdoc />
        public override ExitCode Execute()
        {
            var trustDB = TrustDB.Load();

            string fingerprint = AdditionalArgs[0];
            var    domain      = new Domain(AdditionalArgs[1]);

            if (trustDB.IsTrusted(fingerprint, domain))
            {
                return(ExitCode.NoChanges);
            }
            trustDB.TrustKey(fingerprint, domain);

            trustDB.Save();
            return(ExitCode.OK);
        }
Example #15
0
        public ValidSignature CheckTrust(byte[] data, FeedUri uri, FeedUri mirrorUrl = null)
        {
            #region Sanity checks
            if (uri == null)
            {
                throw new ArgumentNullException("uri");
            }
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }
            #endregion

            if (uri.IsFile)
            {
                throw new UriFormatException(Resources.FeedUriLocal);
            }

            var domain = new Domain(uri.Host);
KeyImported:
            var trustDB = TrustDB.LoadSafe();
            var signatures = FeedUtils.GetSignatures(_openPgp, data);

            foreach (var signature in signatures.OfType <ValidSignature>())
            {
                if (trustDB.IsTrusted(signature.Fingerprint, domain))
                {
                    return(signature);
                }
            }

            foreach (var signature in signatures.OfType <ValidSignature>())
            {
                if (TrustNew(trustDB, uri, signature, domain))
                {
                    return(signature);
                }
            }

            foreach (var signature in signatures.OfType <MissingKeySignature>())
            {
                DownloadMissingKey(uri, mirrorUrl, signature);
                goto KeyImported;
            }

            throw new SignatureException(string.Format(Resources.FeedNoTrustedSignatures, uri));
        }
 /// <summary>
 /// Registers a set of scoped services for using Zero Install functionality.
 /// </summary>
 /// <typeparam name="TTaskHandler">A callback object used when the the user needs to be asked questions or informed about download and IO tasks.</typeparam>
 /// <param name="services">The service collection to add the services to.</param>
 public static IServiceCollection AddZeroInstall <TTaskHandler>(this IServiceCollection services)
     where TTaskHandler : class, ITaskHandler
 => services.AddScoped <ITaskHandler, TTaskHandler>()
 .AddScoped(x => Config.Load())
 .AddScoped(x => ImplementationStores.Default())
 .AddScoped(x => OpenPgp.Verifying())
 .AddScoped(x => FeedCaches.Default(x.GetService <IOpenPgp>()))
 .AddScoped(x => TrustDB.LoadSafe())
 .AddScoped <ITrustManager, TrustManager>()
 .AddScoped <IFeedManager, FeedManager>()
 .AddScoped <ICatalogManager, CatalogManager>()
 .AddScoped(x => PackageManagers.Default())
 .AddScoped <ISelectionsManager, SelectionsManager>()
 .AddScoped <ISolver, BacktrackingSolver>()
 .AddScoped <IFetcher, SequentialFetcher>()
 .AddScoped <IExecutor, Executor>()
 .AddScoped <ISelectionCandidateProvider, SelectionCandidateProvider>();
Example #17
0
 /// <summary>
 /// Registers a set of scoped services for using Zero Install functionality.
 /// </summary>
 /// <typeparam name="TTaskHandler">A callback object used when the the user needs to be asked questions or informed about download and IO tasks.</typeparam>
 /// <param name="services">The service collection to add the services to.</param>
 /// <param name="configuration">An optional configuration source for building <see cref="Config"/> instead of the default config files.</param>
 public static IServiceCollection AddZeroInstall <TTaskHandler>(this IServiceCollection services, IConfiguration?configuration = null)
     where TTaskHandler : class, ITaskHandler
 => services.AddScoped <ITaskHandler, TTaskHandler>()
 .AddScoped(_ => (configuration == null) ? Config.Load() : Config.From(configuration))
 .AddScoped(_ => ImplementationStores.Default())
 .AddScoped(_ => OpenPgp.Verifying())
 .AddScoped(provider => FeedCaches.Default(provider.GetRequiredService <IOpenPgp>()))
 .AddScoped(_ => TrustDB.LoadSafe())
 .AddScoped <ITrustManager, TrustManager>()
 .AddScoped <IFeedManager, FeedManager>()
 .AddScoped <ICatalogManager, CatalogManager>()
 .AddScoped(_ => PackageManagers.Default())
 .AddScoped <ISelectionsManager, SelectionsManager>()
 .AddScoped <ISolver, BacktrackingSolver>()
 .AddScoped <IFetcher, Fetcher>()
 .AddScoped <IExecutor, Executor>()
 .AddScoped <ISelectionCandidateProvider, SelectionCandidateProvider>();
Example #18
0
        /// <summary>
        /// Creates <see cref="TrustNode"/> representations for all entries in a <see cref="TrustDB"/>.
        /// </summary>
        public static NamedCollection <TrustNode> ToNodes(this TrustDB trustDB)
        {
            #region Sanity checks
            if (trustDB == null)
            {
                throw new ArgumentNullException(nameof(trustDB));
            }
            #endregion

            var nodes = new NamedCollection <TrustNode>();
            foreach (var key in trustDB.Keys)
            {
                foreach (var domain in key.Domains)
                {
                    nodes.Add(new TrustNode(key.Fingerprint, domain));
                }
            }
            return(nodes);
        }
Example #19
0
        /// <inheritdoc />
        public override ExitCode Execute()
        {
            var trustDB = TrustDB.Load();

            bool found = AdditionalArgs.Count switch
            {
                1 => trustDB.UntrustKey(AdditionalArgs[0]),
                2 => trustDB.UntrustKey(AdditionalArgs[0], new(AdditionalArgs[1])),
                _ => throw new InvalidOperationException()
            };

            if (!found)
            {
                return(ExitCode.NoChanges);
            }

            trustDB.Save();
            return(ExitCode.OK);
        }
    }
Example #20
0
 private void TrustKey()
 {
     TrustDB.TrustKey(OpenPgpUtilsTest.TestSignature.FormatFingerprint(), new Domain("localhost"));
 }
Example #21
0
 private bool IsKeyTrusted()
 {
     return(TrustDB.IsTrusted(OpenPgpUtilsTest.TestSignature.FormatFingerprint(), new Domain {
         Value = "localhost"
     }));
 }
Example #22
0
 private void LoadTrust()
 {
     treeViewTrustedKeys.Nodes = TrustDB.LoadSafe().ToNodes();
 }
Example #23
0
 /// <summary>
 /// Hook that can be used to register objects in the <see cref="AutoMockContainer"/> before the <see cref="Target"/> is constructed.
 /// </summary>
 protected virtual void Register(AutoMockContainer container)
 {
     container.Register <ITaskHandler>(Handler = new MockTaskHandler());
     container.Register(Config  = new Config());
     container.Register(TrustDB = new TrustDB());
 }