Skip to content

Commit b794b16

Browse files
committed
Queue: read env from user/machine and log safe summary
1 parent 852fa06 commit b794b16

3 files changed

Lines changed: 80 additions & 6 deletions

File tree

DesktopShell.Tests/QueueConfigurationTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ public void IsQueueConfiguredForAccess_WhenEnabledButMissingSecret_ReturnsFalse(
5757
{
5858
Environment.SetEnvironmentVariable(GlobalVar.EnvQueueEnabled, "1");
5959
Environment.SetEnvironmentVariable(GlobalVar.EnvCfAccessClientId, "id");
60-
Environment.SetEnvironmentVariable(GlobalVar.EnvCfAccessClientSecret, null);
60+
// Set to empty string (not null) so the process-scope value wins even if
61+
// the machine/user scopes have a configured secret on the dev box.
62+
Environment.SetEnvironmentVariable(GlobalVar.EnvCfAccessClientSecret, "");
6163

6264
GlobalVar.IsQueueConfiguredForAccess(out var reason).Should().BeFalse();
6365
reason.Should().Contain(GlobalVar.EnvCfAccessClientSecret);

DesktopShell/Forms/ShellForm.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ private void HandleDpiChange()
131131
public Shell()
132132
{
133133
GlobalVar.ResetLog();
134+
GlobalVar.LogQueueEnvSummary();
134135
Settings.ScanSettings();
135136

136137
// Needed for both the local TCP server port selection and remote command routing.

DesktopShell/GlobalVar.cs

Lines changed: 76 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,82 @@ public static partial class GlobalVar
5757
public const string HeaderCfAccessClientId = "CF-Access-Client-Id";
5858
public const string HeaderCfAccessClientSecret = "CF-Access-Client-Secret";
5959

60-
public static bool QueueEnabled => string.Equals(Environment.GetEnvironmentVariable(EnvQueueEnabled), "1", StringComparison.OrdinalIgnoreCase);
61-
public static string QueueBaseUrl => (Environment.GetEnvironmentVariable(EnvQueueBaseUrl) ?? "https://queue.dlamanna.com").TrimEnd('/');
62-
public static string? QueueKeyBase64 => Environment.GetEnvironmentVariable(EnvQueueKeyBase64);
63-
public static string? CfAccessClientId => Environment.GetEnvironmentVariable(EnvCfAccessClientId);
64-
public static string? CfAccessClientSecret => Environment.GetEnvironmentVariable(EnvCfAccessClientSecret);
60+
private static string? GetEnvValue(string name)
61+
{
62+
// IMPORTANT: Environment.GetEnvironmentVariable(name) reads only the *process* environment.
63+
// If variables were set via registry (setx / System Properties), Explorer may not have picked
64+
// them up yet, and new processes can inherit stale values. Falling back to User/Machine makes
65+
// DesktopShell resilient without requiring logoff/rebootestart.
66+
67+
string? processValue = Environment.GetEnvironmentVariable(name);
68+
if (processValue != null)
69+
{
70+
return processValue.Trim();
71+
}
72+
73+
try
74+
{
75+
string? userValue = Environment.GetEnvironmentVariable(name, EnvironmentVariableTarget.User);
76+
if (userValue != null)
77+
{
78+
return userValue.Trim();
79+
}
80+
}
81+
catch
82+
{
83+
// ignore
84+
}
85+
86+
try
87+
{
88+
string? machineValue = Environment.GetEnvironmentVariable(name, EnvironmentVariableTarget.Machine);
89+
if (machineValue != null)
90+
{
91+
return machineValue.Trim();
92+
}
93+
}
94+
catch
95+
{
96+
// ignore
97+
}
98+
99+
return null;
100+
}
101+
102+
private static string GetEnvSource(string name)
103+
{
104+
// Returns the first scope where the variable exists (even if empty string).
105+
if (Environment.GetEnvironmentVariable(name) != null) return "process";
106+
try
107+
{
108+
if (Environment.GetEnvironmentVariable(name, EnvironmentVariableTarget.User) != null) return "user";
109+
}
110+
catch { }
111+
try
112+
{
113+
if (Environment.GetEnvironmentVariable(name, EnvironmentVariableTarget.Machine) != null) return "machine";
114+
}
115+
catch { }
116+
return "missing";
117+
}
118+
119+
public static void LogQueueEnvSummary()
120+
{
121+
// Safe logging: no secret values, only presence/length and source.
122+
string enabledRaw = GetEnvValue(EnvQueueEnabled) ?? "";
123+
string? id = GetEnvValue(EnvCfAccessClientId);
124+
string? secret = GetEnvValue(EnvCfAccessClientSecret);
125+
126+
Log($"^^^ Queue env: {EnvQueueEnabled}='{enabledRaw}' (source={GetEnvSource(EnvQueueEnabled)}), " +
127+
$"{EnvCfAccessClientId}={(string.IsNullOrWhiteSpace(id) ? "missing" : "set")} (len={(id ?? "").Length}, source={GetEnvSource(EnvCfAccessClientId)}), " +
128+
$"{EnvCfAccessClientSecret}={(string.IsNullOrWhiteSpace(secret) ? "missing" : "set")} (len={(secret ?? "").Length}, source={GetEnvSource(EnvCfAccessClientSecret)})");
129+
}
130+
131+
public static bool QueueEnabled => string.Equals(GetEnvValue(EnvQueueEnabled), "1", StringComparison.OrdinalIgnoreCase);
132+
public static string QueueBaseUrl => (GetEnvValue(EnvQueueBaseUrl) ?? "https://queue.dlamanna.com").TrimEnd('/');
133+
public static string? QueueKeyBase64 => GetEnvValue(EnvQueueKeyBase64);
134+
public static string? CfAccessClientId => GetEnvValue(EnvCfAccessClientId);
135+
public static string? CfAccessClientSecret => GetEnvValue(EnvCfAccessClientSecret);
65136

66137
public static string NormalizeHostName(string? hostName)
67138
{

0 commit comments

Comments
 (0)