-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Expand file tree
/
Copy pathCredentialStore.cs
More file actions
364 lines (312 loc) · 14.7 KB
/
CredentialStore.cs
File metadata and controls
364 lines (312 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using GitCredentialManager.Interop.Linux;
using GitCredentialManager.Interop.MacOS;
using GitCredentialManager.Interop.Posix;
using GitCredentialManager.Interop.Windows;
using StoreNames = GitCredentialManager.Constants.CredentialStoreNames;
namespace GitCredentialManager
{
public class CredentialStore : ICredentialStore
{
private readonly ICommandContext _context;
private ICredentialStore _backingStore;
public CredentialStore(ICommandContext context)
{
EnsureArgument.NotNull(context, nameof(context));
_context = context;
}
#region ICredentialStore
public IList<string> GetAccounts(string service)
{
EnsureBackingStore();
return _backingStore.GetAccounts(service);
}
public ICredential Get(string service, string account)
{
EnsureBackingStore();
return _backingStore.Get(service, account);
}
public void AddOrUpdate(string service, string account, string secret)
{
EnsureBackingStore();
_backingStore.AddOrUpdate(service, account, secret);
}
public bool Remove(string service, string account)
{
EnsureBackingStore();
return _backingStore.Remove(service, account);
}
#endregion
private void EnsureBackingStore()
{
if (_backingStore != null)
{
return;
}
string ns = _context.Settings.CredentialNamespace;
string credStoreName = _context.Settings.CredentialBackingStore?.ToLowerInvariant()
?? GetDefaultStore();
switch (credStoreName)
{
case StoreNames.WindowsCredentialManager:
ValidateWindowsCredentialManager();
_backingStore = new WindowsCredentialManager(ns);
break;
case StoreNames.Dpapi:
ValidateDpapi(out string dpapiStoreRoot);
_backingStore = new DpapiCredentialStore(_context.FileSystem, dpapiStoreRoot, ns);
break;
case StoreNames.MacOSKeychain:
ValidateMacOSKeychain();
_backingStore = new MacOSKeychain(ns);
break;
case StoreNames.SecretService:
ValidateSecretService();
_backingStore = new SecretServiceCollection(ns);
break;
case StoreNames.Gpg:
ValidateGpgPass(out string gpgStoreRoot, out string gpgExec);
IGpg gpg = new Gpg(gpgExec, _context.SessionManager, _context.ProcessManager, _context.Trace2);
_backingStore = new GpgPassCredentialStore(_context.FileSystem, gpg, gpgStoreRoot, ns);
break;
case StoreNames.Cache:
ValidateCredentialCache(out string options);
_backingStore = new CredentialCacheStore(_context.Git, options);
break;
case StoreNames.Plaintext:
ValidatePlaintext(out string plainStoreRoot);
_backingStore = new PlaintextCredentialStore(_context.FileSystem, plainStoreRoot, ns);
break;
case StoreNames.None:
_backingStore = new NullCredentialStore();
break;
default:
var sb = new StringBuilder();
sb.AppendLine(string.IsNullOrWhiteSpace(credStoreName)
? "No credential store has been selected."
: $"Unknown credential store '{credStoreName}'.");
_context.Trace2.WriteError(sb.ToString());
sb.AppendFormat(
"{3}Set the {0} environment variable or the {1}.{2} Git configuration setting to one of the following options:{3}{3}",
Constants.EnvironmentVariables.GcmCredentialStore,
Constants.GitConfiguration.Credential.SectionName,
Constants.GitConfiguration.Credential.CredentialStore,
Environment.NewLine);
AppendAvailableStoreList(sb);
sb.AppendLine();
sb.AppendLine($"See {Constants.HelpUrls.GcmCredentialStores} for more information.");
throw new Exception(sb.ToString());
}
}
private static string GetDefaultStore()
{
if (PlatformUtils.IsWindows())
return StoreNames.WindowsCredentialManager;
if (PlatformUtils.IsMacOS())
return StoreNames.MacOSKeychain;
// Other platforms have no default store
return null;
}
private static void AppendAvailableStoreList(StringBuilder sb)
{
if (PlatformUtils.IsWindows())
{
sb.AppendFormat(" {1,-13} : Windows Credential Manager (not available over network/SSH sessions){0}",
Environment.NewLine, StoreNames.WindowsCredentialManager);
sb.AppendFormat(" {1,-13} : DPAPI protected files{0}",
Environment.NewLine, StoreNames.Dpapi);
}
if (PlatformUtils.IsMacOS())
{
sb.AppendFormat(" {1,-13} : macOS Keychain{0}",
Environment.NewLine, StoreNames.MacOSKeychain);
}
if (PlatformUtils.IsLinux())
{
sb.AppendFormat(" {1,-13} : freedesktop.org Secret Service (requires graphical interface){0}",
Environment.NewLine, StoreNames.SecretService);
}
if (PlatformUtils.IsPosix())
{
sb.AppendFormat(" {1,-13} : GNU `pass` compatible credential storage (requires GPG and `pass`){0}",
Environment.NewLine, StoreNames.Gpg);
}
if (!PlatformUtils.IsWindows())
{
sb.AppendFormat(" {1,-13} : Git's in-memory credential cache{0}",
Environment.NewLine, StoreNames.Cache);
}
sb.AppendFormat(" {1,-13} : store credentials in plain-text files (UNSECURE){0}",
Environment.NewLine, StoreNames.Plaintext);
sb.AppendFormat(" {1, -13} : disable internal credential storage{0}",
Environment.NewLine, StoreNames.None);
}
private void ValidateWindowsCredentialManager()
{
if (!PlatformUtils.IsWindows())
{
var message = $"Can only use the '{StoreNames.WindowsCredentialManager}' credential store on Windows.";
_context.Trace2.WriteError(message);
throw new Exception(message + Environment.NewLine +
$"See {Constants.HelpUrls.GcmCredentialStores} for more information."
);
}
if (!WindowsCredentialManager.CanPersist())
{
var message = $"Unable to persist credentials with the '{StoreNames.WindowsCredentialManager}' credential store.";
_context.Trace2.WriteError(message);
throw new Exception(message + Environment.NewLine +
$"See {Constants.HelpUrls.GcmCredentialStores} for more information."
);
}
}
private void ValidateDpapi(out string storeRoot)
{
if (!PlatformUtils.IsWindows())
{
var message = $"Can only use the '{StoreNames.Dpapi}' credential store on Windows.";
_context.Trace2.WriteError(message);
throw new Exception(message + Environment.NewLine +
$"See {Constants.HelpUrls.GcmCredentialStores} for more information."
);
}
// Check for a redirected credential store location
if (!_context.Settings.TryGetSetting(
Constants.EnvironmentVariables.GcmDpapiStorePath,
Constants.GitConfiguration.Credential.SectionName,
Constants.GitConfiguration.Credential.DpapiStorePath,
out storeRoot))
{
// Use default store root at ~/.gcm/dpapi_store
storeRoot = Path.Combine(_context.FileSystem.UserDataDirectoryPath, "dpapi_store");
}
}
private void ValidateMacOSKeychain()
{
if (!PlatformUtils.IsMacOS())
{
var message = $"Can only use the '{StoreNames.MacOSKeychain}' credential store on macOS.";
_context.Trace2.WriteError(message);
throw new Exception(message + Environment.NewLine +
$"See {Constants.HelpUrls.GcmCredentialStores} for more information."
);
}
}
private void ValidateSecretService()
{
if (!PlatformUtils.IsLinux())
{
var message = $"Can only use the '{StoreNames.SecretService}' credential store on Linux.";
_context.Trace2.WriteError(message);
throw new Exception(message + Environment.NewLine +
$"See {Constants.HelpUrls.GcmCredentialStores} for more information."
);
}
if (!_context.SessionManager.IsDesktopSession)
{
var message = $"Cannot use the '{StoreNames.SecretService}' credential backing store without a graphical interface present.";
_context.Trace2.WriteError(message);
throw new Exception(message + Environment.NewLine +
$"See {Constants.HelpUrls.GcmCredentialStores} for more information."
);
}
}
private void ValidateGpgPass(out string storeRoot, out string execPath)
{
if (!PlatformUtils.IsPosix())
{
var message = $"Can only use the '{StoreNames.Gpg}' credential store on POSIX systems.";
_context.Trace2.WriteError(message);
throw new Exception(message + Environment.NewLine +
$"See {Constants.HelpUrls.GcmCredentialStores} for more information."
);
}
execPath = GetGpgPath();
// If we are in a headless environment, and don't have the GPG_TTY or SSH_TTY
// variables set, then error - we need a TTY device path for pin-entry to work headless.
if (!_context.SessionManager.IsDesktopSession &&
!_context.Environment.Variables.ContainsKey("GPG_TTY") &&
!_context.Environment.Variables.ContainsKey("SSH_TTY"))
{
var message = "GPG_TTY is not set; add `export GPG_TTY=$(tty)` to your profile.";
_context.Trace2.WriteError(message);
throw new Exception(message + Environment.NewLine +
$"See {Constants.HelpUrls.GcmCredentialStores} for more information."
);
}
// Check for a redirected pass store location
if (!_context.Settings.TryGetSetting(
GpgPassCredentialStore.PasswordStoreDirEnvar,
Constants.GitConfiguration.Credential.SectionName,
Constants.GitConfiguration.Credential.GpgPassStorePath,
out storeRoot))
{
// Use default store root at ~/.password-store
storeRoot = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".password-store");
}
}
private void ValidateCredentialCache(out string options)
{
if (PlatformUtils.IsWindows())
{
var message = $"Can not use the '{StoreNames.Cache}' credential store on Windows due to lack of UNIX socket support in Git for Windows.";
_context.Trace2.WriteError(message);
throw new Exception(message + Environment.NewLine +
$"See {Constants.HelpUrls.GcmCredentialStores} for more information."
);
}
// allow for --timeout and other options
if (!_context.Settings.TryGetSetting(
Constants.EnvironmentVariables.GcmCredCacheOptions,
Constants.GitConfiguration.Credential.SectionName,
Constants.GitConfiguration.Credential.CredCacheOptions,
out options))
{
options = string.Empty;
}
}
private void ValidatePlaintext(out string storeRoot)
{
// Check for a redirected credential store location
if (!_context.Settings.TryGetSetting(
Constants.EnvironmentVariables.GcmPlaintextStorePath,
Constants.GitConfiguration.Credential.SectionName,
Constants.GitConfiguration.Credential.PlaintextStorePath,
out storeRoot))
{
// Use default store root at ~/.gcm/store
storeRoot = Path.Combine(_context.FileSystem.UserDataDirectoryPath, "store");
}
}
private string GetGpgPath()
{
string gpgPath;
// Use the GCM_GPG_PATH environment variable if set
if (_context.Environment.Variables.TryGetValue(Constants.EnvironmentVariables.GpgExecutablePath,
out gpgPath))
{
if (_context.FileSystem.FileExists(gpgPath))
{
_context.Trace.WriteLine($"Using Git executable from GCM_GPG_PATH: {gpgPath}");
return gpgPath;
}
var format = "GPG executable does not exist with path '{0}'";
var message = string.Format(format, gpgPath);
throw new Trace2Exception(_context.Trace2, message, format);
}
// If no explicit GPG path is specified, mimic the way `pass`
// determines GPG dependency (use gpg2 if available, otherwise gpg)
if (_context.Environment.TryLocateExecutable("gpg2", out string gpg2Path))
{
_context.Trace.WriteLine($"Using PATH-located GPG (gpg2) executable: {gpg2Path}");
return gpg2Path;
}
gpgPath = _context.Environment.LocateExecutable("gpg");
_context.Trace.WriteLine($"Using PATH-located GPG (gpg) executable: {gpgPath}");
return gpgPath;
}
}
}