AuditAppender.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.web.lib.audit.appender;

  21. import java.util.ArrayList;
  22. import java.util.HashMap;
  23. import java.util.Iterator;
  24. import java.util.List;
  25. import java.util.Properties;

  26. import org.openspcoop2.core.config.GenericProperties;
  27. import org.openspcoop2.core.config.MessageSecurityFlow;
  28. import org.openspcoop2.core.config.MessageSecurityFlowParameter;
  29. import org.openspcoop2.core.config.PortaApplicativa;
  30. import org.openspcoop2.core.config.PortaDelegata;
  31. import org.openspcoop2.core.config.Property;
  32. import org.openspcoop2.core.config.ServizioApplicativo;
  33. import org.openspcoop2.core.constants.CostantiConnettori;
  34. import org.openspcoop2.core.constants.CostantiDB;
  35. import org.openspcoop2.core.constants.CostantiProprieta;
  36. import org.openspcoop2.core.registry.AccordoServizioParteSpecifica;
  37. import org.openspcoop2.core.registry.Fruitore;
  38. import org.openspcoop2.core.registry.ProtocolProperty;
  39. import org.openspcoop2.pdd.core.byok.DriverBYOKUtilities;
  40. import org.openspcoop2.utils.UtilsException;
  41. import org.openspcoop2.utils.date.DateManager;
  42. import org.openspcoop2.utils.regexp.RegularExpressionEngine;
  43. import org.openspcoop2.utils.resources.ClassLoaderUtilities;
  44. import org.openspcoop2.utils.serialization.FilteredObject;
  45. import org.openspcoop2.utils.serialization.IDBuilder;
  46. import org.openspcoop2.utils.serialization.SerializationConfig;
  47. import org.openspcoop2.web.lib.audit.AuditException;
  48. import org.openspcoop2.web.lib.audit.costanti.Costanti;
  49. import org.openspcoop2.web.lib.audit.dao.Appender;
  50. import org.openspcoop2.web.lib.audit.dao.Configurazione;
  51. import org.openspcoop2.web.lib.audit.dao.Filtro;
  52. import org.openspcoop2.web.lib.audit.log.Binary;
  53. import org.openspcoop2.web.lib.audit.log.Operation;
  54. import org.openspcoop2.web.lib.audit.log.constants.Stato;
  55. import org.openspcoop2.web.lib.audit.log.constants.Tipologia;

  56. /**
  57.  * Appender per registrare operazione di audit
  58.  *
  59.  *
  60.  * @author Andrea Poli (apoli@link.it)
  61.  * @author Stefano Corallo (corallo@link.it)
  62.  * @author Sandra Giangrandi (sandra@link.it)
  63.  * @author $Author$
  64.  * @version $Rev$, $Date$
  65.  *
  66.  */
  67. public class AuditAppender {

  68.     private static final String AUDIT_DISABILITATO = "Audit engine disabilitato";
  69.     private static final String APPENDER_NON_FORNITI = "Appender non forniti";
  70.    
  71.     /* Configurazione */
  72.     private static Configurazione configurazioneAuditing;
  73.     private static IDBuilder idBuilder;
  74.     private static HashMap<String,IAuditAppender> appenders = new HashMap<>();
  75.    
  76.     public void initializeAudit(Configurazione configurazioneAuditing,
  77.             IDBuilder idBuilder) throws AuditException{
  78.         initializeAuditEngine(configurazioneAuditing, idBuilder);
  79.     }
  80.    
  81.     private static synchronized void initializeAuditEngine(Configurazione configurazioneAuditing,
  82.             IDBuilder idBuilder) throws AuditException{
  83.        
  84.         try{
  85.        
  86.             AuditAppender.appenders.clear();    
  87.            
  88.             AuditAppender.configurazioneAuditing= configurazioneAuditing;
  89.             AuditAppender.idBuilder = idBuilder;
  90.            
  91.             for(int i=0; i<AuditAppender.configurazioneAuditing.sizeAppender(); i++){
  92.                
  93.                 // Istanzio appender
  94.                 Appender appenderConf = AuditAppender.configurazioneAuditing.getAppender(i);
  95.                 Class<?> c = Class.forName(appenderConf.getClassName());
  96.                 IAuditAppender appender = (IAuditAppender) ClassLoaderUtilities.newInstance(c);
  97.                
  98.                 // Inizializzo appender
  99.                 Properties propertiesAppender = new Properties();
  100.                 for(int j=0; j<appenderConf.sizeProperties(); j++){
  101.                     propertiesAppender.put(appenderConf.getProperty(j).getName(),
  102.                             appenderConf.getProperty(j).getValue());
  103.                 }
  104.                 appender.initAppender(appenderConf.getNome(),propertiesAppender);
  105.                
  106.                 AuditAppender.appenders.put(appenderConf.getNome(),appender);
  107.             }
  108.            
  109.         }catch(Exception e){
  110.             throw new AuditException("InizializzazioneFallita: "+e.getMessage(),e);
  111.         }
  112.     }
  113.    
  114.     public void updateConfigurazioneAuditing(Configurazione configurazioneAuditing) throws AuditException{
  115.         try{
  116.             synchronized(AuditAppender.configurazioneAuditing){
  117.            
  118.                 AuditAppender.configurazioneAuditing = configurazioneAuditing;
  119.                
  120.                 AuditAppender.appenders.clear();
  121.                
  122.                 for(int i=0; i<AuditAppender.configurazioneAuditing.sizeAppender(); i++){
  123.                    
  124.                     // Istanzio appender
  125.                     Appender appenderConf = AuditAppender.configurazioneAuditing.getAppender(i);
  126.                     Class<?> c = Class.forName(appenderConf.getClassName());
  127.                     IAuditAppender appender = (IAuditAppender) ClassLoaderUtilities.newInstance(c);
  128.                    
  129.                     // Inizializzo appender
  130.                     Properties propertiesAppender = new Properties();
  131.                     for(int j=0; j<appenderConf.sizeProperties(); j++){
  132.                         propertiesAppender.put(appenderConf.getProperty(j).getName(),
  133.                                 appenderConf.getProperty(j).getValue());
  134.                     }
  135.                     appender.initAppender(appenderConf.getNome(),propertiesAppender);
  136.                    
  137.                     AuditAppender.appenders.put(appenderConf.getNome(),appender);
  138.                 }
  139.             }
  140.         }catch(Exception e){
  141.             throw new AuditException("AggiornamentoFallito: "+e.getMessage(),e);
  142.         }
  143.     }

  144.    
  145.    
  146.     public IDOperazione registraOperazioneInFaseDiElaborazione(Tipologia tipoOperazione,Object object,String user,String interfaceMsg, boolean registrazioneBinari,
  147.             DriverBYOKUtilities byok) throws AuditException,AuditDisabilitatoException{
  148.        
  149.         if(!AuditAppender.configurazioneAuditing.isAuditEngineEnabled()){
  150.             throw new AuditDisabilitatoException(AUDIT_DISABILITATO);
  151.         }
  152.        
  153.         try{
  154.            
  155.                
  156.             Operation operation = new Operation();
  157.             operation.setTipologia(tipoOperazione);
  158.             operation.setUtente(user);
  159.             operation.setStato(Stato.REQUESTING);
  160.             operation.setTimeRequest(DateManager.getDate());
  161.             operation.setTimeExecute(DateManager.getDate());
  162.             operation.setInterfaceMsg(interfaceMsg);
  163.            
  164.             if(object!=null){
  165.                
  166.                 operation.setTipoOggetto(AuditAppender.idBuilder.getSimpleName(object));
  167.                 operation.setObjectId(AuditAppender.idBuilder.toID(object));
  168.                 operation.setObjectOldId(AuditAppender.idBuilder.toOldID(object));
  169.                 operation.setObjectClass(object.getClass().getName());
  170.                
  171.             }else{
  172.                 throw new AuditException("Object riguardante l'operazione non definito");
  173.             }

  174.            
  175.             // Filtro operazioni
  176.             org.openspcoop2.utils.serialization.Filter listFilter = new org.openspcoop2.utils.serialization.Filter();
  177.             String objectDetails = filtraOperazione(operation,object,listFilter, registrazioneBinari, byok);
  178.            
  179.             if(objectDetails!=null){
  180.                 operation.setObjectDetails(objectDetails);
  181.                
  182.                 // Aggiunto binaries filtrati
  183.                 for(int i=0; i<listFilter.sizeFilteredObjects(); i++){
  184.                     FilteredObject filteredObject = listFilter.getFilteredObject(i);
  185.                    
  186.                     Binary binary = new Binary();
  187.                     binary.setBinaryId(filteredObject.getId());
  188.                     binary.setChecksum(filteredObject.getChecksum());
  189.                     operation.addBinary(binary);
  190.                 }
  191.             }
  192.                
  193.            
  194.             // Appender
  195.             IDOperazione idOperazione = new IDOperazione();
  196.             if(AuditAppender.appenders!=null && AuditAppender.appenders.keySet()!=null){
  197.                 Iterator<String> iterator = AuditAppender.appenders.keySet().iterator();
  198.                 while(iterator.hasNext()){
  199.                    
  200.                     String appenderName = iterator.next();
  201.                     IAuditAppender appender =  AuditAppender.appenders.get(appenderName);
  202.                                    
  203.                     Object idOperazioneAppender =  appender.registraOperazioneInFaseDiElaborazione(operation);
  204.                     idOperazione.addIdOperazione(appenderName, idOperazioneAppender);
  205.                 }
  206.                 return idOperazione;
  207.             }
  208.             else{
  209.                 throw new AuditException(APPENDER_NON_FORNITI);
  210.             }
  211.                
  212.            
  213.         }catch(AuditDisabilitatoException e){
  214.             throw e;
  215.         }catch(Exception e){
  216.             throw new AuditException("registraOperazioneInFaseDiElaborazione error: "+e.getMessage(),e);
  217.         }
  218.        
  219.     }
  220.    
  221.    
  222.    
  223.     public void registraOperazioneAccesso(Tipologia tipoOperazione,String user,String interfaceMsg, boolean registrazioneBinari,
  224.             DriverBYOKUtilities byok) throws AuditException,AuditDisabilitatoException{
  225.        
  226.         if(!AuditAppender.configurazioneAuditing.isAuditEngineEnabled()){
  227.             throw new AuditDisabilitatoException(AUDIT_DISABILITATO);
  228.         }
  229.        
  230.         try{
  231.            
  232.                
  233.             Operation operation = new Operation();
  234.             operation.setTipologia(tipoOperazione);
  235.             operation.setUtente(user);
  236.             operation.setStato(Stato.COMPLETED);
  237.             operation.setTimeRequest(DateManager.getDate());
  238.             operation.setTimeExecute(DateManager.getDate());
  239.             operation.setInterfaceMsg(interfaceMsg);
  240.                    
  241.            
  242.             // Filtro operazioni
  243.             filtraOperazione(operation, registrazioneBinari, byok);
  244.            
  245.            
  246.             // Appender
  247.             IDOperazione idOperazione = new IDOperazione();
  248.             if(AuditAppender.appenders!=null && AuditAppender.appenders.keySet()!=null){
  249.                 Iterator<String> iterator = AuditAppender.appenders.keySet().iterator();
  250.                 while(iterator.hasNext()){
  251.                    
  252.                     String appenderName = iterator.next();
  253.                     IAuditAppender appender =  AuditAppender.appenders.get(appenderName);
  254.                                    
  255.                     Object idOperazioneAppender =  appender.registraOperazioneInFaseDiElaborazione(operation);
  256.                     idOperazione.addIdOperazione(appenderName, idOperazioneAppender);
  257.                 }
  258.             }
  259.             else{
  260.                 throw new AuditException(APPENDER_NON_FORNITI);
  261.             }
  262.                
  263.            
  264.         }catch(AuditDisabilitatoException e){
  265.             throw e;
  266.         }catch(Exception e){
  267.             throw new AuditException("registraOperazioneInFaseDiElaborazione error: "+e.getMessage(),e);
  268.         }
  269.        
  270.     }
  271.    
  272.    
  273.    
  274.     public void registraOperazioneCompletataConSuccesso(IDOperazione idOperazione,String interfaceMsg) throws AuditException,AuditDisabilitatoException{
  275.        
  276.         if(!AuditAppender.configurazioneAuditing.isAuditEngineEnabled()){
  277.             throw new AuditDisabilitatoException(AUDIT_DISABILITATO);
  278.         }
  279.        
  280.         try{
  281.            
  282.             if(idOperazione==null){
  283.                 throw new AuditException("Identificativo dell'operazione per cui cambiare stato non fornito");
  284.             }
  285.            
  286.             if(AuditAppender.appenders!=null && AuditAppender.appenders.keySet()!=null){
  287.                 Iterator<String> iterator = AuditAppender.appenders.keySet().iterator();
  288.                 while(iterator.hasNext()){
  289.                    
  290.                     String appenderName = iterator.next();
  291.                     IAuditAppender appender =  AuditAppender.appenders.get(appenderName);
  292.                     Object id = idOperazione.getIdOperazione(appenderName);
  293.                     if(id==null){
  294.                         throw new AuditException("Identificativo dell'operazione per l'appender["+appenderName+"] non fornito");
  295.                     }
  296.                     if(id instanceof Operation){
  297.                         Operation op = (Operation) id;
  298.                         op.setInterfaceMsg(interfaceMsg);
  299.                     }
  300.                    
  301.                     appender.registraOperazioneCompletataConSuccesso(id);
  302.                 }
  303.             }
  304.             else{
  305.                 throw new AuditException(APPENDER_NON_FORNITI);
  306.             }
  307.                
  308.            
  309.         }catch(Exception e){
  310.             throw new AuditException("registraOperazioneCompletataConSuccesso error: "+e.getMessage(),e);
  311.         }
  312.     }
  313.    
  314.    
  315.    
  316.    
  317.    
  318.     public void registraOperazioneTerminataConErrore(IDOperazione idOperazione,String motivoErrore,String interfaceMsg) throws AuditException,AuditDisabilitatoException{
  319.        
  320.         if(!AuditAppender.configurazioneAuditing.isAuditEngineEnabled()){
  321.             throw new AuditDisabilitatoException(AUDIT_DISABILITATO);
  322.         }
  323.        
  324.         try{
  325.            
  326.             if(idOperazione==null){
  327.                 throw new AuditException("Identificativo dell'operazione per cui cambiare stato non fornito");
  328.             }
  329.                        
  330.             if(AuditAppender.appenders!=null && AuditAppender.appenders.keySet()!=null){
  331.                 Iterator<String> iterator = AuditAppender.appenders.keySet().iterator();
  332.                 while(iterator.hasNext()){
  333.                    
  334.                     String appenderName = iterator.next();
  335.                     IAuditAppender appender =  AuditAppender.appenders.get(appenderName);
  336.                     Object id = idOperazione.getIdOperazione(appenderName);
  337.                     if(id==null){
  338.                         throw new AuditException("Identificativo dell'operazione per l'appender["+appenderName+"] non fornito");
  339.                     }      
  340.                     if(id instanceof Operation){
  341.                         Operation op = (Operation) id;
  342.                         op.setInterfaceMsg(interfaceMsg);
  343.                     }
  344.                                    
  345.                     appender.registraOperazioneTerminataConErrore(id,motivoErrore);
  346.                 }
  347.             }
  348.             else{
  349.                 throw new AuditException(APPENDER_NON_FORNITI);
  350.             }
  351.                
  352.            
  353.         }catch(Exception e){
  354.             throw new AuditException("registraOperazioneCompletataConSuccesso error: "+e.getMessage(),e);
  355.         }
  356.     }
  357.    
  358.    
  359.    
  360.    
  361.    
  362.    
  363.    
  364.    
  365.    
  366.    
  367.    
  368.    
  369.    
  370.     // -------------- UTILITY -------------------
  371.    
  372.     private String serializeJsonObject(Object o,org.openspcoop2.utils.serialization.Filter listFilter, boolean registrazioneBinari) throws AuditException{
  373.         try{
  374.             if(!registrazioneBinari) {
  375.                 listFilter.addFilterByValue(byte[].class);
  376.             }
  377.             SerializationConfig config = new SerializationConfig();
  378.             config.setFilter(listFilter);
  379.             config.setIdBuilder(AuditAppender.idBuilder);
  380.             config.setPrettyPrint(true);
  381.             // Deprecato
  382. /**         org.openspcoop2.utils.serialization.JSonSerializer serializer =
  383.                 new org.openspcoop2.utils.serialization.JSonSerializer(config);*/
  384.             org.openspcoop2.utils.serialization.JsonJacksonSerializer serializer =
  385.                 new org.openspcoop2.utils.serialization.JsonJacksonSerializer(config);
  386.             return  serializer.getObject(o);        
  387.         }catch(Exception e){
  388.             throw new AuditException("serializeJsonObject error: "+e.getMessage(),e);
  389.         }
  390.     }
  391.    
  392.     private String serializeXMLObject(Object o,org.openspcoop2.utils.serialization.Filter listFilter, boolean registrazioneBinari) throws AuditException{
  393.         try{
  394.             if(!registrazioneBinari) {
  395.                 listFilter.addFilterByValue(byte[].class);
  396.             }
  397.             SerializationConfig config = new SerializationConfig();
  398.             config.setFilter(listFilter);
  399.             config.setIdBuilder(AuditAppender.idBuilder);
  400.             config.setPrettyPrint(true);
  401.             org.openspcoop2.utils.serialization.XMLSerializer serializer =
  402.                 new org.openspcoop2.utils.serialization.XMLSerializer(config);
  403.             return  serializer.getObject(o);        
  404.         }catch(Exception e){
  405.             throw new AuditException("serializeXMLObject error: "+e.getMessage(),e);
  406.         }
  407.     }
  408.    
  409.     private void filtraOperazione(Operation operation, boolean registrazioneBinari,
  410.             DriverBYOKUtilities byok) throws AuditException,AuditDisabilitatoException{
  411.         this.filtraOperazione(operation, null, null, registrazioneBinari,
  412.                 byok);
  413.     }
  414.     private String filtraOperazione(Operation operation,Object object,org.openspcoop2.utils.serialization.Filter listFilter, boolean registrazioneBinari,
  415.             DriverBYOKUtilities byok) throws AuditException,AuditDisabilitatoException{
  416.        
  417.         try{
  418.        
  419.             ArrayList<Filtro> filtri = AuditAppender.configurazioneAuditing.getFiltri();
  420.             boolean auditEnabledDefault = AuditAppender.configurazioneAuditing.isAuditEnabled();
  421.             boolean dumpEnabledDefault = AuditAppender.configurazioneAuditing.isDumpEnabled();
  422.             String objectDetails = null;
  423.            
  424.             for(int i=0; i<filtri.size(); i++){
  425.                
  426.                 Filtro filtro = filtri.get(i);
  427.                
  428.                 /**System.out.println("ANALIZZO FILTRO["+i+"]: "+filtro.toString());*/
  429.                
  430.                 if(filtro.getUsername()==null &&
  431.                         filtro.getTipoOperazione()==null &&
  432.                         filtro.getTipoOggettoInModifica()==null &&
  433.                         filtro.getStatoOperazione()==null &&
  434.                         filtro.getDump()==null){
  435.                     throw new AuditException("Filtro("+i+1+") non valido: nessun meccanismo di filtro definito");
  436.                 }
  437.                
  438.                 if(filtro.getUsername()!=null &&
  439.                     // Se e' definito un username nel filtro controllo che corrisponda
  440.                     !filtro.getUsername().equals(operation.getUtente())){
  441.                     continue;
  442.                 }
  443.                
  444.                 if(filtro.getTipoOperazione()!=null &&
  445.                     // Se e' definito un tipo di operazione nel filtro controllo che corrisponda
  446.                     !filtro.getTipoOperazione().equals(operation.getTipologia())){
  447.                     continue;
  448.                 }
  449.                
  450.                 if(filtro.getTipoOggettoInModifica()!=null &&
  451.                     // Se e' definito un TipoOggettoInModifica nel filtro controllo che corrisponda
  452.                     !filtro.getTipoOggettoInModifica().equals(operation.getTipoOggetto())){
  453.                     continue;
  454.                 }
  455.                
  456.                 if(filtro.getStatoOperazione()!=null &&
  457.                     // Se e' definito uno stato dell'operazione nel filtro controllo che corrisponda
  458.                     !filtro.getStatoOperazione().equals(operation.getStato())){
  459.                     continue;
  460.                 }
  461.                
  462.                 if(filtro.getDump()!=null){
  463.                    
  464.                     if(object==null){
  465.                         continue; // Questo filtro per essere matchato deve essere controllato con un oggetto
  466.                     }
  467.                    
  468.                     if(objectDetails==null){
  469.                         // Serializzo l'oggetto
  470.                         objectDetails = this.serialize(object, listFilter, registrazioneBinari, byok);
  471.                     }
  472.                    
  473.                     // Se e' definito un filtro sul contenuto dell'operazione nel filtro controllo che corrisponda
  474.                     if(filtro.isDumpExprRegular()){
  475.                         if(RegularExpressionEngine.getStringMatchPattern(objectDetails, filtro.getDump())==null){
  476.                             continue;
  477.                         }
  478.                     }else{
  479.                         if(!objectDetails.contains(filtro.getDump())){
  480.                             continue;
  481.                         }
  482.                     }
  483.                 }
  484.                                
  485.                 // Se arriviamo a questo punto significa che l'operazione soddisfa il filtro.
  486.                 if(!filtro.isAuditEnabled()){
  487.                     throw new AuditDisabilitatoException("Audit disabilitato, criterio adottato in base al filtro numero "+(i+1));
  488.                 }
  489.                 if(filtro.isDumpEnabled()){
  490.                     if(object!=null &&
  491.                         // Se non avevo gia' serializzato prima per la ricerca tramite contenuto
  492.                         objectDetails==null){
  493.                         // Serializzo l'oggetto
  494.                         objectDetails = this.serialize(object, listFilter, registrazioneBinari, byok);
  495.                     }
  496.                 }else{
  497.                     if(objectDetails!=null){
  498.                         objectDetails=null; // era stato utilizzato per effettuare filtro sui contenuti ma non deve essere dumpato
  499.                     }
  500.                 }
  501.                                
  502.                 return objectDetails; // regola matcha
  503.             }
  504.            
  505.             // Se nessun filtro sopra viene matchato, si applica il criterio di default
  506.             if(!auditEnabledDefault){
  507.                 throw new AuditDisabilitatoException("Audit disabilitato nella configurazione generale (Non vi sono filtri che creano eccezioni)");
  508.             }
  509.             if(dumpEnabledDefault){
  510.                 if(object!=null &&
  511.                     // Se non avevo gia' serializzato prima per la ricerca tramite contenuto
  512.                     objectDetails==null){
  513.                     // Serializzo l'oggetto
  514.                     objectDetails = this.serialize(object, listFilter, registrazioneBinari, byok);
  515.                 }
  516.             }else{
  517.                 if(objectDetails!=null){
  518.                     objectDetails=null; // era stato utilizzato per effettuare filtro sui contenuti ma non deve essere dumpato
  519.                 }
  520.             }
  521.            
  522.             return objectDetails;
  523.            
  524.         }catch(AuditDisabilitatoException e){
  525.             throw e;
  526.         }catch(Exception e){
  527.             throw new AuditException("filtraOperazione error: "+e.getMessage(),e);
  528.         }
  529.     }
  530.    
  531.     private Object byok(DriverBYOKUtilities byok, Object object) throws UtilsException {
  532.         if (object instanceof org.openspcoop2.core.config.Soggetto) {
  533.             org.openspcoop2.core.config.Soggetto sCloned = (org.openspcoop2.core.config.Soggetto) ((org.openspcoop2.core.config.Soggetto) object).clone();
  534.             if(sCloned.getConnettore()!=null && !sCloned.getConnettore().isEmpty()) {
  535.                 for (org.openspcoop2.core.config.Connettore connettore : sCloned.getConnettore()) {
  536.                     byokConnettore(byok, connettore);      
  537.                 }
  538.             }
  539.             return sCloned;
  540.         }
  541.         else if (object instanceof org.openspcoop2.core.registry.Soggetto) {
  542.             org.openspcoop2.core.registry.Soggetto sCloned = (org.openspcoop2.core.registry.Soggetto) ((org.openspcoop2.core.registry.Soggetto) object).clone();
  543.             if(sCloned.getConnettore()!=null) {
  544.                 byokConnettore(byok, sCloned.getConnettore());
  545.             }
  546.             return sCloned;
  547.         }
  548.         else if (object instanceof ServizioApplicativo) {
  549.             ServizioApplicativo sCloned = (ServizioApplicativo) ((ServizioApplicativo) object).clone();
  550.             return byok(byok, sCloned);
  551.         }
  552.         else if (object instanceof PortaDelegata) {
  553.             PortaDelegata pdCloned = (PortaDelegata) ((PortaDelegata) object).clone();
  554.             return byok(byok, pdCloned);
  555.         }
  556.         else if (object instanceof PortaApplicativa) {
  557.             PortaApplicativa paCloned = (PortaApplicativa) ((PortaApplicativa) object).clone();
  558.             return byok(byok, paCloned);
  559.         }
  560.         else if(object instanceof GenericProperties) {
  561.             GenericProperties gpCloned = (GenericProperties) ((GenericProperties) object).clone();
  562.             return byok(byok, gpCloned);
  563.         }
  564.         else if(object instanceof AccordoServizioParteSpecifica) {
  565.             AccordoServizioParteSpecifica aspsCloned = (AccordoServizioParteSpecifica) ((AccordoServizioParteSpecifica) object).clone();
  566.             return byok(byok, aspsCloned);
  567.         }
  568.        
  569.         return object;
  570.     }
  571.     private Object byok(DriverBYOKUtilities byok, ServizioApplicativo sCloned) throws UtilsException {
  572.         if(sCloned.getInvocazioneServizio()!=null && sCloned.getInvocazioneServizio().getConnettore()!=null) {
  573.             byokConnettore(byok, sCloned.getInvocazioneServizio().getConnettore());
  574.         }
  575.         if(sCloned.getInvocazioneServizio()!=null && sCloned.getInvocazioneServizio().getAutenticazione()!=null &&
  576.                 org.openspcoop2.core.config.constants.InvocazioneServizioTipoAutenticazione.BASIC.equals(sCloned.getInvocazioneServizio().getAutenticazione()) &&
  577.                 sCloned.getInvocazioneServizio().getCredenziali()!=null) {
  578.             byokCredenzialiConnettore(byok, sCloned.getInvocazioneServizio().getCredenziali());
  579.         }
  580.         if(sCloned.getRispostaAsincrona()!=null && sCloned.getRispostaAsincrona().getConnettore()!=null) {
  581.             byokConnettore(byok, sCloned.getRispostaAsincrona().getConnettore());
  582.         }
  583.         if(sCloned.getRispostaAsincrona()!=null && sCloned.getRispostaAsincrona().getAutenticazione()!=null &&
  584.                 org.openspcoop2.core.config.constants.InvocazioneServizioTipoAutenticazione.BASIC.equals(sCloned.getRispostaAsincrona().getAutenticazione()) &&
  585.                 sCloned.getRispostaAsincrona().getCredenziali()!=null) {
  586.             byokCredenzialiConnettore(byok, sCloned.getRispostaAsincrona().getCredenziali());
  587.         }
  588.         if(sCloned.sizeProtocolPropertyList()>0) {
  589.             for (org.openspcoop2.core.config.ProtocolProperty p : sCloned.getProtocolProperty()) {
  590.                 if(isConfidentialProtocolProperties(p.getName())) {
  591.                     p.setValue(byok.wrap(p.getValue()));
  592.                 }
  593.             }
  594.         }
  595.         return sCloned;
  596.     }
  597.     private void byokCredenzialiConnettore(DriverBYOKUtilities byok, org.openspcoop2.core.config.InvocazioneCredenziali credenziale) throws UtilsException {
  598.         if(credenziale.getPassword()!=null) {
  599.             credenziale.setPassword(byok.wrap(credenziale.getPassword()));
  600.         }
  601.     }
  602.     private void byokConnettore(DriverBYOKUtilities byok, org.openspcoop2.core.config.Connettore connettore) throws UtilsException {
  603.         if(connettore.sizePropertyList()>0) {
  604.             for (org.openspcoop2.core.config.Property p : connettore.getPropertyList()) {
  605.                 if(isConfidentialConnettore(p.getNome())) {
  606.                     p.setValore(byok.wrap(p.getValore()));
  607.                 }
  608.             }
  609.         }  
  610.     }
  611.     private void byokConnettore(DriverBYOKUtilities byok, org.openspcoop2.core.registry.Connettore connettore) throws UtilsException {
  612.         if(connettore.sizePropertyList()>0) {
  613.             for (org.openspcoop2.core.registry.Property p : connettore.getPropertyList()) {
  614.                 if(isConfidentialConnettore(p.getNome())) {
  615.                     p.setValore(byok.wrap(p.getValore()));
  616.                 }
  617.             }
  618.         }  
  619.     }
  620.     private boolean isConfidentialConnettore(String nomeProprieta) {
  621.         for (String nomeProprietaConfidential : CostantiConnettori.getConfidentials()) {
  622.             if(nomeProprieta.equals(nomeProprietaConfidential)) {
  623.                 return true;
  624.             }
  625.         }
  626.         return false;
  627.     }
  628.     private Object byok(DriverBYOKUtilities byok, PortaDelegata pdCloned) throws UtilsException {
  629.         if(pdCloned.getMessageSecurity()!=null) {
  630.             if(pdCloned.getMessageSecurity().getRequestFlow()!=null) {
  631.                 byok(byok, pdCloned.getMessageSecurity().getRequestFlow());
  632.             }
  633.             if(pdCloned.getMessageSecurity().getResponseFlow()!=null) {
  634.                 byok(byok, pdCloned.getMessageSecurity().getResponseFlow());
  635.             }
  636.         }
  637.         return pdCloned;
  638.     }
  639.     private Object byok(DriverBYOKUtilities byok, PortaApplicativa paCloned) throws UtilsException {
  640.         if(paCloned.getMessageSecurity()!=null) {
  641.             if(paCloned.getMessageSecurity().getRequestFlow()!=null) {
  642.                 byok(byok, paCloned.getMessageSecurity().getRequestFlow());
  643.             }
  644.             if(paCloned.getMessageSecurity().getResponseFlow()!=null) {
  645.                 byok(byok, paCloned.getMessageSecurity().getResponseFlow());
  646.             }
  647.         }
  648.         return paCloned;
  649.     }
  650.     private void byok(DriverBYOKUtilities byok, MessageSecurityFlow flow) throws UtilsException {
  651.         if(flow.sizeParameterList()>0) {
  652.             for (MessageSecurityFlowParameter p : flow.getParameterList()) {
  653.                 if(isConfidentialSecurity(p.getNome())) {
  654.                     p.setValore(byok.wrap(p.getValore()));
  655.                 }
  656.             }
  657.         }
  658.     }
  659.     private boolean isConfidentialSecurity(String nomeProprieta) {
  660.         List<String> messageSecurityIds = CostantiProprieta.getMessageSecurityIds();
  661.         if(messageSecurityIds!=null && !messageSecurityIds.isEmpty()) {
  662.             for (String id : messageSecurityIds) {
  663.                 List<String> l =  CostantiProprieta.getMessageSecurityProperties(id);
  664.                 for (String nomeProprietaCheck : l) {
  665.                     if(nomeProprietaCheck.equals(nomeProprieta)) {
  666.                         return true;
  667.                     }
  668.                 }
  669.             }
  670.         }
  671.         return false;
  672.     }
  673.    
  674.     private Object byok(DriverBYOKUtilities byok, GenericProperties gp) throws UtilsException {
  675.         if(gp.sizePropertyList()>0) {
  676.             for (Property p : gp.getPropertyList()) {
  677.                 if(isConfidentialToken(gp.getTipo(), p.getNome())) {
  678.                     p.setValore(byok.wrap(p.getValore()));
  679.                 }
  680.             }
  681.         }
  682.         return gp;
  683.     }
  684.     private boolean isConfidentialToken(String tipo, String nomeProprieta) {
  685.         List<String> l = null;
  686.         if(CostantiProprieta.TOKEN_VALIDATION_ID.equals(tipo)) {
  687.             l = CostantiProprieta.getTokenValidationProperties();
  688.         }
  689.         else if(CostantiProprieta.TOKEN_NEGOZIAZIONE_ID.equals(tipo)) {
  690.             l = CostantiProprieta.getTokenRetrieveProperties();
  691.         }
  692.         else if(CostantiProprieta.ATTRIBUTE_AUTHORITY_ID.equals(tipo)) {
  693.             l = CostantiProprieta.getAttributeAuthorityProperties();
  694.         }
  695.         if(l!=null) {
  696.             for (String nomeProprietaCheck : l) {
  697.                 if(nomeProprietaCheck.equals(nomeProprieta)) {
  698.                     return true;
  699.                 }
  700.             }
  701.         }
  702.         return false;
  703.     }
  704.     private Object byok(DriverBYOKUtilities byok, AccordoServizioParteSpecifica asps) throws UtilsException {
  705.         if(asps.sizeProtocolPropertyList()>0) {
  706.             for (ProtocolProperty p : asps.getProtocolProperty()) {
  707.                 if(isConfidentialProtocolProperties(p.getName())) {
  708.                     p.setValue(byok.wrap(p.getValue()));
  709.                 }
  710.             }
  711.         }
  712.         if(asps.sizeFruitoreList()>0) {
  713.             for (Fruitore fruitore : asps.getFruitoreList()) {
  714.                 byok(byok, fruitore);
  715.             }
  716.         }
  717.         if(asps.getConfigurazioneServizio()!=null && asps.getConfigurazioneServizio().getConnettore()!=null) {
  718.             byokConnettore(byok, asps.getConfigurazioneServizio().getConnettore());
  719.         }
  720.         return asps;
  721.     }
  722.     private Object byok(DriverBYOKUtilities byok, Fruitore fruitore) throws UtilsException {
  723.         if(fruitore.sizeProtocolPropertyList()>0) {
  724.             for (ProtocolProperty p : fruitore.getProtocolProperty()) {
  725.                 if(isConfidentialProtocolProperties(p.getName())) {
  726.                     p.setValue(byok.wrap(p.getValue()));
  727.                 }
  728.             }
  729.         }
  730.         if(fruitore.getConnettore()!=null) {
  731.             byokConnettore(byok, fruitore.getConnettore());
  732.         }
  733.         return fruitore;
  734.     }
  735.     private boolean isConfidentialProtocolProperties(String nomeProprieta) {
  736.         return CostantiDB.MODIPA_KEYSTORE_PASSWORD.equals(nomeProprieta) ||
  737.                 CostantiDB.MODIPA_KEY_PASSWORD.equals(nomeProprieta) ||
  738.                 CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CERTIFICATI_TRUSTSTORE_PASSWORD.equals(nomeProprieta) ||
  739.                 CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SSL_TRUSTSTORE_PASSWORD.equals(nomeProprieta);
  740.     }
  741.    
  742.    
  743.     private String serialize(Object object,org.openspcoop2.utils.serialization.Filter listFilter, boolean registrazioneBinari,
  744.             DriverBYOKUtilities byok)throws AuditException{
  745.         if(byok!=null) {
  746.             try {
  747.                 object = byok(byok, object);
  748.             }catch(Exception e) {
  749.                 throw new AuditException(e.getMessage(),e);
  750.             }
  751.         }
  752.         if(Costanti.DUMP_JSON_FORMAT.equals(AuditAppender.configurazioneAuditing.getDumpFormat())){
  753.             return this.serializeJsonObject(object, listFilter, registrazioneBinari);
  754.         }
  755.         else if(Costanti.DUMP_XML_FORMAT.equals(AuditAppender.configurazioneAuditing.getDumpFormat())){
  756.             return this.serializeXMLObject(object, listFilter, registrazioneBinari);
  757.         }else {
  758.             throw new AuditException("Tipo di formattazione non conosciuta: "+AuditAppender.configurazioneAuditing.getDumpFormat());
  759.         }
  760.     }
  761. }