Skip to content

Commit

Permalink
Use Environment.IsPrivilegedProcess (#77847)
Browse files Browse the repository at this point in the history
* Use Environment.IsPrivilegedProcess

* Remove using System
  • Loading branch information
iSazonov authored Nov 23, 2022
1 parent 10e929a commit 9cafe8f
Show file tree
Hide file tree
Showing 12 changed files with 12 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ private static string GetUserGroupIds(string username)

private static string GetCurrentRealUserName()
{
string realUserName = geteuid() == 0 ?
string realUserName = Environment.IsPrivilegedProcess ?
Environment.GetEnvironmentVariable("SUDO_USER") :
Environment.UserName;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ namespace BasicEventSourceTests
public partial class TestEventCounter
{
// Specifies whether the process is elevated or not.
private static readonly Lazy<bool> s_isElevated = new Lazy<bool>(AdminHelpers.IsProcessElevated);
private static bool IsProcessElevated => s_isElevated.Value;
private static bool IsProcessElevated => Environment.IsPrivilegedProcess;

[ConditionalFact(nameof(IsProcessElevated))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/25035")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ namespace BasicEventSourceTests
internal class TestUtilities
{
// Specifies whether the process is elevated or not.
private static readonly Lazy<bool> s_isElevated = new Lazy<bool>(() => AdminHelpers.IsProcessElevated());
internal static bool IsProcessElevated => s_isElevated.Value;
internal static bool IsProcessElevated => Environment.IsPrivilegedProcess;

/// <summary>
/// Confirms that there are no EventSources running.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ namespace BasicEventSourceTests
public partial class TestsManifestGeneration
{
// Specifies whether the process is elevated or not.
private static readonly Lazy<bool> s_isElevated = new Lazy<bool>(AdminHelpers.IsProcessElevated);
private static bool IsProcessElevated => s_isElevated.Value;
private static bool IsProcessElevated => Environment.IsPrivilegedProcess;
private static bool IsProcessElevatedAndNotWindowsNanoServerAndRemoteExecutorSupported =>
IsProcessElevated && PlatformDetection.IsNotWindowsNanoServer && RemoteExecutor.IsSupported;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ namespace BasicEventSourceTests
public partial class TestsWrite
{
// Specifies whether the process is elevated or not.
private static readonly Lazy<bool> s_isElevated = new Lazy<bool>(AdminHelpers.IsProcessElevated);
private static bool IsProcessElevated => s_isElevated.Value;
private static bool IsProcessElevated => Environment.IsPrivilegedProcess;
private static bool IsProcessElevatedAndNotWindowsNanoServer =>
IsProcessElevated && PlatformDetection.IsNotWindowsNanoServer; // ActiveIssue: https://github.com/dotnet/runtime/issues/26197

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ namespace BasicEventSourceTests
partial class TestsWriteEvent
{
// Specifies whether the process is elevated or not.
private static readonly Lazy<bool> s_isElevated = new Lazy<bool>(AdminHelpers.IsProcessElevated);
private static bool IsProcessElevated => s_isElevated.Value;
private static bool IsProcessElevated => Environment.IsPrivilegedProcess;
private static bool IsProcessElevatedAndNotWindowsNanoServer =>
IsProcessElevated && PlatformDetection.IsNotWindowsNanoServer; // ActiveIssue: https://github.com/dotnet/runtime/issues/26197

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ namespace BasicEventSourceTests
public partial class TestsWriteEventToListener
{
// Specifies whether the process is elevated or not.
private static readonly Lazy<bool> s_isElevated = new Lazy<bool>(AdminHelpers.IsProcessElevated);
private static bool IsProcessElevated => s_isElevated.Value;
private static bool IsProcessElevated => Environment.IsPrivilegedProcess;

[ConditionalFact(nameof(IsProcessElevated))]
public void Test_WriteEvent_TransferEvents()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public NamedPipeTest_CurrentUserOnly_Unix(ITestOutputHelper output)
public async Task Connection_UnderDifferentUsers_BehavesAsExpected(
PipeOptions serverPipeOptions, PipeOptions clientPipeOptions, PipeDirection clientPipeDirection)
{
bool isRoot = AdminHelpers.IsProcessElevated();
bool isRoot = Environment.IsPrivilegedProcess;
if (clientPipeOptions == PipeOptions.CurrentUserOnly && isRoot)
{
throw new SkipTestException("Current user is root, RemoteExecutor is unable to use a different user for CurrentUserOnly.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class NamedPipeTest_RunAsClient
[DllImport("libc", SetLastError = true)]
internal static extern unsafe uint geteuid();

public static bool IsSuperUserAndRemoteExecutorSupported => geteuid() == 0 && RemoteExecutor.IsSupported;
public static bool IsSuperUserAndRemoteExecutorSupported => Environment.IsPrivilegedProcess && RemoteExecutor.IsSupported;

[ConditionalFact(nameof(IsSuperUserAndRemoteExecutorSupported))]
[PlatformSpecific(TestPlatforms.AnyUnix)] // Uses P/Invokes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public CreateSocket(ITestOutputHelper output)
new object[] { SocketType.Unknown, ProtocolType.Udp },
};

private static bool SupportsRawSockets => AdminHelpers.IsProcessElevated();
private static bool SupportsRawSockets => Environment.IsPrivilegedProcess;
private static bool NotSupportsRawSockets => !SupportsRawSockets;

[OuterLoop]
Expand Down Expand Up @@ -480,7 +480,7 @@ await Task.Run(async () =>
}
}
}
}).WaitAsync(TestSettings.PassingTestTimeout);
}).WaitAsync(TestSettings.PassingTestTimeout);
}

[DllImport("libc")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ private void X509Store_MachineStoreLoadSkipsInvalidFiles()
}, new RemoteInvokeOptions { StartInfo = psi }).Dispose();
}

public static bool NotRunningAsRootAndRemoteExecutorSupported => Interop.Sys.GetEUid() != 0 && RemoteExecutor.IsSupported;
public static bool NotRunningAsRootAndRemoteExecutorSupported => !Environment.IsPrivilegedProcess && RemoteExecutor.IsSupported;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,8 @@ internal sealed class TestServiceProvider

private const int readTimeout = 60000;

private static readonly Lazy<bool> s_runningWithElevatedPrivileges = new Lazy<bool>(() =>
{
using WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent();
return new WindowsPrincipal(currentIdentity).IsInRole(WindowsBuiltInRole.Administrator);
});

private NamedPipeClientStream _client;

public static bool RunningWithElevatedPrivileges
{
get { return s_runningWithElevatedPrivileges.Value; }
}

public NamedPipeClientStream Client
{
get
Expand Down

0 comments on commit 9cafe8f

Please sign in to comment.