use of io.fabric8.api.FabricAuthenticationException in project fabric8 by jboss-fuse.
the class ContainerConnectAction method executSshCommand.
/**
* Executes the ssh command.
*/
private void executSshCommand(CommandSession session, String username, String password, String hostname, String port, String cmd) throws Exception {
if (cmd == null || cmd.length() == 0) {
// ENTESB-6826: we're connecting in "shell" mode, which isn't wise when running from bin/client or ssh
if (session.getKeyboard().getClass().getName().equals("org.apache.sshd.common.channel.ChannelPipedInputStream")) {
System.err.println("When connecting to remote container using \"fabric:container-connect\" using ssh or bin/client, please establish SSH session (run bin/client) first and then run \"fabric:container-connect\"");
return;
}
}
// Create the client from prototype
SshClient client = createClient();
String agentSocket;
if (this.session.get(SshAgent.SSH_AUTHSOCKET_ENV_NAME) != null) {
agentSocket = this.session.get(SshAgent.SSH_AUTHSOCKET_ENV_NAME).toString();
client.getProperties().put(SshAgent.SSH_AUTHSOCKET_ENV_NAME, agentSocket);
}
try {
ConnectFuture future = client.connect(hostname, Integer.parseInt(port));
future.await();
sshSession = future.getSession();
Object oldIgnoreInterrupts = this.session.get(Console.IGNORE_INTERRUPTS);
this.session.put(Console.IGNORE_INTERRUPTS, Boolean.TRUE);
try {
System.out.println("Connected");
boolean authed = false;
if (!authed) {
if (username == null) {
throw new FabricAuthenticationException("No username specified.");
}
log.debug("Prompting user for password");
String pwd = password != null ? password : ShellUtils.readLine(session, "Password: ", true);
sshSession.authPassword(username, pwd);
int ret = sshSession.waitFor(ClientSession.WAIT_AUTH | ClientSession.CLOSED | ClientSession.AUTHED, 0);
if ((ret & ClientSession.AUTHED) == 0) {
System.err.println("Password authentication failed");
} else {
authed = true;
}
}
if (!authed) {
throw new FabricAuthenticationException("Failed to authenticate.");
}
// If user is authenticated credentials to session for future use.
ShellUtils.storeFabricCredentials(session, username, password);
ClientChannel channel;
if (cmd != null && cmd.length() > 0) {
channel = sshSession.createChannel("exec", cmd);
channel.setIn(new ByteArrayInputStream(new byte[0]));
} else {
channel = sshSession.createChannel("shell");
channel.setIn(new NoCloseInputStream(System.in));
((ChannelShell) channel).setPtyColumns(ShellUtils.getTermWidth(session));
((ChannelShell) channel).setupSensibleDefaultPty();
((ChannelShell) channel).setAgentForwarding(true);
}
channel.setOut(new NoCloseOutputStream(System.out));
channel.setErr(new NoCloseOutputStream(System.err));
channel.open();
channel.waitFor(ClientChannel.CLOSED, 0);
} finally {
session.put(Console.IGNORE_INTERRUPTS, oldIgnoreInterrupts);
sshSession.close(false);
}
} finally {
client.stop();
}
}
use of io.fabric8.api.FabricAuthenticationException in project fabric8 by jboss-fuse.
the class MQCreateAction method doExecute.
@Override
protected Object doExecute() throws Exception {
MQBrokerConfigDTO dto = createDTO();
Profile profile = MQManager.createOrUpdateProfile(dto, fabricService, runtimeProperties);
if (profile == null) {
return null;
}
String profileId = profile.getId();
System.out.println("MQ profile " + profileId + " ready");
// assign profile to existing containers
if (assign != null) {
String[] assignContainers = assign.split(",");
MQManager.assignProfileToContainers(fabricService, profile, assignContainers);
}
// create containers
if (create != null) {
String[] createContainers = create.split(",");
List<CreateContainerBasicOptions.Builder> builderList = MQManager.createContainerBuilders(dto, fabricService, "child", profileId, dto.version(), createContainers);
for (CreateContainerBasicOptions.Builder builder : builderList) {
CreateContainerMetadata[] metadatas;
try {
if (builder instanceof CreateChildContainerOptions.Builder) {
CreateChildContainerOptions.Builder childBuilder = (CreateChildContainerOptions.Builder) builder;
builder = childBuilder.jmxUser(username).jmxPassword(password);
}
metadatas = fabricService.createContainers(builder.build());
// check if there was a FabricAuthenticationException as failure then we can try again
if (metadatas != null) {
for (CreateContainerMetadata meta : metadatas) {
if (meta.getFailure() != null && meta.getFailure() instanceof FabricAuthenticationException) {
throw (FabricAuthenticationException) meta.getFailure();
}
}
}
ShellUtils.storeFabricCredentials(session, username, password);
} catch (FabricAuthenticationException fae) {
// If authentication fails, prompts for credentials and try again.
if (builder instanceof CreateChildContainerOptions.Builder) {
CreateChildContainerOptions.Builder childBuilder = (CreateChildContainerOptions.Builder) builder;
promptForJmxCredentialsIfNeeded();
metadatas = fabricService.createContainers(childBuilder.jmxUser(username).jmxPassword(password).build());
ShellUtils.storeFabricCredentials(session, username, password);
}
}
}
}
return null;
}
use of io.fabric8.api.FabricAuthenticationException in project fabric8 by jboss-fuse.
the class ContainerEditJvmOptionsAction method connectOrRetry.
private JMXConnector connectOrRetry(HashMap<String, String[]> env, String jmxUrl) throws IOException {
JMXServiceURL target = new JMXServiceURL(jmxUrl);
JMXConnector connector = null;
try {
connector = JMXConnectorFactory.connect(target, env);
} catch (FabricAuthenticationException | SecurityException ex) {
username = null;
password = null;
promptForJmxCredentialsIfNeeded();
Map<String, String[]> newEnv = prepareAuthenticationData();
connector = JMXConnectorFactory.connect(target, newEnv);
}
return connector;
}
use of io.fabric8.api.FabricAuthenticationException in project fabric8 by jboss-fuse.
the class ContainerTemplate method createConnector.
public JMXConnector createConnector() {
String rootUrl = container.getJmxUrl();
if (rootUrl == null) {
throw new IllegalStateException("The jmx url for container '" + container.getId() + "' is not specified");
}
JMXConnector connector;
try {
connector = JMXConnectorFactory.connect(new JMXServiceURL(rootUrl), getEnvironmentCredentials());
} catch (IOException e) {
throw FabricException.launderThrowable(e);
} catch (SecurityException ex) {
throw new FabricAuthenticationException(ex);
}
return connector;
}
use of io.fabric8.api.FabricAuthenticationException in project fabric8 by jboss-fuse.
the class ContainerConnectAction method doExecute.
protected Object doExecute() throws Exception {
validateContainerName(container);
String cmdStr = "";
if (command != null) {
StringBuilder sb = new StringBuilder();
for (String cmd : command) {
if (sb.length() > 0) {
sb.append(' ');
}
sb.append(cmd);
}
sb.append("\n");
cmdStr = sb.toString();
}
Container found = FabricCommand.getContainer(fabricService, container);
String sshUrl = found.getSshUrl();
if (sshUrl == null) {
throw new IllegalArgumentException("Container " + container + " has no SSH URL.");
}
String[] ssh = sshUrl.split(":");
if (ssh.length < 2) {
throw new IllegalArgumentException("Container " + container + " has an invalid SSH URL '" + sshUrl + "'");
}
username = username != null && !username.isEmpty() ? username : ShellUtils.retrieveFabricUser(session);
password = password != null ? password : ShellUtils.retrieveFabricUserPassword(session);
try {
executSshCommand(session, username, password, ssh[0], ssh[1], cmdStr);
} catch (FabricAuthenticationException ex) {
username = null;
password = null;
promptForSshCredentialsIfNeeded();
executSshCommand(session, username, password, ssh[0], ssh[1], cmdStr);
}
return null;
}
Aggregations