Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions GVFS/GVFS.Common/Git/GitAuthentication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ namespace GVFS.Common.Git
public class GitAuthentication
{
private const double MaxBackoffSeconds = 30;
private const int DefaultCredentialTimeoutMs = 30_000;

private readonly Lock gitAuthLock = new Lock();
private readonly ICredentialStore credentialStore;
private readonly GitProcess gitProcess;
private readonly string repoUrl;

private int numberOfAttempts = 0;
Expand All @@ -29,6 +31,7 @@ public class GitAuthentication
public GitAuthentication(GitProcess git, string repoUrl)
{
this.credentialStore = git;
this.gitProcess = git;
this.repoUrl = repoUrl;

if (git.TryGetConfigUrlMatch("http", this.repoUrl, out Dictionary<string, GitConfigSetting> configSettings))
Expand Down Expand Up @@ -213,14 +216,22 @@ public bool TryInitialize(ITracer tracer, Enlistment enlistment, out string erro
/// This saves one HTTP request compared to probing auth separately
/// and then querying config, and reuses the same TCP/TLS connection.
/// </summary>
/// <param name="skipRetries">
/// When true, config queries use a single attempt instead of the full
/// retry loop. Use this when a cache server is already configured
/// locally — mount can proceed without config on first failure rather
/// than blocking on retries that will be masked by the fallback.
/// </param>
public bool TryInitializeAndQueryGVFSConfig(
ITracer tracer,
Enlistment enlistment,
RetryConfig retryConfig,
out ServerGVFSConfig serverGVFSConfig,
out string errorMessage,
out bool isAuthFailure,
Action<string> updateProgress = null)
Action<string> updateProgress = null,
bool skipRetries = false,
int credentialTimeoutMs = DefaultCredentialTimeoutMs)
{
if (this.isInitialized)
{
Expand All @@ -231,7 +242,11 @@ public bool TryInitializeAndQueryGVFSConfig(
errorMessage = null;
isAuthFailure = false;

using (ConfigHttpRequestor configRequestor = new ConfigHttpRequestor(tracer, enlistment, retryConfig))
RetryConfig effectiveRetryConfig = skipRetries
? new RetryConfig(maxRetries: 0, retryConfig.Timeout)
: retryConfig;

using (ConfigHttpRequestor configRequestor = new ConfigHttpRequestor(tracer, enlistment, effectiveRetryConfig))
{
HttpStatusCode? httpStatus;

Expand All @@ -256,7 +271,7 @@ public bool TryInitializeAndQueryGVFSConfig(
this.IsAnonymous = false;
updateProgress?.Invoke("Fetching credentials");

if (!this.TryCallGitCredential(tracer, out errorMessage))
if (!this.TryCallGitCredential(tracer, out errorMessage, credentialTimeoutMs))
{
isAuthFailure = true;
updateProgress?.Invoke("Credential fetch failed: " + errorMessage);
Expand Down Expand Up @@ -379,11 +394,11 @@ private void UpdateBackoff()
this.numberOfAttempts++;
}

private bool TryCallGitCredential(ITracer tracer, out string errorMessage)
private bool TryCallGitCredential(ITracer tracer, out string errorMessage, int timeoutMs = -1)
{
string gitUsername;
string gitPassword;
if (!this.credentialStore.TryGetCredential(tracer, this.repoUrl, out gitUsername, out gitPassword, out errorMessage))
if (!this.gitProcess.TryGetCredential(tracer, this.repoUrl, out gitUsername, out gitPassword, out errorMessage, timeoutMs))
{
this.UpdateBackoff();
return false;
Expand Down
41 changes: 33 additions & 8 deletions GVFS/GVFS.Common/Git/GitProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,17 @@ public virtual bool TryGetCredential(
out string username,
out string password,
out string errorMessage)
{
return this.TryGetCredential(tracer, repoUrl, out username, out password, out errorMessage, timeoutMs: -1);
}

public virtual bool TryGetCredential(
ITracer tracer,
string repoUrl,
out string username,
out string password,
out string errorMessage,
int timeoutMs)
{
username = null;
password = null;
Expand All @@ -311,16 +322,29 @@ public virtual bool TryGetCredential(
GenerateCredentialVerbCommand("fill"),
stdin => stdin.Write($"url={repoUrl}\n\n"),
parseStdOutLine: null,
usePreCommandHook: false);
usePreCommandHook: false,
timeoutMs: timeoutMs);

if (gitCredentialOutput.ExitCodeIsFailure)
{
EventMetadata errorData = new EventMetadata();
tracer.RelatedWarning(
errorData,
"Git could not get credentials: " + gitCredentialOutput.Errors,
Keywords.Network | Keywords.Telemetry);
errorMessage = gitCredentialOutput.Errors;

if (gitCredentialOutput.Errors.StartsWith("Operation timed out"))
{
errorMessage = "Credential manager did not respond within " + (timeoutMs / 1000) + " seconds";
tracer.RelatedWarning(
errorData,
"Git credential fill timed out after " + timeoutMs + "ms",
Keywords.Network | Keywords.Telemetry);
}
else
{
errorMessage = gitCredentialOutput.Errors;
tracer.RelatedWarning(
errorData,
"Git could not get credentials: " + gitCredentialOutput.Errors,
Keywords.Network | Keywords.Telemetry);
}

return false;
}
Expand Down Expand Up @@ -1113,7 +1137,8 @@ private Result InvokeGitAgainstDotGitFolder(
Action<StreamWriter> writeStdIn,
Action<string> parseStdOutLine,
bool usePreCommandHook = true,
string gitObjectsDirectory = null)
string gitObjectsDirectory = null,
int timeoutMs = -1)
{
// This git command should not need/use the working directory of the repo.
// Run git.exe in Environment.SystemDirectory to ensure the git.exe process
Expand All @@ -1125,7 +1150,7 @@ private Result InvokeGitAgainstDotGitFolder(
useReadObjectHook: false,
writeStdIn: writeStdIn,
parseStdOutLine: parseStdOutLine,
timeoutMs: -1,
timeoutMs: timeoutMs,
gitObjectsDirectory: gitObjectsDirectory,
usePreCommandHook: usePreCommandHook);
}
Expand Down
2 changes: 2 additions & 0 deletions GVFS/GVFS.Common/ReturnCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ public enum ReturnCode
DehydrateFolderFailures = 7,
MountAlreadyRunning = 8,
AuthenticationError = 9,
CredentialTimeout = 10,
NetworkError = 11,
}
}
16 changes: 13 additions & 3 deletions GVFS/GVFS.Mount/InProcessMount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ private void MountWithLockAcquired(EventLevel verbosity, Keywords keywords)
// and config query into at most 2 HTTP requests (1 for anonymous repos), reusing
// the same HttpClient/TCP connection.
Stopwatch parallelTimer = Stopwatch.StartNew();
bool hasCacheServer = this.cacheServer != null && !string.IsNullOrWhiteSpace(this.cacheServer.Url);

var networkTask = Task.Run(() =>
{
Expand All @@ -140,17 +141,26 @@ private void MountWithLockAcquired(EventLevel verbosity, Keywords keywords)
if (!this.enlistment.Authentication.TryInitializeAndQueryGVFSConfig(
this.tracer, this.enlistment, this.retryConfig,
out config, out authConfigError, out isAuthFailure,
updateProgress: message => this.mountProgressMessage = message))
updateProgress: message => this.mountProgressMessage = message,
skipRetries: hasCacheServer))
{
if (this.cacheServer != null && !string.IsNullOrWhiteSpace(this.cacheServer.Url))
if (hasCacheServer)
{
this.tracer.RelatedWarning("Mount will proceed with fallback cache server: " + authConfigError);
config = null;
}
else
{
ReturnCode exitCode = ReturnCode.NetworkError;
if (isAuthFailure)
{
exitCode = authConfigError != null && authConfigError.Contains("Credential manager did not respond")
? ReturnCode.CredentialTimeout
: ReturnCode.AuthenticationError;
}

this.FailMountAndExit(
isAuthFailure ? ReturnCode.AuthenticationError : ReturnCode.GenericError,
exitCode,
"Unable to query /gvfs/config" + Environment.NewLine + authConfigError);
}
}
Expand Down