Skip to content

Commit 3e4ffed

Browse files
author
Ozan AYDIN
authored
MSAL support for sf tasks (#18977)
1 parent 1afdd4b commit 3e4ffed

File tree

8 files changed

+99
-18
lines changed

8 files changed

+99
-18
lines changed

Tasks/Common/ServiceFabricHelpers/Connect-ServiceFabricClusterFromServiceEndpoint.ps1

Lines changed: 71 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,82 @@ function Get-AadSecurityToken
3535
$clientApplicationId = $connectResult.AzureActiveDirectoryMetadata.ClientApplication
3636
Write-Host (Get-VstsLocString -Key ClientAppId -ArgumentList $clientApplicationId)
3737

38-
# Acquire AAD access token
39-
Add-Type -LiteralPath "$PSScriptRoot\Microsoft.IdentityModel.Clients.ActiveDirectory.dll"
40-
$authContext = Create-Object -TypeName Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext -ArgumentList @($authority)
41-
$authParams = $ConnectedServiceEndpoint.Auth.Parameters
42-
$userCredential = Create-Object -TypeName Microsoft.IdentityModel.Clients.ActiveDirectory.UserCredential -ArgumentList @($authParams.Username, $authParams.Password)
43-
44-
try
38+
# MSAL flag
39+
$useMSAL = $false
40+
$rawOverrideUseMSAL = Get-VstsTaskVariable -Name 'USE_MSAL'
41+
try
42+
{
43+
if($rawOverrideUseMSAL) {
44+
Write-Verbose "MSAL - USE_MSAL override is found: $rawOverrideUseMSAL"
45+
$useMSAL = [bool]::Parse($rawOverrideUseMSAL)
46+
}
47+
}
48+
catch
4549
{
46-
# Acquiring a token using UserCredential implies a non-interactive flow. No credential prompts will occur.
47-
$accessToken = $authContext.AcquireToken($clusterApplicationId, $clientApplicationId, $userCredential).AccessToken
50+
# this is not a blocker error, so we're informing
51+
$exceptionMessage = $_.Exception.Message.ToString()
52+
Write-Verbose "MSAL - USE_MSAL couldn't be parsed due to error $exceptionMessage. useMSAL=$useMSAL is used instead"
4853
}
49-
catch
54+
55+
# Acquire AAD access token - MSAL
56+
if ($useMSAL)
5057
{
51-
throw (Get-VstsLocString -Key ErrorOnAcquireToken -ArgumentList $_)
58+
$accessToken = @{
59+
token_type = $null
60+
access_token = $null
61+
expires_on = $null
62+
}
63+
64+
$tenantId = $connectResult.AzureActiveDirectoryMetadata.TenantId
65+
66+
# load the MSAL library
67+
Add-Type -Path "$PSScriptRoot\msal\Microsoft.Identity.Client.dll"
68+
69+
$authParams = $ConnectedServiceEndpoint.Auth.Parameters
70+
71+
$msalClientInstance = [Microsoft.Identity.Client.PublicClientApplicationBuilder]::Create($clientApplicationId).WithAuthority($authority, $tenantId).Build()
72+
73+
# scopes
74+
$azureActiveDirectoryResourceId = $clusterApplicationId + "/.default"
75+
$scopes = [Collections.Generic.List[string]]@($azureActiveDirectoryResourceId)
76+
77+
# fetch
78+
try {
79+
Write-Verbose "Fetching Access Token - MSAL"
80+
$tokenResult = $msalClientInstance.AcquireTokenByUsernamePassword($scopes, $authParams.Username, $authParams.Password).ExecuteAsync().GetAwaiter().GetResult()
81+
}
82+
catch {
83+
$exceptionMessage = $_.Exception.Message.ToString()
84+
Write-Error "ExceptionMessage: $exceptionMessage (in function: Get-AadSecurityToken) (MSAL)"
85+
throw (Get-VstsLocString -Key ErrorOnAcquireToken -ArgumentList $_)
86+
}
87+
88+
$accessToken.token_type = $tokenResult.TokenType
89+
$accessToken.access_token = $tokenResult.AccessToken
90+
$accessToken.expires_on = $tokenResult.ExpiresOn.ToUnixTimeSeconds()
91+
92+
return $accessToken.access_token
5293
}
94+
# Acquire AAD access token - ADAL
95+
else
96+
{
97+
Add-Type -LiteralPath "$PSScriptRoot\Microsoft.IdentityModel.Clients.ActiveDirectory.dll"
98+
$authContext = Create-Object -TypeName Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext -ArgumentList @($authority)
99+
$authParams = $ConnectedServiceEndpoint.Auth.Parameters
100+
$userCredential = Create-Object -TypeName Microsoft.IdentityModel.Clients.ActiveDirectory.UserCredential -ArgumentList @($authParams.Username, $authParams.Password)
53101

54-
return $accessToken
102+
try
103+
{
104+
# Acquiring a token using UserCredential implies a non-interactive flow. No credential prompts will occur.
105+
$accessToken = $authContext.AcquireToken($clusterApplicationId, $clientApplicationId, $userCredential).AccessToken
106+
}
107+
catch
108+
{
109+
throw (Get-VstsLocString -Key ErrorOnAcquireToken -ArgumentList $_)
110+
}
111+
112+
return $accessToken
113+
}
55114
}
56115

57116
function Add-Certificate

Tasks/Common/ServiceFabricHelpers/make.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,28 @@
1010
"source": "lib/net45/Microsoft.IdentityModel.Clients.ActiveDirectory.dll"
1111
}
1212
]
13+
},
14+
{
15+
"name": "Microsoft.Identity.Client",
16+
"version": "4.48.0",
17+
"repository": "https://www.nuget.org/api/v2/",
18+
"cp": [
19+
{
20+
"source": "lib/net45/Microsoft.Identity.Client.dll",
21+
"dest": "msal"
22+
}
23+
]
24+
},
25+
{
26+
"name": "Microsoft.IdentityModel.Abstractions",
27+
"version": "6.22.0",
28+
"repository": "https://www.nuget.org/api/v2/",
29+
"cp": [
30+
{
31+
"source": "lib/net45/Microsoft.IdentityModel.Abstractions.dll",
32+
"dest": "msal"
33+
}
34+
]
1335
}
1436
]
1537
}

Tasks/ServiceFabricComposeDeployV0/task.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"author": "Microsoft Corporation",
1818
"version": {
1919
"Major": 0,
20-
"Minor": 198,
20+
"Minor": 228,
2121
"Patch": 0
2222
},
2323
"demands": [

Tasks/ServiceFabricComposeDeployV0/task.loc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"author": "Microsoft Corporation",
1818
"version": {
1919
"Major": 0,
20-
"Minor": 198,
20+
"Minor": 228,
2121
"Patch": 0
2222
},
2323
"demands": [

Tasks/ServiceFabricDeployV1/task.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
],
1818
"version": {
1919
"Major": 1,
20-
"Minor": 205,
20+
"Minor": 228,
2121
"Patch": 0
2222
},
2323
"demands": [

Tasks/ServiceFabricDeployV1/task.loc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
],
1818
"version": {
1919
"Major": 1,
20-
"Minor": 198,
20+
"Minor": 228,
2121
"Patch": 0
2222
},
2323
"demands": [

Tasks/ServiceFabricPowerShellV1/task.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"author": "Microsoft Corporation",
1818
"version": {
1919
"Major": 1,
20-
"Minor": 198,
20+
"Minor": 228,
2121
"Patch": 0
2222
},
2323
"demands": [

Tasks/ServiceFabricPowerShellV1/task.loc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"author": "Microsoft Corporation",
1818
"version": {
1919
"Major": 1,
20-
"Minor": 198,
20+
"Minor": 228,
2121
"Patch": 0
2222
},
2323
"demands": [

0 commit comments

Comments
 (0)