ServerProperties.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.core.config.rs.server.config;

  21. import java.io.File;
  22. import java.io.FileInputStream;
  23. import java.io.InputStream;
  24. import java.util.Enumeration;
  25. import java.util.Properties;

  26. import org.apache.commons.lang.StringUtils;
  27. import org.openspcoop2.core.commons.CoreException;
  28. import org.openspcoop2.utils.BooleanNullable;
  29. import org.openspcoop2.utils.LoggerWrapperFactory;
  30. import org.openspcoop2.utils.UtilsException;
  31. import org.openspcoop2.utils.crypt.CryptConfig;
  32. import org.openspcoop2.utils.crypt.PasswordVerifier;
  33. import org.openspcoop2.utils.service.authorization.AuthorizationConfig;
  34. import org.slf4j.Logger;

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

  43.     /** Logger utilizzato per errori eventuali. */
  44.     private Logger log = null;
  45.    
  46.     /** Reader delle proprieta' impostate nel file 'rs-api-config.properties' */
  47.     private ServerInstanceProperties reader;
  48.    
  49.     /** Copia Statica */
  50.     private static ServerProperties serverProperties = null;
  51.    
  52.    
  53.     private ServerProperties(String confDir,Logger log) throws CoreException {

  54.         if(log!=null)
  55.             this.log = log;
  56.         else
  57.             this.log = LoggerWrapperFactory.getLogger(ServerProperties.class);
  58.        
  59.         /* ---- Lettura del cammino del file di configurazione ---- */
  60.         Properties propertiesReader = new Properties();
  61.         try (java.io.InputStream properties = DatasourceProperties.class.getResourceAsStream("/rs-api-config.properties");){  
  62.             if(properties==null){
  63.                 throw new CoreException("File '/rs-api-config.properties' not found");
  64.             }
  65.             propertiesReader.load(properties);
  66.         }catch(Exception e) {
  67.             String errorMsg = "Riscontrato errore durante la lettura del file 'rs-api-config.properties': "+e.getMessage();
  68.             this.log.error(errorMsg,e);
  69.             throw new CoreException("RS Api ConfigProperties initialize error: "+e.getMessage(), e);
  70.         }

  71.         this.reader = new ServerInstanceProperties(propertiesReader, this.log, confDir);
  72.     }


  73.     /**
  74.      * Il Metodo si occupa di inizializzare il propertiesReader
  75.      *
  76.      *
  77.      */
  78.     public static boolean initialize(String confDir,Logger log){

  79.         try {
  80.             ServerProperties.serverProperties = new ServerProperties(confDir,log);  
  81.             return true;
  82.         }
  83.         catch(Exception e) {
  84.             log.error("Errore durante l'inizializzazione del ServerProperties: "+e.getMessage(),e);        
  85.             return false;
  86.         }
  87.     }
  88.    
  89.     /**
  90.      * Ritorna l'istanza di questa classe
  91.      *
  92.      * @return Istanza di Properties
  93.      *
  94.      */
  95.     public static ServerProperties getInstance() throws UtilsException{
  96.         if(ServerProperties.serverProperties==null){
  97.             // spotbugs warning 'SING_SINGLETON_GETTER_NOT_SYNCHRONIZED': l'istanza viene creata allo startup
  98.             synchronized (ServerProperties.class) {
  99.                 throw new UtilsException("ServerProperties non inizializzato");
  100.             }
  101.         }
  102.         return ServerProperties.serverProperties;
  103.     }
  104.    
  105.     public static void updateLocalImplementation(Properties prop){
  106.         ServerProperties.serverProperties.reader.setLocalObjectImplementation(prop);
  107.     }








  108.     /* ********  M E T O D I  ******** */

  109.     private boolean parse(BooleanNullable b, boolean defaultValue) {
  110.         return (b!=null && b.getValue()!=null) ? b.getValue() : defaultValue;
  111.     }
  112.    
  113.     public String readProperty(boolean required,String property) throws UtilsException{
  114.         String tmp = this.reader.getValueConvertEnvProperties(property);
  115.         if(tmp==null){
  116.             if(required){
  117.                 throw new UtilsException("Property ["+property+"] not found");
  118.             }
  119.             else{
  120.                 return null;
  121.             }
  122.         }else{
  123.             return tmp.trim();
  124.         }
  125.     }
  126.    
  127.     private BooleanNullable readBooleanProperty(boolean required,String property) throws UtilsException{
  128.         String tmp = this.readProperty(required, property);
  129.         if(tmp==null && !required) {
  130.             return BooleanNullable.NULL(); // se e' required viene sollevata una eccezione dal metodo readProperty
  131.         }
  132.         if(!"true".equalsIgnoreCase(tmp) && !"false".equalsIgnoreCase(tmp)){
  133.             throw new UtilsException("Property ["+property+"] with uncorrect value ["+tmp+"] (true/value expected)");
  134.         }
  135.         return Boolean.parseBoolean(tmp) ? BooleanNullable.TRUE() : BooleanNullable.FALSE();
  136.     }
  137.    
  138.     public Properties getProperties() throws UtilsException{
  139.         Properties p = new Properties();
  140.         Enumeration<?> names = this.reader.propertyNames();
  141.         while (names.hasMoreElements()) {
  142.             String name = (String) names.nextElement();
  143.             p.put(name, this.reader.getValueConvertEnvProperties(name));
  144.         }
  145.         return p;
  146.     }
  147.    
  148.     private AuthorizationConfig authConfig = null;
  149.     private synchronized void initAuthorizationConfig() throws UtilsException {
  150.         if(this.authConfig==null) {
  151.             this.authConfig = new AuthorizationConfig(getProperties());
  152.         }
  153.     }
  154.     public AuthorizationConfig getAuthorizationConfig() throws UtilsException {
  155.         if(this.authConfig==null) {
  156.             this.initAuthorizationConfig();
  157.         }
  158.         return this.authConfig;
  159.     }

  160.    
  161.     public String getConfDirectory() throws UtilsException {
  162.         return this.readProperty(false, "confDirectory");
  163.     }
  164.     public String getProtocolloDefault() throws UtilsException {
  165.         return this.readProperty(true, "protocolloDefault");
  166.     }
  167.    
  168.     public boolean isJdbcCloseConnectionCheckIsClosed() throws UtilsException{
  169.         BooleanNullable b = this.readBooleanProperty(true, "jdbc.closeConnection.checkIsClosed");
  170.         return this.parse(b, true);
  171.     }
  172.     public boolean isJdbcCloseConnectionCheckAutocommit() throws UtilsException{
  173.         BooleanNullable b = this.readBooleanProperty(true, "jdbc.closeConnection.checkAutocommit");
  174.         return this.parse(b, true);
  175.     }
  176.    
  177.     public boolean isEnabledAutoMapping() throws UtilsException {
  178.         return Boolean.parseBoolean(this.readProperty(true, "enableAutoMapping"));
  179.     }
  180.    
  181.     public boolean isEnabledAutoMappingEstraiXsdSchemiFromWsdlTypes() throws UtilsException {
  182.         return Boolean.parseBoolean(this.readProperty(true, "enableAutoMapping_estraiXsdSchemiFromWsdlTypes"));
  183.     }
  184.    
  185.     public boolean isValidazioneDocumenti() throws UtilsException {
  186.         return Boolean.parseBoolean(this.readProperty(true, "validazioneDocumenti"));
  187.     }
  188.    
  189.     public boolean isUpdateInterfacciaApiUpdateIfExists() throws UtilsException {
  190.         return Boolean.parseBoolean(this.readProperty(true, "updateInterfacciaApi.updateIfExists"));
  191.     }
  192.    
  193.     public boolean isUpdateInterfacciaApiDeleteIfNotFound() throws UtilsException {
  194.         return Boolean.parseBoolean(this.readProperty(true, "updateInterfacciaApi.deleteIfNotFound"));
  195.     }
  196.    
  197.     public boolean isSoggettiApplicativiCredenzialiBasicPermitSameCredentials() throws UtilsException {
  198.         return Boolean.parseBoolean(this.readProperty(true, "soggettiApplicativi.credenzialiBasic.permitSameCredentials"));
  199.     }
  200.     public boolean isSoggettiApplicativiCredenzialiSslPermitSameCredentials() throws UtilsException {
  201.         return Boolean.parseBoolean(this.readProperty(true, "soggettiApplicativi.credenzialiSsl.permitSameCredentials"));
  202.     }
  203.     public boolean isSoggettiApplicativiCredenzialiPrincipalPermitSameCredentials() throws UtilsException {
  204.         return Boolean.parseBoolean(this.readProperty(true, "soggettiApplicativi.credenzialiPrincipal.permitSameCredentials"));
  205.     }
  206.    
  207.     public boolean isKeystoreJksPasswordRequired() throws UtilsException{
  208.         BooleanNullable b = this.readBooleanProperty(false, "keystore.jks.passwordRequired");
  209.         return parse(b, true);
  210.     }
  211.     public boolean isKeystoreJksKeyPasswordRequired() throws UtilsException{
  212.         BooleanNullable b = this.readBooleanProperty(false, "keystore.jks.key.passwordRequired");
  213.         return parse(b, true);
  214.     }
  215.     public boolean isKeystorePkcs12PasswordRequired() throws UtilsException{
  216.         BooleanNullable b = this.readBooleanProperty(false, "keystore.pkcs12.passwordRequired");
  217.         return parse(b, true);
  218.     }
  219.     public boolean isKeystorePkcs12KeyPasswordRequired() throws UtilsException{
  220.         BooleanNullable b = this.readBooleanProperty(false, "keystore.pkcs12.key.passwordRequired");
  221.         return parse(b, true);
  222.     }
  223.    
  224.     public boolean isTruststoreJksPasswordRequired() throws UtilsException{
  225.         BooleanNullable b = this.readBooleanProperty(false, "truststore.jks.passwordRequired");
  226.         return parse(b, true);
  227.     }
  228.     public boolean isTruststorePkcs12PasswordRequired() throws UtilsException{
  229.         BooleanNullable b = this.readBooleanProperty(false, "truststore.pkcs12.passwordRequired");
  230.         return parse(b, true);
  231.     }
  232.    
  233.     public Properties getApiYamlSnakeLimits() throws UtilsException{

  234.         String pName = "api.yaml.snakeLimits";
  235.        
  236.         try{  
  237.             Properties p = null;
  238.             String file = this.readProperty(false, pName);
  239.             if(file!=null && StringUtils.isNotEmpty(file)) {
  240.                 File f = new File(file);
  241.                 if(f.exists()) {
  242.                     if(!f.isFile()) {
  243.                         throw new UtilsException("Il file indicato '"+f.getAbsolutePath()+"' non è un file");
  244.                     }
  245.                     if(!f.canRead()) {
  246.                         throw new UtilsException("Il file indicato '"+f.getAbsolutePath()+"' non è accessibile in lettura");
  247.                     }
  248.                     try(InputStream is = new FileInputStream(f)){
  249.                         p = new Properties();
  250.                         p.load(is);
  251.                         if (!p.isEmpty()){
  252.                             return p;
  253.                         }
  254.                         else {
  255.                             p = null;
  256.                         }
  257.                     }
  258.                 }
  259.             }
  260.        
  261.             return p;
  262.            
  263.         }catch(java.lang.Exception e) {
  264.             throw new UtilsException("Proprieta' '"+pName+"' non impostate, errore:"+e.getMessage(),e);
  265.         }
  266.     }
  267.    
  268.     public boolean isDelete404() throws UtilsException {
  269.         return Boolean.parseBoolean(this.readProperty(true, "delete_404"));
  270.     }
  271.    
  272.     public boolean isFindall404() throws UtilsException {
  273.         return Boolean.parseBoolean(this.readProperty(true, "findall_404"));
  274.     }
  275.    
  276.     public Boolean isConfigurazioneAllarmiEnabled() throws UtilsException{
  277.         return Boolean.parseBoolean(this.readProperty(true, "allarmi.enabled"));
  278.     }
  279.     public String getAllarmiConfigurazione() throws UtilsException {
  280.         return this.readProperty(true, "allarmi.configurazione");
  281.     }
  282.    
  283.    
  284.     public boolean isSecurityLoadBouncyCastleProvider() throws UtilsException{
  285.         BooleanNullable b = this.readBooleanProperty(false, "security.addBouncyCastleProvider");
  286.         return parse(b, false);
  287.     }
  288.    
  289.    
  290.     public String getEnvMapConfig() throws UtilsException{
  291.         return this.readProperty(false, "env.map.config");
  292.     }
  293.     public boolean isEnvMapConfigRequired() throws UtilsException{
  294.         BooleanNullable b = this.readBooleanProperty(false, "env.map.required");
  295.         return this.parse(b, false);
  296.     }
  297.    
  298.    
  299.     public String getHSMConfigurazione() throws UtilsException {
  300.         return this.readProperty(false, "hsm.config");
  301.     }
  302.     public boolean isHSMRequired() throws UtilsException {
  303.         return Boolean.parseBoolean(this.readProperty(true, "hsm.required"));
  304.     }
  305.     public boolean isHSMKeyPasswordConfigurable() throws UtilsException{
  306.         BooleanNullable b = this.readBooleanProperty(false, "hsm.keyPassword");
  307.         return this.parse(b, false);
  308.     }
  309.    
  310.    
  311.    
  312.     public String getBYOKConfigurazione() throws UtilsException{
  313.         return this.readProperty(false, "byok.config");
  314.     }
  315.     public boolean isBYOKRequired() throws UtilsException{
  316.         BooleanNullable b = this.readBooleanProperty(false, "byok.required");
  317.         return parse(b, false);
  318.     }
  319.     public String getBYOKEnvSecretsConfig() throws UtilsException{
  320.         return this.readProperty(false, "byok.env.secrets.config");
  321.     }
  322.     public boolean isBYOKEnvSecretsConfigRequired() throws UtilsException{
  323.         BooleanNullable b = this.readBooleanProperty(false, "byok.env.secrets.required");
  324.         return this.parse(b, false);
  325.     }
  326.    
  327.    
  328.    
  329.     public String getOCSPConfigurazione() throws UtilsException {
  330.         return this.readProperty(false, "ocsp.config");
  331.     }
  332.     public boolean isOCSPRequired() throws UtilsException {
  333.         return Boolean.parseBoolean(this.readProperty(true, "ocsp.required"));
  334.     }  
  335.     public boolean isOCSPLoadDefault() throws UtilsException {
  336.         String p = this.readProperty(false, "ocsp.loadDefault");
  337.         if(p!=null && StringUtils.isNotEmpty(p)) {
  338.             return Boolean.parseBoolean(p);
  339.         }
  340.         return true;
  341.     }  

  342.    
  343.     public String getConfigurazioneNodiRuntime() throws UtilsException{
  344.         return this.readProperty(false, "configurazioni.configurazioneNodiRun");
  345.     }
  346.    
  347.    
  348.    
  349.     public String getSoggettoDefault(String protocollo) throws UtilsException {
  350.         String p = this.readProperty(false, protocollo+".soggetto");
  351.         if(p!=null) {
  352.             return p;
  353.         }
  354.         return this.readProperty(true, "soggetto");
  355.     }


  356.     public org.openspcoop2.utils.service.context.ContextConfig getContextConfig() throws UtilsException {
  357.         org.openspcoop2.utils.service.context.ContextConfig config = new org.openspcoop2.utils.service.context.ContextConfig();
  358.         config.setClusterId(this.readProperty(false, "clusterId"));
  359.         config.setDump(Boolean.parseBoolean(this.readProperty(true, "dump")));
  360.         config.setEmitTransaction(Boolean.parseBoolean(this.readProperty(true, "transaction")));
  361.         config.setServiceType(this.readProperty(false, "service.type"));
  362.         config.setServiceName(this.readProperty(false, "service.name"));
  363.         config.setServiceVersion(Integer.parseInt(this.readProperty(false, "service.version")));
  364.         return config;
  365.     }
  366.    
  367.    
  368.     public Properties getConsolePasswordCryptConfig() throws UtilsException {
  369.         return this.reader.readPropertiesConvertEnvProperties("console.password.");
  370.     }
  371.    
  372.     public boolean isConsolePasswordCryptBackwardCompatibility() throws UtilsException {
  373.         return "true".equalsIgnoreCase(this.readProperty(true, "console.password.crypt.backwardCompatibility"));
  374.     }

  375.    
  376.     /* ----- Gestione Password ------- */
  377.    
  378.     // Utenze
  379.    
  380.     public String getUtenzePassword() throws UtilsException{
  381.         return this.readProperty(true, "utenze.password");
  382.     }
  383.     private static CryptConfig utenzeCryptConfig = null;
  384.     private static synchronized void initUtenzeCryptConfig(String p) throws UtilsException {
  385.         if(utenzeCryptConfig==null) {
  386.             utenzeCryptConfig = new CryptConfig(p);
  387.         }
  388.     }
  389.     public CryptConfig getUtenzeCryptConfig() throws UtilsException {
  390.         if(utenzeCryptConfig==null) {
  391.             initUtenzeCryptConfig(getUtenzePassword());
  392.         }
  393.         return utenzeCryptConfig;
  394.     }
  395.    
  396.     // Applicativi
  397.    
  398.     public String getApplicativiPassword() throws UtilsException{
  399.         return this.readProperty(true, "applicativi.password");
  400.     }
  401.     private static CryptConfig applicativiCryptConfig = null;
  402.     private static synchronized void initApplicativiCryptConfig(String p) throws UtilsException {
  403.         if(applicativiCryptConfig==null) {
  404.             applicativiCryptConfig = new CryptConfig(p);
  405.         }
  406.     }
  407.     public CryptConfig getApplicativiCryptConfig() throws UtilsException {
  408.         if(applicativiCryptConfig==null) {
  409.             initApplicativiCryptConfig(getApplicativiPassword());
  410.         }
  411.         return applicativiCryptConfig;
  412.     }
  413.    
  414.     public int getApplicativiApiKeyPasswordGeneratedLength() throws UtilsException{
  415.         return Integer.valueOf(this.readProperty(true, "applicativi.api_key.passwordGenerated.length"));
  416.     }
  417.    
  418.     public boolean isApplicativiBasicPasswordEnableConstraints() throws UtilsException{
  419.         return "true".equalsIgnoreCase(this.readProperty(true, "applicativi.basic.password.enableConstraints"));
  420.     }
  421.     private static PasswordVerifier applicativiPasswordVerifier = null;
  422.     private static synchronized void initApplicativiPasswordVerifier(String p) throws UtilsException {
  423.         if(applicativiPasswordVerifier==null) {
  424.             applicativiPasswordVerifier = new PasswordVerifier(p);
  425.         }
  426.     }
  427.     public PasswordVerifier getApplicativiPasswordVerifier() throws UtilsException {
  428.         if(applicativiPasswordVerifier==null) {
  429.             initApplicativiPasswordVerifier(getApplicativiPassword());
  430.         }
  431.         return applicativiPasswordVerifier;
  432.     }
  433.    
  434.     // Soggetti
  435.    
  436.     public String getSoggettiPassword() throws UtilsException{
  437.         return this.readProperty(true, "soggetti.password");
  438.     }
  439.     private static CryptConfig soggettiCryptConfig = null;
  440.     private static synchronized void initSoggettiCryptConfig(String p) throws UtilsException {
  441.         if(soggettiCryptConfig==null) {
  442.             soggettiCryptConfig = new CryptConfig(p);
  443.         }
  444.     }
  445.     public CryptConfig getSoggettiCryptConfig() throws UtilsException {
  446.         if(soggettiCryptConfig==null) {
  447.             initSoggettiCryptConfig(getSoggettiPassword());
  448.         }
  449.         return soggettiCryptConfig;
  450.     }
  451.    
  452.     public int getSoggettiApiKeyPasswordGeneratedLength() throws UtilsException{
  453.         return Integer.valueOf(this.readProperty(true, "soggetti.api_key.passwordGenerated.length"));
  454.     }
  455.    
  456.     public boolean isSoggettiBasicPasswordEnableConstraints() throws UtilsException{
  457.         return "true".equalsIgnoreCase(this.readProperty(true, "soggetti.basic.password.enableConstraints"));
  458.     }
  459.     private static PasswordVerifier soggettiPasswordVerifier = null;
  460.     private static synchronized void initSoggettiPasswordVerifier(String p) throws UtilsException {
  461.         if(soggettiPasswordVerifier==null) {
  462.             soggettiPasswordVerifier = new PasswordVerifier(p);
  463.         }
  464.     }
  465.     public PasswordVerifier getSoggettiPasswordVerifier() throws UtilsException {
  466.         if(soggettiPasswordVerifier==null) {
  467.             initSoggettiPasswordVerifier(getSoggettiPassword());
  468.         }
  469.         return soggettiPasswordVerifier;
  470.     }
  471.    
  472. }