use of org.apache.geode.management.internal.cli.shell.JmxOperationInvoker in project geode by apache.
the class LauncherLifecycleCommands method doAutoConnect.
private boolean doAutoConnect(final String locatorHostname, final int locatorPort, final String gemfirePropertiesPathname, final String gemfireSecurityPropertiesPathname, final InfoResultData infoResultData) {
boolean connectSuccess = false;
boolean jmxManagerAuthEnabled = false;
boolean jmxManagerSslEnabled = false;
Map<String, String> configurationProperties = loadConfigurationProperties(gemfireSecurityPropertiesPathname, loadConfigurationProperties(gemfirePropertiesPathname));
Map<String, String> locatorConfigurationProperties = new HashMap<>(configurationProperties);
String responseFailureMessage = null;
for (int attempts = 0; (attempts < 10 && !connectSuccess); attempts++) {
try {
ConnectToLocatorResult connectToLocatorResult = ShellCommands.connectToLocator(locatorHostname, locatorPort, ShellCommands.getConnectLocatorTimeoutInMS() / 4, locatorConfigurationProperties);
ConnectionEndpoint memberEndpoint = connectToLocatorResult.getMemberEndpoint();
jmxManagerSslEnabled = connectToLocatorResult.isJmxManagerSslEnabled();
if (!jmxManagerSslEnabled) {
configurationProperties.clear();
}
getGfsh().setOperationInvoker(new JmxOperationInvoker(memberEndpoint.getHost(), memberEndpoint.getPort(), null, null, configurationProperties, null));
String shellAndLogMessage = CliStrings.format(CliStrings.CONNECT__MSG__SUCCESS, "JMX Manager " + memberEndpoint.toString(false));
infoResultData.addLine("\n");
infoResultData.addLine(shellAndLogMessage);
getGfsh().logToFile(shellAndLogMessage, null);
connectSuccess = true;
responseFailureMessage = null;
} catch (IllegalStateException unexpected) {
if (CauseFinder.indexOfCause(unexpected, ClassCastException.class, false) != -1) {
responseFailureMessage = "The Locator might require SSL Configuration.";
}
} catch (SecurityException ignore) {
getGfsh().logToFile(ignore.getMessage(), ignore);
jmxManagerAuthEnabled = true;
// no need to continue after SecurityException
break;
} catch (AuthenticationFailedException ignore) {
getGfsh().logToFile(ignore.getMessage(), ignore);
jmxManagerAuthEnabled = true;
// no need to continue after AuthenticationFailedException
break;
} catch (SSLException ignore) {
if (ignore instanceof SSLHandshakeException) {
// try to connect again without SSL since the SSL handshake failed implying a plain text
// connection...
locatorConfigurationProperties.clear();
} else {
// another type of SSL error occurred (possibly a configuration issue); pass the buck...
getGfsh().logToFile(ignore.getMessage(), ignore);
responseFailureMessage = "Check your SSL configuration and try again.";
break;
}
} catch (Exception ignore) {
getGfsh().logToFile(ignore.getMessage(), ignore);
responseFailureMessage = "Failed to connect; unknown cause: " + ignore.getMessage();
}
}
if (!connectSuccess) {
doOnConnectionFailure(locatorHostname, locatorPort, jmxManagerAuthEnabled, jmxManagerSslEnabled, infoResultData);
}
if (StringUtils.isNotBlank(responseFailureMessage)) {
infoResultData.addLine("\n");
infoResultData.addLine(responseFailureMessage);
}
return connectSuccess;
}
use of org.apache.geode.management.internal.cli.shell.JmxOperationInvoker in project geode by apache.
the class ShellCommands method jmxConnect.
private Result jmxConnect(Map<String, String> sslConfigProps, ConnectionEndpoint memberRmiHostPort, ConnectionEndpoint locatorTcpHostPort, boolean useSsl, String userName, String passwordToUse, String gfSecurityPropertiesPath, boolean retry) {
ConnectionEndpoint hostPortToConnect = null;
Gfsh gfsh = getGfsh();
try {
// locator to find the rmi host port
if (memberRmiHostPort != null) {
hostPortToConnect = memberRmiHostPort;
} else {
// Used for gfsh->locator connection & not needed for gfsh->manager connection
if (useSsl || !sslConfigProps.isEmpty()) {
sslConfigProps.put(MCAST_PORT, String.valueOf(0));
sslConfigProps.put(LOCATORS, "");
String sslInfoLogMsg = "Connecting to Locator via SSL.";
if (useSsl) {
sslInfoLogMsg = CliStrings.CONNECT__USE_SSL + " is set to true. " + sslInfoLogMsg;
}
gfsh.logToFile(sslInfoLogMsg, null);
}
Gfsh.println(CliStrings.format(CliStrings.CONNECT__MSG__CONNECTING_TO_LOCATOR_AT_0, new Object[] { locatorTcpHostPort.toString(false) }));
ConnectToLocatorResult connectToLocatorResult = connectToLocator(locatorTcpHostPort.getHost(), locatorTcpHostPort.getPort(), CONNECT_LOCATOR_TIMEOUT_MS, sslConfigProps);
hostPortToConnect = connectToLocatorResult.getMemberEndpoint();
// (jmx-manager-ssl=false)
if ((useSsl || !sslConfigProps.isEmpty()) && !connectToLocatorResult.isJmxManagerSslEnabled()) {
gfsh.logInfo(CliStrings.CONNECT__USE_SSL + " is set to true. But JMX Manager doesn't support SSL, connecting without SSL.", null);
sslConfigProps.clear();
}
}
if (!sslConfigProps.isEmpty()) {
gfsh.logToFile("Connecting to manager via SSL.", null);
}
// print out the connecting endpoint
if (!retry) {
Gfsh.println(CliStrings.format(CliStrings.CONNECT__MSG__CONNECTING_TO_MANAGER_AT_0, new Object[] { hostPortToConnect.toString(false) }));
}
InfoResultData infoResultData = ResultBuilder.createInfoResultData();
JmxOperationInvoker operationInvoker = new JmxOperationInvoker(hostPortToConnect.getHost(), hostPortToConnect.getPort(), userName, passwordToUse, sslConfigProps, gfSecurityPropertiesPath);
gfsh.setOperationInvoker(operationInvoker);
infoResultData.addLine(CliStrings.format(CliStrings.CONNECT__MSG__SUCCESS, hostPortToConnect.toString(false)));
LogWrapper.getInstance().info(CliStrings.format(CliStrings.CONNECT__MSG__SUCCESS, hostPortToConnect.toString(false)));
return ResultBuilder.buildResult(infoResultData);
} catch (Exception e) {
// all other exceptions, just logs it and returns a connection error
if (!(e instanceof SecurityException) && !(e instanceof AuthenticationFailedException)) {
return handleExcpetion(e, hostPortToConnect);
}
// connection error
if (userName != null) {
return handleExcpetion(e, hostPortToConnect);
}
// otherwise, prompt for username and password and retry the conenction
try {
userName = gfsh.readText(CliStrings.CONNECT__USERNAME + ": ");
passwordToUse = gfsh.readPassword(CliStrings.CONNECT__PASSWORD + ": ");
// avoid a stack overflow.
if (userName == null && passwordToUse == null)
return handleExcpetion(e, hostPortToConnect);
return jmxConnect(sslConfigProps, hostPortToConnect, null, useSsl, userName, passwordToUse, gfSecurityPropertiesPath, true);
} catch (IOException ioe) {
return handleExcpetion(ioe, hostPortToConnect);
}
} finally {
Gfsh.redirectInternalJavaLoggers();
}
}
use of org.apache.geode.management.internal.cli.shell.JmxOperationInvoker in project geode by apache.
the class StartJConsoleCommand method getJmxServiceUrlAsString.
protected String getJmxServiceUrlAsString(final String member) {
if (StringUtils.isNotBlank(member)) {
ConnectionEndpointConverter converter = new ConnectionEndpointConverter();
try {
ConnectionEndpoint connectionEndpoint = converter.convertFromText(member, ConnectionEndpoint.class, null);
String hostAndPort = connectionEndpoint.getHost() + ":" + connectionEndpoint.getPort();
return String.format("service:jmx:rmi://%s/jndi/rmi://%s/jmxrmi", hostAndPort, hostAndPort);
} catch (Exception e) {
throw new IllegalArgumentException(CliStrings.START_JCONSOLE__CONNECT_BY_MEMBER_NAME_ID_ERROR_MESSAGE);
}
} else {
if (isConnectedAndReady() && (getGfsh().getOperationInvoker() instanceof JmxOperationInvoker)) {
JmxOperationInvoker jmxOperationInvoker = (JmxOperationInvoker) getGfsh().getOperationInvoker();
return ObjectUtils.toString(jmxOperationInvoker.getJmxServiceUrl());
}
}
return null;
}
Aggregations