Search in sources :

Example 1 with ConnectionEndpoint

use of org.apache.geode.management.internal.cli.util.ConnectionEndpoint 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 2 with ConnectionEndpoint

use of org.apache.geode.management.internal.cli.util.ConnectionEndpoint 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)

Example 3 with ConnectionEndpoint

use of org.apache.geode.management.internal.cli.util.ConnectionEndpoint 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 4 with ConnectionEndpoint

use of org.apache.geode.management.internal.cli.util.ConnectionEndpoint 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;
}
Also used : ConnectionEndpointConverter(org.apache.geode.management.internal.cli.converters.ConnectionEndpointConverter) ConnectionEndpoint(org.apache.geode.management.internal.cli.util.ConnectionEndpoint) JmxOperationInvoker(org.apache.geode.management.internal.cli.shell.JmxOperationInvoker) IOException(java.io.IOException) GemFireException(org.apache.geode.GemFireException)

Example 5 with ConnectionEndpoint

use of org.apache.geode.management.internal.cli.util.ConnectionEndpoint in project geode by apache.

the class ConnectionEndpointConverter method convertFromText.

@Override
public ConnectionEndpoint convertFromText(String value, Class<?> targetType, String optionContext) {
    // expected format host[port], port is optional
    String endpointStr = value.trim();
    String hostStr = DEFAULT_JMX_HOST;
    String portStr = "";
    int port = DEFAULT_JMX_PORT;
    if (!endpointStr.isEmpty()) {
        int openIndex = endpointStr.indexOf("[");
        int closeIndex = endpointStr.indexOf("]");
        if (openIndex != -1) {
            // might have a port
            if (closeIndex == -1) {
                throw new IllegalArgumentException("Expected input: host[port] or host. Invalid value specified endpoints : " + value);
            }
            hostStr = endpointStr.substring(0, openIndex);
            portStr = endpointStr.substring(openIndex + 1, closeIndex);
            if (portStr.isEmpty()) {
                throw new IllegalArgumentException("Expected input: host[port] or host. Invalid value specified endpoints : " + value);
            }
            try {
                port = Integer.valueOf(portStr);
            } catch (NumberFormatException e) {
                throw new IllegalArgumentException("Expected input: host[port], Port should be a valid number between 1024-65536. Invalid value specified endpoints : " + value);
            }
        } else if (closeIndex != -1) {
            // shouldn't be there if opening brace was not there
            throw new IllegalArgumentException("Expected input: host[port] or host. Invalid value specified endpoints : " + value);
        } else {
            // doesn't contain brackets, assume only host name is given & assume default port
            hostStr = endpointStr;
        }
    }
    return new ConnectionEndpoint(hostStr, port);
}
Also used : ConnectionEndpoint(org.apache.geode.management.internal.cli.util.ConnectionEndpoint) ConnectionEndpoint(org.apache.geode.management.internal.cli.util.ConnectionEndpoint)

Aggregations

ConnectionEndpoint (org.apache.geode.management.internal.cli.util.ConnectionEndpoint)5 IOException (java.io.IOException)3 ConnectToLocatorResult (org.apache.geode.management.internal.cli.domain.ConnectToLocatorResult)3 JmxOperationInvoker (org.apache.geode.management.internal.cli.shell.JmxOperationInvoker)3 MalformedURLException (java.net.MalformedURLException)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 GemFireException (org.apache.geode.GemFireException)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 ConnectionEndpointConverter (org.apache.geode.management.internal.cli.converters.ConnectionEndpointConverter)1 InfoResultData (org.apache.geode.management.internal.cli.result.InfoResultData)1 Gfsh (org.apache.geode.management.internal.cli.shell.Gfsh)1