Skip to content

Commit

Permalink
Merge branch 'tmp/expr-profiles-hardening'
Browse files Browse the repository at this point in the history
  • Loading branch information
mederly committed Aug 26, 2023
2 parents 9023311 + 3a5cd8f commit ed76447
Show file tree
Hide file tree
Showing 89 changed files with 994 additions and 444 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.prism.util.CloneUtil;
import com.evolveum.midpoint.xml.ns._public.common.common_3.AbstractMappingType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.VariableBindingDefinitionType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;

import com.evolveum.prism.xml.ns._public.types_3.ItemPathType;

import jakarta.xml.bind.JAXBElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -47,4 +47,25 @@ public interface AbstractMappingConfigItem<M extends AbstractMappingType> extend
default @Nullable String getName() {
return value().getName();
}

default void setDefaultStrong() {
if (value().getStrength() == null) {
value().setStrength(MappingStrengthType.STRONG);
}
}

default void setDefaultRelativityAbsolute() {
ExpressionType expression = value().getExpression();
if (expression == null) {
return;
}
for (JAXBElement<?> evaluator : expression.getExpressionEvaluator()) {
Object evaluatorValue = evaluator.getValue();
if (evaluatorValue instanceof TransformExpressionEvaluatorType transform) {
if (transform.getRelativityMode() == null) {
transform.setRelativityMode(TransformExpressionRelativityModeType.ABSOLUTE);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public AssignmentConfigItem(@NotNull AssignmentType value, @NotNull Configuratio
super(value, origin);
}

public static AbstractAssignmentConfigItem of(
public static AssignmentConfigItem of(
@NotNull AssignmentType bean,
@NotNull OriginProvider<? super AssignmentType> originProvider) {
return new AssignmentConfigItem(bean, originProvider.origin(bean));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import org.jetbrains.annotations.NotNull;

/** Unfortunately, this cannot extend MappingConfigItem because of the conflict in generic type parameters. */
public class AutoAssignMappingConfigItem
extends ConfigurationItem<AutoassignMappingType>
implements AbstractMappingConfigItem<AutoassignMappingType> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class ConfigurationItem<T extends Serializable & Cloneable> implements Co

/** For internal use. */
protected ConfigurationItem(
@NotNull ConfigurationItem<T> original) {
@NotNull ConfigurationItem<? extends T> original) {
this.value = original.value;
this.origin = original.origin;
}
Expand Down Expand Up @@ -85,6 +85,13 @@ public ConfigurationItem(
.toList();
}

public static @NotNull <T extends Serializable & Cloneable, X extends ConfigurationItem<T>> List<X> ofList(
@NotNull List<T> items, @NotNull OriginProvider<? super T> originProvider, @NotNull Class<X> clazz) {
return asList(
ofList(items, originProvider),
clazz);
}

@Override
public @NotNull T value() {
return value;
Expand All @@ -109,6 +116,13 @@ protected static <V extends Serializable & Cloneable, CI extends RAW_CI, RAW_CI
return value != null ? value.as(clazz) : null;
}

public static <T extends Serializable & Cloneable, X extends ConfigurationItem<T>> @NotNull List<X> asList(
@NotNull List<ConfigurationItem<T>> list, @NotNull Class<X> clazz) {
return list.stream()
.map(ci -> ci.as(clazz))
.toList();
}

@Override
public <X extends ConfigurationItem<T>> @NotNull X as(@NotNull Class<X> clazz) {
if (clazz.isAssignableFrom(this.getClass())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@
import com.evolveum.midpoint.util.annotation.Experimental;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;

import com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceType;

import org.jetbrains.annotations.NotNull;

import java.io.Serial;
import java.io.Serializable;
import java.util.Objects;

import static com.evolveum.midpoint.util.MiscUtil.argCheck;

/**
* Description of an origin of a configuration item (expression, mapping, and so on).
* Necessary e.g. for the derivation of an {@link ExpressionProfile}.
Expand All @@ -40,17 +44,6 @@ public abstract class ConfigurationItemOrigin implements Serializable {

@Serial private static final long serialVersionUID = 0L;

/**
* TEMPORARY until the implementation is complete.
*
* We may search for occurrences of this method to find all the places where we need to implement the origin determination.
*
* Use with care! Careless use of this origin may render expression profiles ineffective.
*/
public static ConfigurationItemOrigin undetermined() {
return new ConfigurationItemOrigin.Undetermined(false);
}

/** Undetermined but safe, because it is never used to determine the expression profile. */
public static ConfigurationItemOrigin undeterminedSafe() {
return new ConfigurationItemOrigin.Undetermined(true);
Expand Down Expand Up @@ -115,12 +108,23 @@ public static ConfigurationItemOrigin embedded(

public static ConfigurationItemOrigin inObject(
@NotNull ObjectType originatingObject, @NotNull ItemPath path) {
return new InObject(originatingObject, path, true);
return new InObject(originatingObject, true, path, true);
}

/**
* Very approximate origin: we know that the configuration item is used in a given (resolved, i.e., expanded) resource.
* But we don't know how it got there. It may be the resource object itself, but also any super-resource from which
* this resource inherits.
*
* See MID-9018.
*/
public static ConfigurationItemOrigin inResourceOrAncestor(@NotNull ResourceType resource) {
return new InObject(resource, false, ItemPath.EMPTY_PATH, false);
}

public static ConfigurationItemOrigin inObjectApproximate(
@NotNull ObjectType originatingObject, @NotNull ItemPath knownPath) {
return new InObject(originatingObject, knownPath, false);
return new InObject(originatingObject, true, knownPath, false);
}

public static ConfigurationItemOrigin inDelta(
Expand Down Expand Up @@ -190,6 +194,10 @@ public String toString() {
return "detached(" + channelUri + ")";
}

public @NotNull String getChannelUri() {
return channelUri;
}

@Override
public ConfigurationItemOrigin child(@NotNull ItemPath path) {
return this;
Expand Down Expand Up @@ -223,61 +231,70 @@ public ConfigurationItemOrigin child(@NotNull ItemPath path) {
public static class InObject extends ConfigurationItemOrigin {

private final @NotNull ObjectType originatingObject;

/**
* If `false`, we know the object only approximately. This is currently the case for resource inheritance.
* See MID-9018.
*/
private final boolean preciseObject;

private final @NotNull ItemPath path;

/**
* If `false`, we know the position only approximately. The {@link #path} is then all that we know;
* the value can be anywhere in that subtree.
*/
private final boolean precise;
private final boolean precisePath;

private InObject(
@NotNull ObjectType originatingObject, @NotNull ItemPath path, boolean precise) {
@NotNull ObjectType originatingObject, boolean preciseObject, @NotNull ItemPath path, boolean precisePath) {
// explicit nullity check is here for additional safety
this.originatingObject = Objects.requireNonNull(originatingObject);
this.preciseObject = preciseObject;
this.path = path;
this.precise = precise;
this.precisePath = precisePath;
if (!preciseObject) {
argCheck(!precisePath, "Not knowing the object but knowing the path?! %s, %s", originatingObject, path);
}
}

@Override
public @NotNull ConfigurationItemOrigin toApproximate() {
if (precise) {
return new InObject(originatingObject, path, false);
if (precisePath) {
return new InObject(originatingObject, preciseObject, path, false);
} else {
return this;
}
}

public @NotNull ObjectType getOriginatingObject() {
return originatingObject;
}

public @NotNull PrismObject<? extends ObjectType> getOriginatingPrismObject() {
return originatingObject.asPrismObject();
}

public @NotNull String getOriginatingObjectOid() {
return originatingObject.getOid();
}

public @NotNull ItemPath getPath() {
return path;
}

public boolean isPrecise() {
return precise;
}

@Override
public ConfigurationItemOrigin child(@NotNull ItemPath path) {
return precise ?
new InObject(originatingObject, this.path.append(path), true) :
return precisePath ?
new InObject(originatingObject, preciseObject, this.path.append(path), true) :
this;
}

@Override
public String toString() {
return "in " + originatingObject + " @" + path + (precise ? "" : " (approximate)");
var sb = new StringBuilder();
sb.append("in ").append(originatingObject);
if (preciseObject) {
sb.append(" @").append(path);
if (!precisePath) {
sb.append(" (approximate)");
}
} else {
sb.append(" (or some of its ancestors");
}
return sb.toString();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,11 @@ public static GlobalPolicyRuleConfigItem embedded(@NotNull GlobalPolicyRuleType
public GlobalPolicyRuleConfigItem clone() {
return new GlobalPolicyRuleConfigItem(super.clone());
}

public @Nullable MappingConfigItem getCondition() {
return child(
value().getCondition(),
MappingConfigItem.class,
GlobalPolicyRuleType.F_CONDITION);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class MappingConfigItem
implements AbstractMappingConfigItem<MappingType> {

@SuppressWarnings("unused") // called dynamically
public MappingConfigItem(@NotNull ConfigurationItem<MappingType> original) {
public MappingConfigItem(@NotNull ConfigurationItem<? extends MappingType> original) {
super(original);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (C) 2010-2023 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/

package com.evolveum.midpoint.schema.config;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import com.evolveum.midpoint.xml.ns._public.common.common_3.MultiSourceDataHandlingType;

public class MultiSourceDataHandlingConfigItem extends ConfigurationItem<MultiSourceDataHandlingType> {

public MultiSourceDataHandlingConfigItem(@NotNull ConfigurationItem<MultiSourceDataHandlingType> original) {
super(original);
}

public MultiSourceDataHandlingConfigItem(
@NotNull MultiSourceDataHandlingType value, @NotNull ConfigurationItemOrigin origin) {
super(value, origin);
}

public static MultiSourceDataHandlingConfigItem of(
@NotNull MultiSourceDataHandlingType bean,
@NotNull OriginProvider<? super MultiSourceDataHandlingType> originProvider) {
return new MultiSourceDataHandlingConfigItem(bean, originProvider.origin(bean));
}

@Override
public @NotNull String localDescription() {
return "object template multi-source data handling definition";
}

public @Nullable ObjectTemplateMappingConfigItem getDefaultAuthoritativeSource() {
return child(
value().getDefaultAuthoritativeSource(),
ObjectTemplateMappingConfigItem.class,
MultiSourceDataHandlingType.F_DEFAULT_AUTHORITATIVE_SOURCE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (C) 2010-2023 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/

package com.evolveum.midpoint.schema.config;

import org.jetbrains.annotations.NotNull;

import com.evolveum.midpoint.xml.ns._public.common.common_3.MultiSourceItemDefinitionType;

public class MultiSourceItemDefinitionConfigItem extends ConfigurationItem<MultiSourceItemDefinitionType> {

@SuppressWarnings("unused") // called dynamically
public MultiSourceItemDefinitionConfigItem(@NotNull ConfigurationItem<MultiSourceItemDefinitionType> original) {
super(original);
}

public ObjectTemplateMappingConfigItem getSelection() {
return child(
value().getSelection(),
ObjectTemplateMappingConfigItem.class,
MultiSourceItemDefinitionType.F_SELECTION);
}
}

0 comments on commit ed76447

Please sign in to comment.