BYOKConfig.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.io.Serializable;
  22. import java.util.ArrayList;
  23. import java.util.Enumeration;
  24. import java.util.List;
  25. import java.util.Properties;

  26. import org.apache.commons.lang.StringUtils;
  27. import org.openspcoop2.utils.UtilsException;
  28. import org.slf4j.Logger;

  29. /**
  30.  * BYOKConfig
  31.  *
  32.  * @author Poli Andrea (apoli@link.it)
  33.  * @author $Author$
  34.  * @version $Rev$, $Date$
  35.  */
  36. public class BYOKConfig implements Serializable {

  37.     /**
  38.      *
  39.      */
  40.     private static final long serialVersionUID = -3572589461109860459L;
  41.        
  42.     private String id;
  43.    
  44.     private String type;
  45.     private String label;
  46.     private BYOKMode mode;
  47.    
  48.     private BYOKEncryptionMode encryptionMode;
  49.    
  50.     private List<String>inputParametersIds;
  51.     private List<BYOKConfigParameter> inputParameters = new ArrayList<>();
  52.    
  53.     private BYOKRemoteConfig remoteConfig;
  54.    
  55.     private BYOKLocalConfig localConfig;
  56.    
  57.    
  58.     protected BYOKConfig(String id, Properties p, Logger log, String byokPropertyPrefix) throws UtilsException {
  59.         this.id = id;
  60.        
  61.         if(p==null || p.isEmpty()) {
  62.             log.error("Properties is null");
  63.             throw new UtilsException("Properties '"+byokPropertyPrefix+id+".*' undefined");
  64.         }
  65.        
  66.         this.type = getProperty(id, p, BYOKCostanti.PROPERTY_SUFFIX_TYPE, true, byokPropertyPrefix);    
  67.         this.label = getProperty(id, p, BYOKCostanti.PROPERTY_SUFFIX_LABEL, true, byokPropertyPrefix);  
  68.        
  69.         String tmpMode = getProperty(id, p, BYOKCostanti.PROPERTY_SUFFIX_MODE, true, byokPropertyPrefix);
  70.         try {
  71.             this.mode = BYOKMode.valueOf(tmpMode.toUpperCase());
  72.         }catch(Exception e) {
  73.             throw new UtilsException("Invalid property '"+byokPropertyPrefix+id+"."+BYOKCostanti.PROPERTY_SUFFIX_MODE+"' enum value '"+tmpMode+"': "+e.getMessage());
  74.         }
  75.        
  76.         tmpMode = getProperty(id, p, BYOKCostanti.PROPERTY_SUFFIX_ENCRYPTION_MODE, false, byokPropertyPrefix);
  77.         if(tmpMode!=null && StringUtils.isNotEmpty(tmpMode)) {
  78.             try {
  79.                 this.encryptionMode = BYOKEncryptionMode.valueOf(tmpMode.toUpperCase());
  80.             }catch(Exception e) {
  81.                 throw new UtilsException("Invalid property '"+byokPropertyPrefix+id+"."+BYOKCostanti.PROPERTY_SUFFIX_ENCRYPTION_MODE+"' enum value '"+tmpMode+"': "+e.getMessage());
  82.             }
  83.         }
  84.         else {
  85.             this.encryptionMode = BYOKEncryptionMode.REMOTE;
  86.         }
  87.        
  88.         this.inputParametersIds = new ArrayList<>();
  89.         initInput(p, this.inputParametersIds);
  90.         if(this.inputParametersIds!=null && !this.inputParametersIds.isEmpty()) {
  91.             for (String inputId : this.inputParametersIds) {
  92.                 String nameP = getProperty(id, p, BYOKCostanti.PROPERTY_SUFFIX_INPUT+inputId+BYOKCostanti.PROPERTY_SUFFIX_INPUT_NAME, true, byokPropertyPrefix);    
  93.                 String labelP = getProperty(id, p, BYOKCostanti.PROPERTY_SUFFIX_INPUT+inputId+BYOKCostanti.PROPERTY_SUFFIX_INPUT_LABEL, true, byokPropertyPrefix);  
  94.                 this.inputParameters.add(new BYOKConfigParameter(inputId, nameP, labelP));
  95.             }
  96.         }
  97.        
  98.         if(BYOKEncryptionMode.REMOTE.equals(this.encryptionMode)) {
  99.             this.remoteConfig = new BYOKRemoteConfig(id, p, log, byokPropertyPrefix);
  100.         }
  101.         else {
  102.             this.localConfig = new BYOKLocalConfig(id, p, log, this, byokPropertyPrefix);
  103.         }
  104.        
  105.     }

  106.     void initInput(Properties p, List<String> idKeystore) {
  107.         Enumeration<?> enKeys = p.keys();
  108.         while (enKeys.hasMoreElements()) {
  109.             Object object = enKeys.nextElement();
  110.             if(object instanceof String) {
  111.                 String key = (String) object;
  112.                 initInput(key, BYOKCostanti.PROPERTY_SUFFIX_INPUT, idKeystore);
  113.             }
  114.         }
  115.     }
  116.     void initInput(String key, String prefix, List<String> idKeystore) {
  117.         if(key.startsWith(prefix) && key.length()>(prefix.length())) {
  118.             String tmp = key.substring(prefix.length());
  119.             if(tmp!=null && tmp.contains(".")) {
  120.                 int indeoOf = tmp.indexOf(".");
  121.                 if(indeoOf>0) {
  122.                     String idK = tmp.substring(0,indeoOf);
  123.                     if(!idKeystore.contains(idK)) {
  124.                         idKeystore.add(idK);
  125.                     }
  126.                 }
  127.             }
  128.         }
  129.     }
  130.    
  131.     static String getProperty(String id, Properties p, String name, boolean required, String byokPropertyPrefix) throws UtilsException {
  132.         String tmp = p.getProperty(name);
  133.         if(tmp!=null) {
  134.             return tmp.trim();
  135.         }
  136.         else {
  137.             if(required) {
  138.                 throw new UtilsException("Property '"+byokPropertyPrefix+id+"."+name+"' notFound");
  139.             }
  140.             return null;
  141.         }
  142.     }
  143.     static Integer getIntegerProperty(String id, Properties p, String name, boolean required, String byokPropertyPrefix) throws UtilsException {
  144.         String v = getProperty(id, p, name, required, byokPropertyPrefix);
  145.         if(v!=null && StringUtils.isNotEmpty(v)) {
  146.             try {
  147.                 return Integer.valueOf(v);
  148.             }catch(Exception e) {
  149.                 throw new UtilsException("Invalid integer property '"+byokPropertyPrefix+id+"."+name+"' value '"+e.getMessage()+"': "+e.getMessage());
  150.             }
  151.         }
  152.         return null;
  153.     }
  154.     static Boolean getBooleanProperty(String id, Properties p, String name, boolean required, Boolean defaultValue, String byokPropertyPrefix) throws UtilsException {
  155.         String v = getProperty(id, p, name, required, byokPropertyPrefix);
  156.         if(v!=null && StringUtils.isNotEmpty(v)) {
  157.             try {
  158.                 return Boolean.parseBoolean(v);
  159.             }catch(Exception e) {
  160.                 throw new UtilsException("Invalid boolean property '"+byokPropertyPrefix+id+"."+name+"' value '"+e.getMessage()+"': "+e.getMessage());
  161.             }
  162.         }
  163.         return defaultValue;
  164.     }
  165.    
  166.     public String getId() {
  167.         return this.id;
  168.     }
  169.    
  170.    
  171.    
  172.     public String getPrefixForLog() {
  173.         return "[KMS '"+this.getId()+"' type:"+this.type+" label:"+this.label+"] ";
  174.     }

  175.    
  176.     public String getType() {
  177.         return this.type;
  178.     }
  179.     public String getLabel() {
  180.         return this.label;
  181.     }
  182.     public BYOKMode getMode() {
  183.         return this.mode;
  184.     }

  185.     public BYOKEncryptionMode getEncryptionMode() {
  186.         return this.encryptionMode;
  187.     }
  188.    
  189.     public List<String> getInputParametersIds() {
  190.         return this.inputParametersIds;
  191.     }
  192.     public List<BYOKConfigParameter> getInputParameters() {
  193.         return this.inputParameters;
  194.     }
  195.    
  196.     public BYOKRemoteConfig getRemoteConfig() {
  197.         return this.remoteConfig;
  198.     }
  199.    
  200.     public BYOKLocalConfig getLocalConfig() {
  201.         return this.localConfig;
  202.     }
  203. }