-
Notifications
You must be signed in to change notification settings - Fork 117
Add support for ssh git credentials #1917
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
219fb83
9e80fee
5dcdc68
b27f13d
b254337
a4ff1dd
9b72271
1439f6a
eaff4ff
725c9d5
cc856e0
d92b5ed
2cd2cce
7c8b7af
d18524f
778cbea
b06b2be
757001a
68b8a2d
d434151
22afdad
ee32c01
8cf57a0
193b3b0
4dd5cbe
cb5907c
2974315
df74be6
1918b75
0a0e91b
2a2a169
323d0a6
2af45f9
dee1ef1
249f725
5063c23
d66d72b
2ae0de7
9f1334d
9308011
0c3b11e
e1170ab
03c1cf1
eda90b4
46b6f7b
64b36f0
c0e00c8
18c997a
9512a96
ee1837c
eec8ba7
aebbbaa
9a1154a
619d6e2
af56772
d74e47f
8868697
4ce68bc
c906c25
cfdf35d
82fc102
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using Calamari.Common.Plumbing.Logging; | ||
| using Octopus.Calamari.Contracts.ArgoCD; | ||
|
|
||
|
|
@@ -11,25 +13,50 @@ public class AuthenticatingRepositoryFactory | |
| readonly ILog log; | ||
|
|
||
| public AuthenticatingRepositoryFactory( | ||
| Dictionary<string, IGitCredentialDto> gitCredentials, | ||
| IReadOnlyCollection<IGitCredentialDto> gitCredentials, | ||
| IRepositoryFactory repositoryFactory, | ||
| ILog log) | ||
| { | ||
| this.gitCredentials = gitCredentials; | ||
| // Takes the first git credential per URL, with a preference for username/password credentials (they are more broadly useful as they can be used for PR creation) | ||
| this.gitCredentials = gitCredentials | ||
| .GroupBy(c => c.Url) | ||
| .ToDictionary(g => g.Key, g => g.OfType<GitCredentialDto>().FirstOrDefault<IGitCredentialDto>() ?? g.First()); | ||
|
Comment on lines
+21
to
+23
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we actually preferencing here like the comment suggests?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah we are So the logic is: For each url, get the username password cred. If we don't find that, get any cred (ie. ssh). This is going to change with https://linear.app/octopus/issue/MD-1898/match-ssh-keys-to-ssh-urls-but-also-pass-in-usernamepasswords-for-api, but it matches the logic in server at the moment. Probably also worth noting that server shouldn't be sending down multiple creds at the moment, but keeping this logic in place allows us to have confidence that the credential will be selected deterministically if we do start sending multiple credentials per url to calamari. |
||
|
|
||
| this.repositoryFactory = repositoryFactory; | ||
| this.log = log; | ||
| } | ||
|
|
||
| public RepositoryWrapper CloneRepository(string requestedUrl, string targetRevision) | ||
| { | ||
| var gitCredential = gitCredentials.GetValueOrDefault(requestedUrl); | ||
| if (gitCredential is GitCredentialDto passwordCredential) | ||
| switch (gitCredential) | ||
| { | ||
| var gitConnection = new HttpsGitConnection(passwordCredential.Username, passwordCredential.Password, GitCloneSafeUrl.ConvertToUriString(requestedUrl), GitReference.CreateFromString(targetRevision)); | ||
| return repositoryFactory.CloneRepository(UniqueRepoNameGenerator.Generate(), gitConnection); | ||
| case GitCredentialDto passwordCredential: | ||
| { | ||
| var gitConnection = new HttpsGitConnection(passwordCredential.Username, passwordCredential.Password, GitCloneSafeUrl.ConvertToUriString(requestedUrl), GitReference.CreateFromString(targetRevision)); | ||
| return repositoryFactory.CloneRepository(UniqueRepoNameGenerator.Generate(), gitConnection); | ||
| } | ||
| case SshKeyGitCredentialDto sshCredential: | ||
| { | ||
| var sshConnection = new SshKeyGitConnection( | ||
| sshCredential.Username, | ||
| sshCredential.PrivateKey, | ||
| requestedUrl, | ||
| GitReference.CreateFromString(targetRevision)); | ||
| return repositoryFactory.CloneRepository(UniqueRepoNameGenerator.Generate(), sshConnection); | ||
| } | ||
| case null: | ||
| { | ||
| log.Info($"No Git credentials found for: '{requestedUrl}', will attempt to clone repository anonymously."); | ||
| break; | ||
| } | ||
| default: | ||
| { | ||
| log.Warn($"An unrecognised credential type '{gitCredential.GetType().Name}' was found for '{requestedUrl}'. Ignoring the credentials and attempting an anonymous clone."); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| log.Info($"No Git credentials found for: '{requestedUrl}', will attempt to clone repository anonymously."); | ||
| var anonGitConnection = new HttpsGitConnection(null, null, GitCloneSafeUrl.ConvertToUriString(requestedUrl), GitReference.CreateFromString(targetRevision)); | ||
| return repositoryFactory.CloneRepository(UniqueRepoNameGenerator.Generate(), anonGitConnection); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We enter git usage in a few weird places in the tests, so for now I've just called this method so we get decent errors if the test runner doesn't meet the dependencies.
Normally this gets done in the repo wrapper and it's a bit cleaner.