Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ namespace Calamari.ArgoCD.Git;

public static class LibGit2SharpCredentialsHandlerExtensionMethods
{
public static CredentialsHandler ToLibGit2SharpCredentialHandler(this IGitConnection? connection)
public static CredentialsHandler ToLibGit2SharpCredentialHandler(this IGitConnection? connection, ILog log)
{
return connection switch
{
HttpsGitConnection { Username: null, Password: null } => Anonymous(),
HttpsGitConnection https => UsernamePassword(https),
SshKeyGitConnection sshKey => SshKey(sshKey),
SshKeyGitConnection sshKey => SshKey(sshKey, log),
null => Anonymous(),
_ => throw new NotSupportedException(),
};
Expand All @@ -34,10 +34,11 @@ static CredentialsHandler Anonymous()
return null!; // A null CredentialsHandler is valid for LibGit2Sharp
}

static CredentialsHandler SshKey(SshKeyGitConnection connection)
static CredentialsHandler SshKey(SshKeyGitConnection connection, ILog log)
{
return (_, userFromUrl, types) =>
{
log.Verbose($"Handling SSH key credentials for {connection.Username}");
if (!types.HasFlag(SupportedCredentialTypes.SshMemory))
{
throw new InvalidOperationException("SSH key credentials provided but are not supported by this endpoint.");
Expand Down
2 changes: 1 addition & 1 deletion source/Calamari/ArgoCD/Git/RepositoryFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ RepositoryWrapper CheckoutGitRepository(IGitConnection gitConnection, string che
BranchName = (gitConnection.GitReference as GitBranchName)?.ToFriendlyName()
};

options.FetchOptions.CredentialsProvider = gitConnection.ToLibGit2SharpCredentialHandler();
options.FetchOptions.CredentialsProvider = gitConnection.ToLibGit2SharpCredentialHandler(log);
options.FetchOptions.CertificateCheck = gitConnection.ToLibGit2SharpCertificateCheckHandler(log);

string repoPath;
Expand Down
4 changes: 2 additions & 2 deletions source/Calamari/ArgoCD/Git/RepositoryWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public void PushChanges(GitBranchName branchName)
PushStatusError? errorsDetected = null;
var pushOptions = new PushOptions
{
CredentialsProvider = connection.ToLibGit2SharpCredentialHandler(),
CredentialsProvider = connection.ToLibGit2SharpCredentialHandler(log),
OnPushStatusError = errors => errorsDetected = errors,
CertificateCheck = connection.ToLibGit2SharpCertificateCheckHandler(log)
};
Expand All @@ -209,7 +209,7 @@ void FetchAndRebase(GitBranchName branchName)
var refSpecs = remote.FetchRefSpecs.Select(x => x.Specification).ToList();
var fetchOptions = new FetchOptions
{
CredentialsProvider = connection.ToLibGit2SharpCredentialHandler(),
CredentialsProvider = connection.ToLibGit2SharpCredentialHandler(log),
CertificateCheck = connection.ToLibGit2SharpCertificateCheckHandler(log)
};

Expand Down