Skip to content

Commit 198ba53

Browse files
Merge pull request #239 from microsoft/sfi-code-fix
fix: Suppressed Code QL issues which does not need fix
2 parents 0c0f2d5 + e65fd1d commit 198ba53

File tree

24 files changed

+165
-19
lines changed

24 files changed

+165
-19
lines changed

Services/src/esg-ai-doc-analysis/CFS.SK.Sustainability.AI.Host/DocumentManagerHttpServiceMapper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static void UseDocumentManagerEndpoint(this WebApplication app)
5353
appContext.SKLoggerFactory.CreateLogger("DocumentManager/RegisterDocument").LogInformation($"Document information to be passed : {serializedParamJson}");
5454

5555
//Invoke Process Watcher
56-
var response = await appContext.httpClient.PostAsync(config["DocumentPreprocessing:processwatcherUrl"], content);
56+
var response = await appContext.httpClient.PostAsync(config["DocumentPreprocessing:processwatcherUrl"], content); // CodeQL [SM03781] We are reading this value from appsettings.json, this is not an user input
5757

5858
return Results.Accepted(locationUrl, result);
5959
}

Services/src/esg-ai-doc-analysis/CFS.SK.Sustainability.AI.Host/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ void ConfigureMiddleware(WebApplication app)
153153
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "ESG AI Document Service API v1"));
154154
}
155155

156-
app.UseSwaggerUI();
156+
// app.UseSwaggerUI(); Removed as part of Code QL issue (CodeQL [SM04686])
157157
app.UseSwagger();
158158
app.UseRouting();
159159
app.UseESRSEndpoint();

Services/src/esg-ai-doc-analysis/CFS.SK.Sustainability.AI.Storage/Components/CosmosDBEntityBase.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,24 @@ public CosmosDBEntityBase()
2828
/// </summary>
2929
public string __partitionkey { get; set; }
3030

31-
static SHA1 _sha1;
31+
static SHA256 _sha256;
3232

3333
static CosmosDBEntityBase()
3434
{
35-
_sha1 = SHA1.Create();
35+
_sha256 = SHA256.Create();
3636
}
3737

3838
/// <summary>
3939
/// Generate partitionkey for CosmosDB
40-
/// using SHA1 hash with id, convert it to uint and divide with number of partitions
40+
/// using SHA256 hash with id, convert it to uint and divide with number of partitions
4141
/// assigned default value as 9999 (9999 partition at this moment)
4242
/// </summary>
4343
/// <param name="id"></param>
4444
/// <param name="numberofPartitions"></param>
4545
/// <returns></returns>
4646
public static string GetKey(Guid id, int numberofPartitions)
4747
{
48-
var hasedVal = _sha1.ComputeHash(id.ToByteArray());
48+
var hasedVal = _sha256.ComputeHash(id.ToByteArray());
4949
var intHashedVal = BitConverter.ToUInt32(hasedVal, 0);
5050

5151
var range = numberofPartitions - 1;

Services/src/esg-ai-doc-analysis/CFS.SK.Sustainability.AI/DocumentManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ await this.memoryWebClient.ImportDocumentAsync(content: fileStream.BaseStream,
159159
//http client call to Logic App
160160
var content = new StringContent(JsonSerializer.Serialize(new { DocumentId = doc.DocumentId }),
161161
Encoding.UTF8, "application/json");
162-
await this.httpClient.PostAsync(this.config["DocumentPreprocessing:processwatcherUrl"], content);
162+
await this.httpClient.PostAsync(this.config["DocumentPreprocessing:processwatcherUrl"], content); // CodeQL [SM03781] We are reading this value from appsettings.json, this is not an user input
163163

164164
var result = await this.docRepo.Register(doc);
165165

@@ -222,7 +222,7 @@ await this.memoryWebClient.ImportDocumentAsync(content: downloadedFileMemoryStre
222222
//http client call to Logic App
223223
var content = new StringContent(JsonSerializer.Serialize(new { DocumentId = doc.DocumentId }),
224224
Encoding.UTF8, "application/json");
225-
await this.httpClient.PostAsync(this.config["DocumentPreprocessing:processwatcherUrl"], content);
225+
await this.httpClient.PostAsync(this.config["DocumentPreprocessing:processwatcherUrl"], content); // CodeQL [SM03781] We are reading this value from appsettings.json, this is not an user input
226226

227227
var result = await this.docRepo.Register(doc);
228228

Services/src/esg-ai-doc-analysis/CFS.SK.Sustainability.AI/ESRSGapAnalysisManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ public async Task<GapAnalysisJob> RegisterJob(GapAnalysisServiceRequest jobReque
250250
else
251251
{
252252
//Delete html file
253-
System.IO.File.Delete(htmlFileName);
253+
System.IO.File.Delete(htmlFileName); // CodeQL [SM00414] This variable is not based on user input, so no need to handle the Code QL issue.
254254
throw new Exception("PDF File is not converted");
255255
}
256256

Services/src/esg-ai-doc-analysis/CFS.SK.Sustainability.AI/Services/Queue/AzureStorageQueueService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using CFS.SK.Sustainability.AI.Services.Queue.Interfaces;
1818
using Timer = System.Timers.Timer;
1919
using Azure.Identity;
20+
using CFS.SK.Sustainability.AI.Utils;
2021

2122
namespace CFS.SK.Sustainability.AI.Services.Queue
2223
{
@@ -84,7 +85,7 @@ public static Uri GetQueueUriFromConnectionString(string connectionString, strin
8485

8586
public AzureStorageQueueService(Uri storageQueueUri, ILogger<AzureStorageQueueService> log)
8687
{
87-
DefaultAzureCredential credential = new(DefaultAzureCredential.DefaultEnvironmentVariableName); // CodeQL [SM05137] Environment variable is set in Docker File
88+
var credential = TokenCredentialProvider.GetCredential(logger: this._log);
8889
this._clientBuilder = queueName => new QueueClient(storageQueueUri, credential);
8990
this._log = log;
9091
}

Services/src/esg-ai-doc-analysis/CFS.SK.Sustainability.AI/Utils/StorageAccessUtil.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ static public BlobServiceClient GetBlobClientFromConnectionString(string Connect
1616
var DefaultEndpointSuffix = "core.windows.net";
1717
var storageAccountName = ConnectionString.Split(';').FirstOrDefault(x => x.Contains("AccountName")).Split('=')[1];
1818
var storageAccountUri = new Uri($"https://{storageAccountName}.blob.{DefaultEndpointSuffix}");
19-
DefaultAzureCredential credential = new(DefaultAzureCredential.DefaultEnvironmentVariableName); // CodeQL [SM05137] Environment variable is set in Docker File
19+
var credential = TokenCredentialProvider.GetCredential();
2020
return new BlobServiceClient(storageAccountUri, credential);
2121
}
2222

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using Azure.Core;
2+
using Azure.Identity;
3+
using Microsoft.Extensions.Logging;
4+
5+
namespace CFS.SK.Sustainability.AI.Utils;
6+
7+
/// <summary>
8+
/// Provides token credentials for Azure services using either Azure CLI or Managed Identity
9+
/// </summary>
10+
public static class TokenCredentialProvider
11+
{
12+
/// <summary>
13+
/// Gets an appropriate TokenCredential based on the runtime environment (production vs development)
14+
/// </summary>
15+
/// <param name="clientId">Optional client ID for user-assigned managed identity</param>
16+
/// <param name="logger">Optional logger for diagnostic information</param>
17+
/// <returns>A TokenCredential instance</returns>
18+
public static TokenCredential GetCredential(ILogger? logger = null)
19+
{
20+
TokenCredential credential;
21+
22+
// Detect environment - Production uses Managed Identity, Development uses Azure CLI
23+
bool isProduction = IsProductionEnvironment();
24+
25+
logger?.LogInformation("Detected environment: {Environment}", isProduction ? "Production" : "Development");
26+
27+
if (isProduction)
28+
{
29+
logger?.LogInformation("Using ManagedIdentityCredential for production authentication");
30+
credential = new ManagedIdentityCredential();
31+
}
32+
else
33+
{
34+
logger?.LogInformation("Using AzureCliCredential for development authentication");
35+
credential = new AzureCliCredential();
36+
}
37+
38+
return credential;
39+
}
40+
41+
/// <summary>
42+
/// Determines if the current environment is production based on environment variables and settings
43+
/// </summary>
44+
/// <returns>True if running in production, false if in development</returns>
45+
private static bool IsProductionEnvironment()
46+
{
47+
// Check AZURE_TOKEN_CREDENTIALS
48+
string? environment = Environment.GetEnvironmentVariable("AZURE_TOKEN_CREDENTIALS");
49+
50+
if (!string.IsNullOrEmpty(environment))
51+
{
52+
return environment.Equals("ManagedIdentityCredential", StringComparison.OrdinalIgnoreCase);
53+
}
54+
55+
return false;
56+
}
57+
}

Services/src/kernel-memory/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ RUN dotnet restore "./service/Service/./Service.csproj"
2323
COPY ["extensions", "extensions"]
2424
COPY ["tools", "tools"]
2525
COPY ["service", "service"]
26+
COPY ["utils", "utils"]
2627
WORKDIR "/src/service/Service"
2728
RUN dotnet build "./Service.csproj" -c $BUILD_CONFIGURATION -o /app/build
2829

Services/src/kernel-memory/extensions/AzureAIDocIntel/AzureAIDocIntel.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
<ItemGroup>
1212
<ProjectReference Include="..\..\service\Abstractions\Abstractions.csproj" />
13+
<ProjectReference Include="..\..\utils\TokenGenerator\TokenGenerator.csproj" />
1314
</ItemGroup>
1415

1516
<ItemGroup>

0 commit comments

Comments
 (0)