VaultProperties.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.config.vault.cli;

  21. import java.io.InputStream;
  22. import java.util.Properties;

  23. import org.openspcoop2.core.commons.CoreException;

  24. /**
  25. * VaultProperties
  26. *
  27. * @author Poli Andrea (apoli@link.it)
  28. * @author $Author$
  29. * @version $Rev$, $Date$
  30. */
  31. public class VaultProperties {
  32.    
  33.     private static VaultProperties staticInstance = null;
  34.     private static synchronized void init() throws CoreException{
  35.         if(VaultProperties.staticInstance == null){
  36.             VaultProperties.staticInstance = new VaultProperties();
  37.         }
  38.     }
  39.     public static VaultProperties getInstance() throws CoreException{
  40.         if(VaultProperties.staticInstance == null){
  41.             // spotbugs warning 'SING_SINGLETON_GETTER_NOT_SYNCHRONIZED': l'istanza viene creata allo startup
  42.             synchronized (VaultProperties.class) {
  43.                 VaultProperties.init();
  44.             }
  45.         }
  46.         return VaultProperties.staticInstance;
  47.     }
  48.    
  49.     private static String getPropertyPrefix(String name) {
  50.         return "Property '"+name+"'";
  51.     }
  52.    
  53.    
  54.    
  55.     private static final String PROPERTIES_FILE = "/govway_vault.cli.properties";
  56.    
  57.     private String protocolloDefault = null;
  58.        
  59.     private boolean securityLoadBouncyCastleProvider = false;
  60.    
  61.     private String envMapConfig = null;
  62.     private boolean envMapConfigRequired = false;
  63.    
  64.     private String hsmConfig = null;
  65.     private boolean hsmRequired = false;
  66.     private boolean hsmKeyPasswordConfigurable = false;
  67.    
  68.     private String byokConfigurazione = null;
  69.     private boolean byokRequired = false;
  70.     private String byokEnvSecretsConfig = null;
  71.     private boolean byokEnvSecretsConfigRequired = false;
  72.    
  73.     private VaultProperties() throws CoreException {

  74.         Properties props = new Properties();
  75.         try {
  76.             InputStream is = VaultProperties.class.getResourceAsStream(VaultProperties.PROPERTIES_FILE);
  77.             props.load(is);
  78.         } catch(Exception e) {
  79.             throw new CoreException("Errore durante l'init delle properties", e);
  80.         }
  81.        
  82.         // PROPERTIES
  83.                
  84.         this.protocolloDefault = this.getProperty(props, "protocolloDefault", true);
  85.        
  86.         this.securityLoadBouncyCastleProvider = this.getBooleanProperty(props, "security.addBouncyCastleProvider", false);
  87.        
  88.         this.envMapConfig = this.getProperty(props, "env.map.config", false);
  89.         this.envMapConfigRequired = this.getBooleanProperty(props, "env.map.required", false);
  90.        
  91.         this.hsmConfig = this.getProperty(props, "hsm.config", false);
  92.         this.hsmRequired = this.getBooleanProperty(props, "hsm.required", false);
  93.         this.hsmKeyPasswordConfigurable = this.getBooleanProperty(props, "hsm.keyPassword", false);
  94.        
  95.         this.byokConfigurazione = this.getProperty(props, "byok.config", false);
  96.         this.byokRequired = this.getBooleanProperty(props, "byok.required", false);
  97.         this.byokEnvSecretsConfig = this.getProperty(props, "byok.env.secrets.config", false);
  98.         this.byokEnvSecretsConfigRequired = this.getBooleanProperty(props, "byok.env.secrets.required", false);
  99.        
  100.     }
  101.    
  102.     private String getProperty(Properties props,String name,boolean required) throws CoreException{
  103.         String tmp = props.getProperty(name);
  104.         if(tmp==null){
  105.             if(required){
  106.                 throw new CoreException(getPropertyPrefix(name)+" not found");
  107.             }
  108.             else{
  109.                 return null;
  110.             }
  111.         }
  112.         else{
  113.             return tmp.trim();
  114.         }
  115.     }
  116.     private boolean getBooleanProperty(Properties props,String name,boolean required) throws CoreException{
  117.         String tmp = this.getProperty(props, name, required);
  118.         if(tmp!=null){
  119.             try{
  120.                 return Boolean.parseBoolean(tmp);
  121.             }catch(Exception e){
  122.                 throw new CoreException(getPropertyPrefix(name)+" wrong int format: "+e.getMessage());
  123.             }
  124.         }
  125.         else{
  126.             return false;
  127.         }
  128.     }
  129.     @SuppressWarnings("unused")
  130.     private int getIntProperty(Properties props,String name,boolean required) throws CoreException{
  131.         String tmp = this.getProperty(props, name, required);
  132.         if(tmp!=null){
  133.             try{
  134.                 return Integer.valueOf(tmp);
  135.             }catch(Exception e){
  136.                 throw new CoreException(getPropertyPrefix(name)+" wrong int format: "+e.getMessage());
  137.             }
  138.         }
  139.         else{
  140.             return -1;
  141.         }
  142.     }
  143.    
  144.    
  145.     public String getProtocolloDefault() {
  146.         return this.protocolloDefault;
  147.     }
  148.    
  149.     public boolean isSecurityLoadBouncyCastleProvider() {
  150.         return this.securityLoadBouncyCastleProvider;
  151.     }
  152.    
  153.     public String getEnvMapConfig() {
  154.         return this.envMapConfig;
  155.     }
  156.     public boolean isEnvMapConfigRequired(){
  157.         return this.envMapConfigRequired;
  158.     }
  159.    
  160.     public String getHSMConfigurazione() {
  161.         return this.hsmConfig;
  162.     }
  163.     public boolean isHSMRequired() {
  164.         return this.hsmRequired;
  165.     }
  166.     public boolean isHSMKeyPasswordConfigurable() {
  167.         return this.hsmKeyPasswordConfigurable;
  168.     }
  169.    
  170.     public String getBYOKConfigurazione() {
  171.         return this.byokConfigurazione;
  172.     }
  173.     public boolean isBYOKRequired() {
  174.         return this.byokRequired;
  175.     }
  176.     public String getBYOKEnvSecretsConfig() {
  177.         return this.byokEnvSecretsConfig;
  178.     }
  179.     public boolean isBYOKEnvSecretsConfigRequired() {
  180.         return this.byokEnvSecretsConfigRequired;
  181.     }
  182. }