ConnectCommand extends OfflineGfshCommand { ResultModel jmxConnect(Properties gfProperties, boolean useSsl, ConnectionEndpoint memberRmiHostPort, ConnectionEndpoint locatorTcpHostPort, boolean retry) { ConnectionEndpoint jmxHostPortToConnect = null; Gfsh gfsh = getGfsh(); try { if (memberRmiHostPort != null) { jmxHostPortToConnect = memberRmiHostPort; } else { if (useSsl) { gfsh.logToFile( CliStrings.CONNECT__USE_SSL + " is set to true. Connecting to Locator via SSL.", 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, gfProperties); jmxHostPortToConnect = connectToLocatorResult.getMemberEndpoint(); if (useSsl && !connectToLocatorResult.isJmxManagerSslEnabled()) { gfsh.logInfo( CliStrings.CONNECT__USE_SSL + " is set to true. But JMX Manager doesn't support SSL, connecting without SSL.", null); useSsl = false; } } if (useSsl) { gfsh.logToFile("Connecting to manager via SSL.", null); } if (!retry) { Gfsh.println(CliStrings.format(CliStrings.CONNECT__MSG__CONNECTING_TO_MANAGER_AT_0, new Object[] {jmxHostPortToConnect.toString(false)})); } ResultModel result = new ResultModel(); InfoResultModel infoResultModel = result.addInfo(); JmxOperationInvoker operationInvoker = new JmxOperationInvoker(jmxHostPortToConnect.getHost(), jmxHostPortToConnect.getPort(), gfProperties); gfsh.setOperationInvoker(operationInvoker); infoResultModel.addLine(CliStrings.format(CliStrings.CONNECT__MSG__SUCCESS, jmxHostPortToConnect.toString(false))); LogWrapper.getInstance().info(CliStrings.format(CliStrings.CONNECT__MSG__SUCCESS, jmxHostPortToConnect.toString(false))); return result; } catch (SecurityException | AuthenticationFailedException e) { if (gfProperties.containsKey(ResourceConstants.USER_NAME) || gfProperties.containsKey(ResourceConstants.TOKEN)) { return handleException(e, jmxHostPortToConnect); } gfProperties.setProperty(UserInputProperty.USERNAME.getKey(), UserInputProperty.USERNAME.promptForAcceptableValue(gfsh)); gfProperties.setProperty(UserInputProperty.PASSWORD.getKey(), UserInputProperty.PASSWORD.promptForAcceptableValue(gfsh)); return jmxConnect(gfProperties, useSsl, jmxHostPortToConnect, null, true); } catch (UnknownHostException e) { return handleException(e, "JMX manager can't be reached. Hostname or IP address could not be found."); } catch (Exception e) { return handleException(e, jmxHostPortToConnect); } }  @CliCommand(value = {CliStrings.CONNECT}, help = CliStrings.CONNECT__HELP) @CliMetaData(shellOnly = true, relatedTopic = {CliStrings.TOPIC_GFSH, CliStrings.TOPIC_GEODE_JMX, CliStrings.TOPIC_GEODE_MANAGER}) ResultModel connect(
      @CliOption(key = {CliStrings.CONNECT__LOCATOR},
          unspecifiedDefaultValue = ConnectionEndpointConverter.DEFAULT_LOCATOR_ENDPOINTS,
          optionContext = ConnectionEndpoint.LOCATOR_OPTION_CONTEXT,
          help = CliStrings.CONNECT__LOCATOR__HELP) ConnectionEndpoint locatorEndPoint,
      @CliOption(key = {CliStrings.CONNECT__JMX_MANAGER},
          optionContext = ConnectionEndpoint.JMXMANAGER_OPTION_CONTEXT,
          help = CliStrings.CONNECT__JMX_MANAGER__HELP) ConnectionEndpoint jmxManagerEndPoint,
      @CliOption(key = {CliStrings.CONNECT__USE_HTTP}, specifiedDefaultValue = "true",
          unspecifiedDefaultValue = "false",
          help = CliStrings.CONNECT__USE_HTTP__HELP) boolean useHttp,
      @CliOption(key = {CliStrings.CONNECT__URL}, help = CliStrings.CONNECT__URL__HELP) String url,
      @CliOption(key = {CliStrings.CONNECT__USERNAME}, specifiedDefaultValue = "",
          help = CliStrings.CONNECT__USERNAME__HELP) String userName,
      @CliOption(key = {CliStrings.CONNECT__PASSWORD}, specifiedDefaultValue = "",
          help = CliStrings.CONNECT__PASSWORD__HELP) String password,
      @CliOption(key = {CliStrings.CONNECT__TOKEN}, specifiedDefaultValue = "",
          help = CliStrings.CONNECT__TOKEN__HELP) String token,
      @CliOption(key = {CliStrings.CONNECT__KEY_STORE},
          help = CliStrings.CONNECT__KEY_STORE__HELP) String keystore,
      @CliOption(key = {CliStrings.CONNECT__KEY_STORE_PASSWORD},
          help = CliStrings.CONNECT__KEY_STORE_PASSWORD__HELP) String keystorePassword,
      @CliOption(key = {CliStrings.CONNECT__TRUST_STORE},
          help = CliStrings.CONNECT__TRUST_STORE__HELP) String truststore,
      @CliOption(key = {CliStrings.CONNECT__TRUST_STORE_PASSWORD},
          help = CliStrings.CONNECT__TRUST_STORE_PASSWORD__HELP) String truststorePassword,
      @CliOption(key = {CliStrings.CONNECT__SSL_CIPHERS},
          help = CliStrings.CONNECT__SSL_CIPHERS__HELP) String sslCiphers,
      @CliOption(key = {CliStrings.CONNECT__SSL_PROTOCOLS},
          help = CliStrings.CONNECT__SSL_PROTOCOLS__HELP) String sslProtocols,
      @CliOption(key = CliStrings.CONNECT__SECURITY_PROPERTIES, optionContext = ConverterHint.FILE,
          help = CliStrings.CONNECT__SECURITY_PROPERTIES__HELP) final File gfSecurityPropertiesFile,
      @CliOption(key = {CliStrings.CONNECT__USE_SSL}, specifiedDefaultValue = "true",
          unspecifiedDefaultValue = "false",
          help = CliStrings.CONNECT__USE_SSL__HELP) boolean useSsl,
      @CliOption(key = {"skip-ssl-validation"}, specifiedDefaultValue = "true",
          unspecifiedDefaultValue = "false",
          help = "When connecting via HTTP, connects using 1-way SSL validation rather than 2-way SSL validation.") boolean skipSslValidation); static ConnectToLocatorResult connectToLocator(String host, int port, int timeout,
      Properties props);  }
@Test public void connectToManagerWithOlderMajorVersionAllowed() { when(gfsh.getVersion()).thenReturn("2.2"); when(operationInvoker.getRemoteVersion()).thenReturn("1.10"); when(gfsh.getGeodeSerializationVersion()).thenReturn("2.2"); when(operationInvoker.getRemoteGeodeSerializationVersion()).thenReturn("1.10"); when(operationInvoker.isConnected()).thenReturn(true); ResultModel resultModel = new ResultModel(); when(connectCommand.jmxConnect(any(), anyBoolean(), any(), any(), anyBoolean())) .thenReturn(resultModel); gfshParserRule.executeAndAssertThat(connectCommand, "connect --locator=localhost:4040") .statusIsSuccess() .doesNotContainOutput("Cannot use a 2.2 gfsh client to connect to a 1.10 cluster."); }
@Test public void connectToManager1_10() { when(operationInvoker.getRemoteVersion()).thenReturn("1.10"); when(gfsh.getGeodeSerializationVersion()).thenReturn("1.14"); when(operationInvoker.getRemoteGeodeSerializationVersion()) .thenThrow(new RuntimeException("serialization version not available")); when(operationInvoker.isConnected()).thenReturn(true); ResultModel resultModel = new ResultModel(); when(connectCommand.jmxConnect(any(), anyBoolean(), any(), any(), anyBoolean())) .thenReturn(resultModel); gfshParserRule.executeAndAssertThat(connectCommand, "connect --locator=localhost:4040") .statusIsSuccess() .containsOutput("You are connected to a cluster of version: 1.10"); }
@Test public void connectToManagerBySerializationVersion() { when(gfsh.getVersion()).thenReturn("0.0.0"); when(gfsh.getGeodeSerializationVersion()).thenReturn("1.14.0"); when(operationInvoker.getRemoteVersion()).thenReturn("0.0.0"); when(operationInvoker.getRemoteGeodeSerializationVersion()).thenReturn("1.14.0"); when(operationInvoker.isConnected()).thenReturn(true); ResultModel resultModel = new ResultModel(); when(connectCommand.jmxConnect(any(), anyBoolean(), any(), any(), anyBoolean())) .thenReturn(resultModel); gfshParserRule.executeAndAssertThat(connectCommand, "connect --locator=localhost:4040") .statusIsSuccess() .doesNotContainOutput("Cannot use a 0.0.0 gfsh client to connect to a 0.0.0 cluster"); }