ConnectCommand extends OfflineGfshCommand { Properties resolveSslProperties(Gfsh gfsh, boolean useSsl, File gfPropertiesFile, File gfSecurityPropertiesFile, String... sslOptionValues) { Properties gfProperties = loadProperties(gfPropertiesFile, gfSecurityPropertiesFile); if (containsLegacySSLConfig(gfProperties)) { return gfProperties; } if (!(useSsl || containsSSLConfig(gfProperties) || isSslImpliedBySslOptions(sslOptionValues))) { return gfProperties; } for (int i = 0; i < USER_INPUT_PROPERTIES.length; i++) { UserInputProperty userInputProperty = USER_INPUT_PROPERTIES[i]; String sslOptionValue = null; if (sslOptionValues != null && sslOptionValues.length > i) { sslOptionValue = sslOptionValues[i]; } String sslConfigValue = gfProperties.getProperty(userInputProperty.getKey()); if (sslOptionValue != null) { gfProperties.setProperty(userInputProperty.getKey(), sslOptionValue); } else if (sslConfigValue == null) { gfProperties.setProperty(userInputProperty.getKey(), userInputProperty.promptForAcceptableValue(gfsh)); } } return gfProperties; }  @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 givenTokenIsSetInProperties() { doReturn(properties).when(connectCommand).resolveSslProperties(any(), anyBoolean(), any(), any()); gfshParserRule.executeAndAssertThat(connectCommand, "connect --token=FOO_BAR"); assertThat(properties.getProperty("security-token")).isEqualTo("FOO_BAR"); }
@Test public void promptForPasswordIfUsernameIsGiven() { doReturn(properties).when(connectCommand).resolveSslProperties(any(), anyBoolean(), any(), any()); result = gfshParserRule.executeCommandWithInstance(connectCommand, "connect --user=user"); verify(gfsh).readPassword(CliStrings.CONNECT__PASSWORD + ": "); assertThat(properties.getProperty("security-username")).isEqualTo("user"); assertThat(properties.getProperty("security-password")).isEqualTo(""); }
@Test public void notPromptForPasswordIfUsernameIsGiven() { doReturn(properties).when(connectCommand).resolveSslProperties(any(), anyBoolean(), any(), any()); result = gfshParserRule.executeCommandWithInstance(connectCommand, "connect --user=user --password=pass"); verify(gfsh, times(0)).readPassword(CliStrings.CONNECT__PASSWORD + ": "); assertThat(properties.getProperty("security-username")).isEqualTo("user"); assertThat(properties.getProperty("security-password")).isEqualTo("pass"); }
@Test public void resolveSslProperties() { properties.setProperty(SSL_KEYSTORE, "keystore"); properties = connectCommand.resolveSslProperties(gfsh, false, null, null); assertThat(properties).hasSize(9); properties.clear(); properties.setProperty(SSL_KEYSTORE, "keystore"); properties = connectCommand.resolveSslProperties(gfsh, false, null, null, "keystore2", "password"); assertThat(properties).hasSize(9); assertThat(properties.getProperty(SSL_KEYSTORE)).isEqualTo("keystore2"); assertThat(properties.getProperty(SSL_KEYSTORE_PASSWORD)).isEqualTo("password"); }