public SshHandler()

in git-agent/src/main/java/jetbrains/buildServer/buildTriggers/vcs/git/agent/command/impl/SshHandler.java [55:151]


  public SshHandler(@NotNull GitSSHService ssh,
                    @Nullable VcsRootSshKeyManager sshKeyManager,
                    @NotNull AuthSettings authSettings,
                    @NotNull AgentGitCommandLine cmd,
                    @NotNull Context ctx) throws VcsException {
    mySsh = ssh;
    myAuthSettings = authSettings;
    cmd.addEnvParam(GitSSHHandler.SSH_PORT_ENV, Integer.toString(mySsh.getXmlRcpPort()));
    if (ctx.knownHostsEnabled()) {
      String knownHostsFromParam = ctx.getSshKnownHosts(authSettings);
      if (knownHostsFromParam != null) {
        try {
          File knownHostsFile = FileUtil.createTempFile(ctx.getTempDir(), "known_hosts", "", true);
          try (FileWriter writer = new FileWriter(knownHostsFile)) {
            writer.write(knownHostsFromParam);
          }
          cmd.addEnvParam(GitSSHHandler.SSH_KNOWN_HOSTS_FILE, knownHostsFile.getAbsolutePath());
          myFilesToClean.add(knownHostsFile);
        } catch (IOException e) {
          throw new VcsException("SSH script cannot be generated: " + e.getMessage(), e);
        }

      }
    } else {
      if (myAuthSettings.isIgnoreKnownHosts()) {
        cmd.addEnvParam(GitSSHHandler.SSH_IGNORE_KNOWN_HOSTS_ENV, "true");
      }
    }

    if (authSettings.getAuthMethod() == AuthenticationMethod.TEAMCITY_SSH_KEY) {
      String keyId = authSettings.getTeamCitySshKeyId();
      if (keyId != null && sshKeyManager != null) {
        VcsRoot root = myAuthSettings.getRoot();
        if (root != null) {
          TeamCitySshKey key = sshKeyManager.getKey(root);
          if (key != null) {
            try {
              File privateKey = FileUtil.createTempFile(ctx.getTempDir(), "key", "", true);
              myFilesToClean.add(privateKey);
              FileUtil.writeFileAndReportErrors(privateKey, new String(key.getPrivateKey()));
              cmd.addEnvParam(GitSSHHandler.TEAMCITY_PRIVATE_KEY_PATH, privateKey.getCanonicalPath());
              String passphrase = myAuthSettings.getPassphrase();
              cmd.addEnvParam(GitSSHHandler.TEAMCITY_PASSPHRASE, passphrase != null ? passphrase : "");
            } catch (Exception e) {
              deleteKeys();
              throw new VcsException(e);
            }
          }
        }
      }
    }
    if (ctx.getSshMacType() != null)
      cmd.addEnvParam(GitSSHHandler.TEAMCITY_SSH_MAC_TYPE, ctx.getSshMacType());
    if (ctx.getPreferredSshAuthMethods() != null)
      cmd.addEnvParam(GitSSHHandler.TEAMCITY_SSH_PREFERRED_AUTH_METHODS, ctx.getPreferredSshAuthMethods());
    cmd.addEnvParam(GitSSHHandler.TEAMCITY_DEBUG_SSH, String.valueOf(ctx.isDebugSsh()));
    cmd.addEnvParam(GitSSHHandler.TEAMCITY_SSH_IDLE_TIMEOUT_SECONDS, String.valueOf(ctx.getIdleTimeoutSeconds()));
    cmd.addEnvParam(GitSSHHandler.TEAMCITY_SSH_CONNECT_TIMEOUT_SECONDS, String.valueOf(ctx.getSshConnectTimeoutSeconds()));
    String teamCityVersion = getTeamCityVersion();
    if (teamCityVersion != null) {
      cmd.addEnvParam(GitSSHHandler.TEAMCITY_VERSION, teamCityVersion);
    }

    try {
      File intPropsFile = FileUtil.createTempFile(ctx.getTempDir(), "int_props", "", true);
      Properties props = new Properties();
      props.putAll(TeamCityProperties.getPropertiesWithPrefix("teamcity."));
      try (FileWriter fw = new FileWriter(intPropsFile)) {
        props.store(fw, "Internal properties");
      }
      cmd.addEnvParam(GitSSHHandler.TEAMCITY_INT_PROPS_PATH, intPropsFile.getAbsolutePath());
      myFilesToClean.add(intPropsFile);
    } catch (IOException e) {
      //
    }

    try {

      boolean shouldUseSshCommandEnv = !ctx.getGitVersion().isLessThan(MIN_GIT_SSH_COMMAND) && ctx.getBooleanInternalProperty(USE_SSH_COMMAND_ENV_INTERNAL_PROPERTY, true);
      String path = ssh.getScriptPath();
      if (SystemInfo.isWindows && shouldUseSshCommandEnv) {
        // replace the backslashes because they don't work with GIT_SSH_COMMAND env param on windows(but they work with GIT_SSH)
        path = path.replaceAll("\\\\", "/");
      }
      cmd.addEnvParam(shouldUseSshCommandEnv ? GitSSHHandler.GIT_SSH_COMMAND_ENV : GitSSHHandler.GIT_SSH_ENV, path);
      // ask git to treat our command as OpenSSH compatible:
      cmd.addEnvParam(GitSSHHandler.GIT_SSH_VARIANT_ENV, "ssh");
    } catch (IOException e) {
      deleteKeys();
      throw new VcsException("SSH script cannot be generated: " + e.getMessage(), e);
    }

    final String sendEnv = ctx.getSshRequestToken();
    if (StringUtil.isNotEmpty(sendEnv)) {
      cmd.addEnvParam(GitSSHHandler.TEAMCITY_SSH_REQUEST_TOKEN, sendEnv);
    }
  }