use of org.forgerock.openam.entitlement.ResourceType in project OpenAM by OpenRock.
the class ApplicationV1FilterTest method resourceTypeCreationOnCreate.
/**
* Verifies that the appropriate resource type is created for the application being created.
*/
@Test
public void resourceTypeCreationOnCreate() throws Exception {
// Given
given(contextHelper.getRealm(context)).willReturn("/abc");
given(contextHelper.getSubject(context)).willReturn(subject);
// Build application JSON representation.
JsonValue jsonValue = json(object(field("name", "testApplication"), TestData.DATA_SET_1.getResources().asJson(), TestData.DATA_SET_1.getActions().asJson(), field("realm", "/abc")));
CreateRequest createRequest = mock(CreateRequest.class);
given(createRequest.getContent()).willReturn(jsonValue);
Set<ResourceType> resourceTypes = Collections.emptySet();
given(resourceTypeService.getResourceTypes(queryFilterCaptor.capture(), eq(subject), eq("/abc"))).willReturn(resourceTypes);
ResourceType resourceType = ResourceType.builder().setName("test").setUUID("some-test-uuid").setActions(TestData.DATA_SET_1.getActions().getUnderlyingMap()).setPatterns(TestData.DATA_SET_1.getResources().getUnderlyingSet()).build();
given(resourceTypeService.saveResourceType(eq(subject), eq("/abc"), resourceTypeCaptor.capture())).willReturn(resourceType);
// When
filter.filterCreate(context, createRequest, requestHandler);
// Then
assertThat(jsonValue.get("resourceTypeUuids").asSet(String.class)).containsOnly("some-test-uuid");
verify(requestHandler).handleCreate(eq(context), eq(createRequest));
ResourceType capturedResourceType = resourceTypeCaptor.getValue();
assertThat(capturedResourceType.getName()).startsWith("testApplicationResourceType");
assertThat(capturedResourceType.getActions()).isEqualTo(TestData.DATA_SET_1.getActions().getUnderlyingMap());
assertThat(capturedResourceType.getPatterns()).isEqualTo(TestData.DATA_SET_1.getResources().getUnderlyingSet());
}
use of org.forgerock.openam.entitlement.ResourceType in project OpenAM by OpenRock.
the class PrivilegeManager method validate.
/**
* Validates the passed policy.
*
* @param privilege
* the policy instance
*
* @throws EntitlementException
* should validator fail
*/
protected void validate(Privilege privilege) throws EntitlementException {
final String pName = privilege.getName();
if (pName == null || pName.trim().isEmpty()) {
throw new EntitlementException(EntitlementException.EMPTY_PRIVILEGE_NAME);
}
final Entitlement entitlement = privilege.getEntitlement();
if (entitlement == null) {
throw new EntitlementException(EntitlementException.NULL_ENTITLEMENT);
}
privilege.validateSubject(privilege.getSubject());
ApplicationService applicationService = applicationServiceFactory.create(adminSubject, realm);
Application application = applicationService.getApplication(entitlement.getApplicationName());
if (application == null) {
throw new EntitlementException(EntitlementException.APP_RETRIEVAL_ERROR, realm);
}
if (CollectionUtils.isEmpty(application.getResourceTypeUuids())) {
if (StringUtils.isNotEmpty(privilege.getResourceTypeUuid())) {
throw new EntitlementException(EntitlementException.NO_RESOURCE_TYPE_EXPECTED);
}
// If no resource types have been defined then the following resource type validation is irrelevant.
return;
}
if (!application.getResourceTypeUuids().contains(privilege.getResourceTypeUuid())) {
throw new EntitlementException(EntitlementException.POLICY_DEFINES_INVALID_RESOURCE_TYPE, privilege.getResourceTypeUuid());
}
final ResourceType resourceType = resourceTypeService.getResourceType(superAdminSubject, realm, privilege.getResourceTypeUuid());
if (resourceType == null) {
throw new EntitlementException(EntitlementException.NO_SUCH_RESOURCE_TYPE, privilege.getResourceTypeUuid(), realm);
}
validator.verifyActions(entitlement.getActionValues().keySet()).against(resourceType).throwExceptionIfFailure();
validator.verifyResources(entitlement.getResourceNames()).using(entitlement.getResourceComparator(superAdminSubject, realm)).against(resourceType).throwExceptionIfFailure();
}
use of org.forgerock.openam.entitlement.ResourceType in project OpenAM by OpenRock.
the class ResourceTypeServiceTest method shouldModifyResourceTypeMetaData.
@Test
public void shouldModifyResourceTypeMetaData() throws EntitlementException {
// given
ResourceType resourceType = ResourceType.builder().generateUUID().setName("URL").setDescription("This is a URL resource type").addPattern("*://*:*/*").addPattern("*://*:*/*?*").addAction("GET", true).addAction("PUT", false).build();
// when
resourceType = service.saveResourceType(subject, "/testRealm", resourceType);
// then
assertNotNull(resourceType.getCreatedBy());
assertEquals(resourceType.getCreatedBy(), TEST_USER);
assertNotNull(resourceType.getCreationDate());
assertNotEquals(resourceType.getCreationDate(), 0);
assertNotNull(resourceType.getLastModifiedBy());
assertEquals(resourceType.getLastModifiedBy(), TEST_USER);
assertNotNull(resourceType.getLastModifiedDate());
assertNotEquals(resourceType.getLastModifiedDate(), 0);
}
use of org.forgerock.openam.entitlement.ResourceType in project OpenAM by OpenRock.
the class ApplicationV1FilterTransformerTest method testSuccessfulTransformation.
@Test
public void testSuccessfulTransformation() throws EntitlementException {
//given
JsonValue jsonValue = json(object(field(RESOURCE_TYPE_UUIDS, array("abc-def-ghi", "def-ghj-kli"))));
Subject subject = new Subject();
ResourceType resourceType = ResourceType.builder().setName("name").setUUID("uuid").addAction("action", true).addPattern("pattern").build();
ResourceType resourceType2 = ResourceType.builder().setName("name2").setUUID("uuid2").addAction("action2", true).addPattern("pattern2").build();
given(mockResourceTypeService.getResourceType(eq(subject), eq("realm"), eq("abc-def-ghi"))).willReturn(resourceType);
given(mockResourceTypeService.getResourceType(eq(subject), eq("realm"), eq("def-ghj-kli"))).willReturn(resourceType2);
//when
transformer.transformJson(jsonValue, subject, "realm");
//then
assertTrue(jsonValue.get(RESOURCE_TYPE_UUIDS).isNull());
assertTrue(jsonValue.get("actions").get("action").asBoolean());
assertTrue(jsonValue.get("actions").get("action2").asBoolean());
assertTrue(jsonValue.get("resources").contains("pattern"));
assertTrue(jsonValue.get("resources").contains("pattern2"));
}
use of org.forgerock.openam.entitlement.ResourceType in project OpenAM by OpenRock.
the class ConfigureOAuth2 method getUrlResourceTypeId.
private String getUrlResourceTypeId(Subject adminSubject, String realm) throws EntitlementException, WorkflowException {
Application application = ApplicationManager.getApplication(adminSubject, realm, POLICY_APPLICATION_NAME);
if (application == null) {
ApplicationType applicationType = ApplicationTypeManager.getAppplicationType(adminSubject, ApplicationTypeManager.URL_APPLICATION_TYPE_NAME);
application = ApplicationManager.newApplication(POLICY_APPLICATION_NAME, applicationType);
}
Set<String> resourceTypeIds = application.getResourceTypeUuids();
ResourceTypeService resourceTypeService = InjectorHolder.getInstance(ResourceTypeService.class);
for (String id : resourceTypeIds) {
ResourceType resourceType = resourceTypeService.getResourceType(adminSubject, realm, id);
if (POLICY_RESOURCE_TYPE_NAME.equalsIgnoreCase(resourceType.getName())) {
return id;
}
}
QueryFilter<SmsAttribute> name = equalTo(SmsAttribute.newSearchableInstance("name"), POLICY_RESOURCE_TYPE_NAME);
Set<ResourceType> types = resourceTypeService.getResourceTypes(name, adminSubject, realm);
ResourceType resourceType;
if (types == null || types.isEmpty()) {
resourceType = ResourceType.builder().addPatterns(asSet("*://*:*/*/authorize?*")).addActions(new ImmutableMap.Builder<String, Boolean>().put("GET", true).put("POST", true).build()).setName(POLICY_RESOURCE_TYPE_NAME).setUUID(UUID.randomUUID().toString()).build();
resourceType = resourceTypeService.saveResourceType(adminSubject, realm, resourceType);
} else {
resourceType = types.iterator().next();
}
application.addAllResourceTypeUuids(asSet(resourceType.getUUID()));
application.setEntitlementCombiner(DenyOverride.class);
ApplicationManager.saveApplication(adminSubject, realm, application);
return resourceType.getUUID();
}
Aggregations