Skip to content

Commit

Permalink
Merge pull request #615 from cabinetoffice/EHD-1057-simplify-hosting-…
Browse files Browse the repository at this point in the history
…-code-changes

EHD-1057: Simplify hosting: Code changes
  • Loading branch information
jamesgriff authored Jan 22, 2025
2 parents 35a73b4 + 7582511 commit 7b14f3e
Show file tree
Hide file tree
Showing 17 changed files with 44 additions and 121 deletions.
26 changes: 9 additions & 17 deletions GenderPayGap.Core/Extensions/AspNetCore/Config.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Diagnostics;
using System.Globalization;
using GenderPayGap.Core;
using Microsoft.Extensions.Configuration;

namespace GenderPayGap.Extensions.AspNetCore
Expand All @@ -11,14 +12,12 @@ public static class Config

public static IConfiguration Configuration;

private static TimeSpan? SingletonOffsetCurrentDateTimeForSite;

static Config()
{
Console.WriteLine($"Environment: {EnvironmentName}");

Configuration = Build();
VirtualDateTime.Initialise(OffsetCurrentDateTimeForSite());
VirtualDateTime.Initialise(Global.OffsetCurrentDateTimeForSite);
}

public static string EnvironmentName => Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
Expand Down Expand Up @@ -105,10 +104,15 @@ public static int GetAppSettingInt(string key, int defaultValue = 0)
return defaultValue;
}

public static DateTime GetAppSettingDateTime(string key)
public static DateTime? GetAppSettingDateTime(string key)
{
string settingValue = GetAppSetting(key);

if (settingValue == "null")
{
return null;
}

if (DateTime.TryParseExact(settingValue, "yyMMddHHmmss", null, DateTimeStyles.AssumeLocal, out DateTime parsedValueShortFormat))
{
return parsedValueShortFormat;
Expand All @@ -119,7 +123,7 @@ public static DateTime GetAppSettingDateTime(string key)
return parsedValueOtherFormat;
}

return DateTime.MinValue;
return null;
}

private static IConfiguration GetAppSettings()
Expand All @@ -139,17 +143,5 @@ public static void SetAppSetting(string key, string value)
appSettings[key] = value;
}

public static TimeSpan OffsetCurrentDateTimeForSite()
{
if (SingletonOffsetCurrentDateTimeForSite == null)
{
SingletonOffsetCurrentDateTimeForSite = IsProduction()
? TimeSpan.Zero
: TimeSpan.Parse(GetAppSetting("OffsetCurrentDateTimeForSite", "0"));
}

return (TimeSpan) SingletonOffsetCurrentDateTimeForSite;
}

}
}
9 changes: 5 additions & 4 deletions GenderPayGap.Core/Global.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using GenderPayGap.Extensions.AspNetCore;
using GenderPayGap.Extensions.AspNetCore;
using Newtonsoft.Json;

namespace GenderPayGap.Core
Expand All @@ -19,9 +19,9 @@ public static class Global
: "SslMode=Require;Trust Server Certificate=true");

public static string S3BucketName => Config.GetAppSetting("S3_BUCKET_NAME");
public static string S3BucketAwsAccessKeyId => Config.GetAppSetting("S3_BUCKET_AWS_ACCESS_KEY_ID");
public static string S3BucketAwsSecretAccessKey => Config.GetAppSetting("S3_BUCKET_AWS_SECRET_ACCESS_KEY");
public static string S3BucketAwsRegion => Config.GetAppSetting("S3_BUCKET_AWS_REGION");
public static string S3BucketAwsAccessKeyId => Config.GetAppSetting("AWS_ACCESS_KEY_ID");
public static string S3BucketAwsSecretAccessKey => Config.GetAppSetting("AWS_SECRET_ACCESS_KEY");
public static string S3BucketAwsRegion => Config.GetAppSetting("AWS_DEFAULT_REGION");

public static string CompaniesHouseApiKey => Config.GetAppSetting("CompaniesHouseApiKey");
public static string GovUkNotifyApiKey => Config.GetAppSetting("GovUkNotifyApiKey");
Expand All @@ -38,6 +38,7 @@ public static class Global

#region Settings that we expect to want to update at short notice

public static TimeSpan OffsetCurrentDateTimeForSite => TimeSpan.Parse(Config.GetAppSetting("OffsetCurrentDateTimeForSite", "0"));
public static bool MaintenanceMode => Config.GetAppSettingBool("MaintenanceMode", defaultValue: false);
public static DateTime? MaintenanceModeUpAgainTime => Config.GetAppSettingDateTime("MaintenanceModeUpAgainTime");
public static List<int> ReportingStartYearsToExcludeFromLateFlagEnforcement =>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class CloseAccountTests
public void Setup()
{
UiTestHelper.SetDefaultEncryptionKeys();
VirtualDateTime.Initialise(Config.OffsetCurrentDateTimeForSite());
VirtualDateTime.Initialise(Global.OffsetCurrentDateTimeForSite);
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public void CsvWriter_Sanitizes_Strings_That_Start_With(char character)
{
// Arrange
var value = character + "test";
var expectedCsvRow = "\"'" + character + $"test\"{Environment.NewLine}";
var expectedCsvRow = "\"'" + character + $"test\"\r\n";

// Act
var actualCsvRow = WebUI.Helpers.CsvWriter.Write(WriteValue(value));
Expand All @@ -28,7 +28,7 @@ public void CsvWriter_Does_Not_Sanitize_Negative_Values()
{
// Arrange
var value = -10.2;
var expectedCsvRow = $"\"-10.2\"{Environment.NewLine}";
var expectedCsvRow = $"\"-10.2\"\r\n";

// Act
var actualCsvRow = WebUI.Helpers.CsvWriter.Write(WriteValue(value));
Expand All @@ -42,7 +42,7 @@ public void CsvWriter_Does_Not_Sanitize_Strings_That_Do_Not_Start_With_Injection
{
// Arrange
var value = "Test - string that doesn't start with an injection character";
var expectedCsvRow = $"\"Test - string that doesn't start with an injection character\"{Environment.NewLine}";
var expectedCsvRow = $"\"Test - string that doesn't start with an injection character\"\r\n";

// Act
var actualCsvRow = WebUI.Helpers.CsvWriter.Write(WriteValue(value));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using GenderPayGap.Core;
using GenderPayGap.Extensions.AspNetCore;

namespace Tests
Expand All @@ -13,7 +14,7 @@ public void Config_DefaultDate_Returns_DateTime_Now_Plus_917_Days_When_Configure
var expectedOffsetCurrentDateTimeForSite = new TimeSpan(917, 0, 0, 0, 0);

// Act
TimeSpan actualOffsetCurrentDateTimeForSite = Config.OffsetCurrentDateTimeForSite();
TimeSpan actualOffsetCurrentDateTimeForSite = Global.OffsetCurrentDateTimeForSite;

// Assert
Assert.AreEqual(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using GenderPayGap.Core;
using GenderPayGap.Extensions.AspNetCore;

namespace GenderPayGap.Extensions.Tests
Expand All @@ -15,7 +16,7 @@ public void Setup()
[TearDown]
public void TearDown()
{
VirtualDateTime.Initialise(Config.OffsetCurrentDateTimeForSite());
VirtualDateTime.Initialise(Global.OffsetCurrentDateTimeForSite);
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<CopyToPublishDirectory>Always</CopyToPublishDirectory>
</Content>
<Content Include="AppSettings.UnitTests.json" Condition="Exists('AppSettings.UnitTests.json')">
<DependentUpon>appsettings.json</DependentUpon>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<CopyToPublishDirectory>Always</CopyToPublishDirectory>
</Content>
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"DefaultEncryptionKey": "", // some unit tests require this key to be empty
"OffsetCurrentDateTimeForSite": "917",
"GoogleAnalyticsAccountId": null
"GoogleAnalyticsAccountId": null,
"GEODistributionList": "[email protected]",
"ReportingStartYearsToExcludeFromLateFlagEnforcement": "[2019]",
"ReportingStartYearsWithFurloughScheme": "[2020,2021]"
}
69 changes: 0 additions & 69 deletions GenderPayGap.WebUI/.ebextensions/upgrade-nginx.config

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@ private void ImportDataFromJsonString(string fileAsString)
DELETE FROM ""DraftReturns"" WHERE 1 = 1;
DELETE FROM ""InactiveUserOrganisations"" WHERE 1 = 1;
DELETE FROM ""ReturnStatus"" WHERE 1 = 1;
DELETE FROM ""Returns"" WHERE 1 = 1;
DELETE FROM ""AuditLogs"" WHERE 1 = 1;
Expand All @@ -221,7 +220,6 @@ private void ImportDataFromJsonString(string fileAsString)
DELETE FROM ""OrganisationScopes"" WHERE 1 = 1;
DELETE FROM ""UserOrganisations"" WHERE 1 = 1;
DELETE FROM ""OrganisationStatus"" WHERE 1 = 1;
DELETE FROM ""OrganisationReferences"" WHERE 1 = 1;
DELETE FROM ""OrganisationAddresses"" WHERE 1 = 1;
DELETE FROM ""OrganisationNames"" WHERE 1 = 1;
Expand Down
2 changes: 1 addition & 1 deletion GenderPayGap.WebUI/Controllers/HealthCheckController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private void CheckFileConnection()
}
catch (Exception e)
{
throw new Exception($"Could not read or write a file: {e.Message}");
throw new Exception($"Could not read or write a file: {e.Message}", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,7 @@ public bool FileExists(string relativeFilePath)

private AmazonS3Client CreateAmazonS3Client()
{
var credentials = new BasicAWSCredentials(awsAccessKeyId, awsSecretAccessKey);
var amazonS3Client = new AmazonS3Client(credentials, RegionEndpoint.GetBySystemName(awsRegion));
var amazonS3Client = new AmazonS3Client();

return amazonS3Client;
}
Expand Down
6 changes: 0 additions & 6 deletions GenderPayGap.WebUI/GenderPayGap.WebUI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@
<ItemGroup>
<Content Include=".platform\nginx\conf.d\elasticbeanstalk\00_application.conf" CopyToOutputDirectory="Always" />
</ItemGroup>

<ItemGroup>
<None Include=".ebextensions\upgrade-nginx.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
Expand Down
10 changes: 10 additions & 0 deletions GenderPayGap.WebUI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using GenderPayGap.WebUI.Search;
using GenderPayGap.WebUI.Services;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.ModelBinding.Binders;
Expand Down Expand Up @@ -130,6 +131,13 @@ private static void ConfigureServices(IServiceCollection services)
options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
options.JsonSerializerOptions.PropertyNamingPolicy = null;
});

// Configure forwarded headers - this is so that the anti-forgery middleware (see below) is allowed to set a "Secure only" cookie
services.Configure<ForwardedHeadersOptions>(
options =>
{
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
});

// Add anti-forgery token by default to forms making sure the Secure flag is always set
services.AddAntiforgery(
Expand Down Expand Up @@ -237,6 +245,8 @@ private static void ConfigureApp(WebApplication app)
{
app.Urls.Add($"http://*:{Environment.GetEnvironmentVariable("PORT")}/");
}

app.UseForwardedHeaders();

app.UseStaticFiles();

Expand Down
4 changes: 3 additions & 1 deletion GenderPayGap.WebUI/appsettings.PROD.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
"MaxNumCallsCompaniesHouseApiPerFiveMins": "100",
"SendGoogleAnalyticsDataToGovUk": "true",
"UseStartUrl": "true",
"LogToSentry": "true"
"LogToSentry": "true",
"GEODistributionList": "[email protected]",
"ReminderEmailDays": "[114, 93, 62, 31, 15, 4]"
}
5 changes: 3 additions & 2 deletions GenderPayGap.WebUI/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
},

"DaysToKeepBackupFiles": 35,
"FeatureFlagPrivateManualRegistration": "false",
"FeatureFlagSendRegistrationReviewEmails": "true",
"FeatureFlagPrivateManualRegistration": "true",
"FeatureFlagSendRegistrationReviewEmails": "false",
"GEODistributionList": "",
"MaxNumCallsCompaniesHouseApiPerFiveMins": "10",
"ReminderEmailDays": "[]",
"ReportingStartYearsToExcludeFromLateFlagEnforcement": "[2019]",
Expand Down

0 comments on commit 7b14f3e

Please sign in to comment.