Port 7.7 - WIP
This commit is contained in:
@@ -7,7 +7,6 @@ using Umbraco.Core.Services;
|
||||
|
||||
namespace Umbraco.Web.HealthCheck.Checks.Config
|
||||
{
|
||||
|
||||
public abstract class AbstractConfigCheck : HealthCheck
|
||||
{
|
||||
private readonly ConfigurationService _configurationService;
|
||||
@@ -44,10 +43,18 @@ namespace Umbraco.Web.HealthCheck.Checks.Config
|
||||
/// </summary>
|
||||
public abstract ValueComparisonType ValueComparisonType { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the flag indicating if the check is considered successful if the config value is missing (defaults to false - an error - if missing)
|
||||
/// </summary>
|
||||
public virtual bool ValidIfConfigMissing
|
||||
{
|
||||
get { return false; }
|
||||
}
|
||||
|
||||
protected AbstractConfigCheck(ILocalizedTextService textService)
|
||||
{
|
||||
TextService = textService;
|
||||
_configurationService = new ConfigurationService(AbsoluteFilePath, XPath);
|
||||
_configurationService = new ConfigurationService(AbsoluteFilePath, XPath, _textService);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -121,11 +128,18 @@ namespace Umbraco.Web.HealthCheck.Checks.Config
|
||||
|
||||
public override IEnumerable<HealthCheckStatus> GetStatus()
|
||||
{
|
||||
var successMessage = string.Format(CheckSuccessMessage, FileName, XPath, Values, CurrentValue);
|
||||
|
||||
var configValue = _configurationService.GetConfigurationValue();
|
||||
if (configValue.Success == false)
|
||||
{
|
||||
var message = configValue.Result;
|
||||
return new[] { new HealthCheckStatus(message) { ResultType = StatusResultType.Error } };
|
||||
if (ValidIfConfigMissing)
|
||||
{
|
||||
return new[] { new HealthCheckStatus(successMessage) { ResultType = StatusResultType.Success } };
|
||||
}
|
||||
|
||||
var errorMessage = configValue.Result;
|
||||
return new[] { new HealthCheckStatus(errorMessage) { ResultType = StatusResultType.Error } };
|
||||
}
|
||||
|
||||
CurrentValue = configValue.Result;
|
||||
@@ -133,8 +147,7 @@ namespace Umbraco.Web.HealthCheck.Checks.Config
|
||||
var valueFound = Values.Any(value => string.Equals(CurrentValue, value.Value, StringComparison.InvariantCultureIgnoreCase));
|
||||
if (ValueComparisonType == ValueComparisonType.ShouldEqual && valueFound || ValueComparisonType == ValueComparisonType.ShouldNotEqual && valueFound == false)
|
||||
{
|
||||
var message = string.Format(CheckSuccessMessage, FileName, XPath, Values, CurrentValue);
|
||||
return new[] { new HealthCheckStatus(message) { ResultType = StatusResultType.Success } };
|
||||
return new[] { new HealthCheckStatus(successMessage) { ResultType = StatusResultType.Success } };
|
||||
}
|
||||
|
||||
// Declare the action for rectifying the config value
|
||||
|
||||
@@ -18,6 +18,8 @@ namespace Umbraco.Web.HealthCheck.Checks.Config
|
||||
|
||||
public override ValueComparisonType ValueComparisonType => ValueComparisonType.ShouldEqual;
|
||||
|
||||
public override bool ValidIfConfigMissing => true;
|
||||
|
||||
public override IEnumerable<AcceptableConfiguration> Values => new List<AcceptableConfiguration>
|
||||
{
|
||||
new AcceptableConfiguration { IsRecommended = true, Value = bool.FalseString.ToLower() }
|
||||
|
||||
@@ -18,11 +18,11 @@ namespace Umbraco.Web.HealthCheck.Checks.Config
|
||||
/// <param name="configFilePath">The absolute file location of the configuration file</param>
|
||||
/// <param name="xPath">The XPath to select the value</param>
|
||||
/// <returns></returns>
|
||||
public ConfigurationService(string configFilePath, string xPath)
|
||||
public ConfigurationService(string configFilePath, string xPath, ILocalizedTextService textService)
|
||||
{
|
||||
_configFilePath = configFilePath;
|
||||
_xPath = xPath;
|
||||
_textService = Current.Services.TextService;
|
||||
_textService = textService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -31,7 +31,7 @@ namespace Umbraco.Web.HealthCheck.Checks.Config
|
||||
get
|
||||
{
|
||||
return TextService.Localize("healthcheck/customErrorsCheckSuccessMessage",
|
||||
new[] { CurrentValue, Values.First(v => v.IsRecommended).Value });
|
||||
new[] { Values.First(v => v.IsRecommended).Value });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -66,13 +66,10 @@ namespace Umbraco.Web.HealthCheck.Checks.Security
|
||||
{
|
||||
var message = string.Empty;
|
||||
var success = false;
|
||||
var url = _runtime.ApplicationUrl;
|
||||
|
||||
// Access the site home page and check for the click-jack protection header or meta tag
|
||||
var serverVariables = _httpContextAccessor.HttpContext.Request.ServerVariables;
|
||||
var useSsl = GlobalSettings.UseSSL || _httpContextAccessor.HttpContext.Request.ServerVariables["SERVER_PORT"] == "443";
|
||||
var address = string.Format("http{0}://{1}:{2}", useSsl ? "s" : "", url.Host.ToLower(), url.Port);
|
||||
var request = WebRequest.Create(address);
|
||||
var url = _runtime.ApplicationUrl;
|
||||
var request = WebRequest.Create(url);
|
||||
request.Method = "GET";
|
||||
try
|
||||
{
|
||||
@@ -93,7 +90,7 @@ namespace Umbraco.Web.HealthCheck.Checks.Security
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
message = _textService.Localize("healthcheck/httpsCheckInvalidUrl", new[] { address, ex.Message });
|
||||
message = _textService.Localize("healthcheck/httpsCheckInvalidUrl", new[] { url, ex.Message });
|
||||
}
|
||||
|
||||
var actions = new List<HealthCheckAction>();
|
||||
|
||||
@@ -54,10 +54,7 @@ namespace Umbraco.Web.HealthCheck.Checks.Security
|
||||
var url = _runtime.ApplicationUrl;
|
||||
|
||||
// Access the site home page and check for the headers
|
||||
var serverVariables = _httpContextAccessor.HttpContext.Request.ServerVariables;
|
||||
var useSsl = GlobalSettings.UseSSL || _httpContextAccessor.HttpContext.Request.ServerVariables["SERVER_PORT"] == "443";
|
||||
var address = string.Format("http{0}://{1}:{2}", useSsl ? "s" : "", url.Host.ToLower(), url.Port);
|
||||
var request = WebRequest.Create(address);
|
||||
var request = WebRequest.Create(url);
|
||||
request.Method = "HEAD";
|
||||
try
|
||||
{
|
||||
@@ -74,7 +71,7 @@ namespace Umbraco.Web.HealthCheck.Checks.Security
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
message = _textService.Localize("healthcheck/httpsCheckInvalidUrl", new[] { address, ex.Message });
|
||||
message = _textService.Localize("healthcheck/httpsCheckInvalidUrl", new[] { url, ex.Message });
|
||||
}
|
||||
|
||||
var actions = new List<HealthCheckAction>();
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Web;
|
||||
using Umbraco.Core;
|
||||
using Umbraco.Core.IO;
|
||||
@@ -56,19 +57,48 @@ namespace Umbraco.Web.HealthCheck.Checks.Security
|
||||
private HealthCheckStatus CheckForValidCertificate()
|
||||
{
|
||||
var message = string.Empty;
|
||||
var success = false;
|
||||
var url = _runtime.ApplicationUrl;
|
||||
StatusResultType result;
|
||||
|
||||
// Attempt to access the site over HTTPS to see if it HTTPS is supported
|
||||
// and a valid certificate has been configured
|
||||
var address = string.Format("https://{0}:{1}", url.Host.ToLower(), url.Port);
|
||||
var request = (HttpWebRequest)WebRequest.Create(address);
|
||||
var url = _runtime.ApplicationUrl.Replace("http:", "https:");
|
||||
var request = (HttpWebRequest)WebRequest.Create(url);
|
||||
request.Method = "HEAD";
|
||||
|
||||
try
|
||||
{
|
||||
var response = (HttpWebResponse)request.GetResponse();
|
||||
success = response.StatusCode == HttpStatusCode.OK;
|
||||
if (response.StatusCode == HttpStatusCode.OK)
|
||||
{
|
||||
// Got a valid response, check now for if certificate expiring within 14 days
|
||||
// Hat-tip: https://stackoverflow.com/a/15343898/489433
|
||||
const int NumberOfDaysForExpiryWarning = 14;
|
||||
var cert = request.ServicePoint.Certificate;
|
||||
var cert2 = new X509Certificate2(cert);
|
||||
var expirationDate = cert2.NotAfter;
|
||||
|
||||
var daysToExpiry = (int)Math.Floor((cert2.NotAfter - DateTime.Now).TotalDays);
|
||||
if (daysToExpiry <= 0)
|
||||
{
|
||||
result = StatusResultType.Error;
|
||||
message = _textService.Localize("healthcheck/httpsCheckExpiredCertificate");
|
||||
}
|
||||
else if (daysToExpiry < NumberOfDaysForExpiryWarning)
|
||||
{
|
||||
result = StatusResultType.Warning;
|
||||
message = _textService.Localize("healthcheck/httpsCheckExpiringCertificate", new[] { daysToExpiry.ToString() });
|
||||
}
|
||||
else
|
||||
{
|
||||
result = StatusResultType.Success;
|
||||
message = _textService.Localize("healthcheck/httpsCheckValidCertificate");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
result = StatusResultType.Error;
|
||||
message = _textService.Localize("healthcheck/httpsCheckInvalidUrl", new[] { url, response.StatusDescription });
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -77,30 +107,29 @@ namespace Umbraco.Web.HealthCheck.Checks.Security
|
||||
{
|
||||
message = exception.Status == WebExceptionStatus.TrustFailure
|
||||
? _textService.Localize("healthcheck/httpsCheckInvalidCertificate", new [] { exception.Message })
|
||||
: _textService.Localize("healthcheck/httpsCheckInvalidUrl", new [] { address, exception.Message });
|
||||
: _textService.Localize("healthcheck/httpsCheckInvalidUrl", new [] { url, exception.Message });
|
||||
}
|
||||
else
|
||||
{
|
||||
message = _textService.Localize("healthcheck/httpsCheckInvalidUrl", new[] { address, ex.Message });
|
||||
message = _textService.Localize("healthcheck/httpsCheckInvalidUrl", new[] { url, ex.Message });
|
||||
}
|
||||
|
||||
result = StatusResultType.Error;
|
||||
}
|
||||
|
||||
var actions = new List<HealthCheckAction>();
|
||||
|
||||
if (success)
|
||||
message = _textService.Localize("healthcheck/httpsCheckValidCertificate");
|
||||
|
||||
return
|
||||
new HealthCheckStatus(message)
|
||||
{
|
||||
ResultType = success ? StatusResultType.Success : StatusResultType.Error,
|
||||
ResultType = result,
|
||||
Actions = actions
|
||||
};
|
||||
}
|
||||
|
||||
private HealthCheckStatus CheckIfCurrentSchemeIsHttps()
|
||||
{
|
||||
var uri = HttpContext.Current.Request.Url;
|
||||
var uri = new Uri(HealthCheckContext.SiteUrl);
|
||||
var success = uri.Scheme == "https";
|
||||
|
||||
var actions = new List<HealthCheckAction>();
|
||||
@@ -116,7 +145,7 @@ namespace Umbraco.Web.HealthCheck.Checks.Security
|
||||
private HealthCheckStatus CheckHttpsConfigurationSetting()
|
||||
{
|
||||
var httpsSettingEnabled = Core.Configuration.GlobalSettings.UseSSL;
|
||||
var uri = HttpContext.Current.Request.Url;
|
||||
var uri = new Uri(HealthCheckContext.SiteUrl);
|
||||
var actions = new List<HealthCheckAction>();
|
||||
|
||||
string resultMessage;
|
||||
@@ -152,7 +181,7 @@ namespace Umbraco.Web.HealthCheck.Checks.Security
|
||||
{
|
||||
var configFile = IOHelper.MapPath("~/Web.config");
|
||||
const string xPath = "/configuration/appSettings/add[@key='umbracoUseSSL']/@value";
|
||||
var configurationService = new ConfigurationService(configFile, xPath);
|
||||
var configurationService = new ConfigurationService(configFile, xPath, _textService);
|
||||
var updateConfigFile = configurationService.UpdateConfigFile("true");
|
||||
|
||||
if (updateConfigFile.Success)
|
||||
|
||||
@@ -53,7 +53,7 @@ namespace Umbraco.Web.HealthCheck.Checks.Services
|
||||
|
||||
// appPath is the virtual application root path on the server
|
||||
var appPath = "";
|
||||
var config = WebConfigurationManager.OpenWebConfiguration(_runtime.ApplicationVirtualPath);
|
||||
var config = WebConfigurationManager.OpenWebConfiguration(_runtime.ApplicationPath);
|
||||
var settings = (MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");
|
||||
if (settings == null)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user