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

Azure.Identity adding SharedTokenCacheCredential #7546

Merged
merged 6 commits into from
Sep 10, 2019

Conversation

schaabs
Copy link
Member

@schaabs schaabs commented Sep 9, 2019

No description provided.

{
internal class MsalCacheReader
{
private string _cachePath;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

readonly in most of these

// Note: this only works on windows if we extend to work on unix systems we need to set FileShare.None
fileStream = new FileStream(lockfilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read, 4096, FileOptions.DeleteOnClose);

using (var writer = new StreamWriter(fileStream, Encoding.UTF8, 4096, leaveOpen: true))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const for 4096

{
// We are using the file locking to synchronize the store, do not allow multiple writers for the file.
// Note: this only works on windows if we extend to work on unix systems we need to set FileShare.None
fileStream = new FileStream(lockfilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read, 4096, FileOptions.DeleteOnClose);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it be FileMode.Truncate?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used the same FileMode as was used in the MSAL extensions library which VS is using.

/// <summary>
/// Path to the persisted token cache
/// </summary>
public string CacheFilePath { get; set; } = Path.Combine(DefaultCacheDirectory, "msal.cache");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense for customers to change this?

/// <summary>
/// Millisecond delay between attempts to read the persisted token cache
/// </summary>
public TimeSpan CacheAccessRetryDelay { get; set; } = TimeSpan.FromMilliseconds(60000 / 100);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TimeSpan.FromMilliseconds(600)?

}
break;
}
catch (IOException ex)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do this using the exception filter catch (Exception ex) when (ex is IOException || ex is UnauthorizedAccessException )

byte[] cacheBytesFromDisk = await ReadCacheFromProtectedStorageAsync().ConfigureAwait(false);

// update the last read time before deserialization so if deserialization fails we won't continually read the invalid file
_lastReadTime = DateTimeOffset.UtcNow;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are comparing to LastWriteTimeUtc in initial check shouldn't we setting _lastReadTime to File.GetLastWriteTimeUtc? Otherwise, we might miss a change that happened in between our check an setting _lastReadTime = DateTimeOffset.UtcNow;

@pakrym
Copy link
Contributor

pakrym commented Sep 9, 2019

Logs and tests would've been nice. Otherwise LGTM

@@ -25,7 +25,7 @@
<PackageReference Include="System.Text.Json" />
<PackageReference Include="System.Threading.Tasks.Extensions" />
<PackageReference Include="Microsoft.Identity.Client" />

<PackageReference Include="System.Security.Cryptography.ProtectedData" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just so you are aware this only works on windows (https://github.com/dotnet/corefx/issues/22510) so if we need it for cross-platform then we will need another solution.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I'm aware of this. We're only supporting SSO on windows since the only participating App is VS on windows so far. They are possibly going to use a different approach on different platforms so until that's decided the code assumes that we're executing on windows.

if (!string.IsNullOrEmpty(AzureUsername))
// if only the username is specified via the enviornment (not password as well) and we're running on windows add the SharedTokenCacheCredential to the
// default credential to enable SSO
if (!string.IsNullOrEmpty(AzureUsername) && string.IsNullOrEmpty(AzurePassword) && (Environment.OSVersion.Platform == PlatformID.Win32NT))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should SharedTokenCacheCredential also throw on non-windows?

@danieljurek
Copy link
Member

/azp run net - client - ci

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Copy link
Contributor

@pakrym pakrym left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, lack of tests makes me sad. Still, wonder is we should expose SharedTokenCacheCredentialOptions it seems to contain things that only we know how to get right.

@schaabs schaabs merged commit e443f8a into Azure:master Sep 10, 2019
JoshLove-msft pushed a commit to JoshLove-msft/azure-sdk-for-net that referenced this pull request Sep 10, 2019
* SharedTokenCacheCredential initial implementation

* minor code formatting / refactoring

* adding SharedTokenCacheCredential to the DefaultAzureCredential chain

* Addressing PR feedback

* only add SharedTokenCacheCredential to DefaultAzureCredential on when running on windows

* updating Azure.Identity release notes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants