Exemple #1
0
        public void Regular()
        {
            var siteId  = 1;
            var site    = iis.GetWebSite(siteId);
            var options = Options($"--siteid {siteId}");

            Assert.IsNotNull(options);
            if (options != null)
            {
                Assert.IsNotNull(options.IncludeSiteIds);
                if (options.IncludeSiteIds != null)
                {
                    Assert.AreEqual(options.IncludeSiteIds.Count(), 1);
                    Assert.IsTrue(options.IncludeSiteIds.Contains(1));
                    Assert.IsNull(options.CommonName);
                    Assert.IsNull(options.ExcludeHosts);
                    var target = Target(options);
                    Assert.AreEqual(target.IsValid(log), true);
                    Assert.AreEqual(target.CommonName, site.Bindings.First().Host); // First binding
                    Assert.AreEqual(target.IIS, true);
                    Assert.AreEqual(target.Parts.Count(), 1);
                    Assert.AreEqual(target.Parts.First().SiteId, siteId);
                    Assert.AreEqual(target.Parts.First().Identifiers.Count(), site.Bindings.Count());
                    Assert.AreEqual(target.Parts.First().Identifiers.All(x => site.Bindings.Any(b => b.Host == x)), true);
                }
            }
        }
Exemple #2
0
        public void Regular()
        {
            var siteIdA = 1;
            var siteIdB = 2;
            var siteA   = iis.GetWebSite(siteIdA);
            var siteB   = iis.GetWebSite(siteIdB);
            var options = Options($"--siteid {siteIdA},{siteIdB}");

            Assert.IsNotNull(options);
            if (options != null)
            {
                Assert.IsNotNull(options.IncludeSiteIds);
                if (options.IncludeSiteIds != null)
                {
                    Assert.AreEqual(options.IncludeSiteIds.Contains(siteIdA), true);
                    Assert.AreEqual(options.IncludeSiteIds.Contains(siteIdB), true);
                    Assert.IsNull(options.CommonName);
                    Assert.IsNull(options.ExcludeHosts);

                    var target = Target(options);
                    Assert.AreEqual(target.IsValid(log), true);
                    Assert.AreEqual(target.CommonName.Value, siteA.Bindings.First().Host); // First binding
                    Assert.AreEqual(target.IIS, true);
                    Assert.AreEqual(target.Parts.Count(), 2);
                    Assert.AreEqual(target.Parts.First().SiteId, siteIdA);
                    Assert.AreEqual(target.Parts.First().Identifiers.Count(), siteA.Bindings.Count());
                    Assert.AreEqual(target.Parts.First().Identifiers.All(x => siteA.Bindings.Any(b => b.Host == x.Value)), true);

                    Assert.AreEqual(target.Parts.Last().SiteId, siteIdB);
                    Assert.AreEqual(target.Parts.Last().Identifiers.Count(), siteB.Bindings.Count());
                    Assert.AreEqual(target.Parts.Last().Identifiers.All(x => siteB.Bindings.Any(b => b.Host == x.Value)), true);
                }
            }
        }
        public override Task <IISWebOptions> Default(Target target)
        {
            var args = _arguments.GetArguments <IISWebArguments>();
            var ret  = new IISWebOptions(args);

            if (args.InstallationSiteId != null)
            {
                // Throws exception when not found
                var site = _iisClient.GetWebSite(args.InstallationSiteId.Value);
                ret.SiteId = site.Id;
            }
            else if (!target.IIS)
            {
                throw new Exception($"Missing parameter --{nameof(args.InstallationSiteId).ToLower()}");
            }
            return(Task.FromResult(ret));
        }
Exemple #4
0
        public override FileSystemOptions Default(Target target, IArgumentsService arguments)
        {
            var args = arguments.GetArguments <FileSystemArguments>();
            var ret  = new FileSystemOptions(BaseDefault(target, arguments));

            if (target.IIS && _iisClient.HasWebSites)
            {
                if (args.ValidationSiteId != null)
                {
                    // Throws exception when not found
                    var site = _iisClient.GetWebSite(args.ValidationSiteId.Value);
                    ret.Path   = site.Path;
                    ret.SiteId = args.ValidationSiteId.Value;
                }
            }
            return(ret);
        }
Exemple #5
0
        public override async Task <FileSystemOptions?> Default(Target target)
        {
            var args = _arguments.GetArguments <FileSystemArguments>();
            var ret  = new FileSystemOptions(BaseDefault(target));

            if (string.IsNullOrEmpty(ret.Path))
            {
                if (target.IIS && _iisClient.HasWebSites)
                {
                    if (args?.ValidationSiteId != null)
                    {
                        // Throws exception when not found
                        _iisClient.GetWebSite(args.ValidationSiteId.Value);
                        ret.SiteId = args.ValidationSiteId.Value;
                    }
                }
            }
            return(ret);
        }
Exemple #6
0
 /// <summary>
 /// Update webroot
 /// </summary>
 /// <param name="scheduled"></param>
 protected override void Refresh()
 {
     if (string.IsNullOrEmpty(_options.Path))
     {
         // Update web root path
         var siteId = _options.SiteId ?? _targetPart.SiteId;
         if (siteId > 0)
         {
             _path = _iisClient.GetWebSite(siteId.Value).Path;
         }
         else
         {
             throw new Exception("No path specified");
         }
     }
     else
     {
         _path = _options.Path;
     }
 }
Exemple #7
0
        /// <summary>
        /// Update/create bindings for all host names in the certificate
        /// </summary>
        /// <param name="target"></param>
        /// <param name="flags"></param>
        /// <param name="thumbprint"></param>
        /// <param name="store"></param>
        public int AddOrUpdateBindings(
            IEnumerable <string> identifiers,
            BindingOptions bindingOptions,
            byte[] oldThumbprint)
        {
            // Helper function to get updated sites
            IEnumerable <(TSite site, TBinding binding)> GetAllSites() => _client.WebSites.
            SelectMany(site => site.Bindings, (site, binding) => (site, binding)).
            ToList();

            try
            {
                var allBindings     = GetAllSites();
                var bindingsUpdated = 0;
                var found           = new List <string>();
                if (oldThumbprint != null)
                {
                    var siteBindings = allBindings.
                                       Where(sb => StructuralComparisons.StructuralEqualityComparer.Equals(sb.binding.CertificateHash, oldThumbprint)).
                                       ToList();

                    // Update all bindings created using the previous certificate
                    foreach (var(site, binding) in siteBindings)
                    {
                        try
                        {
                            UpdateBinding(site, binding, bindingOptions);
                            found.Add(binding.Host);
                            bindingsUpdated += 1;
                        }
                        catch (Exception ex)
                        {
                            _log.Error(ex, "Error updating binding {host}", binding.BindingInformation);
                            throw;
                        }
                    }
                }

                // Find all hostnames which are not covered by any of the already updated
                // bindings yet, because we will want to make sure that those are accessable
                // in the target site
                var targetSite = _client.GetWebSite(bindingOptions.SiteId ?? -1);
                var todo       = identifiers;
                while (todo.Any())
                {
                    // Filter by previously matched bindings
                    todo = todo.Where(cert => !found.Any(iis => Fits(iis, cert, bindingOptions.Flags) > 0));
                    if (!todo.Any())
                    {
                        break;
                    }

                    allBindings = GetAllSites();
                    var current = todo.First();
                    try
                    {
                        var binding = AddOrUpdateBindings(
                            allBindings.Select(x => x.binding).ToArray(),
                            targetSite,
                            bindingOptions.WithHost(current));

                        // Allow a single newly created binding to match with
                        // multiple hostnames on the todo list, e.g. the *.example.com binding
                        // matches with both a.example.com and b.example.com
                        if (binding == null)
                        {
                            // We were unable to create the binding because it would
                            // lead to a duplicate. Pretend that we did add it to
                            // still be able to get out of the loop;
                            found.Add(current);
                        }
                        else
                        {
                            found.Add(binding);
                            bindingsUpdated += 1;
                        }
                    }
                    catch (Exception ex)
                    {
                        _log.Error(ex, "Error creating binding {host}: {ex}", current, ex.Message);

                        // Prevent infinite retry loop, we just skip the domain when
                        // an error happens creating a new binding for it. User can
                        // always change/add the bindings manually after all.
                        found.Add(current);
                    }
                }
                return(bindingsUpdated);
            }
            catch (Exception ex)
            {
                _log.Error(ex, "Error installing");
                throw;
            }
        }