@BeforeMethod(alwaysRun = true)
 public void setUp() throws Exception {
   currentTime = new AtomicLong(System.currentTimeMillis());
   ticker =
       new Ticker() {
         // strictly not a ticker because returns millis UTC, but it works fine even so
         @Override
         public long read() {
           return currentTime.get();
         }
       };
   promotionListener = new RecordingPromotionListener();
   managementContext = newLocalManagementContext();
   ownNodeId = managementContext.getManagementNodeId();
   objectStore = newPersistenceObjectStore();
   objectStore.injectManagementContext(managementContext);
   objectStore.prepareForUse(PersistMode.CLEAN, HighAvailabilityMode.DISABLED);
   persister =
       new ManagementPlaneSyncRecordPersisterToObjectStore(
           managementContext, objectStore, classLoader);
   BrooklynMementoPersisterToObjectStore persisterObj =
       new BrooklynMementoPersisterToObjectStore(objectStore, classLoader);
   managementContext.getRebindManager().setPersister(persisterObj);
   manager =
       new HighAvailabilityManagerImpl(managementContext)
           .setPollPeriod(Duration.millis(10))
           .setHeartbeatTimeout(Duration.THIRTY_SECONDS)
           .setPromotionListener(promotionListener)
           .setTicker(ticker)
           .setPersister(persister);
 }
  public void addChild(Location child) {
    // Previously, setParent delegated to addChildLocation and we sometimes ended up with
    // duplicate entries here. Instead this now uses a similar scheme to
    // AbstractLocation.setParent/addChild (with any weaknesses for distribution that such a
    // scheme might have...).
    //
    // We continue to use a list to allow identical-looking locations, but they must be different
    // instances.

    synchronized (children) {
      for (Location contender : children) {
        if (contender == child) {
          // don't re-add; no-op
          return;
        }
      }

      children.add(child);
    }

    if (isManaged()) {
      Locations.manage(child, managementContext);
    } else if (managementContext != null) {
      if (((LocalLocationManager) managementContext.getLocationManager())
              .getLocationEvenIfPreManaged(child.getId())
          == null) {
        ((ManagementContextInternal) managementContext).prePreManage(child);
      }
    }

    children.add(child);
    child.setParent(this);
  }
  public void setManagementContext(ManagementContextInternal managementContext) {
    this.managementContext = managementContext;
    if (displayNameAutoGenerated && id != null)
      name.set(getClass().getSimpleName() + ":" + id.substring(0, Math.min(id.length(), 4)));

    Location oldParent = parent.get();
    Set<Location> oldChildren = children;
    Map<String, Object> oldConfig = configBag.getAllConfig();
    Long oldCreationTimeUtc = creationTimeUtc.get();
    String oldDisplayName = name.get();
    HostGeoInfo oldHostGeoInfo = hostGeoInfo.get();

    parent = managementContext.getStorage().getReference(id + "-parent");
    children =
        SetFromLiveMap.create(
            managementContext.getStorage().<Location, Boolean>getMap(id + "-children"));
    creationTimeUtc = managementContext.getStorage().getReference(id + "-creationTime");
    hostGeoInfo = managementContext.getStorage().getReference(id + "-hostGeoInfo");
    name = managementContext.getStorage().getReference(id + "-displayName");

    // Only override stored defaults if we have actual values. We might be in setManagementContext
    // because we are reconstituting an existing entity in a new brooklyn management-node (in which
    // case believe what is already in the storage), or we might be in the middle of creating a new
    // entity. Normally for a new entity (using EntitySpec creation approach), this will get called
    // before setting the parent etc. However, for backwards compatibility we still support some
    // things calling the entity's constructor directly.
    if (oldParent != null) parent.set(oldParent);
    if (oldChildren.size() > 0) children.addAll(oldChildren);
    if (creationTimeUtc.isNull()) creationTimeUtc.set(oldCreationTimeUtc);
    if (hostGeoInfo.isNull()) hostGeoInfo.set(oldHostGeoInfo);
    if (name.isNull()) {
      name.set(oldDisplayName);
    } else {
      displayNameAutoGenerated = false;
    }

    configBag =
        ConfigBag.newLiveInstance(
            managementContext.getStorage().<String, Object>getMap(id + "-config"));
    if (oldConfig.size() > 0) {
      configBag.putAll(oldConfig);
    }
  }
  /**
   * [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();
    }
  }
Example #5
0
 @AfterMethod(alwaysRun = true)
 public void tearDown() {
   if (app != null) Entities.destroy(app);
   if (managementContext instanceof ManagementContextInternal)
     ((ManagementContextInternal) managementContext).terminate();
 }