Skip to content

Commit 142d7ac

Browse files
feat: Identity based auth implementation
1 parent c2d1e73 commit 142d7ac

File tree

6 files changed

+58
-6
lines changed

6 files changed

+58
-6
lines changed

Deployment/bicep/modules/azurecognitiveservice.bicep

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ resource cognitiveService 'Microsoft.CognitiveServices/accounts@2022-03-01' = {
1515
}
1616
kind: 'FormRecognizer'
1717
properties: {
18+
customSubDomainName: cognitiveServiceName
1819
}
1920
}
2021

Deployment/bicep/modules/azureopenaiservice.bicep

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ resource openAIService 'Microsoft.CognitiveServices/accounts@2023-05-01' = {
1212
name: 'S0'
1313
}
1414
properties: {
15+
customSubDomainName: openAIServiceName
1516
// Add any specific properties if needed
1617
}
1718
}

Deployment/bicep/modules/azuresearch.bicep

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ resource searchService 'Microsoft.Search/searchServices@2023-11-01' = {
1313
properties: {
1414
replicaCount: 1
1515
partitionCount: 1
16+
disableLocalAuth: true
1617
}
1718
}
1819

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

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,10 @@ public void OnDequeue(Func<string, Task<bool>> processMessageAction)
158158
{
159159
if (message.DequeueCount <= MaxRetryBeforePoisonQueue)
160160
{
161-
bool success = await processMessageAction.Invoke(message.MessageText).ConfigureAwait(false);
161+
// Extract the original message text in case this is a re-queued poison message
162+
string messageTextToProcess = ExtractOriginalMessageText(message.MessageText);
163+
164+
bool success = await processMessageAction.Invoke(messageTextToProcess).ConfigureAwait(false);
162165
if (success)
163166
{
164167
this._log.LogTrace("Message '{0}' successfully processed, deleting message", message.MessageId);
@@ -283,16 +286,26 @@ private async Task DeleteMessageAsync(QueueMessage message, CancellationToken ca
283286

284287
private async Task UnlockMessageAsync(QueueMessage message, TimeSpan delay, CancellationToken cancellationToken)
285288
{
286-
await this._queue!.UpdateMessageAsync(message.MessageId, message.PopReceipt, visibilityTimeout: delay, cancellationToken: cancellationToken).ConfigureAwait(false);
289+
// Pass the original message body to preserve it when updating visibility timeout
290+
// Without passing the message content, Azure may distort the message body
291+
await this._queue!.UpdateMessageAsync(
292+
message.MessageId,
293+
message.PopReceipt,
294+
message.Body,
295+
visibilityTimeout: delay,
296+
cancellationToken: cancellationToken).ConfigureAwait(false);
287297
}
288298

289299
private async Task MoveMessageToPoisonQueueAsync(QueueMessage message, CancellationToken cancellationToken)
290300
{
291301
await this._poisonQueue!.CreateIfNotExistsAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
292302

303+
// Extract the original message text, unwrapping if it was previously wrapped as a poison message
304+
string originalMessageText = ExtractOriginalMessageText(message.MessageText);
305+
293306
var poisonMsg = new
294307
{
295-
MessageText = message.MessageText,
308+
MessageText = originalMessageText,
296309
Id = message.MessageId,
297310
InsertedOn = message.InsertedOn,
298311
DequeueCount = message.DequeueCount,
@@ -305,6 +318,40 @@ await this._poisonQueue.SendMessageAsync(
305318
timeToLive: neverExpire, cancellationToken: cancellationToken).ConfigureAwait(false);
306319
await this.DeleteMessageAsync(message, cancellationToken).ConfigureAwait(false);
307320
}
321+
322+
/// <summary>
323+
/// Extracts the original message text from a potentially wrapped poison message format.
324+
/// This prevents double-wrapping when a message is moved to poison queue multiple times.
325+
/// </summary>
326+
private static string ExtractOriginalMessageText(string messageText)
327+
{
328+
if (string.IsNullOrEmpty(messageText))
329+
{
330+
return messageText;
331+
}
332+
333+
try
334+
{
335+
using var doc = JsonDocument.Parse(messageText);
336+
var root = doc.RootElement;
337+
338+
// Check if this is a wrapped poison message format (has MessageText, Id, InsertedOn, DequeueCount)
339+
if (root.TryGetProperty("MessageText", out var innerMessageText) &&
340+
root.TryGetProperty("Id", out _) &&
341+
root.TryGetProperty("DequeueCount", out _))
342+
{
343+
// This is a wrapped poison message, recursively extract the original
344+
return ExtractOriginalMessageText(innerMessageText.GetString() ?? messageText);
345+
}
346+
}
347+
catch (JsonException)
348+
{
349+
// Not a valid JSON, return as-is
350+
}
351+
352+
return messageText;
353+
}
354+
308355
private static string ToJson(object data, bool indented = false)
309356
{
310357
return JsonSerializer.Serialize(data, indented ? s_indentedJsonOptions : s_notIndentedJsonOptions);

Services/src/esg-ai-doc-analysis/CFS.SK.Sustainability.AI/plugins/CSRDPlugin/BenchmarkReportGenerator/config.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"description": "CSRD Report Disclosure Statement Benchmark Analyzer",
44
"execution_settings": {
55
"default": {
6-
//just in case GPT-4o is not available, we can use GPT-4o-mini instead
76
"max_tokens": 16384,
87
"temperature": 0.3,
98
"top_p": 0.0,

Services/src/kernel-memory/service/Core/DataFormats/Pdf/PdfDecoder.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
using DocumentFormat.OpenXml.ExtendedProperties;
1414
using Microsoft.Extensions.Configuration;
1515
using Microsoft.Extensions.Logging;
16+
using Utils.TokenGenerator;
17+
1618
//using UglyToad.PdfPig;
1719
//using UglyToad.PdfPig.Content;
1820
//using UglyToad.PdfPig.DocumentLayoutAnalysis.TextExtractor;
@@ -49,7 +51,7 @@ static PdfDecoder()
4951
.Build();
5052

5153
//Read configuration value under KernelMemory/Services/AzureAIDocIntel/APIKey
52-
PdfDecoder.apiKey = config["KernelMemory:Services:AzureAIDocIntel:APIKey"];
54+
//PdfDecoder.apiKey = config["KernelMemory:Services:AzureAIDocIntel:APIKey"];
5355
//Read configuration value under KernelMemory/Services/AzureAIDocIntel/Endpoint
5456
PdfDecoder.endpoint = config["KernelMemory:Services:AzureAIDocIntel:Endpoint"];
5557

@@ -84,7 +86,8 @@ public FileContent ExtractContent(BinaryData data)
8486
Retry = { Delay = TimeSpan.FromSeconds(90), MaxDelay = TimeSpan.FromSeconds(180), MaxRetries = 3, Mode = RetryMode.Exponential },
8587
};
8688

87-
this._client = new DocumentIntelligenceClient(new Uri(PdfDecoder.endpoint), new AzureKeyCredential(PdfDecoder.apiKey), options);
89+
var credential = TokenCredentialProvider.GetCredential();
90+
this._client = new DocumentIntelligenceClient(new Uri(PdfDecoder.endpoint), credential, options);
8891

8992
Operation<AnalyzeResult> operation = null;
9093
operation = this._client.AnalyzeDocument(WaitUntil.Completed, "prebuilt-layout", content, outputContentFormat: ContentFormat.Markdown);

0 commit comments

Comments
 (0)