ConfigurazioneNodiRuntimeInit.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;

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

  24. import org.openspcoop2.pdd.core.byok.BYOKMapProperties;
  25. import org.openspcoop2.pdd.core.dynamic.DynamicInfo;
  26. import org.openspcoop2.pdd.core.dynamic.DynamicUtils;
  27. import org.openspcoop2.utils.Utilities;
  28. import org.openspcoop2.utils.threads.BaseThread;
  29. import org.slf4j.Logger;

  30. /**
  31.  * InitRuntimeConfigReader
  32.  *
  33.  *
  34.  * @author Andrea Poli (apoli@link.it)
  35.  * @author $Author$
  36.  * @version $Rev$, $Date$
  37.  *
  38.  */
  39. public class ConfigurazioneNodiRuntimeInit extends BaseThread {

  40.     protected Logger log;
  41.     protected ConfigurazioneNodiRuntime configurazioneNodiRuntime = null;
  42.    
  43.     private boolean reInitSecretMaps = false;
  44.     private String secretsConfig;
  45.     private boolean secretsConfigRequired;
  46.    
  47.     private boolean first = true;
  48.    
  49.     public ConfigurazioneNodiRuntimeInit(Logger log, ConfigurazioneNodiRuntime configurazioneNodiRuntime,
  50.             boolean reInitSecretMaps, String secretsConfig, boolean secretsConfigRequired) {
  51.         this.log = log;
  52.         this.configurazioneNodiRuntime = configurazioneNodiRuntime;
  53.         this.reInitSecretMaps = reInitSecretMaps;
  54.         this.secretsConfig = secretsConfig;
  55.         this.secretsConfigRequired = secretsConfigRequired;
  56.     }
  57.    
  58.     @Override
  59.     protected void process() {
  60.        
  61.         // provo ad iterare fino a che un nodo runtime non risulta disponibile
  62.        
  63.         boolean finish = false;
  64.        
  65.         while(!finish) {
  66.        
  67.             try{
  68.                 finish = analyze();
  69.             } catch (Exception e) {
  70.                 String msgErrore = "Errore durante l'inizializzazione della configurazione ("+getDescrizione()+"): " + e.getMessage();
  71.                 this.log.error(msgErrore,e);
  72.                 //throw new UtilsRuntimeException(msgErrore,e); non sollevo l'eccezione, e' solo una informazione informativa, non voglio mettere un vincolo che serve per forza un nodo acceso
  73.             }
  74.        
  75.             if(!finish) {
  76.                 String msg = "Non รจ stato possibile ottenere informazioni sulla configurazione ("+getDescrizione()+") da nessun nodo runtime (prossimo controllo tra 30 secondi)";
  77.                 if(this.first) {
  78.                     this.log.debug(msg);
  79.                     this.first = false;
  80.                 }
  81.                 else {
  82.                     this.log.error(msg);
  83.                 }
  84.                 Utilities.sleep(30000); // riprovo dopo 10 secondi
  85.             }
  86.             else {
  87.                 String msg = "Configurazione ("+getDescrizione()+") completata con successo";
  88.                 this.log.info(msg);
  89.             }
  90.         }
  91.        
  92.         this.setStop(true);
  93.        
  94.     }
  95.    
  96.     protected String getDescrizione() {
  97.         return "secrets";
  98.     }
  99.    
  100.     private boolean analyze() {
  101.         boolean finish = false;
  102.         List<String> aliases = this.configurazioneNodiRuntime!=null ? this.configurazioneNodiRuntime.getAliases() : null;
  103.         if(aliases!=null && !aliases.isEmpty()) {
  104.             finish = finish(aliases);
  105.         }
  106.         return finish;
  107.     }
  108.     private boolean finish(List<String> aliases) {
  109.         boolean finish = false;
  110.         for (String alias : aliases) {
  111.            
  112.             boolean continueCheck = true;
  113.            
  114.             ConfigurazioneNodiRuntimeBYOKRemoteConfig remoteConfig = new ConfigurazioneNodiRuntimeBYOKRemoteConfig();
  115.             if(!this.configurazioneNodiRuntime.isActiveNode(this.log,
  116.                     alias, remoteConfig)) {
  117.                 continueCheck = false;
  118.             }
  119.            
  120.             if(continueCheck && this.reInitSecretMaps &&
  121.                     !reInitSecretMaps(this.configurazioneNodiRuntime, remoteConfig)) {
  122.                 continueCheck = false;
  123.             }
  124.            
  125.             if(continueCheck &&
  126.                 this.isCompleted(alias)) {
  127.                 finish = true;
  128.                 break; // non itero su altri nodi
  129.             }
  130.         }
  131.         return finish;
  132.     }
  133.    
  134.     protected boolean isCompleted(String alias) {
  135.         if(alias!=null) {
  136.             // usato nei metodi implementati
  137.         }
  138.         return true;
  139.     }
  140.    
  141.     private boolean reInitSecretMaps(ConfigurazioneNodiRuntime configurazioneNodiRuntime, ConfigurazioneNodiRuntimeBYOKRemoteConfig remoteConfig) {
  142.         try{
  143.             Map<String, Object> dynamicMap = new HashMap<>();
  144.             DynamicInfo dynamicInfo = new  DynamicInfo();
  145.             DynamicUtils.fillDynamicMap(this.log, dynamicMap, dynamicInfo);
  146.             configurazioneNodiRuntime.initBYOKDynamicMapRemoteGovWayNode(this.log,dynamicMap, false, true, remoteConfig);
  147.                    
  148.             BYOKMapProperties.initialize(this.log, this.secretsConfig, this.secretsConfigRequired,
  149.                     true,
  150.                     dynamicMap, true);
  151.             BYOKMapProperties secretsProperties = BYOKMapProperties.getInstance();
  152.             secretsProperties.initEnvironment();
  153.             String msgInit = "Environment re-inizializzato con i secrets definiti nel file '"+this.secretsConfig+"'"+
  154.                     "\n\tJavaProperties: "+secretsProperties.getJavaMap().keys()+
  155.                     "\n\tEnvProperties: "+secretsProperties.getEnvMap().keys()+
  156.                     "\n\tObfuscateMode: "+secretsProperties.getObfuscateModeDescription();
  157.             this.log.info(msgInit);
  158.             return true;
  159.         } catch (Exception e) {
  160.             this.log.debug(e.getMessage(),e);
  161.             // provo su tutti i nodi, non voglio mettere un vincolo che serve per forza un nodo acceso
  162.             return false;
  163.         }
  164.     }
  165.    
  166. }