DriverBYOKUtilities.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.pdd.core.byok;

  21. import java.util.HashMap;
  22. import java.util.Map;

  23. import org.apache.commons.lang.StringUtils;
  24. import org.openspcoop2.core.byok.BYOKUtilities;
  25. import org.openspcoop2.core.byok.BYOKWrappedValue;
  26. import org.openspcoop2.pdd.config.ConfigurazioneNodiRuntime;
  27. import org.openspcoop2.pdd.config.ConfigurazioneNodiRuntimeBYOKRemoteConfig;
  28. import org.openspcoop2.pdd.core.dynamic.DynamicInfo;
  29. import org.openspcoop2.pdd.core.dynamic.DynamicUtils;
  30. import org.openspcoop2.utils.UtilsException;
  31. import org.openspcoop2.utils.certificate.byok.BYOKManager;
  32. import org.slf4j.Logger;

  33. /**
  34.  * DriverBYOKUtilities
  35.  *
  36.  * @author Andrea Poli (poli@link.it)
  37.  * @author $Author$
  38.  * @version $Rev$, $Date$
  39.  */
  40. public class DriverBYOKUtilities {

  41.     public static DriverBYOK newInstanceDriverBYOKRuntimeNode(Logger log, boolean wrap, boolean unwrap) throws UtilsException {
  42.         return newInstanceDriverBYOKRuntimeNode(log, wrap, unwrap, false);
  43.     }
  44.     public static DriverBYOK newInstanceDriverBYOKRuntimeNodeForJmxOperation(Logger log, boolean wrap, boolean unwrap) throws UtilsException {
  45.         return newInstanceDriverBYOKRuntimeNode(log, wrap, unwrap, true);
  46.     }
  47.     private static DriverBYOK newInstanceDriverBYOKRuntimeNode(Logger log, boolean wrap, boolean unwrap, boolean initFromJmx) throws UtilsException {
  48.         String securityRuntimePolicy = BYOKManager.getSecurityEngineGovWayPolicy();
  49.         if(securityRuntimePolicy!=null) {
  50.             DriverBYOKUtilities utils = new DriverBYOKUtilities(log);
  51.             utils.setInitFromJmx(initFromJmx);
  52.             return utils.getDriverBYOKRuntimeNode(wrap, unwrap);
  53.         }
  54.         return null;
  55.     }
  56.    
  57.     private Logger log;
  58.     private ConfigurazioneNodiRuntime configurazioneNodiRuntime;
  59.     private boolean nodoruntime;
  60.     private boolean initFromJmx=false;
  61.     public void setInitFromJmx(boolean initFromJmx) {
  62.         this.initFromJmx = initFromJmx;
  63.     }
  64.    
  65.     public DriverBYOKUtilities(Logger log) {
  66.         this.log = log;
  67.         this.nodoruntime = true;
  68.     }
  69.     public DriverBYOKUtilities(boolean nodoruntime, Logger log, ConfigurazioneNodiRuntime config) {
  70.         this.log = log;
  71.         this.configurazioneNodiRuntime = config;
  72.         this.nodoruntime = nodoruntime;
  73.     }
  74.    
  75.     private DriverBYOK getDriverBYOK(boolean wrap, boolean unwrap) throws UtilsException{
  76.         return this.nodoruntime ?
  77.                 getDriverBYOKRuntimeNode(wrap, unwrap) :
  78.                 getDriverBYOKManagerNode(wrap, unwrap);
  79.     }
  80.     public DriverBYOK getDriverBYOKRuntimeNode(boolean wrap, boolean unwrap) throws UtilsException{
  81.         // Se definito un remoteEngine, e sono su un nodo runtime, significa che non c'è una distinzione tra ambiente runtime e ambiente manager
  82.         String securityManagerPolicy = BYOKManager.getSecurityRemoteEngineGovWayPolicy();
  83.         if(securityManagerPolicy==null || StringUtils.isEmpty(securityManagerPolicy)) {
  84.             securityManagerPolicy = BYOKManager.getSecurityEngineGovWayPolicy();
  85.         }
  86.         return getDriverBYOK(securityManagerPolicy, null, wrap, unwrap);
  87.     }
  88.     public DriverBYOK getDriverBYOKManagerNode(boolean wrap, boolean unwrap) throws UtilsException{
  89.         return getDriverBYOK(BYOKManager.getSecurityEngineGovWayPolicy(), BYOKManager.getSecurityRemoteEngineGovWayPolicy(), wrap, unwrap);
  90.     }
  91.     private DriverBYOK getDriverBYOK(String securityManagerPolicy, String securityManagerRemotePolicy, boolean wrap, boolean unwrap) throws UtilsException{
  92.         if(securityManagerPolicy!=null && StringUtils.isNotEmpty(securityManagerPolicy)) {
  93.             Map<String, Object> dynamicMap = new HashMap<>();
  94.             DynamicInfo dynamicInfo = new  DynamicInfo();
  95.             DynamicUtils.fillDynamicMap(this.log, dynamicMap, dynamicInfo, this.initFromJmx);
  96.            
  97.             if(securityManagerRemotePolicy!=null && StringUtils.isNotEmpty(securityManagerRemotePolicy) &&
  98.                     !securityManagerPolicy.equals(securityManagerRemotePolicy) &&
  99.                     isBYOKRemoteGovWayNodeConfig(securityManagerPolicy, wrap, unwrap) // quella attuale sarà una chiamata http verso la remote
  100.                     ) {
  101.                 initBYOKDynamicMapRemoteGovWayNode(dynamicMap, wrap, unwrap);
  102.             }
  103.            
  104.             return new DriverBYOK(this.log, securityManagerPolicy, securityManagerRemotePolicy, dynamicMap, true);
  105.         }
  106.         return null;
  107.     }
  108.     private boolean isBYOKRemoteGovWayNodeConfig(String securityManagerPolicy, boolean wrap, boolean unwrap) throws UtilsException {
  109.         BYOKManager byokManager = BYOKManager.getInstance();
  110.         if(byokManager!=null) {
  111.             return byokManager.isBYOKRemoteGovWayNodeConfig(securityManagerPolicy, wrap, unwrap);
  112.         }
  113.         return false;
  114.     }
  115.    
  116.     private void initBYOKDynamicMapRemoteGovWayNode(Map<String, Object> dynamicMap, boolean wrap, boolean unwrap) {
  117.         ConfigurazioneNodiRuntimeBYOKRemoteConfig remoteConfig = new ConfigurazioneNodiRuntimeBYOKRemoteConfig();
  118.         this.configurazioneNodiRuntime.initBYOKDynamicMapRemoteGovWayNode(this.log,dynamicMap, wrap, unwrap, remoteConfig);
  119.     }
  120.    
  121.     public String wrap(String value) throws UtilsException {
  122.         try {
  123.             if(value==null || StringUtils.isEmpty(value)) {
  124.                 return value;
  125.             }
  126.             DriverBYOK driverBYOK = getDriverBYOK(true, false);
  127.             if(driverBYOK==null) {
  128.                 return value;
  129.             }
  130.             BYOKWrappedValue v = driverBYOK.wrap(value);
  131.             if(v!=null && v.getWrappedValue()!=null) {
  132.                 return v.getWrappedValue();
  133.             }
  134.             throw new UtilsException("Wrap value failed");
  135.         }catch(Exception e) {
  136.             throw new UtilsException(e.getMessage(),e);
  137.         }
  138.     }
  139.     public String unwrap(String value) throws UtilsException {
  140.         try {
  141.             if(value==null || StringUtils.isEmpty(value)) {
  142.                 return value;
  143.             }
  144.             DriverBYOK driverBYOK = getDriverBYOK(false, true);
  145.             if(driverBYOK==null) {
  146.                 return value;
  147.             }
  148.             return driverBYOK.unwrapAsString(value);
  149.         }catch(Exception e) {
  150.             throw new UtilsException(e.getMessage(),e);
  151.         }
  152.     }
  153.    
  154.     public byte[] wrap(byte[] value) throws UtilsException {
  155.         try {
  156.             if(value==null || value.length<=0) {
  157.                 return value;
  158.             }
  159.             DriverBYOK driverBYOK = getDriverBYOK(true, false);
  160.             if(driverBYOK==null) {
  161.                 return value;
  162.             }
  163.             BYOKWrappedValue v = driverBYOK.wrap(value);
  164.             if(v!=null && v.getWrappedValue()!=null) {
  165.                 return v.getWrappedValue().getBytes();
  166.             }
  167.             throw new UtilsException("Wrap value failed");
  168.         }catch(Exception e) {
  169.             throw new UtilsException(e.getMessage(),e);
  170.         }
  171.     }
  172.     public byte[] unwrap(byte[] value) throws UtilsException {
  173.         try {
  174.             if(value==null || value.length<=0) {
  175.                 return value;
  176.             }
  177.             DriverBYOK driverBYOK = getDriverBYOK(false, true);
  178.             if(driverBYOK==null) {
  179.                 return value;
  180.             }
  181.             return driverBYOK.unwrap(value);
  182.         }catch(Exception e) {
  183.             throw new UtilsException(e.getMessage(),e);
  184.         }
  185.     }
  186.    
  187.     public boolean isWrappedWithAnyPolicy(String value) {
  188.         if(value!=null && StringUtils.isNotEmpty(value)) {
  189.             return BYOKUtilities.isWrappedValue(value);
  190.         }
  191.         return false;
  192.     }
  193.     public boolean isWrappedWithActivePolicy(String value) {
  194.         String driverSecurityManagerPolicy = null;
  195.         try {
  196.             if(value==null || StringUtils.isEmpty(value) || !isEnabledBYOK()) {
  197.                 return false;
  198.             }
  199.             String securityManagerPolicy = BYOKManager.getSecurityEngineGovWayPolicy();
  200.             driverSecurityManagerPolicy = BYOKManager.getSecurityRemoteEngineGovWayPolicy();
  201.             if(driverSecurityManagerPolicy==null || StringUtils.isEmpty(driverSecurityManagerPolicy)) {
  202.                 driverSecurityManagerPolicy = securityManagerPolicy;
  203.             }
  204.         }catch(Exception e) {
  205.             this.log.error("isWrapped failed ["+value+"]: "+e.getMessage(),e);
  206.             /**throw new DriverControlStationException(e.getMessage(),e);*/
  207.             return false;
  208.         }
  209.         return isWrappedWithPolicy(this.log, value, driverSecurityManagerPolicy);
  210.     }
  211.    
  212.     public static boolean isWrappedWithPolicy(Logger log, String value, String policy) {
  213.         try {
  214.             if(value==null || StringUtils.isEmpty(value)) {
  215.                 return false;
  216.             }
  217.            
  218.             String prefix = BYOKUtilities.newPrefixWrappedValue(policy);
  219.             return value.startsWith(prefix) && value.length()>prefix.length();
  220.            
  221.         }catch(Exception e) {
  222.             log.error("isWrapped failed ["+value+"]: "+e.getMessage(),e);
  223.             /**throw new DriverControlStationException(e.getMessage(),e);*/
  224.             return false;
  225.         }
  226.     }
  227.    
  228.     public boolean isEnabledBYOK() {
  229.         return BYOKManager.isEnabledBYOK();
  230.     }
  231. }