|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using LibGit2Sharp; |
| 5 | + |
| 6 | +namespace SshCloneTestApp; |
| 7 | + |
| 8 | +/// <summary>The classified result of one clone attempt.</summary> |
| 9 | +public enum Outcome |
| 10 | +{ |
| 11 | + /// <summary>Authentication was rejected — the SSH pipeline worked end to end. PASS.</summary> |
| 12 | + AuthFailure, |
| 13 | + |
| 14 | + /// <summary>The key could not be parsed or its algorithm is unsupported by the backend. FAIL.</summary> |
| 15 | + KeyParseOrUnsupported, |
| 16 | + |
| 17 | + /// <summary>The clone unexpectedly succeeded with a throwaway key. FAIL.</summary> |
| 18 | + UnexpectedSuccess, |
| 19 | + |
| 20 | + /// <summary>The failure matched no known signature. FAIL — needs investigation/calibration.</summary> |
| 21 | + Unknown, |
| 22 | +} |
| 23 | + |
| 24 | +/// <summary>The outcome of a probe plus a human-readable detail string.</summary> |
| 25 | +public sealed record ProbeResult(Outcome Outcome, string Detail) |
| 26 | +{ |
| 27 | + public bool IsPass => Outcome == Outcome.AuthFailure; |
| 28 | +} |
| 29 | + |
| 30 | +/// <summary> |
| 31 | +/// Attempts an SSH clone with an in-memory key and classifies the result. |
| 32 | +/// </summary> |
| 33 | +public static class AuthProbe |
| 34 | +{ |
| 35 | + // Signatures (matched case-insensitively) indicating the key could not be loaded or the |
| 36 | + // algorithm is unsupported by the active crypto backend. CHECKED FIRST, because a |
| 37 | + // backend may wrap a parse failure inside a generic "failed to authenticate" message. |
| 38 | + private static readonly string[] ParseOrUnsupportedSignatures = |
| 39 | + { |
| 40 | + "extract public key", |
| 41 | + "unable to extract", |
| 42 | + "unsupported", |
| 43 | + "unimplemented", |
| 44 | + "invalid privatekey", |
| 45 | + "unable to parse", |
| 46 | + "failed to initialize ssh", |
| 47 | + "could not load", |
| 48 | + "wrong passphrase", |
| 49 | + }; |
| 50 | + |
| 51 | + // Signatures indicating authentication was attempted and rejected (the expected outcome). |
| 52 | + private static readonly string[] AuthFailureSignatures = |
| 53 | + { |
| 54 | + "authentication", |
| 55 | + "authenticate", |
| 56 | + "too many redirects or authentication replays", |
| 57 | + "permission denied", |
| 58 | + "combination invalid", |
| 59 | + "username/publickey", |
| 60 | + "username does not match", |
| 61 | + "callback returned an invalid", |
| 62 | + }; |
| 63 | + |
| 64 | + /// <summary>Clones <paramref name="url"/> with the given in-memory key and classifies the result.</summary> |
| 65 | + public static ProbeResult Probe(string url, SshKeyGenerator.GeneratedKey key) |
| 66 | + { |
| 67 | + var options = new CloneOptions |
| 68 | + { |
| 69 | + FetchOptions = |
| 70 | + { |
| 71 | + CredentialsProvider = (_, userFromUrl, _) => new SshKeyMemoryCredentials |
| 72 | + { |
| 73 | + Username = string.IsNullOrEmpty(userFromUrl) ? "git" : userFromUrl, |
| 74 | + PublicKey = key.PublicKey, |
| 75 | + PrivateKey = key.PrivateKey, |
| 76 | + Passphrase = string.Empty, |
| 77 | + }, |
| 78 | + CertificateCheck = (_, _, _) => true, // accept the host key; part of "the process working" |
| 79 | + }, |
| 80 | + }; |
| 81 | + |
| 82 | + string destination = Path.Combine(Path.GetTempPath(), "octossh-" + Path.GetRandomFileName()); |
| 83 | + |
| 84 | + try |
| 85 | + { |
| 86 | + Repository.Clone(url, destination, options); |
| 87 | + return new ProbeResult(Outcome.UnexpectedSuccess, |
| 88 | + "Clone succeeded with a throwaway key — the key must not be authorized."); |
| 89 | + } |
| 90 | + catch (Exception ex) |
| 91 | + { |
| 92 | + string message = Flatten(ex); |
| 93 | + string lower = message.ToLowerInvariant(); |
| 94 | + |
| 95 | + foreach (var sig in ParseOrUnsupportedSignatures) |
| 96 | + { |
| 97 | + if (lower.Contains(sig)) |
| 98 | + { |
| 99 | + return new ProbeResult(Outcome.KeyParseOrUnsupported, message); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + foreach (var sig in AuthFailureSignatures) |
| 104 | + { |
| 105 | + if (lower.Contains(sig)) |
| 106 | + { |
| 107 | + return new ProbeResult(Outcome.AuthFailure, message); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + return new ProbeResult(Outcome.Unknown, message); |
| 112 | + } |
| 113 | + finally |
| 114 | + { |
| 115 | + TryDelete(destination); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private static string Flatten(Exception ex) |
| 120 | + { |
| 121 | + var parts = new List<string>(); |
| 122 | + for (Exception? e = ex; e != null; e = e.InnerException) |
| 123 | + { |
| 124 | + parts.Add($"{e.GetType().Name}: {e.Message}"); |
| 125 | + } |
| 126 | + return string.Join(" | ", parts); |
| 127 | + } |
| 128 | + |
| 129 | + private static void TryDelete(string path) |
| 130 | + { |
| 131 | + try |
| 132 | + { |
| 133 | + if (Directory.Exists(path)) |
| 134 | + { |
| 135 | + Directory.Delete(path, recursive: true); |
| 136 | + } |
| 137 | + } |
| 138 | + catch |
| 139 | + { |
| 140 | + // best-effort cleanup of the (empty/partial) clone target |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments