Search in sources :

Example 6 with RestSTSInstanceConfig

use of org.forgerock.openam.sts.rest.config.user.RestSTSInstanceConfig in project OpenAM by OpenRock.

the class RestSTSPublishServiceRequestHandler method handleRead.

public Promise<ResourceResponse, ResourceException> handleRead(Context context, ReadRequest request) {
    try {
        if (EMPTY_STRING.equals(request.getResourcePath())) {
            List<RestSTSInstanceConfig> publishedInstances = publisher.getPublishedInstances();
            JsonObject jsonObject = JsonValueBuilder.jsonValue();
            for (RestSTSInstanceConfig instanceConfig : publishedInstances) {
                jsonObject.put(instanceConfig.getDeploymentSubPath(), mapStringToJson(instanceConfig.toJson().toString()));
            }
            /*
                Note that the revision etag is not set, as this is not a resource which should really be cached.
                If caching becomes necessary, a string composed of the hash codes of each of the RestSTSInstanceConfig
                instances could be used (or a hash of that string).
                 */
            return newResultPromise(newResourceResponse(PUBLISHED_INSTANCES, EMPTY_STRING, jsonObject.build()));
        } else {
            final String realm = getRealmFromResourceName(request.getResourcePath());
            if (!realmValidator.isRealm(realm)) {
                logger.warn("Read of rest STS instance state for instance " + request.getResourcePath() + " in realm " + realm + " rejected because realm does not exist");
                return new NotFoundException("The specified realm does not exist.").asPromise();
            }
            RestSTSInstanceConfig instanceConfig = publisher.getPublishedInstance(request.getResourcePath(), realm);
            return newResultPromise(newResourceResponse(instanceConfig.getDeploymentSubPath(), Integer.toString(instanceConfig.hashCode()), JsonValueBuilder.jsonValue().put(instanceConfig.getDeploymentSubPath(), mapStringToJson(instanceConfig.toJson().toString())).build()));
        }
    } catch (STSPublishException e) {
        String message = "Exception caught obtaining rest sts instance corresponding to id: " + request.getResourcePath() + "; Exception: " + e;
        logger.error(message, e);
        return e.asPromise();
    }
}
Also used : RestSTSInstanceConfig(org.forgerock.openam.sts.rest.config.user.RestSTSInstanceConfig) STSPublishException(org.forgerock.openam.sts.STSPublishException) JsonObject(org.forgerock.openam.utils.JsonObject) NotFoundException(org.forgerock.json.resource.NotFoundException)

Example 7 with RestSTSInstanceConfig

use of org.forgerock.openam.sts.rest.config.user.RestSTSInstanceConfig in project OpenAM by OpenRock.

the class RestSTSInstanceStateProviderTest method verifyLookup.

@Test
public void verifyLookup() throws TokenCreationException, STSPublishException {
    RestSTSInstanceConfig instanceConfig = createSAMLRestInstanceConfig();
    when(mockConfigStore.getSTSInstanceConfig(DEPLOYMENT_URL_ELEMENT, REALM)).thenReturn(instanceConfig);
    assertEquals(provider.getSTSInstanceState(DEPLOYMENT_URL_ELEMENT, REALM).getConfig(), instanceConfig);
}
Also used : RestSTSInstanceConfig(org.forgerock.openam.sts.rest.config.user.RestSTSInstanceConfig) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 8 with RestSTSInstanceConfig

use of org.forgerock.openam.sts.rest.config.user.RestSTSInstanceConfig in project OpenAM by OpenRock.

the class RestSTSInstanceStateProviderTest method verifyCaching.

@Test
public void verifyCaching() throws TokenCreationException, STSPublishException {
    RestSTSInstanceConfig instanceConfig = createSAMLRestInstanceConfig();
    when(mockConfigStore.getSTSInstanceConfig(DEPLOYMENT_URL_ELEMENT, REALM)).thenReturn(instanceConfig);
    //initializes the cache with the mocked config
    provider.getSTSInstanceState(DEPLOYMENT_URL_ELEMENT, REALM);
    verify(mockConfigStore, times(1)).getSTSInstanceConfig(DEPLOYMENT_URL_ELEMENT, REALM);
    //now insure that the config store will return null, to insure that only the cache can return a valid result
    when(mockConfigStore.getSTSInstanceConfig(DEPLOYMENT_URL_ELEMENT, REALM)).thenReturn(null);
    assertEquals(provider.getSTSInstanceState(DEPLOYMENT_URL_ELEMENT, REALM).getConfig(), instanceConfig);
    //the mockConfigStore should only have been called once, with the first invocation of provider.getSTSInstanceState -
    //the second call should be resolved in the caching layer.
    verify(mockConfigStore, times(1)).getSTSInstanceConfig(DEPLOYMENT_URL_ELEMENT, REALM);
}
Also used : RestSTSInstanceConfig(org.forgerock.openam.sts.rest.config.user.RestSTSInstanceConfig) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 9 with RestSTSInstanceConfig

use of org.forgerock.openam.sts.rest.config.user.RestSTSInstanceConfig in project OpenAM by OpenRock.

the class RestSTSInstanceStateServiceListenerTest method testServiceListenerCacheInvalidation.

/**
     * This test will seed the RestSTSInstanceStateProvider class with a RestSTSInstanceConfig instance, which should
     * populate the cache entry. Then the ServiceListener#organizationalConfigChanged method will be invoked, which should
     * invalidate the cache entry.
     */
@Test
public void testServiceListenerCacheInvalidation() throws UnsupportedEncodingException, STSPublishException, TokenCreationException {
    RestSTSInstanceConfig instanceConfig = createInstanceConfig("issuer_one");
    when(mockConfigStore.getSTSInstanceConfig(DEPLOYMENT_URL_ELEMENT, REALM)).thenReturn(instanceConfig);
    //initializes the cache with the mocked config
    provider.getSTSInstanceState(DEPLOYMENT_URL_ELEMENT, REALM);
    //this should invalidate the cache. Lower-case is necessary on the DEPLOYMENT_URL_ELEMENT as
    //due to ldap case-insensitivity.
    serviceListener.organizationConfigChanged(AMSTSConstants.REST_STS_SERVICE_NAME, AMSTSConstants.REST_STS_SERVICE_VERSION, "irrelevant", "irrelevant", DEPLOYMENT_URL_ELEMENT.toLowerCase(), ServiceListener.REMOVED);
    RestSTSInstanceConfig instanceConfig1 = createInstanceConfig("issuer_one_prime");
    //should initialize cache with new entry when getSTSInstanceConfig is called
    when(mockConfigStore.getSTSInstanceConfig(DEPLOYMENT_URL_ELEMENT, REALM)).thenReturn(instanceConfig1);
    RestSTSInstanceConfig providerConfig = provider.getSTSInstanceState(DEPLOYMENT_URL_ELEMENT, REALM).getConfig();
    assertEquals(instanceConfig1, providerConfig);
    assertNotEquals(instanceConfig, providerConfig);
}
Also used : RestSTSInstanceConfig(org.forgerock.openam.sts.rest.config.user.RestSTSInstanceConfig) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

RestSTSInstanceConfig (org.forgerock.openam.sts.rest.config.user.RestSTSInstanceConfig)9 Injector (com.google.inject.Injector)5 ResourceException (org.forgerock.json.resource.ResourceException)5 STSPublishException (org.forgerock.openam.sts.STSPublishException)5 RestSTS (org.forgerock.openam.sts.rest.RestSTS)4 NotFoundException (org.forgerock.json.resource.NotFoundException)3 BeforeTest (org.testng.annotations.BeforeTest)3 Test (org.testng.annotations.Test)3 BadRequestException (org.forgerock.json.resource.BadRequestException)2 STSInitializationException (org.forgerock.openam.sts.STSInitializationException)1 RestSTSInstanceModule (org.forgerock.openam.sts.rest.config.RestSTSInstanceModule)1 JsonObject (org.forgerock.openam.utils.JsonObject)1