Example #1
0
        /// <summary>
        /// Remove all rewrite rules that start with the given prefix.
        /// </summary>
        /// <param name="prefix"></param>
        /// <param name="logger"></param>
        public void RemoveRewriteRulesWithPrefix(string prefix, ILoggerInterface logger)
        {
            // If there is no CDN site, do nothing
            using (ServerManager manager = new ServerManager())
            {
                var site = UtilsIis.FindSiteWithName(manager, this.CstChefCndSiteName, logger).SingleOrDefault();

                if (site == null)
                {
                    return;
                }
            }

            var       webconfigfilepath = this.GetCdnWebConfigPathInitialized();
            XDocument webcfg            = XDocument.Parse(File.ReadAllText(webconfigfilepath));

            var rules = (from p in webcfg.Descendants("rule")
                         where p.Attribute("name")?.Value?.StartsWith(prefix) == true
                         select p).ToList();

            foreach (var rule in rules)
            {
                rule?.Remove();
            }

            UtilsIis.WriteWebConfig(webconfigfilepath, webcfg.ToString());
        }
Example #2
0
        /// <summary>
        /// Get the webroot for the CDN site, initialized with a base web.config prepared for URL REWRITING
        /// </summary>
        /// <returns></returns>
        public string GetCdnWebConfigPathInitialized()
        {
            var basedir =
                UtilsSystem.EnsureDirectoryExists(
                    UtilsSystem.CombinePaths(this.GlobalSettings.GetDefaultApplicationStorage().path, "__chef_cdn"), true);

            var webconfigfilepath = UtilsSystem.CombinePaths(basedir, "web.config");

            // Si no hay un web.config plantilla, crearlo ahora.
            if (!File.Exists(webconfigfilepath))
            {
                File.WriteAllText(webconfigfilepath, @"
                        <configuration>
                          <system.webServer>
                            <rewrite>
                              <rules>
                              </rules>
                              <outboundRules>
                              </outboundRules>
                            </rewrite>
                          </system.webServer>
                        </configuration>
                        ");
            }

            UtilsWindowsAccounts.AddPermissionToDirectoryIfMissing(new SecurityIdentifier(UtilsWindowsAccounts.WELL_KNOWN_SID_USERS), basedir, FileSystemRights.ReadAndExecute);

            // Make sure that the site exists
            using (ServerManager manager = new ServerManager())
            {
                bool configChanged = false;

                var site = UtilsIis.FindSiteWithName(manager, this.CstChefCndSiteName, this.Logger)
                           .FirstOrDefault();

                if (site == null)
                {
                    manager.Sites.Add(this.CstChefCndSiteName, "http", $"{UtilsIis.LOCALHOST_ADDRESS}:80:{this.CstChefInternalHostname}", basedir);

                    configChanged = true;
                }
                else
                {
                    if (site.Applications.First().VirtualDirectories.First().PhysicalPath != basedir)
                    {
                        site.Applications.First().VirtualDirectories.First().PhysicalPath = basedir;
                        configChanged = true;
                    }
                }

                if (configChanged)
                {
                    UtilsIis.CommitChanges(manager);
                }
            }

            this.UtilsHosts.AddHostsMapping(UtilsIis.LOCALHOST_ADDRESS, this.CstChefInternalHostname, "chf_IISDeployer_CDN");

            // Add a cross domain file
            var crossdomainfilepath = UtilsSystem.CombinePaths(Path.GetDirectoryName(webconfigfilepath), "crossdomain.xml");

            File.WriteAllText(crossdomainfilepath, UtilsSystem.GetEmbededResourceAsString(Assembly.GetExecutingAssembly(), "IIS.crossdomain.xml"));

            // Add common proxy headers
            UtilsIis.AddAllowedServerVariablesForUrlRewrite(
                this.CstChefCndSiteName,
                "HTTP_X_FORWARDED_FOR",
                "HTTP_X_FORWARDED_PROTO",
                "HTTP_X_FORWARDED_HOST");

            return(webconfigfilepath);
        }
        public void DeleteHttpsBindings()
        {
            var logger = new TestLogsLogger(this, nameof(this.DeleteHttpsBindings));

            using (ServerManager sm = new ServerManager())
            {
                var site = sm.Sites.Add("site0", Path.GetTempPath(), 443);
                site.Bindings.Add("127.0.0.1:5879:site0", null, null, SslFlags.CentralCertStore | SslFlags.Sni);
                site.Bindings.Add("127.0.0.1:5879:site1", null, null, SslFlags.CentralCertStore | SslFlags.Sni);
                sm.CommitChanges();
            }

            Thread.Sleep(500);
            var certificates = UtilsIis.GetBoundCertificates();

            Assert.True(certificates.Any((i) =>
                                         i.Attributes.Any((j) => j.Key == "Central Certificate Store" && j.Value == "5879")));

            using (ServerManager sm = new ServerManager())
            {
                var site = UtilsIis.FindSiteWithName(sm, "site0", logger).Single();
                UtilsIis.RemoveSiteBindings(site, sm, (i) => i.Host == "site1", logger);
                sm.CommitChanges();
            }

            certificates = UtilsIis.GetBoundCertificates();
            Assert.True(certificates.Any((i) =>
                                         i.Attributes.Any((j) => j.Key == "Central Certificate Store" && j.Value == "5879")));

            using (ServerManager sm = new ServerManager())
            {
                var site = UtilsIis.FindSiteWithName(sm, "site0", logger).Single();
                UtilsIis.RemoveSiteBindings(site, sm, (i) => i.Host == "site0", logger);
                sm.CommitChanges();
            }

            Thread.Sleep(500);
            certificates = UtilsIis.GetBoundCertificates();
            Assert.False(certificates.Any((i) =>
                                          i.Attributes.Any((j) => j.Key == "Central Certificate Store" && j.Value == "5879")));

            using (ServerManager sm = new ServerManager())
            {
                var site = UtilsIis.FindSiteWithName(sm, "site0", logger).Single();
                site.Bindings.Add("127.0.0.1:5879:site0", null, null, SslFlags.CentralCertStore | SslFlags.Sni);
                site.Bindings.Add("127.0.0.1:5879:site1", null, null, SslFlags.CentralCertStore | SslFlags.Sni);
                sm.CommitChanges();
            }

            Thread.Sleep(500);
            certificates = UtilsIis.GetBoundCertificates();
            Assert.True(certificates.Any((i) =>
                                         i.Attributes.Any((j) => j.Key == "Central Certificate Store" && j.Value == "5879")));

            using (ServerManager sm = new ServerManager())
            {
                var site = UtilsIis.FindSiteWithName(sm, "site0", logger).Single();
                UtilsIis.RemoveSite(site, sm, logger);
                sm.CommitChanges();
            }

            Thread.Sleep(500);
            certificates = UtilsIis.GetBoundCertificates();
            Assert.False(certificates.Any((i) =>
                                          i.Attributes.Any((j) => j.Key == "Central Certificate Store" && j.Value == "5879")));
        }