This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Remove dependencies on WindowsIdentity from Unix binaries #5576
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace System.Net | ||
{ | ||
partial class ContextAwareResult | ||
{ | ||
private void SafeCaptureIdentity() | ||
{ | ||
// WindowsIdentity is not supported on Unix | ||
} | ||
|
||
private void CleanupInternal() | ||
{ | ||
// Nothing to cleanup | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Diagnostics; | ||
using System.Security; | ||
using System.Security.Principal; | ||
using System.Threading; | ||
|
||
namespace System.Net | ||
{ | ||
partial class ContextAwareResult | ||
{ | ||
private WindowsIdentity _windowsIdentity; | ||
|
||
// Security: We need an assert for a call into WindowsIdentity.GetCurrent. | ||
private void SafeCaptureIdentity() | ||
{ | ||
_windowsIdentity = WindowsIdentity.GetCurrent(); | ||
} | ||
|
||
// Just like ContextCopy. | ||
internal WindowsIdentity Identity | ||
{ | ||
get | ||
{ | ||
if (InternalPeekCompleted) | ||
{ | ||
if ((_flags & StateFlags.ThreadSafeContextCopy) == 0) | ||
{ | ||
if (GlobalLog.IsEnabled) | ||
{ | ||
GlobalLog.AssertFormat("ContextAwareResult#{0}::Identity|Called on completed result.", LoggingHash.HashString(this)); | ||
} | ||
Debug.Fail("ContextAwareResult#" + LoggingHash.HashString(this) + "::Identity |Called on completed result."); | ||
} | ||
|
||
throw new InvalidOperationException(SR.net_completed_result); | ||
} | ||
|
||
if (_windowsIdentity != null) | ||
{ | ||
return _windowsIdentity; | ||
} | ||
|
||
// Make sure the identity was requested. | ||
if ((_flags & StateFlags.CaptureIdentity) == 0) | ||
{ | ||
if (GlobalLog.IsEnabled) | ||
{ | ||
GlobalLog.AssertFormat("ContextAwareResult#{0}::Identity|No identity captured - specify captureIdentity.", LoggingHash.HashString(this)); | ||
} | ||
Debug.Fail("ContextAwareResult#" + LoggingHash.HashString(this) + "::Identity |No identity captured - specify captureIdentity."); | ||
} | ||
|
||
// Just use the lock to block. We might be on the thread that owns the lock which is great, it means we | ||
// don't need an identity anyway. | ||
if ((_flags & StateFlags.PostBlockFinished) == 0) | ||
{ | ||
if (_lock == null) | ||
{ | ||
if (GlobalLog.IsEnabled) | ||
{ | ||
GlobalLog.AssertFormat("ContextAwareResult#{0}::Identity|Must lock (StartPostingAsyncOp()) { ... FinishPostingAsyncOp(); } when calling Identity (unless it's only called after FinishPostingAsyncOp).", LoggingHash.HashString(this)); | ||
} | ||
Debug.Fail("ContextAwareResult#" + LoggingHash.HashString(this) + "::Identity |Must lock (StartPostingAsyncOp()) { ... FinishPostingAsyncOp(); } when calling Identity (unless it's only called after FinishPostingAsyncOp)."); | ||
} | ||
lock (_lock) { } | ||
} | ||
|
||
if (InternalPeekCompleted) | ||
{ | ||
if ((_flags & StateFlags.ThreadSafeContextCopy) == 0) | ||
{ | ||
if (GlobalLog.IsEnabled) | ||
{ | ||
GlobalLog.AssertFormat("ContextAwareResult#{0}::Identity|Result became completed during call.", LoggingHash.HashString(this)); | ||
} | ||
Debug.Fail("ContextAwareResult#" + LoggingHash.HashString(this) + "::Identity |Result became completed during call."); | ||
} | ||
|
||
throw new InvalidOperationException(SR.net_completed_result); | ||
} | ||
|
||
return _windowsIdentity; | ||
} | ||
} | ||
|
||
private void CleanupInternal() | ||
{ | ||
if (_windowsIdentity != null) | ||
{ | ||
_windowsIdentity.Dispose(); | ||
_windowsIdentity = null; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace System.Net | ||
{ | ||
partial class ContextAwareResult | ||
{ | ||
private void SafeCaptureIdentity() | ||
{ | ||
// WindowsIdentity is not supported on NETCore50 | ||
} | ||
|
||
private void CleanupInternal() | ||
{ | ||
// Nothing to cleanup | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,12 @@ | |
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" /> | ||
<ItemGroup> | ||
<Project Include="System.Data.SqlClient.csproj" /> | ||
<Project Include="System.Data.SqlClient.csproj"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ericstj What do these changes do ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Those are specifying that you actually have 2 distinct builds of the library: one for windows and one for linux (actually, unix but we don't have a unix only convention /cc @weshaggard) |
||
<OSGroup>Linux</OSGroup> | ||
</Project> | ||
<Project Include="System.Data.SqlClient.csproj"> | ||
<OSGroup>Windows_NT</OSGroup> | ||
</Project> | ||
<Project Include="facade\System.Data.SqlClient.csproj"> | ||
<TargetGroup>net46</TargetGroup> | ||
</Project> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/System.Data.SqlClient/src/System/Data/ProviderBase/DbConnectionPoolIdentity.Unix.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
|
||
//------------------------------------------------------------------------------ | ||
|
||
using System.Security.Principal; | ||
|
||
|
||
namespace System.Data.ProviderBase | ||
{ | ||
partial class DbConnectionPoolIdentity | ||
{ | ||
static internal DbConnectionPoolIdentity GetCurrent() | ||
{ | ||
throw new PlatformNotSupportedException(); | ||
} | ||
} | ||
} | ||
|
43 changes: 43 additions & 0 deletions
43
src/System.Data.SqlClient/src/System/Data/ProviderBase/DbConnectionPoolIdentity.Windows.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
|
||
//------------------------------------------------------------------------------ | ||
|
||
using System.Security.Principal; | ||
|
||
|
||
namespace System.Data.ProviderBase | ||
{ | ||
partial class DbConnectionPoolIdentity | ||
{ | ||
static private DbConnectionPoolIdentity s_lastIdentity = null; | ||
|
||
static internal DbConnectionPoolIdentity GetCurrent() | ||
{ | ||
DbConnectionPoolIdentity current; | ||
using (WindowsIdentity identity = WindowsIdentity.GetCurrent()) | ||
{ | ||
IntPtr token = identity.AccessToken.DangerousGetHandle(); | ||
bool isNetwork = identity.User.IsWellKnown(WellKnownSidType.NetworkSid); | ||
string sidString = identity.User.Value; | ||
|
||
// Win32NativeMethods.IsTokenRestricted will raise exception if the native call fails | ||
bool isRestricted = Win32NativeMethods.IsTokenRestrictedWrapper(token); | ||
|
||
var lastIdentity = s_lastIdentity; | ||
if ((lastIdentity != null) && (lastIdentity._sidString == sidString) && (lastIdentity._isRestricted == isRestricted) && (lastIdentity._isNetwork == isNetwork)) | ||
{ | ||
current = lastIdentity; | ||
} | ||
else | ||
{ | ||
current = new DbConnectionPoolIdentity(sidString, isRestricted, isNetwork); | ||
} | ||
} | ||
s_lastIdentity = current; | ||
return current; | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we should have a single file ContextAwareResult.WindowsIdentityNop.cs, or something like that, which we use for both the unix and netcore50 builds, rather than duplicating the same contents.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thought about that. My imppression was the networking team would want to fill in the real implementation for unix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My guess is these will remain nops, but ok.