TW-52308 add TeamCity version to ssh client version on server
authorDmitry Neverov <dmitry.neverov@gmail.com>
Mon, 4 Dec 2017 11:04:03 +0000 (12:04 +0100)
committerDmitry Neverov <dmitry.neverov@gmail.com>
Mon, 4 Dec 2017 11:04:03 +0000 (12:04 +0100)
git-common/src/jetbrains/buildServer/buildTriggers/vcs/git/GitUtils.java
git-server/src/jetbrains/buildServer/buildTriggers/vcs/git/TransportFactoryImpl.java
git-tests/src/jetbrains/buildServer/buildTriggers/vcs/git/tests/GitUtilsTest.java

index a0472b6a97a9b2a389fa22d38dd23baf82f35f66..75f1ab9ecfc210883fe2fcae2f33ca1aadb8449f 100644 (file)
@@ -39,6 +39,8 @@ import java.util.List;
  * Commands that allows working with git repositories
  */
 public class GitUtils {
+  private static final String SSH_V2 = "SSH-2.0";
+
   /**
    * Convert remote URL to JGIT form
    *
@@ -257,4 +259,13 @@ public class GitUtils {
 
     return res.getStdout().trim();
   }
+
+
+  @NotNull
+  public static String getSshClientVersion(@NotNull String originalSshClientVersion, @NotNull String teamcityVersion) {
+    if (!originalSshClientVersion.startsWith(SSH_V2))
+      return originalSshClientVersion;
+    //rfc4253-4.2
+    return SSH_V2 + "-" + teamcityVersion.replace(' ', '-') + originalSshClientVersion.substring(SSH_V2.length());
+  }
 }
index ad91816e8d8786c75be131eba7b1e84fb5ed6888..eebd7d91c58777d8d4f91f59af2252e63cd765de 100644 (file)
@@ -26,6 +26,8 @@ import jetbrains.buildServer.ssh.TeamCitySshKey;
 import jetbrains.buildServer.ssh.VcsRootSshKeyManager;
 import jetbrains.buildServer.vcs.VcsException;
 import jetbrains.buildServer.vcs.VcsRoot;
+import jetbrains.buildServer.version.ServerVersionHolder;
+import jetbrains.buildServer.version.ServerVersionInfo;
 import org.eclipse.jgit.errors.NotSupportedException;
 import org.eclipse.jgit.errors.TransportException;
 import org.eclipse.jgit.lib.Repository;
@@ -34,6 +36,7 @@ import org.eclipse.jgit.transport.http.HttpConnectionFactory;
 import org.eclipse.jgit.transport.http.apache.HttpClientConnectionFactory;
 import org.eclipse.jgit.util.FS;
 import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
 
 import java.io.File;
 import java.util.*;
@@ -50,7 +53,7 @@ public class TransportFactoryImpl implements TransportFactory {
 
   private final ServerPluginConfig myConfig;
   private final Map<String,String> myJSchOptions;
-  private VcsRootSshKeyManager mySshKeyManager;
+  private final VcsRootSshKeyManager mySshKeyManager;
 
   public TransportFactoryImpl(@NotNull ServerPluginConfig config,
                               @NotNull VcsRootSshKeyManager sshKeyManager) {
@@ -289,6 +292,10 @@ public class TransportFactoryImpl implements TransportFactory {
     protected void configure(OpenSshConfig.Host hc, Session session) {
       super.configure(hc, session);
       session.setConfig("StrictHostKeyChecking", "no");
+      String teamCityVersion = getTeamCityVersion();
+      if (teamCityVersion != null) {
+        session.setClientVersion(GitUtils.getSshClientVersion(session.getClientVersion(), teamCityVersion));
+      }
     }
   }
 
@@ -378,4 +385,13 @@ public class TransportFactoryImpl implements TransportFactory {
     return builder.toString();
   }
 
+  @Nullable
+  private static String getTeamCityVersion() {
+    try {
+      ServerVersionInfo version = ServerVersionHolder.getVersion();
+      return "TeamCity Server " + version.getDisplayVersion();
+    } catch (Exception e) {
+      return null;
+    }
+  }
 }
index 96c248094b5b08cb1f7251d2906449627ddaa66a..a5bce7fcbde05b16a5a95adc307f31417b5cf03a 100644 (file)
@@ -66,4 +66,16 @@ public class GitUtilsTest extends BaseTestCase {
     assertEquals("Short name file content doesn't match", content, FileUtil.readText(new File(shortFileName)));
   }
 
+
+  @Test
+  public void ssh_client_version() {
+    then(GitUtils.getSshClientVersion("SSH-1.0", "whatever"))
+      .isEqualTo("SSH-1.0");
+    then(GitUtils.getSshClientVersion("SSH-2.0", "TeamCity Server 2017.2.1"))
+      .isEqualTo("SSH-2.0-TeamCity-Server-2017.2.1");
+    then(GitUtils.getSshClientVersion("SSH-2.0-LIB-VERSION", "TeamCity-Server-2017.2.1"))
+      .isEqualTo("SSH-2.0-TeamCity-Server-2017.2.1-LIB-VERSION");
+    then(GitUtils.getSshClientVersion("SSH-2.0-LIB-VERSION", "TeamCity Server 2017.2.1 EAP"))
+      .isEqualTo("SSH-2.0-TeamCity-Server-2017.2.1-EAP-LIB-VERSION");
+  }
 }