Search in sources :

Example 1 with ConnectToLocatorResult

use of org.apache.geode.management.internal.cli.domain.ConnectToLocatorResult 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;
}
Also used : HashMap(java.util.HashMap) AuthenticationFailedException(org.apache.geode.security.AuthenticationFailedException) ConnectToLocatorResult(org.apache.geode.management.internal.cli.domain.ConnectToLocatorResult) JmxOperationInvoker(org.apache.geode.management.internal.cli.shell.JmxOperationInvoker) SSLException(javax.net.ssl.SSLException) ConnectionEndpoint(org.apache.geode.management.internal.cli.util.ConnectionEndpoint) ConverterHint(org.apache.geode.management.cli.ConverterHint) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) MalformedObjectNameException(javax.management.MalformedObjectNameException) SSLException(javax.net.ssl.SSLException) AuthenticationFailedException(org.apache.geode.security.AuthenticationFailedException) ClusterConfigurationNotAvailableException(org.apache.geode.internal.process.ClusterConfigurationNotAvailableException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ConnectionEndpoint(org.apache.geode.management.internal.cli.util.ConnectionEndpoint)

Example 2 with ConnectToLocatorResult

use of org.apache.geode.management.internal.cli.domain.ConnectToLocatorResult in project geode by apache.

the class ShellCommands method connectToLocator.

public static ConnectToLocatorResult connectToLocator(String host, int port, int timeout, Map<String, String> props) throws IOException {
    // register DSFID types first; invoked explicitly so that all message type
    // initializations do not happen in first deserialization on a possibly
    // "precious" thread
    DSFIDFactory.registerTypes();
    JmxManagerLocatorResponse locatorResponse = JmxManagerLocatorRequest.send(host, port, timeout, props);
    if (StringUtils.isBlank(locatorResponse.getHost()) || locatorResponse.getPort() == 0) {
        Throwable locatorResponseException = locatorResponse.getException();
        String exceptionMessage = CliStrings.CONNECT__MSG__LOCATOR_COULD_NOT_FIND_MANAGER;
        if (locatorResponseException != null) {
            String locatorResponseExceptionMessage = locatorResponseException.getMessage();
            locatorResponseExceptionMessage = (StringUtils.isNotBlank(locatorResponseExceptionMessage) ? locatorResponseExceptionMessage : locatorResponseException.toString());
            exceptionMessage = "Exception caused JMX Manager startup to fail because: '".concat(locatorResponseExceptionMessage).concat("'");
        }
        throw new IllegalStateException(exceptionMessage, locatorResponseException);
    }
    ConnectionEndpoint memberEndpoint = new ConnectionEndpoint(locatorResponse.getHost(), locatorResponse.getPort());
    String resultMessage = CliStrings.format(CliStrings.CONNECT__MSG__CONNECTING_TO_MANAGER_AT_0, memberEndpoint.toString(false));
    return new ConnectToLocatorResult(memberEndpoint, resultMessage, locatorResponse.isJmxManagerSslEnabled());
}
Also used : JmxManagerLocatorResponse(org.apache.geode.management.internal.JmxManagerLocatorResponse) ConnectionEndpoint(org.apache.geode.management.internal.cli.util.ConnectionEndpoint) ConnectToLocatorResult(org.apache.geode.management.internal.cli.domain.ConnectToLocatorResult)

Example 3 with ConnectToLocatorResult

use of org.apache.geode.management.internal.cli.domain.ConnectToLocatorResult 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();
    }
}
Also used : ConnectionEndpoint(org.apache.geode.management.internal.cli.util.ConnectionEndpoint) InfoResultData(org.apache.geode.management.internal.cli.result.InfoResultData) Gfsh(org.apache.geode.management.internal.cli.shell.Gfsh) AuthenticationFailedException(org.apache.geode.security.AuthenticationFailedException) IOException(java.io.IOException) ConnectToLocatorResult(org.apache.geode.management.internal.cli.domain.ConnectToLocatorResult) JmxOperationInvoker(org.apache.geode.management.internal.cli.shell.JmxOperationInvoker) AuthenticationFailedException(org.apache.geode.security.AuthenticationFailedException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Aggregations

ConnectToLocatorResult (org.apache.geode.management.internal.cli.domain.ConnectToLocatorResult)3 ConnectionEndpoint (org.apache.geode.management.internal.cli.util.ConnectionEndpoint)3 IOException (java.io.IOException)2 MalformedURLException (java.net.MalformedURLException)2 JmxOperationInvoker (org.apache.geode.management.internal.cli.shell.JmxOperationInvoker)2 AuthenticationFailedException (org.apache.geode.security.AuthenticationFailedException)2 HashMap (java.util.HashMap)1 MalformedObjectNameException (javax.management.MalformedObjectNameException)1 SSLException (javax.net.ssl.SSLException)1 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)1 ClusterConfigurationNotAvailableException (org.apache.geode.internal.process.ClusterConfigurationNotAvailableException)1 ConverterHint (org.apache.geode.management.cli.ConverterHint)1 JmxManagerLocatorResponse (org.apache.geode.management.internal.JmxManagerLocatorResponse)1 InfoResultData (org.apache.geode.management.internal.cli.result.InfoResultData)1 Gfsh (org.apache.geode.management.internal.cli.shell.Gfsh)1