Java Code Examples for org.apache.sshd.common.util.OsUtils#isUNIX()

The following examples show how to use org.apache.sshd.common.util.OsUtils#isUNIX() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GitReadSaveTest.java    From blueocean-plugin with MIT License 5 votes vote down vote up
@Test
public void bareRepoReadWriteOverSSH() throws Exception {
    if (!OsUtils.isUNIX()) {
        return; // can't really run this on windows
    }
    startSSH();
    String userHostPort = "[email protected]:" + sshd.getPort();
    String remote = "ssh://" + userHostPort + "" + repoForSSH.getRoot().getCanonicalPath() + "/.git";
    testGitReadWrite(GitReadSaveService.ReadSaveType.CACHE_BARE, remote, repoForSSH, masterPipelineScript);
}
 
Example 2
Source File: GitReadSaveTest.java    From blueocean-plugin with MIT License 5 votes vote down vote up
@Test
public void bareRepoReadWriteNoEmail() throws Exception {
    if (!OsUtils.isUNIX()) {
        return; // can't really run this on windows
    }
    User user = login("bob", "Bob Smith", null);

    startSSH(user);
    String userHostPort = "[email protected]:" + sshd.getPort();
    String remote = "ssh://" + userHostPort + "" + repoForSSH.getRoot().getCanonicalPath() + "/.git";
    testGitReadWrite(GitReadSaveService.ReadSaveType.CACHE_BARE, remote, repoForSSH, masterPipelineScript, user);
}
 
Example 3
Source File: NodesRecoveryProcessHelper.java    From scheduling with GNU Affero General Public License v3.0 5 votes vote down vote up
private static String buildJpsCommand() {
    OperatingSystem os;
    String jpsCommandPath;
    if (OsUtils.isWin32()) {
        os = OperatingSystem.WINDOWS;
        jpsCommandPath = "\"" + System.getProperty("java.home") + os.fs + ".." + os.fs + "bin" + os.fs +
                         "jps.exe\"";
    } else if (OsUtils.isUNIX()) {
        os = OperatingSystem.UNIX;
        jpsCommandPath = System.getProperty("java.home") + os.fs + ".." + os.fs + "bin" + os.fs + "jps";
    } else {
        throw new IllegalStateException("Operating system is not recognized");
    }
    return jpsCommandPath;
}
 
Example 4
Source File: SshdProxySettings.java    From artifactory_ssh_proxy with Apache License 2.0 5 votes vote down vote up
@Override
public Factory<Command> getShellFactory() {
    EnumSet<TtyOptions> ttyOptions;

    if (OsUtils.isUNIX()) {
        /**
         * org.apache.sshd.server.shell.ProcessShellFactory does this: ttyOptions = EnumSet.of(TtyOptions.ONlCr);
         * 
         * However, it doesn't seem to work for me. So in our copy of
         * org.apache.sshd.server.shell.TtyFilterOutputStream.TtyFilterOutputStream(EnumSet<TtyOptions>,
         * OutputStream, TtyFilterInputStream), we have a special hack that if TtyOptions.INlCr and TtyOptions.ICrNl
         * are both set, send cr nl instead. no idea if the windows even works.
         */
        // ttyOptions = EnumSet.of(TtyOptions.ONlCr);
        ttyOptions = EnumSet.of(TtyOptions.OCrNl, TtyOptions.INlCr, TtyOptions.ICrNl);
    } else {
        ttyOptions = EnumSet.of(TtyOptions.Echo, TtyOptions.OCrNl, TtyOptions.INlCr, TtyOptions.ICrNl);
    }

    switch (shellMode) {
        case FORWARDING_ECHO_SHELL:
            return new ForwardingShellFactory(ttyOptions);

        case GROOVY_SHELL:
            return new GroovyShellFactory(ttyOptions);

        case MESSAGE:
        default:
            // TODO when separating out settings, we'll provide a different success
            // message, or a file path for it.
            return new MessageShellFactory(SshProxyMessage.MESSAGE_STRING);
    }
}
 
Example 5
Source File: GitReadSaveTest.java    From blueocean-plugin with MIT License 4 votes vote down vote up
@Test
public void testGitScmValidate() throws Exception {
    if (!OsUtils.isUNIX()) {
        return; // can't really run this on windows
    }
    startSSH();
    String userHostPort = "[email protected]:" + sshd.getPort();
    String remote = "ssh://" + userHostPort + "" + repoForSSH.getRoot().getCanonicalPath();

    User bob = login();

    // Validate bob via repositoryUrl
    Map r = new RequestBuilder(baseUrl)
        .status(200)
        .crumb( crumb )
        .jwtToken(getJwtToken(j.jenkins, bob.getId(), bob.getId()))
        .put("/organizations/" + getOrgName() + "/scm/git/validate/")
        .data(ImmutableMap.of(
            "repositoryUrl", remote,
            "credentialId", UserSSHKeyManager.getOrCreate(bob).getId()
        )).build(Map.class);

    assertTrue(r.get("error") == null);

    // Create a job
    String jobName = "test-token-validation";
    r = new RequestBuilder(baseUrl)
        .status(201)
        .crumb( crumb )
        .jwtToken(getJwtToken(j.jenkins, bob.getId(), bob.getId()))
        .post("/organizations/" + getOrgName() + "/pipelines/")
        .data(ImmutableMap.of(
            "name", jobName,
            "$class", "io.jenkins.blueocean.blueocean_git_pipeline.GitPipelineCreateRequest",
            "scmConfig", ImmutableMap.of(
                "uri", remote,
                "credentialId", UserSSHKeyManager.getOrCreate(bob).getId())
        )).build(Map.class);

    assertEquals(jobName, r.get("name"));

    // Test for existing pipeline/job
    r = new RequestBuilder(baseUrl)
        .status(200)
        .crumb( crumb )
        .jwtToken(getJwtToken(j.jenkins, bob.getId(), bob.getId()))
        .put("/organizations/" + getOrgName() + "/scm/git/validate/")
        .data(ImmutableMap.of(
            "pipeline", ImmutableMap.of("fullName", jobName),
            "credentialId", UserSSHKeyManager.getOrCreate(bob).getId()
        )).build(Map.class);

    User alice = login("alice", "Alice Cooper", "[email protected]");

    // Test alice fails
    r = new RequestBuilder(baseUrl)
        .status(428)
        .crumb( crumb )
        .jwtToken(getJwtToken(j.jenkins, alice.getId(), alice.getId()))
        .put("/organizations/" + getOrgName() + "/scm/git/validate/")
        .data(ImmutableMap.of(
            "repositoryUrl", remote,
            "credentialId", UserSSHKeyManager.getOrCreate(alice).getId()
        )).build(Map.class);

    r = new RequestBuilder(baseUrl)
        .status(428)
        .crumb( crumb )
        .jwtToken(getJwtToken(j.jenkins, alice.getId(), alice.getId()))
        .put("/organizations/" + getOrgName() + "/scm/git/validate/")
        .data(ImmutableMap.of(
            "pipeline", ImmutableMap.of("fullName", jobName),
            "credentialId", UserSSHKeyManager.getOrCreate(alice).getId()
        )).build(Map.class);
}
 
Example 6
Source File: TestSSHInfrastructureV2.java    From scheduling with GNU Affero General Public License v3.0 4 votes vote down vote up
@BeforeClass
public static void startSSHServer() throws Exception {
    // Disable bouncy castle to avoid versions conflict
    System.setProperty("org.apache.sshd.registerBouncyCastle", "false");

    sshd = SshServer.setUpDefaultServer();

    SimpleGeneratorHostKeyProvider keyProvider = new SimpleGeneratorHostKeyProvider();
    keyProvider.setAlgorithm("RSA");
    sshd.setKeyPairProvider(keyProvider);

    List<NamedFactory<UserAuth>> userAuthFactories = new ArrayList<>(1);
    userAuthFactories.add(new UserAuthPasswordFactory());
    sshd.setUserAuthFactories(userAuthFactories);

    sshd.setPasswordAuthenticator(new PasswordAuthenticator() {
        @Override
        public boolean authenticate(String username, String password, ServerSession session) {
            return username != null && username.equals(password);
        }
    });

    CommandFactory cf = new CommandFactory() {
        @Override
        public Command createCommand(String command) {
            String[] splitCommand;
            if (OsUtils.isUNIX()) {
                splitCommand = SSHInfrastructureHelper.splitCommand(command);
            } else if (OsUtils.isWin32()) {
                splitCommand = SSHInfrastructureHelper.splitCommandWithoutRemovingQuotes(command);
            } else {
                throw new IllegalStateException("Operating system is not recognized");
            }
            StringBuilder rebuiltCommand = new StringBuilder();
            for (String commandPiece : splitCommand) {
                rebuiltCommand.append(commandPiece).append(" ");
            }
            rebuiltCommand.trimToSize();

            if (OsUtils.isUNIX()) {
                return new ProcessShellFactory(new String[] { "/bin/sh", "-c",
                                                              rebuiltCommand.toString() }).create();
            } else {
                return new ProcessShellFactory(new String[] { "cmd.exe", "/C",
                                                              rebuiltCommand.toString() }).create();
            }
        }
    };

    sshd.setCommandFactory(cf);

    sshd.start();

    port = sshd.getPort();

    javaExePath = System.getProperty("java.home") + File.separator + "bin" + File.separator +
                  (OsUtils.isWin32() ? "java.exe" : "java");
    javaExePath = "\"" + javaExePath + "\"";

    infraParams = new Object[] { ("localhost " + NB_NODES + "\n").getBytes(), //hosts
                                 60000, //timeout
                                 0, //attempts
                                 10, //wait between failures
                                 port, //ssh server port
                                 "toto", //ssh username
                                 "toto", //ssh password
                                 new byte[0], // optional ssh private key
                                 new byte[0], // optional ssh options file
                                 javaExePath, //java path on the remote machines
                                 PAResourceManagerProperties.RM_HOME.getValueAsString(), //Scheduling path on remote machines
                                 OperatingSystem.getOperatingSystem(), "" }; // extra java options

    policyParameters = new Object[] { AccessType.ALL.toString(), AccessType.ALL.toString(), "20000" };

}