PolicyDecisionPoint.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */

  20. package org.openspcoop2.utils.xacml;

  21. import java.io.ByteArrayInputStream;
  22. import java.io.IOException;
  23. import java.util.HashMap;
  24. import java.util.List;
  25. import java.util.Map;

  26. import org.herasaf.xacml.core.SyntaxException;
  27. import org.herasaf.xacml.core.api.PDP;
  28. import org.herasaf.xacml.core.api.PolicyRetrievalPoint;
  29. import org.herasaf.xacml.core.context.RequestMarshaller;
  30. import org.herasaf.xacml.core.context.impl.RequestType;
  31. import org.herasaf.xacml.core.context.impl.ResponseType;
  32. import org.herasaf.xacml.core.context.impl.ResultType;
  33. import org.herasaf.xacml.core.policy.Evaluatable;
  34. import org.herasaf.xacml.core.policy.PolicyMarshaller;
  35. import org.herasaf.xacml.core.policy.impl.EvaluatableIDImpl;
  36. import org.herasaf.xacml.core.policy.impl.PolicyType;
  37. import org.herasaf.xacml.core.simplePDP.MapBasedSimplePolicyRepository;
  38. import org.herasaf.xacml.core.simplePDP.SimplePDPConfiguration;
  39. import org.herasaf.xacml.core.simplePDP.SimplePDPFactory;
  40. import org.herasaf.xacml.core.simplePDP.initializers.InitializerExecutor;
  41. import org.openspcoop2.utils.LoggerWrapperFactory;
  42. import org.slf4j.Logger;

  43. /**
  44.  * PolicyDecisionPoint
  45.  *
  46.  * @author Bussu Giovanni (bussu@link.it)
  47.  * @author $Author$
  48.  * @version $Rev$, $Date$
  49.  */
  50. public class PolicyDecisionPoint {

  51.     private static final String SINGLE = "SINGLE";
  52.     private Map<String, PDP> pdp;
  53.     private boolean singlePDP;
  54.     private Logger log;

  55.     public static void runInitializers() {
  56.         InitializerExecutor.runInitializers(); //Inizializza gli unmarshaller
  57.     }
  58.    
  59.     public PolicyDecisionPoint() throws PolicyException {
  60.         this(LoggerWrapperFactory.getLogger(PolicyDecisionPoint.class));
  61.     }

  62.     public PolicyDecisionPoint(Logger log) throws PolicyException {
  63.         this(log, true);
  64.     }

  65.     public PolicyDecisionPoint(boolean singlePDP) throws PolicyException {
  66.         this(LoggerWrapperFactory.getLogger(PolicyDecisionPoint.class), singlePDP);
  67.     }

  68.     public PolicyDecisionPoint(Logger log, boolean singlePDP) throws PolicyException {
  69.         runInitializers();
  70.         this.singlePDP=singlePDP;
  71.         this.pdp = new HashMap<String, PDP>();
  72.         this.log = log;
  73.         if(singlePDP) {
  74.             this.pdp.put(SINGLE, this.newPDP());
  75.         }
  76.     }
  77.    
  78.     private PDP newPDP() throws PolicyException {
  79.         SimplePDPConfiguration configuration = new SimplePDPConfiguration();
  80.         PolicyRetrievalPoint policyRetrievalPoint = new CachedMapBasedSimplePolicyRepository(this.log);
  81.         configuration.setPolicyRetrievalPoint(policyRetrievalPoint);
  82.         return SimplePDPFactory.getSimplePDP(configuration);
  83.     }

  84.     public List<ResultType> evaluate(String requestString) throws PolicyException {
  85.         RequestType request;
  86.         try {
  87.             request = _unmarshalRequest(requestString);
  88.         } catch (SyntaxException e) {
  89.             throw new PolicyException(e);
  90.         }
  91.         return this._evaluate(request);
  92.     }

  93.     public List<ResultType> evaluate(XacmlRequest request) throws PolicyException {
  94.         return this.evaluate(request.getXacmlRequest());
  95.     }
  96.    
  97.     public List<ResultType> evaluate(RequestType request) throws PolicyException {
  98.         return this._evaluate(request);
  99.     }

  100.     public static RequestType _unmarshalRequest(String requestString) throws PolicyException, SyntaxException {
  101.         ByteArrayInputStream bais = null;
  102.         try{
  103.             InitializerExecutor.runInitializers(); //Inizializza gli unmarshaller
  104.             bais = new ByteArrayInputStream(requestString.getBytes());
  105.             return RequestMarshaller.unmarshal(bais);
  106.         } finally {
  107.             if(bais != null) {
  108.                 try {
  109.                     bais.close();
  110.                 } catch (IOException e) {}
  111.             }
  112.         }
  113.     }

  114.     public static boolean isValidRequest(String requestString) throws PolicyException {
  115.         try{
  116.             _unmarshalRequest(requestString);
  117.         } catch(SyntaxException e) {
  118.             return false;
  119.         }
  120.         return true;
  121.     }

  122.     public static Evaluatable _unmarshalPolicy(String policyString) throws PolicyException, SyntaxException {
  123.         ByteArrayInputStream bais = null;
  124.         try{
  125.             bais = new ByteArrayInputStream(policyString.getBytes());
  126.             InitializerExecutor.runInitializers(); //Inizializza gli unmarshaller
  127.             return PolicyMarshaller.unmarshal(bais);
  128.         } finally {
  129.             if(bais != null) {
  130.                 try {
  131.                     bais.close();
  132.                 } catch (IOException e) {}
  133.             }
  134.         }
  135.     }

  136.     public void addPolicy(Evaluatable eval, String key) throws PolicyException {
  137.         this._addPolicy(eval, key);
  138.     }

  139.     public void addPolicy(String policyString, String key) throws PolicyException {
  140.         try {
  141.             Evaluatable eval;
  142.             eval = _unmarshalPolicy(policyString);
  143.             ((PolicyType)eval).setPolicyId(new EvaluatableIDImpl(key));
  144.             this._addPolicy(eval, key);
  145.         } catch (SyntaxException e) {
  146.             throw new PolicyException(e);
  147.         }
  148.     }

  149.     private void _addPolicy(Evaluatable eval, String key) throws PolicyException {
  150.        
  151.         EvaluatableIDImpl policyId = new EvaluatableIDImpl(key);
  152.         ((PolicyType)eval).setPolicyId(policyId);

  153.         if(this.singlePDP) {
  154.             PDP pdp = this.getPDP(SINGLE);
  155.             MapBasedSimplePolicyRepository repo = (MapBasedSimplePolicyRepository)pdp.getPolicyRepository();
  156.             repo.deploy(eval);
  157.         } else {
  158.             PDP newPDP = this.newPDP();
  159.             MapBasedSimplePolicyRepository repo = (MapBasedSimplePolicyRepository)newPDP.getPolicyRepository();
  160.             repo.deploy(eval);
  161.             this.pdp.put(key, newPDP);
  162.         }
  163.     }

  164.     private List<ResultType> _evaluate(RequestType request) throws PolicyException {
  165.         PDP pdp = (this.singlePDP) ? this.getPDP(SINGLE): this.getPDP(request);
  166.         ResponseType response = pdp.evaluate(request);
  167.         return response.getResults();
  168.     }

  169.     private PDP getPDP(RequestType request) throws PolicyException {
  170.         if(request == null) {
  171.             throw new PolicyException("request non puo essere null");
  172.         }
  173.        
  174.         if(request.getAction() == null) {
  175.             throw new PolicyException("request.action non puo essere null");
  176.         }
  177.        
  178.         if(request.getAction().getAttributes() == null || request.getAction().getAttributes().isEmpty()) {
  179.             throw new PolicyException("request.action.attributes non puo essere null o vuoto");
  180.         }
  181.        
  182.         if(request.getAction().getAttributes().get(0).getAttributeValues() == null || request.getAction().getAttributes().get(0).getAttributeValues().isEmpty()) {
  183.             throw new PolicyException("request.action.attributes[0].attributeValues non puo essere null o vuoto");
  184.         }
  185.        
  186.         if(request.getAction().getAttributes().get(0).getAttributeValues().get(0).getContent() == null || request.getAction().getAttributes().get(0).getAttributeValues().get(0).getContent().isEmpty()) {
  187.             throw new PolicyException("request.action.attributes[0].attributeValues[0].content non puo essere null o vuoto");
  188.         }
  189.        
  190.         String key = (String) request.getAction().getAttributes().get(0).getAttributeValues().get(0).getContent().get(0);
  191.         return this.pdp.get(key);
  192.     }
  193.    
  194.     private PDP getPDP(String key) {
  195.         return this.pdp.get(key);
  196.     }

  197. }