private Session createSession()

in deploy-runner-agent/src/main/java/jetbrains/buildServer/deployer/agent/ssh/SSHSessionProvider.java [119:177]


  private Session createSession(@NotNull BuildRunnerContext context, @NotNull InternalPropertiesHolder holder) throws JSchException {
    final String username = context.getRunnerParameters().get(DeployerRunnerConstants.PARAM_USERNAME);
    final String password = context.getRunnerParameters().get(DeployerRunnerConstants.PARAM_PASSWORD);
    final String authMethod = context.getRunnerParameters().get(SSHRunnerConstants.PARAM_AUTH_METHOD);
    final String isNativeOpenSSHOnWin = context.getRunnerParameters().get(SSHRunnerConstants.PARAM_AUTH_METHOD);

    JSch jsch = new JSch();
    AgentSshKnownHostsContext sshKnownHostsContext = new AgentSshKnownHostsContext(context.getBuild());
    boolean ignoreKnownHosts = !myKnownHostsManager.isKnownHostsEnabled(sshKnownHostsContext);
    if (!ignoreKnownHosts) {
      String knownHosts = myKnownHostsManager.getKnownHosts(sshKnownHostsContext);
      jsch.setKnownHosts(new ByteArrayInputStream(knownHosts.getBytes()));
    }


    myLog.debug("Initializing ssh session.");
    if (SSHRunnerConstants.AUTH_METHOD_DEFAULT_KEY.equals(authMethod)) {
      final String configPath = holder.getInternalProperty(TEAMCITY_DEPLOYER_SSH_CONFIG_PATH, System.getProperty("user.home") + File.separator + ".ssh" + File.separator + "config");
      //noinspection ConstantConditions
      final File config = new File(configPath);
      if (config.exists()) {
        myLog.debug("Found config at [" + config.getAbsolutePath() + "], reading.");
        return applyKnownHostsConfig(initSessionSSHConfig(username, jsch, config), ignoreKnownHosts);
      } else {
        final String keyPath = holder.getInternalProperty(TEAMCITY_DEPLOYER_SSH_DEFAULT_KEY, myDefaultKeyPath);
        //noinspection ConstantConditions
        final File keyFile = new File(keyPath);
        myLog.debug("Using keyfile at [" + keyFile.getAbsolutePath() + "], load.");
        return applyKnownHostsConfig(initSessionKeyFile(username, password, keyFile, jsch), ignoreKnownHosts);
      }
    } else if (SSHRunnerConstants.AUTH_METHOD_CUSTOM_KEY.equals(authMethod)) {
      String keyFilePath = context.getRunnerParameters().get(SSHRunnerConstants.PARAM_KEYFILE);
      if (StringUtil.isEmpty(keyFilePath)) {
        keyFilePath = myDefaultKeyPath;
      }
      final File keyFile = FileUtil.resolvePath(context.getBuild().getCheckoutDirectory(), keyFilePath);
      myLog.debug("Using keyfile at [" + keyFile.getAbsolutePath() + "], load.");
      return applyKnownHostsConfig(initSessionKeyFile(username, password, keyFile, jsch), ignoreKnownHosts);

    } else if (SSHRunnerConstants.AUTH_METHOD_SSH_AGENT.equals(authMethod)) {
      final ProcessingResult result = context.getParametersResolver().resolve("%env.SSH_AUTH_SOCK%");
      String socketPath = null;
      if (result.isFullyResolved()) {
        socketPath = result.getResult();
      }
      return applyKnownHostsConfig(initSessionSshAgent(username, socketPath, jsch), ignoreKnownHosts);
    } else if (SSHRunnerConstants.AUTH_METHOD_UPLOADED_KEY.equals(authMethod)) {

      final String keyId = context.getRunnerParameters().get("teamcitySshKey");
      if (StringUtil.isEmptyOrSpaces(keyId)) {
        throw new JSchException("SSH key is not specified");
      }
      return applyKnownHostsConfig(initSessionUploadedKey(username, password, keyId, jsch), ignoreKnownHosts);

    } else {
      myLog.debug("Using provided username/password");
      return applyKnownHostsConfig(initSessionUserPassword(username, password, jsch), ignoreKnownHosts);
    }
  }