com.sap.cloud.security.ams.logging.PolicyEvaluationSlf4jLogger Maven / Gradle / Ivy
/************************************************************************
* © 2019-2023 SAP SE or an SAP affiliate company. All rights reserved. *
************************************************************************/
package com.sap.cloud.security.ams.logging;
import com.sap.cloud.security.ams.dcl.client.pdp.PolicyEvaluationResult;
import com.sap.cloud.security.ams.dcl.client.pdp.PolicyEvaluationResultSerializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import static com.sap.cloud.security.ams.dcl.client.pdp.PolicyEvaluationKind.TECHNICAL;
/**
* This logger is by default active and logs the policy evaluation result using
* SLF4J logger.
*/
public class PolicyEvaluationSlf4jLogger implements Consumer {
protected static final Logger LOGGER = LoggerFactory.getLogger(PolicyEvaluationSlf4jLogger.class);
private final static PolicyEvaluationSlf4jLogger instance = new PolicyEvaluationSlf4jLogger();
private PolicyEvaluationSlf4jLogger() {
// use getInstance() instead
}
public static Consumer getInstance() {
return instance;
}
/**
* Is always called when the
* {@link com.sap.cloud.security.ams.dcl.client.pdp.PolicyDecisionPoint} returns
* with a result.
*
* @param result
* the result of the policy evaluation containing both, the input and
* decision
*/
@Override
public void accept(PolicyEvaluationResult result) {
switch (result.getResultType()) {
case ERROR:
LOGGER.error("Policy evaluation result: {}.", stringifyResult(result));
break;
case DENY:
LOGGER.warn("Policy evaluation result: {}.", stringifyResult(result));
break;
default:
if (result.getKind().equals(TECHNICAL)) {
return;
}
LOGGER.info("Policy evaluation result: {}.", stringifyResult(result));
}
}
private String stringifyResult(PolicyEvaluationResult result) {
final StringBuilder serializedResult = new StringBuilder(256).append("{");
BiConsumer fillSerializedResult = (key, value) -> {
serializedResult.append("\"");
serializedResult.append(key);
serializedResult.append("\":\"");
serializedResult.append(value);
serializedResult.append("\",");
};
PolicyEvaluationResultSerializer.getInstance().process(result, fillSerializedResult);
int index = serializedResult.lastIndexOf(",");
if (index != -1) {
serializedResult.deleteCharAt(index);
}
return serializedResult.append("}").toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy