BYOKSecurityConfig.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.openspcoop2.utils.UtilsException;
  27. import org.slf4j.Logger;

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

  36.     /**
  37.      *
  38.      */
  39.     private static final long serialVersionUID = -3572589461109860459L;
  40.        
  41.     private String id;
  42.    
  43.     private String wrapId;
  44.     private String unwrapId;
  45.    
  46.     private List<String>inputParametersIds;
  47.     private List<BYOKSecurityConfigParameter> inputParameters = new ArrayList<>();
  48.        
  49.     protected BYOKSecurityConfig(String id, Properties p, Logger log) throws UtilsException {
  50.         this.id = id;
  51.        
  52.         if(p==null || p.isEmpty()) {
  53.             log.error("Properties is null");
  54.             throw new UtilsException("Properties '"+BYOKCostanti.SECURITY_PROPERTY_PREFIX+id+".*' undefined");
  55.         }
  56.        
  57.         boolean deprecatedMode = false;
  58.        
  59.         this.wrapId = getProperty(id, p, BYOKCostanti.SECURITY_PROPERTY_SUFFIX_WRAP_KMS, false);
  60.         if(this.wrapId==null) {
  61.             this.wrapId = getProperty(id, p, BYOKCostanti.SECURITY_PROPERTY_SUFFIX_WRAP_KSM_DEPRECATED, false);
  62.             if(this.wrapId!=null) {
  63.                 deprecatedMode = true;
  64.             }
  65.         }
  66.         if(this.wrapId==null) {
  67.             this.wrapId = getProperty(id, p, BYOKCostanti.SECURITY_PROPERTY_SUFFIX_WRAP_KMS, true); // sollevo eccezione con required true
  68.         }
  69.        
  70.         this.unwrapId = getProperty(id, p, BYOKCostanti.SECURITY_PROPERTY_SUFFIX_UNWRAP_KMS, false);    
  71.         if(this.unwrapId==null) {
  72.             this.unwrapId = getProperty(id, p, BYOKCostanti.SECURITY_PROPERTY_SUFFIX_UNWRAP_KSM_DEPRECATED, false);
  73.             if(this.unwrapId!=null) {
  74.                 deprecatedMode = true;
  75.             }
  76.         }
  77.         if(this.unwrapId==null) {
  78.             this.unwrapId = getProperty(id, p, BYOKCostanti.SECURITY_PROPERTY_SUFFIX_UNWRAP_KMS, true); // sollevo eccezione con required true
  79.         }
  80.        
  81.         String securityPropertySuffixInput = deprecatedMode ? BYOKCostanti.SECURITY_PROPERTY_SUFFIX_INPUT_KSM_DEPRECATED : BYOKCostanti.SECURITY_PROPERTY_SUFFIX_INPUT_KMS;
  82.        
  83.         this.inputParametersIds = new ArrayList<>();
  84.         initInput(p, this.inputParametersIds, securityPropertySuffixInput);
  85.         if(this.inputParametersIds!=null && !this.inputParametersIds.isEmpty()) {
  86.             for (String inputId : this.inputParametersIds) {
  87.                 String value = getProperty(id, p, securityPropertySuffixInput+inputId, true);  
  88.                 this.inputParameters.add(new BYOKSecurityConfigParameter(inputId, value));
  89.             }
  90.         }
  91.        
  92.     }

  93.     void initInput(Properties p, List<String> idKeystore, String securityPropertySuffixInput) {
  94.         Enumeration<?> enKeys = p.keys();
  95.         while (enKeys.hasMoreElements()) {
  96.             Object object = enKeys.nextElement();
  97.             if(object instanceof String) {
  98.                 String key = (String) object;
  99.                 initInput(key, securityPropertySuffixInput, idKeystore);    
  100.             }
  101.         }
  102.     }
  103.     void initInput(String key, String prefix, List<String> idKeystore) {
  104.         if(key.startsWith(prefix) && key.length()>(prefix.length())) {
  105.             String tmp = key.substring(prefix.length());
  106.             if(!idKeystore.contains(tmp)) {
  107.                 idKeystore.add(tmp);
  108.             }
  109.         }
  110.     }
  111.    
  112.     static String getProperty(String id, Properties p, String name, boolean required) throws UtilsException {
  113.         String tmp = p.getProperty(name);
  114.         if(tmp!=null) {
  115.             return tmp.trim();
  116.         }
  117.         else {
  118.             if(required) {
  119.                 throw new UtilsException("Property '"+BYOKCostanti.SECURITY_PROPERTY_PREFIX+id+"."+name+"' notFound");
  120.             }
  121.             return null;
  122.         }
  123.     }

  124.    
  125.     public String getId() {
  126.         return this.id;
  127.     }
  128.        
  129.     public String getPrefixForLog() {
  130.         return "[Security BYOK '"+this.getId()+"'] ";
  131.     }

  132.     public String getWrapId() {
  133.         return this.wrapId;
  134.     }

  135.     public String getUnwrapId() {
  136.         return this.unwrapId;
  137.     }
  138.    
  139.     public List<String> getInputParametersIds() {
  140.         return this.inputParametersIds;
  141.     }
  142.     public List<BYOKSecurityConfigParameter> getInputParameters() {
  143.         return this.inputParameters;
  144.     }
  145.    

  146. }