Skip to content

Commit ba77bbc

Browse files
committed
feat: [DTOSS-12154] disabled shared access key for storage account and enabled use managed identity for storage account
1 parent 05a5bf9 commit ba77bbc

35 files changed

Lines changed: 565 additions & 268 deletions

File tree

.azuredevops/pipelines/cd-infrastructure-dev-core.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ resources:
99
- repository: dtos-devops-templates
1010
type: github
1111
name: NHSDigital/dtos-devops-templates
12-
ref: 0abead1e42da2bb60cbc85054ac2452746679d29
12+
ref: feat/DTOSS-12154-disable-sas
1313
endpoint: NHSDigital
1414

1515
parameters:

.github/workflows/stage-3-build-images-devtest.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ jobs:
6565
with:
6666
repository: NHSDigital/dtos-devops-templates
6767
path: templates
68-
ref: main
68+
ref: feat/DTOSS-12154-disable-sas
6969

7070
- name: Determine which Docker container(s) to build
7171
id: get-function-names
@@ -193,7 +193,7 @@ jobs:
193193
with:
194194
repository: NHSDigital/dtos-devops-templates
195195
path: templates
196-
ref: main
196+
ref: feat/DTOSS-12154-disable-sas
197197

198198
- name: Az CLI login
199199
uses: azure/login@v2

application/CohortManager/src/Functions/CaasIntegration/RetrieveMeshFile/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
services.AddSingleton<IBlobStorageHelper, BlobStorageHelper>();
6969
services.AddTransient<IMeshToBlobTransferHandler, MeshToBlobTransferHandler>();
7070
// Register health checks
71-
services.AddBlobStorageHealthCheck("RetrieveMeshFile");
71+
services.AddBlobStorageHealthCheck("RetrieveMeshFile", config.AzureWebJobsStorage);
7272
})
7373
.AddTelemetry()
7474
.AddExceptionHandler();

application/CohortManager/src/Functions/CaasIntegration/RetrieveMeshFile/RetrieveMeshFile.cs

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
namespace NHS.Screening.RetrieveMeshFile;
22

3-
using System;
4-
using System.Diagnostics.CodeAnalysis;
5-
using System.Globalization;
6-
using System.Text;
7-
using System.Text.Json;
8-
using System.Threading.Tasks;
93
using Common;
104
using Microsoft.Azure.Functions.Worker;
115
using Microsoft.Extensions.Logging;
126
using Microsoft.Extensions.Options;
137
using Model;
148
using NHS.MESH.Client.Models;
9+
using System;
10+
using System.Globalization;
11+
using System.Text.Json;
12+
using System.Threading.Tasks;
1513

1614

1715
public class RetrieveMeshFile
@@ -20,7 +18,8 @@ public class RetrieveMeshFile
2018

2119
private readonly IMeshToBlobTransferHandler _meshToBlobTransferHandler;
2220
private readonly string _mailboxId;
23-
private readonly string _blobConnectionString;
21+
private readonly string? _blobConnectionString;
22+
private readonly Uri? _blobServiceUri;
2423
private readonly IBlobStorageHelper _blobStorageHelper;
2524
private readonly RetrieveMeshFileConfig _config;
2625
private const string NextHandShakeTimeConfigKey = "NextHandShakeTime";
@@ -33,7 +32,14 @@ public RetrieveMeshFile(ILogger<RetrieveMeshFile> logger, IMeshToBlobTransferHan
3332
_blobStorageHelper = blobStorageHelper;
3433
_mailboxId = options.Value.BSSMailBox;
3534
_config = options.Value;
36-
_blobConnectionString = _config.caasfolder_STORAGE;
35+
if (_config.nemsmeshfolder_STORAGE != null)
36+
{
37+
_blobServiceUri = new Uri(_config.nemsmeshfolder_STORAGE.BlobServiceUri);
38+
}
39+
else
40+
{
41+
_blobConnectionString = Environment.GetEnvironmentVariable("nemsmeshfolder_STORAGE");
42+
}
3743
}
3844
/// <summary>
3945
/// This function polls the MESH Mailbox every 5 minutes, if there is a file posted to the mailbox.
@@ -51,8 +57,7 @@ public async Task RunAsync([TimerTrigger("0 */5 * * * *")] TimerInfo myTimer)
5157
try
5258
{
5359
var shouldExecuteHandShake = await ShouldExecuteHandShake();
54-
var result = await _meshToBlobTransferHandler.MoveFilesFromMeshToBlob(messageFilter, fileNameFunction, _mailboxId, _blobConnectionString, "inbound", shouldExecuteHandShake);
55-
60+
var result = await _meshToBlobTransferHandler.MoveFilesFromMeshToBlob(messageFilter, fileNameFunction, _mailboxId, _blobServiceUri, _blobConnectionString, "inbound", shouldExecuteHandShake);
5661
if (!result)
5762
{
5863
_logger.LogError("An error was encountered while moving files from Mesh to Blob");
@@ -74,10 +79,18 @@ private async Task<bool> ShouldExecuteHandShake()
7479

7580
Dictionary<string, string> configValues;
7681
TimeSpan handShakeInterval = new TimeSpan(0, 23, 54, 0);
77-
var meshState = await _blobStorageHelper.GetFileFromBlobStorage(_blobConnectionString, "config", ConfigFileName);
78-
if (meshState == null)
82+
BlobFile meshState = null;
83+
if (_blobServiceUri != null)
7984
{
85+
meshState = await _blobStorageHelper.GetFileFromBlobStorage(_blobServiceUri, "config", ConfigFileName);
86+
}
87+
else if (_blobConnectionString != null)
88+
{
89+
meshState = await _blobStorageHelper.GetFileFromBlobStorage(_blobConnectionString, "config", ConfigFileName);
90+
}
8091

92+
if (meshState == null)
93+
{
8194
_logger.LogInformation("MeshState File did not exist, Creating new MeshState File in blob Storage");
8295
configValues = new Dictionary<string, string>
8396
{
@@ -140,7 +153,15 @@ private async Task<bool> SetConfigState(Dictionary<string, string> state)
140153
using (var stream = GenerateStreamFromString(jsonString))
141154
{
142155
var blobFile = new BlobFile(stream, ConfigFileName);
143-
var result = await _blobStorageHelper.UploadFileToBlobStorage(_blobConnectionString, "config", blobFile, true);
156+
var result = false;
157+
if (_blobServiceUri != null)
158+
{
159+
result = await _blobStorageHelper.UploadFileToBlobStorage(_blobServiceUri, "config", blobFile, true);
160+
}
161+
else if (_blobConnectionString != null)
162+
{
163+
result = await _blobStorageHelper.UploadFileToBlobStorage(_blobConnectionString, "config", blobFile, true);
164+
}
144165
return result;
145166
}
146167
}

application/CohortManager/src/Functions/CaasIntegration/RetrieveMeshFile/RetrieveMeshFileConfig.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace NHS.Screening.RetrieveMeshFile;
22

3+
using Common;
34
using System.ComponentModel.DataAnnotations;
45

56
public class RetrieveMeshFileConfig
@@ -14,8 +15,8 @@ public class RetrieveMeshFileConfig
1415
public string? MeshKeyPassphrase { get; set; }
1516
public string? MeshKeyName { get; set; }
1617
public string KeyVaultConnectionString { get; set; }
17-
[Required]
18-
public required string caasfolder_STORAGE { get; set; }
18+
public BlobStorageConfig? AzureWebJobsStorage { get; set; }
19+
public BlobStorageConfig? nemsmeshfolder_STORAGE { get; set; }
1920
public string? ServerSideCerts { get; set; }
2021
public string? MeshCertName { get; set; }
2122
public bool? BypassServerCertificateValidation { get; set; }

application/CohortManager/src/Functions/CaasIntegration/receiveCaasFile/ProcessFileClasses/CopyFailedBatchToBlob.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,19 @@ public async Task<bool> writeBatchToBlob(string jsonFromBatch, InvalidOperationE
2929
{
3030
// we do this so that we do not have files with the same names either failing to be added or over writing another failed batch
3131
var blobFile = new BlobFile(stream, $"failedBatch-{Guid.NewGuid()}.json");
32-
var copied = await _blobStorageHelper.UploadFileToBlobStorage(_config.caasfolder_STORAGE, "failed-batch", blobFile);
32+
bool copied = false;
33+
if (_config.caasfolder_STORAGE != null)
34+
{
35+
copied = await _blobStorageHelper.UploadFileToBlobStorage(new Uri(_config.caasfolder_STORAGE.BlobServiceUri), "failed-batch", blobFile);
36+
}
37+
else
38+
{
39+
var connectionString = Environment.GetEnvironmentVariable("caasfolder_STORAGE");
40+
if (connectionString != null)
41+
{
42+
copied = await _blobStorageHelper.UploadFileToBlobStorage(connectionString, "failed-batch", blobFile);
43+
}
44+
}
3345

3446
if (copied)
3547
{

application/CohortManager/src/Functions/CaasIntegration/receiveCaasFile/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
services.AddTransient<ICopyFailedBatchToBlob, CopyFailedBatchToBlob>();
4040
services.AddScoped<IValidateDates, ValidateDates>();
4141
// Register health checks
42-
services.AddBlobStorageHealthCheck("receiveCaasFile");
42+
services.AddBlobStorageHealthCheck("receiveCaasFile", config.AzureWebJobsStorage!);
4343
})
4444
.AddTelemetry()
4545
.AddHttpClient()

application/CohortManager/src/Functions/CaasIntegration/receiveCaasFile/receiveCaasFile.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,18 @@ public async Task Run([BlobTrigger("inbound/{name}", Connection = "caasfolder_ST
9999
{
100100
_logger.LogError(ex, "There was a system exception in receive-caas-file");
101101
await _exceptionHandler.CreateSystemExceptionLogFromNhsNumber(ex, "", name, screeningName, "");
102-
await _blobStorageHelper.CopyFileToPoisonAsync(_config.caasfolder_STORAGE, name, _config.inboundBlobName);
102+
if (_config.caasfolder_STORAGE != null)
103+
{
104+
await _blobStorageHelper.CopyFileToPoisonAsync(new Uri(_config.caasfolder_STORAGE.BlobServiceUri), name, _config.inboundBlobName);
105+
}
106+
else
107+
{
108+
var connectionString = Environment.GetEnvironmentVariable("caasfolder_STORAGE");
109+
if (connectionString != null)
110+
{
111+
await _blobStorageHelper.CopyFileToPoisonAsync(connectionString, name, _config.inboundBlobName);
112+
}
113+
}
103114
}
104115
finally
105116
{

application/CohortManager/src/Functions/CaasIntegration/receiveCaasFile/receiveCaasFileConfig.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace NHS.Screening.ReceiveCaasFile;
22

3+
using Common;
34
using System.ComponentModel.DataAnnotations;
45

56
public class ReceiveCaasFileConfig
@@ -16,7 +17,9 @@ public class ReceiveCaasFileConfig
1617
[Required]
1718
public required int maxNumberOfChecks { get; set; }
1819
[Required]
19-
public required string caasfolder_STORAGE { get; set; }
20+
public BlobStorageConfig? AzureWebJobsStorage { get; set; }
21+
[Required]
22+
public BlobStorageConfig? caasfolder_STORAGE { get; set; }
2023
[Required]
2124
public required string inboundBlobName { get; set; }
2225
[Required]

application/CohortManager/src/Functions/NemsSubscriptionService/NemsMeshRetrieval/NemsMeshRetrieval.cs

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
namespace NHS.Screening.NemsMeshRetrieval;
22

3-
using System;
4-
using System.Globalization;
5-
using System.Text.Json;
6-
using System.Threading.Tasks;
73
using Common;
84
using Microsoft.Azure.Functions.Worker;
95
using Microsoft.Extensions.Logging;
106
using Microsoft.Extensions.Options;
117
using Model;
128
using NHS.MESH.Client.Models;
9+
using System;
10+
using System.Globalization;
11+
using System.Text.Json;
12+
using System.Threading.Tasks;
1313

1414

1515
public class NemsMeshRetrieval
@@ -18,7 +18,8 @@ public class NemsMeshRetrieval
1818

1919
private readonly IMeshToBlobTransferHandler _meshToBlobTransferHandler;
2020
private readonly string _mailboxId;
21-
private readonly string _blobConnectionString;
21+
private readonly string? _blobConnectionString;
22+
private readonly Uri? _blobServiceUri;
2223
private readonly IBlobStorageHelper _blobStorageHelper;
2324
private readonly NemsMeshRetrievalConfig _config;
2425
private const string NextHandShakeTimeConfigKey = "NextHandShakeTime";
@@ -31,7 +32,14 @@ public NemsMeshRetrieval(ILogger<NemsMeshRetrieval> logger, IMeshToBlobTransferH
3132
_blobStorageHelper = blobStorageHelper;
3233
_mailboxId = options.Value.NemsMeshMailBox;
3334
_config = options.Value;
34-
_blobConnectionString = _config.nemsmeshfolder_STORAGE;
35+
if (_config.nemsmeshfolder_STORAGE != null)
36+
{
37+
_blobServiceUri = new Uri(_config.nemsmeshfolder_STORAGE.BlobServiceUri);
38+
}
39+
else
40+
{
41+
_blobConnectionString = Environment.GetEnvironmentVariable("nemsmeshfolder_STORAGE");
42+
}
3543
}
3644
/// <summary>
3745
/// This function polls the MESH Mailbox every 5 minutes, if there is a file posted to the mailbox.
@@ -49,7 +57,7 @@ public async Task RunAsync([TimerTrigger("0 */5 * * * *")] TimerInfo myTimer)
4957
try
5058
{
5159
var shouldExecuteHandShake = await ShouldExecuteHandShake();
52-
var result = await _meshToBlobTransferHandler.MoveFilesFromMeshToBlob(messageFilter, fileNameFunction, _mailboxId, _blobConnectionString, _config.NemsMeshInboundContainer, shouldExecuteHandShake);
60+
var result = await _meshToBlobTransferHandler.MoveFilesFromMeshToBlob(messageFilter, fileNameFunction, _mailboxId, _blobServiceUri, _blobConnectionString, _config.NemsMeshInboundContainer, shouldExecuteHandShake);
5361

5462
if (!result)
5563
{
@@ -69,13 +77,19 @@ public async Task RunAsync([TimerTrigger("0 */5 * * * *")] TimerInfo myTimer)
6977

7078
private async Task<bool> ShouldExecuteHandShake()
7179
{
72-
7380
Dictionary<string, string> configValues;
7481
TimeSpan handShakeInterval = new TimeSpan(0, 23, 54, 0);
75-
var meshState = await _blobStorageHelper.GetFileFromBlobStorage(_blobConnectionString, _config.NemsMeshConfigContainer, ConfigFileName);
82+
BlobFile meshState = null;
83+
if (_blobServiceUri != null)
84+
{
85+
meshState = await _blobStorageHelper.GetFileFromBlobStorage(_blobServiceUri, _config.NemsMeshConfigContainer, ConfigFileName);
86+
}
87+
else if (_blobConnectionString != null)
88+
{
89+
meshState = await _blobStorageHelper.GetFileFromBlobStorage(_blobConnectionString, _config.NemsMeshConfigContainer, ConfigFileName);
90+
}
7691
if (meshState == null)
7792
{
78-
7993
_logger.LogInformation("MeshState File did not exist, Creating new MeshState File in blob Storage");
8094
configValues = new Dictionary<string, string>
8195
{
@@ -84,8 +98,8 @@ private async Task<bool> ShouldExecuteHandShake()
8498
await SetConfigState(configValues);
8599

86100
return true;
87-
88101
}
102+
89103
using (StreamReader reader = new StreamReader(meshState.Data))
90104
{
91105
meshState.Data.Seek(0, SeekOrigin.Begin);
@@ -101,8 +115,6 @@ private async Task<bool> ShouldExecuteHandShake()
101115
configValues.Add(NextHandShakeTimeConfigKey, DateTime.UtcNow.Add(handShakeInterval).ToString());
102116
await SetConfigState(configValues);
103117
return true;
104-
105-
106118
}
107119
DateTime nextHandShakeDateTime;
108120
//date cannot be parsed
@@ -138,7 +150,15 @@ private async Task<bool> SetConfigState(Dictionary<string, string> state)
138150
using (var stream = GenerateStreamFromString(jsonString))
139151
{
140152
var blobFile = new BlobFile(stream, ConfigFileName);
141-
var result = await _blobStorageHelper.UploadFileToBlobStorage(_blobConnectionString, _config.NemsMeshConfigContainer, blobFile, true);
153+
var result = false;
154+
if (_blobServiceUri != null)
155+
{
156+
result = await _blobStorageHelper.UploadFileToBlobStorage(_blobServiceUri, _config.NemsMeshConfigContainer, blobFile, true);
157+
}
158+
else if (_blobConnectionString != null)
159+
{
160+
result = await _blobStorageHelper.UploadFileToBlobStorage(_blobConnectionString, _config.NemsMeshConfigContainer, blobFile, true);
161+
}
142162
return result;
143163
}
144164
}

0 commit comments

Comments
 (0)