Skip to content

Commit a1b7416

Browse files
committed
Address feedback.
1 parent 89d410f commit a1b7416

File tree

9 files changed

+270
-107
lines changed

9 files changed

+270
-107
lines changed

.azure-pipelines/validate-pr-auth-module.yml

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pr:
88
include:
99
- dev
1010
- master
11+
- milestone/*
1112
paths:
1213
include:
1314
- src/Authentication/*
@@ -18,9 +19,7 @@ jobs:
1819
displayName: MS Graph PS SDK Auth Validation - Windows
1920
timeoutInMinutes: 300
2021
pool:
21-
name: Microsoft Graph
22-
demands: 'Agent.Name -equals Local-Agent'
23-
22+
vmImage: 'windows-latest'
2423
steps:
2524
- task: securedevelopmentteam.vss-secure-development-tools.build-task-credscan.CredScan@2
2625
displayName: 'Run CredScan'
@@ -62,6 +61,16 @@ jobs:
6261
command: 'test'
6362
projects: '$(System.DefaultWorkingDirectory)/src/Authentication/Authentication.Test/*.csproj'
6463
testRunTitle: 'Run Enabled Tests'
64+
65+
- task: YodLabs.O365PostMessage.O365PostMessageBuild.O365PostMessageBuild@0
66+
displayName: 'Graph Client Tooling pipeline fail notification'
67+
inputs:
68+
addressType: serviceEndpoint
69+
serviceEndpointName: 'microsoftgraph pipeline status'
70+
title: '$(Build.DefinitionName) failure notification'
71+
text: 'This pipeline has failed. View the build details for further information. This is a blocking failure. '
72+
condition: and(failed(), ne(variables['Build.Reason'], 'Manual'))
73+
enabled: true
6574

6675
- job: MSGraphPSSDKValidation_MacOS
6776
displayName: MS Graph PS SDK Auth Validation - MacOS
@@ -73,4 +82,14 @@ jobs:
7382
inputs:
7483
command: 'test'
7584
projects: '$(System.DefaultWorkingDirectory)/src/Authentication/Authentication.Test/*.csproj'
76-
testRunTitle: 'Run Enabled Tests'
85+
testRunTitle: 'Run Enabled Tests'
86+
87+
- task: YodLabs.O365PostMessage.O365PostMessageBuild.O365PostMessageBuild@0
88+
displayName: 'Graph Client Tooling pipeline fail notification'
89+
inputs:
90+
addressType: serviceEndpoint
91+
serviceEndpointName: 'microsoftgraph pipeline status'
92+
title: '$(Build.DefinitionName) failure notification'
93+
text: 'This pipeline has failed. View the build details for further information. This is a blocking failure. '
94+
condition: and(failed(), ne(variables['Build.Reason'], 'Manual'))
95+
enabled: true

.azure-pipelines/validate-pr-beta-modules.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pr:
88
include:
99
- master
1010
- dev
11+
- milestone/*
1112
paths:
1213
include:
1314
- src/Beta/*

src/Authentication/Authentication.Test/TokenCache/TokenCacheStorageTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public void ShouldMakeParallelCallsToTokenCache()
137137

138138
// Assert
139139
Assert.Equal(executions, count);
140-
Assert.False(failed);
140+
Assert.False(failed, "Unexpected content found.");
141141
}
142142

143143
public void Dispose()

src/Authentication/Authentication/Constants.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ public static class Constants
1414
internal const string AppParameterSet = "AppParameterSet";
1515
internal const int MaxDeviceCodeTimeOut = 120; // 2 mins timeout.
1616
internal static readonly string TokenCacheDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".graph");
17-
internal const string TokenCahceServiceName = "com.microsoft.graph.powershell.sdkcache";
17+
internal const string TokenCacheServiceName = "com.microsoft.graph.powershell.sdkcache";
1818
}
1919
}

src/Authentication/Authentication/ErrorConstants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ internal static class Message
2020
internal const string InvalidJWT = "Invalid JWT access token.";
2121
internal const string MissingAuthContext = "Authentication needed, call Connect-Graph.";
2222
internal const string InstanceExists = "An instance of {0} already exists. Call {1} to overwrite it.";
23+
internal const string NullOrEmptyParameter = "Parameter '{0}' cannot be null or empty.";
2324
internal const string MacKeyChainFailed = "{0} failed with result code {1}.";
2425
}
2526
}

src/Authentication/Authentication/TokenCache/LinuxTokenCache.cs

Lines changed: 61 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace Microsoft.Graph.PowerShell.Authentication.TokenCache
66
{
77
using Microsoft.Graph.PowerShell.Authentication.TokenCache.NativePlatformLibs;
88
using System;
9+
using System.Globalization;
910
using System.Runtime.InteropServices;
1011

1112
/// <summary>
@@ -14,80 +15,112 @@ namespace Microsoft.Graph.PowerShell.Authentication.TokenCache
1415
internal static class LinuxTokenCache
1516
{
1617
/// <summary>
17-
/// Gets an app's token from Linux kerings faciility.
18+
/// Gets an app's token from Linux keyrings facility.
1819
/// </summary>
1920
/// <param name="appId">An app/client id.</param>
20-
/// <returns>A decypted token.</returns>
21+
/// <returns>A decrypted token.</returns>
2122
public static byte[] GetToken(string appId)
2223
{
24+
if (string.IsNullOrEmpty(appId))
25+
{
26+
throw new ArgumentNullException(string.Format(
27+
CultureInfo.CurrentCulture,
28+
ErrorConstants.Message.NullOrEmptyParameter,
29+
nameof(appId)));
30+
}
31+
2332
int key = LinuxNativeKeyUtils.request_key(
2433
type: LinuxNativeKeyUtils.KeyTypes.User,
25-
description: $"{Constants.TokenCahceServiceName}:{appId}",
34+
description: $"{Constants.TokenCacheServiceName}:{appId}",
2635
callout_info: IntPtr.Zero,
2736
dest_keyring: (int)LinuxNativeKeyUtils.KeyringType.KEY_SPEC_SESSION_KEYRING);
2837

2938
if (key == -1)
3039
return new byte[0];
3140

32-
LinuxNativeKeyUtils.keyctl_read_alloc(
33-
key: key,
34-
buffer: out IntPtr contentPtr);
41+
LinuxNativeKeyUtils.keyctl_read_alloc(key: key, buffer: out IntPtr contentPtr);
3542
string content = Marshal.PtrToStringAnsi(contentPtr);
3643
Marshal.FreeHGlobal(contentPtr);
3744

3845
if (string.IsNullOrEmpty(content))
46+
{
3947
return new byte[0];
48+
}
4049

4150
return Convert.FromBase64String(content);
4251
}
4352

4453
/// <summary>
45-
/// Adds or updates an app's token to Linux kerings faciility.
54+
/// Adds or updates an app's token to Linux keyrings facility.
4655
/// </summary>
4756
/// <param name="appId">An app/client id.</param>
4857
/// <param name="plainContent">The content to store.</param>
4958
public static void SetToken(string appId, byte[] plainContent)
5059
{
51-
if (plainContent != null && plainContent.Length > 0)
60+
if (string.IsNullOrEmpty(appId))
5261
{
53-
string encodedContent = Convert.ToBase64String(plainContent);
54-
int key = LinuxNativeKeyUtils.request_key(
55-
type: LinuxNativeKeyUtils.KeyTypes.User,
56-
description: $"{Constants.TokenCahceServiceName}:{appId}",
57-
callout_info: IntPtr.Zero,
58-
dest_keyring: (int)LinuxNativeKeyUtils.KeyringType.KEY_SPEC_SESSION_KEYRING);
62+
throw new ArgumentNullException(string.Format(
63+
CultureInfo.CurrentCulture,
64+
ErrorConstants.Message.NullOrEmptyParameter,
65+
nameof(appId)));
66+
}
5967

60-
if (key == -1)
61-
LinuxNativeKeyUtils.add_key(
62-
type: LinuxNativeKeyUtils.KeyTypes.User,
63-
description: $"{Constants.TokenCahceServiceName}:{appId}",
64-
payload: encodedContent,
65-
plen: encodedContent.Length,
66-
keyring: (int)LinuxNativeKeyUtils.KeyringType.KEY_SPEC_SESSION_KEYRING);
67-
else
68-
LinuxNativeKeyUtils.keyctl_update(
69-
key: key,
70-
payload: encodedContent,
71-
plen: encodedContent.Length);
68+
if (plainContent == null || plainContent.Length == 0)
69+
{
70+
return ;
71+
}
72+
73+
string encodedContent = Convert.ToBase64String(plainContent);
74+
int key = LinuxNativeKeyUtils.request_key(
75+
type: LinuxNativeKeyUtils.KeyTypes.User,
76+
description: $"{Constants.TokenCacheServiceName}:{appId}",
77+
callout_info: IntPtr.Zero,
78+
dest_keyring: (int)LinuxNativeKeyUtils.KeyringType.KEY_SPEC_SESSION_KEYRING);
79+
80+
if (key == -1)
81+
{
82+
LinuxNativeKeyUtils.add_key(
83+
type: LinuxNativeKeyUtils.KeyTypes.User,
84+
description: $"{Constants.TokenCacheServiceName}:{appId}",
85+
payload: encodedContent,
86+
plen: encodedContent.Length,
87+
keyring: (int)LinuxNativeKeyUtils.KeyringType.KEY_SPEC_SESSION_KEYRING);
88+
}
89+
else
90+
{
91+
LinuxNativeKeyUtils.keyctl_update(
92+
key: key,
93+
payload: encodedContent,
94+
plen: encodedContent.Length);
7295
}
7396
}
7497

7598
/// <summary>
76-
/// Deletes an app's token from Linux kerings faciility.
99+
/// Deletes an app's token from Linux keyrings facility.
77100
/// </summary>
78101
/// <param name="appId">An app/client id.</param>
79102
public static void DeleteToken(string appId)
80103
{
104+
if (string.IsNullOrEmpty(appId))
105+
{
106+
throw new ArgumentNullException(string.Format(
107+
CultureInfo.CurrentCulture,
108+
ErrorConstants.Message.NullOrEmptyParameter,
109+
nameof(appId)));
110+
}
111+
81112
int key = LinuxNativeKeyUtils.request_key(
82113
type: LinuxNativeKeyUtils.KeyTypes.User,
83-
description: $"{Constants.TokenCahceServiceName}:{appId}",
114+
description: $"{Constants.TokenCacheServiceName}:{appId}",
84115
callout_info: IntPtr.Zero,
85116
dest_keyring: (int)LinuxNativeKeyUtils.KeyringType.KEY_SPEC_SESSION_KEYRING);
86117
if (key != -1)
87118
{
88119
int removedState = LinuxNativeKeyUtils.keyctl_revoke(key);
89120
if (removedState == -1)
121+
{
90122
throw new Exception("Failed to revoke token from cache.");
123+
}
91124
}
92125
}
93126
}

0 commit comments

Comments
 (0)