@Override
 public BrooklynProperties submap(Predicate<ConfigKey<?>> filter) {
   BrooklynProperties result = Factory.newEmpty();
   for (Object entry : entrySet()) {
     ConfigKey<?> k = new BasicConfigKey<Object>(Object.class, "" + ((Map.Entry) entry).getKey());
     if (filter.apply(k)) result.put(((Map.Entry) entry).getKey(), ((Map.Entry) entry).getValue());
   }
   return result;
 }
    public static BrooklynProperties newDefault() {
      BrooklynProperties properties = new BrooklynProperties();

      addGlobalProperties(properties);

      properties.addEnvironmentVars();

      properties.addSystemProperties();

      return properties;
    }
 public static BrooklynProperties newDefault() {
   BrooklynProperties p = new BrooklynProperties().addEnvironmentVars().addSystemProperties();
   File f =
       new File(
           p.getFirst(MutableMap.of("defaultIfNone", "/"), "user.home", "HOME")
               + File.separatorChar
               + ".brooklyn"
               + File.separatorChar
               + "brooklyn.properties");
   if (f.exists()) p.addFrom(f);
   return p;
 }
  @Test
  public void testTakesDeprecatedProperties() {
    brooklynProperties.put("brooklyn.localhost.private-key-file", "myprivatekeyfile");
    brooklynProperties.put("brooklyn.localhost.public-key-file", "mypublickeyfile");
    brooklynProperties.put("brooklyn.localhost.private-key-data", "myprivateKeyData");
    brooklynProperties.put("brooklyn.localhost.public-key-data", "myPublicKeyData");
    brooklynProperties.put("brooklyn.localhost.private-key-passphrase", "myprivateKeyPassphrase");
    Map<String, Object> conf = resolve("localhost").getAllConfig(true);

    assertEquals(conf.get("privateKeyFile"), "myprivatekeyfile");
    assertEquals(conf.get("publicKeyFile"), "mypublickeyfile");
    assertEquals(conf.get("privateKeyData"), "myprivateKeyData");
    assertEquals(conf.get("publicKeyData"), "myPublicKeyData");
    assertEquals(conf.get("privateKeyPassphrase"), "myprivateKeyPassphrase");
  }
 private static <T> void setFailingIfConflicting(
     BrooklynProperties brooklynProperties, ConfigKey<T> key, T value) {
   Object old = brooklynProperties.put(key, value);
   if (old != null && !old.equals(value)) {
     throw new IllegalStateException(
         "Cannot change value for '" + key + "' from " + old + " to " + value);
   }
 }
  @Test
  public void testPropertyScopePrescedence() {
    brooklynProperties.put("brooklyn.location.named.mynamed", "localhost");

    // prefer those in "named" over everything else
    brooklynProperties.put(
        "brooklyn.location.named.mynamed.privateKeyFile", "privateKeyFile-inNamed");
    brooklynProperties.put(
        "brooklyn.location.localhost.privateKeyFile", "privateKeyFile-inProviderSpecific");
    brooklynProperties.put("brooklyn.localhost.privateKeyFile", "privateKeyFile-inGeneric");

    // prefer those in provider-specific over generic
    brooklynProperties.put(
        "brooklyn.location.localhost.publicKeyFile", "publicKeyFile-inProviderSpecific");
    brooklynProperties.put("brooklyn.location.publicKeyFile", "publicKeyFile-inGeneric");

    // prefer location-generic if nothing else
    brooklynProperties.put("brooklyn.location.privateKeyData", "privateKeyData-inGeneric");

    Map<String, Object> conf = resolve("named:mynamed").getAllConfig(true);

    assertEquals(conf.get("privateKeyFile"), "privateKeyFile-inNamed");
    assertEquals(conf.get("publicKeyFile"), "publicKeyFile-inProviderSpecific");
    assertEquals(conf.get("privateKeyData"), "privateKeyData-inGeneric");
  }
    private static void addGlobalProperties(BrooklynProperties p) {
      String userHome = System.getProperty("user.home");
      File globalPropertiesFile =
          new File(
              userHome
                  + File.separatorChar
                  + ".brooklyn"
                  + File.separatorChar
                  + "brooklyn.properties");

      if (globalPropertiesFile.exists()) {
        p.addFrom(globalPropertiesFile);
      }
    }
 public static BrooklynProperties setEmptyCatalogAsDefault(BrooklynProperties brooklynProperties) {
   if (brooklynProperties == null) return null;
   brooklynProperties.putIfAbsent(
       BrooklynServerConfig.BROOKLYN_CATALOG_URL, "classpath://brooklyn-catalog-empty.xml");
   return brooklynProperties;
 }
  /**
   * [sam] Other tests rely on brooklyn.properties not containing security properties so .. I think
   * the best way to test this is to set a security provider, then reload properties and check no
   * authentication is required.
   *
   * <p>[aled] Changing this test so doesn't rely on brooklyn.properties having no security provider
   * (that can lead to failures locally when running just this test). Asserts
   */
  @Test(groups = "Integration")
  public void testSecurityProviderUpdatesWhenPropertiesReloaded() {
    BrooklynProperties brooklynProperties = BrooklynProperties.Factory.newEmpty();
    brooklynProperties.put("brooklyn.webconsole.security.users", "admin");
    brooklynProperties.put("brooklyn.webconsole.security.user.admin.password", "mypassword");
    UsernamePasswordCredentials defaultCredential =
        new UsernamePasswordCredentials("admin", "mypassword");

    ManagementContext mgmt = new LocalManagementContext(brooklynProperties);

    try {
      Server server =
          useServerForTest(
              BrooklynRestApiLauncher.launcher()
                  .managementContext(mgmt)
                  .withoutJsgui()
                  .securityProvider(TestSecurityProvider.class)
                  .start());
      String baseUri = getBaseUri(server);

      HttpToolResponse response;
      final URI uri = URI.create(getBaseUri() + "/v1/server/properties/reload");
      final Map<String, String> args = Collections.emptyMap();

      // Unauthorised when no credentials, and when default credentials.
      response = HttpTool.httpPost(httpClientBuilder().uri(baseUri).build(), uri, args, args);
      assertEquals(response.getResponseCode(), HttpStatus.SC_UNAUTHORIZED);

      response =
          HttpTool.httpPost(
              httpClientBuilder().uri(baseUri).credentials(defaultCredential).build(),
              uri,
              args,
              args);
      assertEquals(response.getResponseCode(), HttpStatus.SC_UNAUTHORIZED);

      // Accepts TestSecurityProvider credentials, and we reload.
      response =
          HttpTool.httpPost(
              httpClientBuilder().uri(baseUri).credentials(TestSecurityProvider.CREDENTIAL).build(),
              uri,
              args,
              args);
      HttpTestUtils.assertHealthyStatusCode(response.getResponseCode());

      // Has no gone back to credentials from brooklynProperties; TestSecurityProvider credentials
      // no longer work
      response =
          HttpTool.httpPost(
              httpClientBuilder().uri(baseUri).credentials(defaultCredential).build(),
              uri,
              args,
              args);
      HttpTestUtils.assertHealthyStatusCode(response.getResponseCode());

      response =
          HttpTool.httpPost(
              httpClientBuilder().uri(baseUri).credentials(TestSecurityProvider.CREDENTIAL).build(),
              uri,
              args,
              args);
      assertEquals(response.getResponseCode(), HttpStatus.SC_UNAUTHORIZED);

    } finally {
      ((ManagementContextInternal) mgmt).terminate();
    }
  }