Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Jun 2, 2017
1 parent 58e552c commit 928a249
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;

import org.apache.karaf.shell.api.action.lifecycle.Manager;
import org.apache.karaf.shell.api.console.CommandLoggingFilter;
Expand Down Expand Up @@ -184,8 +184,8 @@ protected SshServer createSshServer(SessionFactory sessionFactory) {
server.setCipherFactories(SshUtils.buildCiphers(ciphers));
server.setKeyExchangeFactories(SshUtils.buildKexAlgorithms(kexAlgorithms));
server.setShellFactory(new ShellFactoryImpl(sessionFactory));
server.setCommandFactory(new ScpCommandFactory.Builder().withDelegate(new ShellCommandFactory(sessionFactory)).build());
server.setSubsystemFactories(Arrays.asList(new SftpSubsystemFactory()));
server.setCommandFactory(new ScpCommandFactory.Builder().withDelegate(cmd -> new ShellCommand(sessionFactory, cmd)).build());
server.setSubsystemFactories(Collections.singletonList(new SftpSubsystemFactory()));
server.setKeyPairProvider(keyPairProvider);
server.setPasswordAuthenticator(authenticator);
server.setPublickeyAuthenticator(authenticator);
Expand All @@ -194,7 +194,7 @@ protected SshServer createSshServer(SessionFactory sessionFactory) {
server.setAgentFactory(KarafAgentFactory.getInstance());
server.setTcpipForwardingFilter(AcceptAllForwardingFilter.INSTANCE);
server.getProperties().put(SshServer.IDLE_TIMEOUT, Long.toString(sshIdleTimeout));
server.getProperties().put(SshServer.NIO_WORKERS, new Integer(nioWorkers).toString());
server.getProperties().put(SshServer.NIO_WORKERS, Integer.toString(nioWorkers));
if (moduliUrl != null) {
server.getProperties().put(SshServer.MODULI_URL, moduliUrl);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,14 @@ public void setRealm(String realm) {
public boolean authenticate(final String username, final String password, final ServerSession session) {
try {
Subject subject = new Subject();
LoginContext loginContext = new LoginContext(realm, subject, new CallbackHandler() {
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
for (Callback callback : callbacks) {
if (callback instanceof NameCallback) {
((NameCallback) callback).setName(username);
} else if (callback instanceof PasswordCallback) {
((PasswordCallback) callback).setPassword(password.toCharArray());
} else {
throw new UnsupportedCallbackException(callback);
}
LoginContext loginContext = new LoginContext(realm, subject, callbacks -> {
for (Callback callback : callbacks) {
if (callback instanceof NameCallback) {
((NameCallback) callback).setName(username);
} else if (callback instanceof PasswordCallback) {
((PasswordCallback) callback).setPassword(password.toCharArray());
} else {
throw new UnsupportedCallbackException(callback);
}
}
});
Expand Down Expand Up @@ -102,16 +100,14 @@ public void handle(Callback[] callbacks) throws IOException, UnsupportedCallback
public boolean authenticate(final String username, final PublicKey key, final ServerSession session) {
try {
Subject subject = new Subject();
LoginContext loginContext = new LoginContext(realm, subject, new CallbackHandler() {
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
for (Callback callback : callbacks) {
if (callback instanceof NameCallback) {
((NameCallback) callback).setName(username);
} else if (callback instanceof PublickeyCallback) {
((PublickeyCallback) callback).setPublicKey(key);
} else {
throw new UnsupportedCallbackException(callback);
}
LoginContext loginContext = new LoginContext(realm, subject, callbacks -> {
for (Callback callback : callbacks) {
if (callback instanceof NameCallback) {
((NameCallback) callback).setName(username);
} else if (callback instanceof PublickeyCallback) {
((PublickeyCallback) callback).setPublicKey(key);
} else {
throw new UnsupportedCallbackException(callback);
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class ServerKeyVerifierImpl implements ServerKeyVerifier {
private final boolean quiet;

private final static String keyChangedMessage =
" @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ \n" +
" @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ \n" +
" @ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @ \n" +
" @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ \n" +
"IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ShellCommand implements Command, Runnable, SessionAware {
public class ShellCommand implements Command, SessionAware {

public static final String SHELL_INIT_SCRIPT = "karaf.shell.init.script";
public static final String EXEC_INIT_SCRIPT = "karaf.exec.init.script";
Expand Down Expand Up @@ -93,7 +93,7 @@ public void setSession(ServerSession session) {

public void start(final Environment env) throws IOException {
this.env = env;
new Thread(this).start();
new Thread(this::run).start();
}

public void run() {
Expand All @@ -108,15 +108,13 @@ public void run() {
Object result;
if (subject != null) {
try {
result = JaasHelper.doAs(subject, new PrivilegedExceptionAction<Object>() {
public Object run() throws Exception {
String scriptFileName = System.getProperty(EXEC_INIT_SCRIPT);
if (scriptFileName == null) {
scriptFileName = System.getProperty(SHELL_INIT_SCRIPT);
}
executeScript(scriptFileName, session);
return session.execute(command);
result = JaasHelper.doAs(subject, (PrivilegedExceptionAction<Object>) () -> {
String scriptFileName = System.getProperty(EXEC_INIT_SCRIPT);
if (scriptFileName == null) {
scriptFileName = System.getProperty(SHELL_INIT_SCRIPT);
}
executeScript(scriptFileName, session);
return session.execute(command);
});
} catch (PrivilegedActionException e) {
throw e.getException();
Expand Down

This file was deleted.

0 comments on commit 928a249

Please sign in to comment.