BYOKProvider.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.certificate.byok;

  21. import java.util.ArrayList;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. import java.util.Map;

  25. import org.apache.commons.lang.StringUtils;
  26. import org.openspcoop2.utils.SortedMap;
  27. import org.openspcoop2.utils.UtilsException;

  28. /**    
  29.  * BYOKProvider
  30.  *
  31.  * @author Poli Andrea (poli@link.it)
  32.  * @author $Author$
  33.  * @version $Rev$, $Date$
  34.  */
  35. public class BYOKProvider {
  36.    
  37.     public static final String BYOK_POLICY_UNDEFINED_EMPTY = "";
  38.     public static final String BYOK_POLICY_UNDEFINED = "-"; // nelle maschere generic_properties ho bisogno di usare questo valore
  39.     public static final String BYOK_POLICY_UNDEFINED_LABEL = BYOK_POLICY_UNDEFINED;
  40.    
  41.     private static boolean unwrapKeystoreFileEnabled = true;
  42.     public static boolean isUnwrapKeystoreFileEnabled() {
  43.         return unwrapKeystoreFileEnabled;
  44.     }
  45.     public static void setUnwrapKeystoreFileEnabled(boolean unwrapKeystoreFileEnabled) {
  46.         BYOKProvider.unwrapKeystoreFileEnabled = unwrapKeystoreFileEnabled;
  47.     }
  48.    
  49.     public static BYOKProvider getWrapInstance() throws UtilsException {
  50.         return new BYOKProvider(true);
  51.     }
  52.     public static BYOKProvider getUnwrapInstance() throws UtilsException {
  53.         return new BYOKProvider(false);
  54.     }
  55.    
  56.     private boolean byok = false;
  57.     public boolean isByokEnabled() {
  58.         return this.byok;
  59.     }
  60.     public boolean isUnwrapByokKeystoreEnabled() {
  61.         return this.byok && unwrapKeystoreFileEnabled;
  62.     }
  63.     private List<String> byokTypes = new ArrayList<>();
  64.     private List<String> byokLabels = new ArrayList<>();
  65.     private Map<String,BYOKSecurityConfig> byokSecurity = new HashMap<>();
  66.     private static final String NO_BYOK = "--no_byok--";
  67.     private static List<String> noBYOK = new ArrayList<>();
  68.     static{
  69.         noBYOK.add(NO_BYOK);    
  70.     }
  71.     private static final String DEFAULT_BYOK = "Default";
  72.     private BYOKProvider(boolean wrap) throws UtilsException {
  73.         BYOKManager byokManager = BYOKManager.getInstance();
  74.         SortedMap<String> sortedMap = wrap ? byokManager.getKeystoreWrapConfigTypesLabels() : byokManager.getKeystoreUnwrapConfigTypesLabels();
  75.         this.byok = sortedMap!=null && !sortedMap.isEmpty();
  76.         if(this.byok) {
  77.             List<String> byokTypesAdd = new ArrayList<>();
  78.             List<String> byokLabelsAdd = new ArrayList<>();
  79.             String typeDefault = null;
  80.             if(!sortedMap.isEmpty()) {
  81.                 for (String type : sortedMap.keys()) {
  82.                     if(init(byokManager, type, byokTypesAdd, byokLabelsAdd, sortedMap)) {
  83.                         typeDefault = type;
  84.                     }
  85.                 }
  86.             }
  87.             boolean byokEnabled = !byokTypesAdd.isEmpty();
  88.             if(byokEnabled) {
  89.                 fillList(typeDefault, byokTypesAdd, byokLabelsAdd);
  90.             }
  91.         }
  92.     }
  93.     private boolean init(BYOKManager byokManager, String type, List<String> byokTypesAdd, List<String> byokLabelsAdd, SortedMap<String> sortedMap) throws UtilsException {
  94.        
  95.         boolean addDefault = false;
  96.        
  97.         String label = sortedMap.get(type);
  98.        
  99.         StringBuilder securityId = new StringBuilder();
  100.         if(byokManager.isKMSUsedInSecurityUnwrapConfig(type, securityId)) {
  101.             String secId =  securityId.toString();
  102.             BYOKSecurityConfig secConfig = byokManager.getKMSSecurityConfig(secId);
  103.            
  104.             if( BYOKManager.getSecurityRemoteEngineGovWayPolicy()!=null &&
  105.                 BYOKManager.getSecurityRemoteEngineGovWayPolicy().equals(secId)) {
  106.                 addDefault = true;
  107.             }
  108.            
  109.             if(BYOKManager.getSecurityEngineGovWayPolicy()!=null &&
  110.                 BYOKManager.getSecurityEngineGovWayPolicy().equals(secId)) {
  111.                 if( BYOKManager.getSecurityRemoteEngineGovWayPolicy()!=null ) {
  112.                     return false;
  113.                 }
  114.                 else {
  115.                     addDefault = true;
  116.                 }
  117.             }

  118.             this.byokSecurity.put(type,secConfig);
  119.                        
  120.             if(!addDefault) {
  121.                 byokTypesAdd.add(type);
  122.                 byokLabelsAdd.add(label);
  123.             }

  124.         }
  125.         else {
  126.             byokTypesAdd.add(type);
  127.             byokLabelsAdd.add(label);
  128.         }
  129.        
  130.         return addDefault;
  131.     }
  132.     private void fillList(String typeDefault, List<String> byokTypesAdd, List<String> byokLabelsAdd) {
  133.         this.byokTypes.add(BYOK_POLICY_UNDEFINED_EMPTY);
  134.         if(typeDefault!=null) {
  135.             this.byokTypes.add(typeDefault);
  136.         }
  137.         this.byokTypes.addAll(byokTypesAdd);
  138.         this.byokLabels.add(BYOK_POLICY_UNDEFINED_LABEL);
  139.         if(typeDefault!=null) {
  140.             this.byokLabels.add(DEFAULT_BYOK);
  141.         }
  142.         this.byokLabels.addAll(byokLabelsAdd);
  143.     }
  144.    
  145.     public List<String> getValues() {
  146.         return this.byok ? this.byokTypes : noBYOK;
  147.     }

  148.     public List<String> getLabels() {
  149.         return this.byok ? this.byokLabels : noBYOK;
  150.     }

  151.     public Map<String, String> getInputMap(String kmsId) {
  152.         Map<String, String> inputMap = new HashMap<>();
  153.         BYOKSecurityConfig secConfig = this.byokSecurity.get(kmsId);
  154.         if(secConfig!=null && secConfig.getInputParameters()!=null && !secConfig.getInputParameters().isEmpty()) {
  155.             for (BYOKSecurityConfigParameter param : secConfig.getInputParameters()) {
  156.                 inputMap.put(param.getName(), param.getValue());
  157.             }
  158.         }
  159.         return inputMap;
  160.     }
  161.    
  162.     public static boolean isPolicyDefined(String kmsId) {
  163.         return kmsId!=null && StringUtils.isNotEmpty(kmsId) && !BYOK_POLICY_UNDEFINED_EMPTY.equals(kmsId) && !BYOK_POLICY_UNDEFINED.equals(kmsId);
  164.     }
  165.    
  166.     public static BYOKRequestParams getBYOKRequestParamsByUnwrapBYOKPolicy(String kmsId,
  167.             Map<String,Object> dynamicMap) throws UtilsException {
  168.         // configurato via console
  169.        
  170.         if(!isPolicyDefined(kmsId)) {
  171.             return null;
  172.         }
  173.        
  174.         BYOKProvider provider = BYOKProvider.getUnwrapInstance();
  175.         Map<String, String> inputMap = provider.getInputMap(kmsId);
  176.        
  177.         return BYOKRequestParams.getBYOKRequestParamsByKmsId(kmsId,
  178.                 inputMap, dynamicMap);
  179.     }
  180.    
  181. }