use of org.apache.sshd.server.command.Command in project gerrit by GerritCodeReview.
the class DispatchCommand method start.
@Override
public void start(ChannelSession channel, Environment env) throws IOException {
try (DynamicOptions pluginOptions = new DynamicOptions(injector, dynamicBeans)) {
parseCommandLine(pluginOptions);
if (Strings.isNullOrEmpty(commandName)) {
StringWriter msg = new StringWriter();
msg.write(usage());
throw die(msg.toString());
}
final CommandProvider p = commands.get(commandName);
if (p == null) {
String msg = (getName().isEmpty() ? "Gerrit Code Review" : getName()) + ": " + commandName + ": not found";
throw die(msg);
}
final Command cmd = p.getProvider().get();
checkRequiresCapability(cmd);
String actualCommandName = commandName;
if (cmd instanceof BaseCommand) {
final BaseCommand bc = (BaseCommand) cmd;
if (!getName().isEmpty()) {
actualCommandName = getName() + " " + commandName;
}
bc.setName(actualCommandName);
bc.setArguments(args.toArray(new String[args.size()]));
} else if (!args.isEmpty()) {
throw die(commandName + " does not take arguments");
}
for (SshExecuteCommandInterceptor commandInterceptor : commandInterceptors) {
if (!commandInterceptor.accept(actualCommandName, args)) {
throw new UnloggedFailure(126, String.format("blocked by %s, contact gerrit administrators for more details", commandInterceptor.name()));
}
}
provideStateTo(cmd);
atomicCmd.set(cmd);
cmd.start(channel, env);
} catch (UnloggedFailure e) {
String msg = e.getMessage();
if (!msg.endsWith("\n")) {
msg += "\n";
}
err.write(msg.getBytes(ENC));
err.flush();
onExit(e.exitCode);
}
}
use of org.apache.sshd.server.command.Command in project testcases by coheigea.
the class SSHTest method setup.
@Before
public void setup() throws Exception {
int port = AvailablePortFinder.getNextAvailable(10000);
// Write port number to configuration file in target
String basedir = System.getProperty("basedir");
if (basedir == null) {
basedir = new File(".").getCanonicalPath();
}
// Read in camel configuration file and substitute in the correct port
File f = new File(basedir + "/src/test/resources/camel-ssh.xml");
FileInputStream inputStream = new FileInputStream(f);
String content = IOUtils.toString(inputStream, "UTF-8");
inputStream.close();
content = content.replaceAll("portno", "" + port);
File f2 = new File(basedir + "/target/test-classes/camel-ssh.xml");
FileOutputStream outputStream = new FileOutputStream(f2);
IOUtils.write(content, outputStream, "UTF-8");
outputStream.close();
sshServer = SshServer.setUpDefaultServer();
sshServer.setPort(port);
// Generate a key
sshServer.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(Paths.get("target/generatedkey.pem")));
// Simple CommandFactory to run commands in a process
sshServer.setCommandFactory(new CommandFactory() {
public Command createCommand(String command) {
return new ProcessShellFactory(command.split(";")).create();
}
});
// sshServer.setShellFactory(new ProcessShellFactory(new String[] { "/bin/sh", "-i", "-l" }));
sshServer.setPasswordAuthenticator(new CamelPasswordAuthenticator());
sshServer.setPublickeyAuthenticator(new CamelPublicKeyAuthenticator());
sshServer.start();
}
use of org.apache.sshd.server.command.Command in project testcases by coheigea.
the class SFTPTest method setup.
@Before
public void setup() throws Exception {
int port = AvailablePortFinder.getNextAvailable(10000);
// Write port number to configuration file in target
String basedir = System.getProperty("basedir");
if (basedir == null) {
basedir = new File(".").getCanonicalPath();
}
// Read in camel configuration file and substitute in the correct port
File f = new File(basedir + "/src/test/resources/camel-sftp.xml");
FileInputStream inputStream = new FileInputStream(f);
String content = IOUtils.toString(inputStream, "UTF-8");
inputStream.close();
content = content.replaceAll("portno", "" + port);
File f2 = new File(basedir + "/target/test-classes/camel-sftp.xml");
FileOutputStream outputStream = new FileOutputStream(f2);
IOUtils.write(content, outputStream, "UTF-8");
outputStream.close();
sshServer = SshServer.setUpDefaultServer();
sshServer.setPort(port);
// Generate a key
sshServer.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(Paths.get("target/generatedkey.pem")));
List<NamedFactory<Command>> subsystemFactories = new ArrayList<NamedFactory<Command>>(1);
subsystemFactories.add(new SftpSubsystemFactory());
sshServer.setSubsystemFactories(subsystemFactories);
sshServer.setCommandFactory(new ScpCommandFactory());
sshServer.setPasswordAuthenticator(new CamelPasswordAuthenticator());
sshServer.start();
// Create "storage_sftp" directory (delete it first if it exists)
File storageDirectory = new File(basedir + "/target/storage_sftp");
if (storageDirectory.exists()) {
storageDirectory.delete();
}
storageDirectory.mkdir();
}
use of org.apache.sshd.server.command.Command in project nomulus by google.
the class TestSftpServer method createSftpServer.
public static FtpServer createSftpServer(final String authorizedUser, @Nullable final String authorizedPassword, @Nullable final PublicKey authorizedPublicKey, int port, final File home) {
ServerBuilder serverBuilder = ServerBuilder.builder();
serverBuilder.randomFactory(secureRandomFactory);
if (authorizedPublicKey != null) {
// This authenticator checks that the user is presenting the right key. If authenticate
// returns true, then the server will make sure that the user can prove they have that key.
// Not that you would know this from the Apache javadocs.
serverBuilder.publickeyAuthenticator(new PublickeyAuthenticator() {
@Override
public boolean authenticate(String username, PublicKey publicKey, ServerSession session) {
return Arrays.equals(publicKey.getEncoded(), authorizedPublicKey.getEncoded());
}
});
}
serverBuilder.fileSystemFactory(new VirtualFileSystemFactory(home.toPath()));
SshServer server = serverBuilder.build();
server.setCommandFactory(new ScpCommandFactory());
server.setPort(port);
NamedFactory<Command> sftpSubsystemFactory = new SftpSubsystemFactory.Builder().build();
server.setSubsystemFactories(ImmutableList.of(sftpSubsystemFactory));
if (authorizedPassword != null) {
server.setPasswordAuthenticator(new PasswordAuthenticator() {
@Override
public boolean authenticate(String username, String password, ServerSession session) {
return username.equals(authorizedUser) && password.equals(authorizedPassword);
}
});
}
KeyPairProvider keyPairProvider = new KeyPairProvider() {
final ImmutableMap<String, KeyPair> keyPairByTypeMap = ImmutableMap.of(KEY_TYPE, HOST_KEY_PAIR);
@Override
public Iterable<KeyPair> loadKeys() {
return keyPairByTypeMap.values();
}
@Override
public Iterable<String> getKeyTypes() {
return keyPairByTypeMap.keySet();
}
@Override
public KeyPair loadKey(final String type) {
return keyPairByTypeMap.get(type);
}
};
server.setKeyPairProvider(keyPairProvider);
return new TestSftpServer(server);
}
use of org.apache.sshd.server.command.Command in project gerrit by GerritCodeReview.
the class AliasCommand method begin.
private void begin(ChannelSession channel, Environment env) throws IOException, Failure {
Map<String, CommandProvider> map = root.getMap();
for (String name : chain(command)) {
CommandProvider p = map.get(name);
if (p == null) {
throw die(getName() + ": not found");
}
Command cmd = p.getProvider().get();
if (!(cmd instanceof DispatchCommand)) {
throw die(getName() + ": not found");
}
map = ((DispatchCommand) cmd).getMap();
}
CommandProvider p = map.get(command.value());
if (p == null) {
throw die(getName() + ": not found");
}
Command cmd = p.getProvider().get();
checkRequiresCapability(cmd);
if (cmd instanceof BaseCommand) {
BaseCommand bc = (BaseCommand) cmd;
bc.setName(getName());
bc.setArguments(getArguments());
}
provideStateTo(cmd);
atomicCmd.set(cmd);
cmd.start(channel, env);
}
Aggregations