use of com.sun.identity.entitlement.xacml3.XACMLExportImport.ImportStep in project OpenAM by OpenRock.
the class XacmlServiceTest method testImportXACMLDryRun.
@Test
public void testImportXACMLDryRun() throws Exception {
//given
query.add("dryrun", "true");
Representation representation = mock(Representation.class);
InputStream is = new ByteArrayInputStream("Hello World".getBytes());
doReturn(is).when(representation).getStream();
StubPrivilege privilege = new StubPrivilege();
privilege.setName("fred");
XACMLExportImport.ImportStep importStep = mock(XACMLExportImport.ImportStep.class);
doReturn(XACMLExportImport.DiffStatus.ADD).when(importStep).getDiffStatus();
doReturn(privilege).when(importStep).getPrivilege();
List<ImportStep> steps = Arrays.asList(importStep);
doReturn(steps).when(importExport).importXacml(eq("/"), eq(is), any(Subject.class), eq(true));
//when
Representation result = service.importXACML(representation);
//then
assertThat(result).isInstanceOf(JacksonRepresentation.class);
Map<String, Object> resultMap = JsonValueBuilder.toJsonArray(result.getText()).get(0).asMap();
assertThat(resultMap).contains(entry("status", "A"), entry("name", "fred"));
verify(response).setStatus(Status.SUCCESS_OK);
}
use of com.sun.identity.entitlement.xacml3.XACMLExportImport.ImportStep in project OpenAM by OpenRock.
the class XACMLExportImportTest method canImportPrivilegesIntoRealm.
@Test
public void canImportPrivilegesIntoRealm() throws Exception {
// Given
// shared test state
Privilege privilegeToUpdate = existing(valid(privilege("p1")));
Privilege privilegeToAdd = notExisting(valid(privilege("p2")));
PrivilegeSet privilegeSet = new PrivilegeSet(Collections.<ReferralPrivilege>emptyList(), asList(privilegeToUpdate, privilegeToAdd));
given(xacmlReaderWriter.read(eq(NULL_INPUT))).willReturn(privilegeSet);
// When
List<ImportStep> importSteps = xacmlExportImport.importXacml(ROOT_REALM, NULL_INPUT, NULL_SUBJECT, false);
// Then
assertThat(importSteps).hasSize(2);
assertImportStep(importSteps.get(0), DiffStatus.UPDATE, privilegeToUpdate);
assertImportStep(importSteps.get(1), DiffStatus.ADD, privilegeToAdd);
verify(validator).validatePrivilege(privilegeToAdd);
verify(validator).validatePrivilege(privilegeToUpdate);
verify(pm).add(privilegeToAdd);
verify(pm).modify(privilegeToUpdate);
}
use of com.sun.identity.entitlement.xacml3.XACMLExportImport.ImportStep in project OpenAM by OpenRock.
the class XACMLExportImportTest method canPerformAnImportDryRun.
@Test
public void canPerformAnImportDryRun() throws Exception {
// Given
// shared test state
Privilege privilegeToUpdate = existing(valid(privilege("p1")));
Privilege privilegeToAdd = notExisting(valid(privilege("p2")));
PrivilegeSet privilegeSet = new PrivilegeSet(Collections.<ReferralPrivilege>emptyList(), asList(privilegeToUpdate, privilegeToAdd));
given(xacmlReaderWriter.read(eq(NULL_INPUT))).willReturn(privilegeSet);
// When
List<ImportStep> importSteps = xacmlExportImport.importXacml(ROOT_REALM, NULL_INPUT, NULL_SUBJECT, true);
// Then
assertThat(importSteps).hasSize(2);
assertImportStep(importSteps.get(0), DiffStatus.UPDATE, privilegeToUpdate);
assertImportStep(importSteps.get(1), DiffStatus.ADD, privilegeToAdd);
verify(validator).validatePrivilege(privilegeToAdd);
verify(validator).validatePrivilege(privilegeToUpdate);
verify(pm, times(0)).add(any(Privilege.class));
verify(pm, times(0)).modify(any(Privilege.class));
}
use of com.sun.identity.entitlement.xacml3.XACMLExportImport.ImportStep in project OpenAM by OpenRock.
the class XacmlService method importXACML.
/**
* Expects to receive XACML formatted XML which will be read and imported.
*/
@Post
public Representation importXACML(Representation entity) {
boolean dryRun = "true".equalsIgnoreCase(getQuery().getFirstValue("dryrun"));
List<ImportStep> steps;
try {
if (!checkPermission("MODIFY")) {
// not allowed
throw new ResourceException(new Status(FORBIDDEN));
}
String realm = RestletRealmRouter.getRealmFromRequest(getRequest());
steps = importExport.importXacml(realm, entity.getStream(), getAdminToken(), dryRun);
if (steps.isEmpty()) {
throw new ResourceException(new Status(BAD_REQUEST, "No policies found in XACML document", null, null));
}
List<Map<String, String>> result = new ArrayList<Map<String, String>>();
for (XACMLExportImport.ImportStep step : steps) {
Map<String, String> stepResult = new HashMap<String, String>();
stepResult.put("status", String.valueOf(step.getDiffStatus().getCode()));
stepResult.put("name", step.getPrivilege().getName());
result.add(stepResult);
}
getResponse().setStatus(Status.SUCCESS_OK);
return jacksonRepresentationFactory.create(result);
} catch (EntitlementException e) {
debug.warning("Importing XACML to policies failed", e);
throw new ResourceException(new Status(BAD_REQUEST, e, e.getLocalizedMessage(getRequestLocale()), null, null));
} catch (IOException e) {
debug.warning("Reading XACML import failed", e);
throw new ResourceException(new Status(BAD_REQUEST, e, e.getLocalizedMessage(), null, null));
}
}
use of com.sun.identity.entitlement.xacml3.XACMLExportImport.ImportStep in project OpenAM by OpenRock.
the class CreateXACML method handleRequest.
/**
* Services the command line request to import XACML.
*
* Required Arguments:
* realm - Defines the realm the Policies will be imported into.
* xmlfile - References the XACML file from which the Policies should be read.
*
* Optional Arguments:
* dryrun - Optional flag indicates that, rather than carrying out the import,
* a report of anticipated affects should be generated.
* outfile - Optional reference to a file for dryrun report to be written, if not provided
* the dryrun report is written directly to stdout.
*
* @param rc Request Context.
* @throws CLIException if the request cannot serviced.
*/
public void handleRequest(RequestContext rc) throws CLIException {
super.handleRequest(rc);
ldapLogin();
SSOToken adminSSOToken = getAdminSSOToken();
Subject adminSubject = SubjectUtils.createSubject(adminSSOToken);
String realm = getStringOptionValue(IArgument.REALM_NAME);
ensureEntitlementServiceActive(adminSubject, realm);
InputStream xacmlInputStream = getXacmlInputStream(realm);
logStart(realm);
if (!XACMLUtils.hasPermission(realm, adminSSOToken, "MODIFY")) {
String errorMessage = MessageFormat.format(getResourceString("permission-denied"), "create-xacml", getAdminID());
CLIException clie = new CLIException(errorMessage, ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
logException(realm, clie);
throw clie;
}
List<ImportStep> importSteps;
try {
PrivilegeValidator privilegeValidator = new PrivilegeValidator(new RealmValidator(new OrganizationConfigManager(adminSSOToken, realm)));
XACMLExportImport xacmlExportImport = new XACMLExportImport(new XACMLExportImport.PrivilegeManagerFactory(), new XACMLReaderWriter(), privilegeValidator, new SearchFilterFactory(), PrivilegeManager.debug);
importSteps = xacmlExportImport.importXacml(realm, xacmlInputStream, adminSubject, isDryRun());
} catch (EntitlementException e) {
debugError("CreateXACML.handleRequest", e);
logException(realm, e);
throw new CLIException(e, ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
} catch (SMSException e) {
debugError("CreateXACML.handleRequest", e);
logException(realm, e);
throw new CLIException(e, ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
}
if (importSteps.isEmpty()) {
String message = getResourceString("no-policies-provided");
logNothingToImport(realm, message);
getOutputWriter().printlnMessage(message);
} else {
logSuccess(realm);
if (isDryRun()) {
outputDryRunResults(importSteps);
} else {
getOutputWriter().printlnMessage(MessageFormat.format(getResourceString("create-policy-in-realm-succeed"), realm));
}
}
}
Aggregations