All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sap.cloud.security.ams.dcn.DcnPolicyDecisionPointFactory Maven / Gradle / Ivy

Go to download

Client Library for integrating Jakarta EE applications with SAP Authorization Management Service (AMS)

The newest version!
/************************************************************************
 * © 2019-2024 SAP SE or an SAP affiliate company. All rights reserved. *
 ************************************************************************/
package com.sap.cloud.security.ams.dcn;

import com.sap.cloud.security.ams.dcl.client.pdp.PolicyDecisionPoint;
import com.sap.cloud.security.ams.dcl.client.pdp.PolicyDecisionPointFactory;
import com.sap.cloud.security.ams.logging.PolicyEvaluationSlf4jLogger;
import com.sap.cloud.security.config.Environments;
import java.io.File;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DcnPolicyDecisionPointFactory extends PolicyDecisionPointFactory {
  public static final String DEFAULT = "default";
  public static final String DCN_SERVER = "server:dcn-new";
  public static final String ARG_SOURCES = "sources";
  public static final String SKIP_CACHE = "skip_cache";
  // Overwrites the DEFAULT of AmsPolicyDecisionPointFactory by priority
  private static final int PRIORITY = 100;
  private static final Map CACHE = new ConcurrentHashMap<>();

  private static final Logger LOGGER = LoggerFactory.getLogger(DcnPolicyDecisionPointFactory.class);

  @Override
  protected PolicyDecisionPoint tryCreate(String kind, Object[] arguments) {
    return getOrCreate(kind, arguments);
  }

  private PolicyDecisionPoint create(String kind, Object[] arguments) {
    if (DEFAULT.equalsIgnoreCase(kind) || DCN_SERVER.equals(kind)) {
      if (toMap(arguments).get(ARG_SOURCES) instanceof File argValue) {
        LOGGER.debug("Creating DcnPolicyDecisionPoint with sources from {}", argValue);
        return new DcnPolicyDecisionPoint(new LocalDirectoryUpdater(argValue.toString()), true);
      } else {
        var updaterResult =
            BundleGatewayUpdater.create(
                () -> BundleGatewayUpdater.environmentToConnectionData(Environments.getCurrent()),
                BundleGatewayUpdater.buildHttpClient);

        if (updaterResult.isSuccess()) {
          return new DcnPolicyDecisionPoint(updaterResult.getValue(), true);
        } else {
          LOGGER.error(updaterResult.getError().errorMsg());
          return new DcnPolicyDecisionPoint();
        }
      }
    }
    return null;
  }

  PolicyDecisionPoint getOrCreate(String kind, Object[] arguments) {
    if (toMap(arguments).get(SKIP_CACHE) instanceof Boolean skipCache && skipCache) {
      LOGGER.debug("skipping cache for kind {} and args {}.", kind, arguments);
      return createAndRegisterLogger(kind, arguments);
    }

    String key = kind + Arrays.toString(arguments);
    PolicyDecisionPoint pdp = CACHE.get(key);
    if (Objects.isNull(pdp) || pdp.isClosed()) {
      synchronized (CACHE) {
        pdp = CACHE.get(key);
        if (Objects.isNull(pdp) || pdp.isClosed()) {
          LOGGER.debug("instantiate PolicyDecisionPoint for kind {} and args {}.", kind, arguments);
          pdp = createAndRegisterLogger(kind, arguments);
          CACHE.put(key, pdp);
        }
      }
    }
    return CACHE.get(key);
  }

  PolicyDecisionPoint createAndRegisterLogger(String kind, Object[] arguments) {
    PolicyDecisionPoint pdp = create(kind, arguments);
    if (pdp == null) {
      return null; // indicates that this factory is not responsible for the given kind
    }
    pdp.registerListener(PolicyEvaluationSlf4jLogger.getInstance());
    return pdp;
  }

  @Override
  protected int getPriority() {
    return PRIORITY;
  }

  private static Map toMap(Object[] arguments) {
    Map map = new ConcurrentHashMap<>();
    for (int i = 0; i < arguments.length - 1; i += 2) {
      map.put(arguments[i], arguments[i + 1]);
    }
    return map;
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy