Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatic access verification for AOAI services to develop and run on CAPI/managed AI resources #2764

Merged
merged 44 commits into from
Feb 14, 2025
Merged
Changes from 1 commit
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
1702f14
Slice 535826: [AI][Public Preview]Copilot Toolkit: Automatic access v…
Nov 12, 2024
09f9d04
Restored SetManagedResourceAuthorization with old parameters
Nov 12, 2024
4119ef8
Removed unnessecary variables when verifying Microsoft managed resour…
Nov 12, 2024
9ed666f
Merge commit '9fc5c8aac7c0c57e78b1f75d0830ee8ea718f9cb' into aoai-acc…
Nov 26, 2024
da93eee
Merge branch 'main' of https://github.com/microsoft/BCApps into aoai-…
christian-andersen-msft Dec 12, 2024
0264589
Merge branch 'main' of https://github.com/microsoft/BCApps into aoai-…
christian-andersen-msft Dec 16, 2024
22af527
Simplified configuration check and made it more flexible
christian-andersen-msft Dec 16, 2024
f9abbb7
Merge branch 'main' into aoai-access-verification-rebranch
christian-andersen-msft Dec 19, 2024
5fc3ec2
Merge branch 'main' into aoai-access-verification-rebranch
christian-andersen-msft Jan 6, 2025
2b18667
Temporary Database and notification templates added
christian-andersen-msft Jan 8, 2025
8a08eb2
tmp template changes
christian-andersen-msft Jan 10, 2025
d6c54fe
Grace period with caching added
christian-andersen-msft Jan 10, 2025
54ad50a
Added notification and telemetry to verification logic as well as reo…
christian-andersen-msft Jan 13, 2025
02bd29c
corercted comments
christian-andersen-msft Jan 13, 2025
16e7169
added debugging messages
christian-andersen-msft Jan 14, 2025
e3680d2
Merge branch 'main' into aoai-access-verification-rebranch
christian-andersen-msft Jan 14, 2025
8c41f6d
removed wrong reference
christian-andersen-msft Jan 14, 2025
10ec3e8
Added execution permissions to table and increased test timeouts
christian-andersen-msft Jan 14, 2025
e6ce450
added additional debugging messages
christian-andersen-msft Jan 15, 2025
b6d0848
Added obsolete to old SetManagedResourceAuthorization, Removed NonDeb…
christian-andersen-msft Jan 15, 2025
91c85c7
Updated debugging messages to handle duration
christian-andersen-msft Jan 16, 2025
8d03c7f
fixed errors in debugging formatting
christian-andersen-msft Jan 17, 2025
efd3c2e
Merge branch 'main' into aoai-access-verification-rebranch
christian-andersen-msft Jan 17, 2025
8f292ec
Merge branch 'main' into aoai-access-verification-rebranch
christian-andersen-msft Jan 21, 2025
b7750b2
Updated quality and functionality of logging messages and user notifi…
christian-andersen-msft Jan 22, 2025
e711ea4
Merge branch 'main' into aoai-access-verification-rebranch
christian-andersen-msft Jan 22, 2025
69ae41a
Notification text improved. Logic regarding grace period updated. Deb…
christian-andersen-msft Jan 23, 2025
a1bf072
Variables sorted alphabetically
christian-andersen-msft Jan 23, 2025
41f99e6
field caption clarified
christian-andersen-msft Jan 23, 2025
2df6e9a
Description of parameters improved
christian-andersen-msft Jan 23, 2025
1d2a760
Added session telemetry
christian-andersen-msft Jan 23, 2025
09e7ac7
Added telemetry tags
christian-andersen-msft Jan 23, 2025
8047054
Implemented suggestions from PR
christian-andersen-msft Feb 7, 2025
9b32e37
Merge branch 'main' into aoai-access-verification-rebranch
christian-andersen-msft Feb 7, 2025
cb7580e
Clean tag moved to the correct procedure
christian-andersen-msft Feb 12, 2025
76ae32e
Validates aoai account name and aoai url before account validation
christian-andersen-msft Feb 12, 2025
9582ffb
Merge branch 'main' into aoai-access-verification-rebranch
christian-andersen-msft Feb 12, 2025
485d1f8
fixed hostname verification
christian-andersen-msft Feb 12, 2025
659f07e
Telemetry tags added
christian-andersen-msft Feb 12, 2025
f17d2c1
Merge branch 'main' into aoai-access-verification-rebranch
christian-andersen-msft Feb 13, 2025
d6fa420
Merge branch 'main' into aoai-access-verification-rebranch
christian-andersen-msft Feb 13, 2025
9c25aea
Merge branch 'main' into aoai-access-verification-rebranch
christian-andersen-msft Feb 14, 2025
cf1f046
Replaced CopilotCapability reference with AzureOpenAI
christian-andersen-msft Feb 14, 2025
a48ce42
added references to URI and Regex
christian-andersen-msft Feb 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Notification text improved. Logic regarding grace period updated. Deb…
…ugging features removed.
  • Loading branch information
christian-andersen-msft committed Jan 23, 2025
commit 69ae41a409f6cb212a56a8a97113ea1bfc8f7294
Original file line number Diff line number Diff line change
@@ -186,32 +186,6 @@ codeunit 7767 "AOAI Authorization"
exit(true);
end;

// FOR DEBUGGING ONLY
local procedure FormatDurationAsString(DurationValue: Duration): Text
var
Hours: Integer;
Minutes: Integer;
Seconds: Integer;
Milliseconds: Integer;
begin
// Convert milliseconds into hours, minutes, seconds
Hours := DurationValue div (60 * 60 * 1000);
DurationValue := DurationValue mod (60 * 60 * 1000);

Minutes := DurationValue div (60 * 1000);
DurationValue := DurationValue mod (60 * 1000);

Seconds := DurationValue div 1000;
Milliseconds := DurationValue mod 1000;

// Format as HH:MM:SS.mmm
exit(StrSubstNo('%1:%2:%3.%4',
Format(Hours, 2, '<Sign><Integer,2>'),
Format(Minutes, 2, '<Sign><Integer,2>'),
Format(Seconds, 2, '<Sign><Integer,2>'),
Format(Milliseconds, 3, '<Sign><Integer,3>')));
end;

local procedure VerifyAOAIAccount(AccountName: Text; NewApiKey: SecretText): Boolean
var
VerificationLog: Record "AOAI Account Verification Log";
@@ -221,30 +195,25 @@ codeunit 7767 "AOAI Authorization"
TruncatedAccountName: Text[100];
IsWithinCachePeriod: Boolean;
RemainingGracePeriod: Duration;
AuthFailedWithinGracePeriodLogMessageLbl: Label 'Azure Open AI authorization failed for account %1 on %2 because it is not authorized to access AI services. The connection will be terminated in %3 if not rectified', Comment = 'Telemetry message where %1 is the name of the Azure Open AI account name, %2 is the date where verification has taken place, and %3 is the remaining time until the grace period expires';
AuthFailedWithinGracePeriodLogMessageLbl: Label 'Azure Open AI authorization failed for account %1 on %2 because it is not authorized to access AI services. The connection will be terminated within %3 if not rectified', Comment = 'Telemetry message where %1 is the name of the Azure Open AI account name, %2 is the date where verification has taken place, and %3 is the remaining time until the grace period expires';
AuthFailedOutsideGracePeriodLogMessageLbl: Label 'Azure Open AI authorization failed for account %1 on %2 because it is not authorized to access AI services. The grace period has been exceeded and the connection has been terminated', Comment = 'Telemetry message where %1 is the name of the Azure Open AI account name and %2 is the date where verification has taken place';
AuthFailedWithinGracePeriodUserNotificationLbl: Label 'Azure Open AI authorization failed. AI functionality will be disabled in %1. Please contact your system administrator or the extension developer for assistance.', Comment = 'User notification explaining that AI functionality will be disabled soon, where %1 is the remaining time until the grace period expires';
AuthFailedWithinGracePeriodUserNotificationLbl: Label 'Azure Open AI authorization failed. AI functionality will be disabled within %1. Please contact your system administrator or the extension developer for assistance.', Comment = 'User notification explaining that AI functionality will be disabled soon, where %1 is the remaining time until the grace period expires';
AuthFailedOutsideGracePeriodUserNotificationLbl: Label 'Azure Open AI authorization failed and the AI functionality has been disabled. Please contact your system administrator or the extension developer for assistance.', Comment = 'User notification explaining that AI functionality has been disabled';

begin
Message('Starting VerifyAOAIAccount procedure. Variables: AOAIAccountName=' + AccountName);
GracePeriod := 15 * 60 * 1000;//14 * 24 * 60 * 60 * 1000; // 2 weeks in milliseconds
CachePeriod := 1 * 60 * 1000;//24 * 60 * 60 * 1000; // 1 day in milliseconds
GracePeriod := 14 * 24 * 60 * 60 * 1000; // 2 weeks in milliseconds
CachePeriod := 24 * 60 * 60 * 1000; // 1 day in milliseconds
TruncatedAccountName := CopyStr(DelChr(AccountName, '<>', ' '), 1, 100);
Message('Variables: GracePeriod=' + FormatDurationAsString(GracePeriod) + ', CachePeriod=' + FormatDurationAsString(CachePeriod) + ', TruncatedAccountName=' + TruncatedAccountName);

// Within CACHE period
IsWithinCachePeriod := IsAccountVerifiedWithinPeriod(TruncatedAccountName, CachePeriod);
if IsWithinCachePeriod then begin
Message('Function IsAccountVerifiedWithinPeriod called. Result: Verification skipped (within cache period).');
// Within CACHE period
if IsWithinCachePeriod then
exit(true);
end;

// Outside CACHE period
AccountVerified := PerformAOAIAccountVerification(AccountName, NewApiKey);
Message('Function PerformAOAIAccountVerification called. Result: IsVerified=' + Format(AccountVerified));

if not AccountVerified then begin
// Calculate remaining grace period
if VerificationLog.Get(TruncatedAccountName) then
RemainingGracePeriod := GracePeriod - (CurrentDateTime - VerificationLog.LastSuccessfulVerification)
else
@@ -254,20 +223,17 @@ codeunit 7767 "AOAI Authorization"
if IsAccountVerifiedWithinPeriod(TruncatedAccountName, GracePeriod) then begin
ShowUserNotification(StrSubstNo(AuthFailedWithinGracePeriodUserNotificationLbl, FormatDurationAsDays(RemainingGracePeriod)));
LogTelemetry(AccountName, Today, StrSubstNo(AuthFailedWithinGracePeriodLogMessageLbl, AccountName, Today, FormatDurationAsDays(RemainingGracePeriod)));
Message('Function IsAccountVerifiedWithinPeriod called. Result: Verification failed, but account is still valid (within grace period).');
exit(true); // Verified if within grace period
exit(true);
end
// Outside GRACE period
else begin
ShowUserNotification(AuthFailedOutsideGracePeriodUserNotificationLbl);
LogTelemetry(AccountName, Today, AuthFailedOutsideGracePeriodLogMessageLbl);
Message('Function IsAccountVerifiedWithinPeriod called. Result: Verification failed, and account is no longer valid (grace period expired).');
exit(false); // Failed verification if grace period has been exceeded
exit(false);
end;
end;

SaveVerificationTime(TruncatedAccountName);
Message('Function SaveVerificationTime called. Verification successful. Record saved.');
exit(true);
end;

@@ -276,35 +242,26 @@ codeunit 7767 "AOAI Authorization"
VerificationLog: Record "AOAI Account Verification Log";
IsVerified: Boolean;
begin
Message('Starting IsAccountVerifiedWithinPeriod procedure. Variables: AccountName=' + AccountName + ', Period=' + FormatDurationAsString(Period));

if VerificationLog.Get(AccountName) then begin
Message('Record found. Variables: CurrentDateTime=' + Format(CurrentDateTime) + ', Rec.LastSuccessfulVerification=' + Format(VerificationLog.LastSuccessfulVerification));
IsVerified := CurrentDateTime - VerificationLog.LastSuccessfulVerification <= Period;
Message('Verification result: ' + Format(IsVerified));
exit(IsVerified);
end;

Message('Record not found. Exiting with false.');
exit(false);
end;

local procedure SaveVerificationTime(AccountName: Text[100])
var
VerificationLog: Record "AOAI Account Verification Log";
begin

Message('Starting SaveVerificationTime procedure. Variables: AccountName=' + AccountName);
if VerificationLog.Get(AccountName) then begin
VerificationLog.LastSuccessfulVerification := CurrentDateTime;
VerificationLog.Modify();
Message('Record updated. Variables: Rec.LastSuccessfulVerification=' + Format(VerificationLog.LastSuccessfulVerification));
end else begin
VerificationLog.Init();
VerificationLog.AccountName := AccountName;
VerificationLog.LastSuccessfulVerification := CurrentDateTime;
if VerificationLog.Insert() then
Message('Record inserted. Variables: Rec.AccountName=' + VerificationLog.AccountName + ', Rec.LastSuccessfulVerification=' + Format(VerificationLog.LastSuccessfulVerification))
VerificationLog.Insert()
end;
end;

@@ -322,13 +279,9 @@ codeunit 7767 "AOAI Authorization"
Telemetry: Codeunit Telemetry;
CustomDimensions: Dictionary of [Text, Text];
begin
Message('Starting LogTelemetry procedure. Variables: AccountName=' + AccountName + ', VerificationDate=' + Format(VerificationDate));

// Add default dimensions
CustomDimensions.Add('AccountName', AccountName);
CustomDimensions.Add('VerificationDate', Format(VerificationDate));

// Log the telemetry with the custom message
Telemetry.LogMessage(
'0000AA1', // Event ID
StrSubstNo(LogMessage, AccountName, VerificationDate),
@@ -337,24 +290,21 @@ codeunit 7767 "AOAI Authorization"
Enum::"AL Telemetry Scope"::All,
CustomDimensions
);

Message('Telemetry logged successfully. CustomDimensions: AccountName=' + AccountName + ', VerificationDate=' + Format(VerificationDate));
end;

local procedure FormatDurationAsDays(DurationValue: Duration): Text
var
Days: Decimal;
Hours: Decimal;
DaysLabelLbl: Label '%1 days', Comment = '%1 is the number of days';
HoursLabelLbl: Label '%1 hours', Comment = '%1 is the number of hours';
DaysLabelLbl: Label '%1 days', Comment = 'Days in plural. %1 is the number of days';
DayLabelLbl: Label '1 day', Comment = 'A single day';
begin
// Convert milliseconds into days and hours
Days := DurationValue / (24 * 60 * 60 * 1000); // Total days
Hours := (DurationValue mod (24 * 60 * 60 * 1000)) / (60 * 60 * 1000); // Remaining hours
Days := DurationValue / (24 * 60 * 60 * 1000);

if Days >= 1 then
exit(StrSubstNo(DaysLabelLbl, Format(Days, 0, 9))) // Display days if more than 1 day
if Days <= 1 then
exit(DayLabelLbl)
else
exit(StrSubstNo(HoursLabelLbl, Format(Hours, 0, 9))); // Display hours if less than 1 day
// Round up to the nearest whole day
Days := Round(Days, 1, '>');
exit(StrSubstNo(DaysLabelLbl, Format(Days, 0, 0)));
end;
}