Skip to content

Commit 4d250b3

Browse files
committed
settings: add extra settings layer from WinRegistry
Introduce the concept of another layer of settings (lowest precedence; default values only) below the existing environment variable and Git configuration file mechanisms. Implement on Windows using the Registry, under key: HKLM\SOFTWARE\GitCredentialManager\Configuration
1 parent 0f8144a commit 4d250b3

File tree

4 files changed

+82
-1
lines changed

4 files changed

+82
-1
lines changed

src/shared/Microsoft.Git.CredentialManager/CommandContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public CommandContext(string appPath)
101101
gitPath,
102102
FileSystem.GetCurrentDirectory()
103103
);
104-
Settings = new Settings(Environment, Git);
104+
Settings = new WindowsSettings(Environment, Git, Trace);
105105
CredentialStore = new WindowsCredentialManager(Settings.CredentialNamespace);
106106
}
107107
else if (PlatformUtils.IsMacOS())

src/shared/Microsoft.Git.CredentialManager/Constants.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ public static class Remote
107107
}
108108
}
109109

110+
public static class WindowsRegistry
111+
{
112+
public const string HKAppBasePath = @"SOFTWARE\GitCredentialManager";
113+
public const string HKConfigurationPath = HKAppBasePath + @"\Configuration";
114+
}
115+
110116
public static class HelpUrls
111117
{
112118
public const string GcmProjectUrl = "https://aka.ms/gcmcore";
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
namespace Microsoft.Git.CredentialManager.Interop.Windows
5+
{
6+
/// <summary>
7+
/// Reads settings from Git configuration, environment variables, and defaults from the Windows Registry.
8+
/// </summary>
9+
public class WindowsSettings : Settings
10+
{
11+
private readonly ITrace _trace;
12+
13+
public WindowsSettings(IEnvironment environment, IGit git, ITrace trace)
14+
: base(environment, git)
15+
{
16+
EnsureArgument.NotNull(trace, nameof(trace));
17+
_trace = trace;
18+
19+
PlatformUtils.EnsureWindows();
20+
}
21+
22+
protected override bool TryGetExternalDefault(string section, string property, out string value)
23+
{
24+
value = null;
25+
26+
#if NETFRAMEWORK
27+
// Check for machine (HKLM) registry keys that match the Git configuration name.
28+
// These can be set by system administrators via Group Policy, so make useful defaults.
29+
using (Win32.RegistryKey configKey = Win32.Registry.LocalMachine.OpenSubKey(Constants.WindowsRegistry.HKConfigurationPath))
30+
{
31+
if (configKey is null)
32+
{
33+
// No configuration key exists
34+
return false;
35+
}
36+
37+
string name = $"{section}.{property}";
38+
object registryValue = configKey.GetValue(name);
39+
if (registryValue is null)
40+
{
41+
// No property exists
42+
return false;
43+
}
44+
45+
value = registryValue.ToString();
46+
_trace.WriteLine($"Default setting found in registry: {name}={value}");
47+
48+
return true;
49+
}
50+
#else
51+
return base.TryGetExternalDefault(section, property, out value);
52+
#endif
53+
}
54+
}
55+
}

src/shared/Microsoft.Git.CredentialManager/Settings.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,29 @@ public IEnumerable<string> GetSettingValues(string envarName, string section, st
289289
{
290290
yield return value;
291291
}
292+
293+
// Check for an externally specified default value
294+
if (TryGetExternalDefault(section, property, out string defaultValue))
295+
{
296+
yield return defaultValue;
297+
}
292298
}
293299
}
294300

301+
/// <summary>
302+
/// Try to get the default value for a configuration setting.
303+
/// This may come from external policies or the Operating System.
304+
/// </summary>
305+
/// <param name="section">Configuration section name.</param>
306+
/// <param name="property">Configuration property name.</param>
307+
/// <param name="value">Value of the configuration setting, or null.</param>
308+
/// <returns>True if a default setting has been set, false otherwise.</returns>
309+
protected virtual bool TryGetExternalDefault(string section, string property, out string value)
310+
{
311+
value = null;
312+
return false;
313+
}
314+
295315
public Uri RemoteUri { get; set; }
296316

297317
public bool IsDebuggingEnabled => _environment.Variables.GetBooleanyOrDefault(KnownEnvars.GcmDebug, false);

0 commit comments

Comments
 (0)