use of com.evolveum.midpoint.xml.ns._public.common.common_3.ValuePolicyType in project midpoint by Evolveum.
the class ModelInteractionServiceImpl method validateValue.
private <T, O extends ObjectType> boolean validateValue(PrismObject<O> object, ValuePolicyType policy, PolicyItemDefinitionType policyItemDefinition, Task task, OperationResult parentResult) throws ExpressionEvaluationException, SchemaException, ObjectNotFoundException, CommunicationException, ConfigurationException, SecurityViolationException, PolicyViolationException {
ValuePolicyType stringPolicy = resolveValuePolicy(policyItemDefinition, policy, task, parentResult);
RawType rawValue = (RawType) policyItemDefinition.getValue();
String valueToValidate = null;
List<String> valuesToValidate = new ArrayList<>();
PolicyItemTargetType target = policyItemDefinition.getTarget();
ItemPath path = null;
if (target != null) {
path = target.getPath().getItemPath();
}
if (rawValue != null) {
valueToValidate = rawValue.getParsedRealValue(String.class);
valuesToValidate.add(valueToValidate);
} else {
if (target == null || target.getPath() == null) {
LOGGER.error("Target item path must be defined");
parentResult.recordFatalError("Target item path must be defined");
throw new SchemaException("Target item path must be defined");
}
path = target.getPath().getItemPath();
PrismProperty<T> property = object.findProperty(path);
if (property == null || property.isEmpty()) {
LOGGER.error("Attribute {} has no value. Nothing to validate.", property);
parentResult.recordFatalError("Attribute " + property + " has no value. Nothing to validate");
throw new SchemaException("Attribute " + property + " has no value. Nothing to validate");
}
PrismPropertyDefinition<T> itemToValidateDefinition = property.getDefinition();
QName definitionName = itemToValidateDefinition.getTypeName();
if (!isSupportedType(definitionName)) {
LOGGER.error("Trying to validate string policy on the property of type {} failed. Unsupported type.", itemToValidateDefinition);
parentResult.recordFatalError("Trying to validate string policy on the property of type " + itemToValidateDefinition + " failed. Unsupported type.");
throw new SchemaException("Trying to validate string policy on the property of type " + itemToValidateDefinition + " failed. Unsupported type.");
}
if (itemToValidateDefinition.isSingleValue()) {
if (definitionName.equals(PolyStringType.COMPLEX_TYPE)) {
valueToValidate = ((PolyString) property.getRealValue()).getOrig();
} else if (definitionName.equals(ProtectedStringType.COMPLEX_TYPE)) {
ProtectedStringType protectedString = ((ProtectedStringType) property.getRealValue());
valueToValidate = getClearValue(protectedString);
} else {
valueToValidate = (String) property.getRealValue();
}
valuesToValidate.add(valueToValidate);
} else {
if (definitionName.equals(DOMUtil.XSD_STRING)) {
valuesToValidate.addAll(property.getRealValues(String.class));
} else if (definitionName.equals(ProtectedStringType.COMPLEX_TYPE)) {
for (ProtectedStringType protectedString : property.getRealValues(ProtectedStringType.class)) {
valuesToValidate.add(getClearValue(protectedString));
}
} else {
for (PolyString val : property.getRealValues(PolyString.class)) {
valuesToValidate.add(val.getOrig());
}
}
}
}
for (String newValue : valuesToValidate) {
OperationResult result = parentResult.createSubresult(OPERATION_VALIDATE_VALUE + ".value");
if (path != null)
result.addParam("path", path);
result.addParam("valueToValidate", newValue);
if (!policyProcessor.validateValue(newValue, stringPolicy, object, "validate value " + (path != null ? "for " + path : "") + " for " + object + " value " + valueToValidate, task, result)) {
result.recordFatalError("Validation for value " + newValue + " against policy " + stringPolicy + " failed");
LOGGER.error("Validation for value {} against policy {} failed", newValue, stringPolicy);
}
result.computeStatusIfUnknown();
}
parentResult.computeStatus();
policyItemDefinition.setResult(parentResult.createOperationResultType());
return parentResult.isAcceptable();
}
use of com.evolveum.midpoint.xml.ns._public.common.common_3.ValuePolicyType in project midpoint by Evolveum.
the class GenerateExpressionEvaluator method evaluate.
/*
* (non-Javadoc)
*
* @see
* com.evolveum.midpoint.common.expression.ExpressionEvaluator#evaluate(java
* .util.Collection, java.util.Map, boolean, java.lang.String,
* com.evolveum.midpoint.schema.result.OperationResult)
*/
@Override
public PrismValueDeltaSetTriple<V> evaluate(ExpressionEvaluationContext context) throws SchemaException, ExpressionEvaluationException, ObjectNotFoundException {
StringPolicyType stringPolicyType = null;
ObjectReferenceType generateEvaluatorValuePolicyRef = generateEvaluatorType.getValuePolicyRef();
if (generateEvaluatorValuePolicyRef != null) {
if (generateEvaluatorType.getValuePolicyRef() != null) {
ValuePolicyType valuePolicyType = objectResolver.resolve(generateEvaluatorValuePolicyRef, ValuePolicyType.class, null, "resolving value policy reference in generateExpressionEvaluator", context.getTask(), context.getResult());
stringPolicyType = valuePolicyType.getStringPolicy();
}
}
// would be generated
if (stringPolicyType == null) {
StringPolicyResolver stringPolicyResolver = context.getStringPolicyResolver();
if (stringPolicyResolver != null) {
stringPolicyType = stringPolicyResolver.resolve();
}
}
elementStringPolicy = stringPolicyType;
// } else {
// stringPolicyType = elementStringPolicy;
// }
//
String stringValue = null;
GenerateExpressionEvaluatorModeType mode = generateEvaluatorType.getMode();
Item<V, D> output = outputDefinition.instantiate();
if (mode == null || mode == GenerateExpressionEvaluatorModeType.POLICY) {
PrismObject<? extends ObjectType> object = getObject(context);
// TODO: generate value based on stringPolicyType (if not null)
if (stringPolicyType != null) {
if (isNotEmptyMinLength(stringPolicyType)) {
stringValue = valuePolicyGenerator.generate(output.getPath(), stringPolicyType, DEFAULT_LENGTH, true, object, context.getContextDescription(), context.getTask(), context.getResult());
} else {
stringValue = valuePolicyGenerator.generate(output.getPath(), stringPolicyType, DEFAULT_LENGTH, false, object, context.getContextDescription(), context.getTask(), context.getResult());
}
context.getResult().computeStatus();
if (context.getResult().isError()) {
throw new ExpressionEvaluationException("Failed to generate value according to policy: " + stringPolicyType.getDescription() + ". " + context.getResult().getMessage());
}
}
if (stringValue == null) {
int length = DEFAULT_LENGTH;
RandomString randomString = new RandomString(length);
stringValue = randomString.nextString();
}
} else if (mode == GenerateExpressionEvaluatorModeType.UUID) {
UUID randomUUID = UUID.randomUUID();
stringValue = randomUUID.toString();
} else {
throw new ExpressionEvaluationException("Unknown mode for generate expression: " + mode);
}
Object value = ExpressionUtil.convertToOutputValue(stringValue, outputDefinition, protector);
if (output instanceof PrismProperty) {
PrismPropertyValue<Object> pValue = new PrismPropertyValue<Object>(value);
((PrismProperty<Object>) output).add(pValue);
} else {
throw new UnsupportedOperationException("Can only generate values of property, not " + output.getClass());
}
return ItemDelta.toDeltaSetTriple(output, null);
}
use of com.evolveum.midpoint.xml.ns._public.common.common_3.ValuePolicyType in project midpoint by Evolveum.
the class ValuePolicyProcessor method validateValue.
public <O extends ObjectType> boolean validateValue(String newValue, ValuePolicyType pp, PrismObject<O> object, StringBuilder message, String shortDesc, Task task, OperationResult parentResult) throws SchemaException, ObjectNotFoundException, ExpressionEvaluationException {
Validate.notNull(pp, "Value policy must not be null.");
OperationResult result = parentResult.createSubresult(OPERATION_STRING_POLICY_VALIDATION);
result.addParam("policyName", pp.getName());
normalize(pp);
if (newValue == null && (pp.getMinOccurs() == null || XsdTypeMapper.multiplicityToInteger(pp.getMinOccurs()) == 0)) {
// No password is allowed
result.recordSuccess();
return true;
}
if (newValue == null) {
newValue = "";
}
LimitationsType lims = pp.getStringPolicy().getLimitations();
testMinimalLength(newValue, lims, result, message);
testMaximalLength(newValue, lims, result, message);
testMinimalUniqueCharacters(newValue, lims, result, message);
if (lims.getLimit() == null || lims.getLimit().isEmpty()) {
if (message.toString() == null || message.toString().isEmpty()) {
result.computeStatus();
} else {
result.computeStatus(message.toString());
}
return result.isAcceptable();
}
// check limitation
HashSet<String> validChars = null;
HashSet<String> allValidChars = new HashSet<>();
List<String> passwd = StringPolicyUtils.stringTokenizer(newValue);
for (StringLimitType stringLimitationType : lims.getLimit()) {
OperationResult limitResult = new OperationResult("Tested limitation: " + stringLimitationType.getDescription());
validChars = getValidCharacters(stringLimitationType.getCharacterClass(), pp);
int count = countValidCharacters(validChars, passwd);
allValidChars.addAll(validChars);
testMinimalOccurence(stringLimitationType, count, limitResult, message);
testMaximalOccurence(stringLimitationType, count, limitResult, message);
testMustBeFirst(stringLimitationType, count, limitResult, message, newValue, validChars);
limitResult.computeStatus();
result.addSubresult(limitResult);
}
testInvalidCharacters(passwd, allValidChars, result, message);
testCheckExpression(newValue, lims, object, shortDesc, task, result, message);
if (message.toString() == null || message.toString().isEmpty()) {
result.computeStatus();
} else {
result.computeStatus(message.toString());
}
return result.isAcceptable();
}
use of com.evolveum.midpoint.xml.ns._public.common.common_3.ValuePolicyType in project midpoint by Evolveum.
the class RValuePolicy method toJAXB.
@Override
public ValuePolicyType toJAXB(PrismContext prismContext, Collection<SelectorOptions<GetOperationOptions>> options) throws DtoTranslationException {
ValuePolicyType policy = new ValuePolicyType();
RUtil.revive(policy, prismContext);
RValuePolicy.copyToJAXB(this, policy, prismContext, options);
return policy;
}
use of com.evolveum.midpoint.xml.ns._public.common.common_3.ValuePolicyType in project midpoint by Evolveum.
the class TestPasswordPolicy method passwordGeneratorTest.
public void passwordGeneratorTest(final String TEST_NAME, String policyFilename) throws JAXBException, SchemaException, IOException, ExpressionEvaluationException, ObjectNotFoundException {
TestUtil.displayTestTile(TEST_NAME);
Task task = createTask(TEST_NAME);
OperationResult result = task.getResult();
File file = new File(TEST_DIR, policyFilename);
LOGGER.info("Positive testing {}: {}", TEST_NAME, policyFilename);
ValuePolicyType pp = (ValuePolicyType) PrismTestUtil.parseObject(file).asObjectable();
String psswd;
// generate minimal size passwd
for (int i = 0; i < 100; i++) {
psswd = valuePolicyProcessor.generate(SchemaConstants.PATH_PASSWORD_VALUE, pp.getStringPolicy(), 10, true, null, TEST_NAME, task, result);
LOGGER.info("Generated password:" + psswd);
result.computeStatus();
if (!result.isSuccess()) {
LOGGER.info("Result:" + result.debugDump());
AssertJUnit.fail("Password generator failed:\n" + result.debugDump());
}
assertNotNull(psswd);
assertPassword(psswd, pp);
}
// genereata to meet as possible
LOGGER.info("-------------------------");
// Generate up to possible
for (int i = 0; i < 100; i++) {
psswd = valuePolicyProcessor.generate(SchemaConstants.PATH_PASSWORD_VALUE, pp.getStringPolicy(), 10, false, null, TEST_NAME, task, result);
LOGGER.info("Generated password:" + psswd);
result.computeStatus();
if (!result.isSuccess()) {
LOGGER.info("Result:" + result.debugDump());
}
AssertJUnit.assertTrue(result.isSuccess());
assertNotNull(psswd);
}
}
Aggregations