TokenKeystoreInjectUtilities.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.core.token;

  21. import java.util.Map;
  22. import java.util.Properties;

  23. import org.openspcoop2.core.config.PortaApplicativa;
  24. import org.openspcoop2.core.config.PortaDelegata;
  25. import org.openspcoop2.core.config.driver.DriverConfigurazioneException;
  26. import org.openspcoop2.pdd.core.connettori.ConnettoreBase;
  27. import org.openspcoop2.pdd.core.connettori.ConnettoreBaseWithResponse;
  28. import org.openspcoop2.pdd.core.controllo_traffico.PolicyTimeoutConfig;
  29. import org.openspcoop2.pdd.core.controllo_traffico.ReadTimeoutConfigurationUtils;
  30. import org.openspcoop2.pdd.core.controllo_traffico.ReadTimeoutContextParam;
  31. import org.openspcoop2.pdd.core.controllo_traffico.SogliaReadTimeout;
  32. import org.openspcoop2.pdd.core.controllo_traffico.TimeoutNotifier;
  33. import org.openspcoop2.pdd.core.controllo_traffico.TimeoutNotifierType;
  34. import org.openspcoop2.pdd.core.dynamic.DynamicMapBuilderUtils;
  35. import org.openspcoop2.protocol.sdk.Busta;
  36. import org.openspcoop2.protocol.sdk.Context;
  37. import org.openspcoop2.protocol.sdk.IProtocolFactory;
  38. import org.openspcoop2.protocol.sdk.ProtocolException;
  39. import org.openspcoop2.protocol.sdk.state.IState;
  40. import org.openspcoop2.protocol.sdk.state.RequestInfo;
  41. import org.openspcoop2.security.SecurityException;
  42. import org.openspcoop2.security.message.jose.JOSECostanti;
  43. import org.openspcoop2.security.message.jose.JOSEUtils;
  44. import org.openspcoop2.utils.transport.http.HttpUtilities;
  45. import org.slf4j.Logger;

  46. /**    
  47.  * TokenKeystoreInjectUtilities
  48.  *
  49.  * @author Poli Andrea (poli@link.it)
  50.  * @author $Author$
  51.  * @version $Rev$, $Date$
  52.  */
  53. public class TokenKeystoreInjectUtilities {
  54.    
  55.     private Logger log;
  56.     private RequestInfo requestInfo;
  57.     private IProtocolFactory<?> protocolFactory;
  58.     private Context context;
  59.     private IState state;
  60.     private Busta busta;
  61.    
  62.     private boolean portaDelegata;
  63.     private PortaDelegata pd;
  64.     private PortaApplicativa pa;
  65.    
  66.     private PolicyTimeoutConfig policyTimeoutConfig;
  67.     private int connectionTimeout = -1;
  68.     private boolean connectionTimeoutConfigurazioneGlobale = true;
  69.     private int readConnectionTimeout = -1;
  70.     private boolean readConnectionTimeoutConfigurazioneGlobale = true;
  71.    
  72.     public TokenKeystoreInjectUtilities(Logger log, RequestInfo requestInfo,IProtocolFactory<?> protocolFactory,Context context,IState state,Busta busta) {
  73.         this.log = log;
  74.         this.requestInfo = requestInfo;
  75.         this.protocolFactory = protocolFactory;
  76.         this.context = context;
  77.         this.state = state;
  78.         this.busta = busta;
  79.     }
  80.        
  81.     public void initTokenPolicyValidazioneJwt(String nomePolicy,boolean portaDelegata, PortaDelegata pd, PortaApplicativa pa, Properties p) {
  82.         this.policyTimeoutConfig = new PolicyTimeoutConfig();
  83.         this.policyTimeoutConfig.setPolicyValidazioneJwt(nomePolicy);
  84.         init(portaDelegata, pd, pa, p);
  85.     }
  86.     public void initAttributeAuthorityValidazioneRispostaJwt(String nomePolicy,boolean portaDelegata, PortaDelegata pd, PortaApplicativa pa, Properties p) {
  87.         this.policyTimeoutConfig = new PolicyTimeoutConfig();
  88.         this.policyTimeoutConfig.setAttributeAuthorityResponseJwt(nomePolicy);
  89.         init(portaDelegata, pd, pa, p);
  90.     }
  91.     private void init(boolean portaDelegata, PortaDelegata pd, PortaApplicativa pa, Properties p) {
  92.         this.portaDelegata = portaDelegata;
  93.         this.pd = pd;
  94.         this.pa = pa;
  95.        
  96.         String trustStoreSslConnectionTimeoutPropertyName =  JOSECostanti.ID_TRUSTSTORE_SSL_KEYSTORE_CONNECTION_TIMEOUT;
  97.         String trustStoreSslReadTimeoutPropertyName =  JOSECostanti.ID_TRUSTSTORE_SSL_KEYSTORE_READ_TIMEOUT;
  98.         String trustStoreSslConnectionTimeoutProperty =  p.getProperty(trustStoreSslConnectionTimeoutPropertyName);
  99.         String trustStoreSslReadTimeoutProperty =  p.getProperty(trustStoreSslReadTimeoutPropertyName);
  100.         if(trustStoreSslConnectionTimeoutProperty!=null && trustStoreSslReadTimeoutProperty!=null) {
  101.             this.connectionTimeout = Integer.valueOf(trustStoreSslConnectionTimeoutProperty);
  102.             this.readConnectionTimeout = Integer.valueOf(trustStoreSslReadTimeoutProperty);
  103.             this.connectionTimeoutConfigurazioneGlobale = false;
  104.             this.readConnectionTimeoutConfigurazioneGlobale = false;
  105.         }
  106.         else {
  107.             this.connectionTimeout = HttpUtilities.HTTP_CONNECTION_TIMEOUT;
  108.             this.readConnectionTimeout = HttpUtilities.HTTP_READ_CONNECTION_TIMEOUT;
  109.         }
  110.     }
  111.    
  112.     public void inject(Properties p) throws SecurityException {
  113.        
  114.         try {
  115.             boolean throwError = true;
  116.             Map<String,Object> dynamicMap = DynamicMapBuilderUtils.buildDynamicMap(this.busta, this.requestInfo, this.context, this.log);
  117.             JOSEUtils.injectKeystore(this.requestInfo, dynamicMap, p, this.log, throwError); // serve per leggere il keystore dalla cache
  118.         }catch(SecurityException e) {
  119.            
  120.             String msgErrore = ConnettoreBase.readConnectionExceptionMessageFromException(e);
  121.            
  122.             this.processConnectionTimeoutException(this.connectionTimeout, this.connectionTimeoutConfigurazioneGlobale, e, msgErrore);
  123.            
  124.             this.processReadTimeoutException(this.readConnectionTimeout, this.readConnectionTimeoutConfigurazioneGlobale, e, msgErrore);
  125.            
  126.             throw e;
  127.         }
  128.        
  129.     }
  130.    
  131.     private void processReadTimeoutException(int timeout, boolean configurazioneGlobale, Exception e, String message) {
  132.         try {
  133.             if(timeout>0 && ConnettoreBaseWithResponse.containsReadTimeoutException(e, message)) {
  134.                 TimeoutNotifier notifier = getTimeoutNotifier(timeout, configurazioneGlobale, TimeoutNotifierType.WAIT_RESPONSE);
  135.                 notifier.notify(timeout);
  136.             }
  137.         }catch(Exception error) {
  138.             if(this.log!=null) {
  139.                 this.log.error("Errore avvenuto durante la registrazione dell'evento di read timeout: "+error.getMessage(),error);
  140.             }
  141.         }
  142.     }
  143.    
  144.     private void processConnectionTimeoutException(int timeout, boolean configurazioneGlobale, Exception e, String message) {
  145.         try {
  146.             if(timeout>0 && ConnettoreBaseWithResponse.containsConnectionTimeoutException(e, message)) {
  147.                 TimeoutNotifier notifier = getTimeoutNotifier(timeout, configurazioneGlobale, TimeoutNotifierType.CONNECTION);
  148.                 notifier.notify(timeout);
  149.             }
  150.         }catch(Exception error) {
  151.             if(this.log!=null) {
  152.                 this.log.error("Errore avvenuto durante la registrazione dell'evento di connection timeout: "+error.getMessage(),error);
  153.             }
  154.         }
  155.     }
  156.    
  157.     private TimeoutNotifier getTimeoutNotifier(int timeout, boolean configurazioneGlobale, TimeoutNotifierType type) throws DriverConfigurazioneException, ProtocolException {
  158.         SogliaReadTimeout soglia = null;
  159.         if(!this.portaDelegata) {
  160.             soglia = (this.pa!=null) ?
  161.                     ReadTimeoutConfigurationUtils.buildSogliaResponseTimeout(timeout, configurazioneGlobale, this.pa, null, this.policyTimeoutConfig,
  162.                             new ReadTimeoutContextParam(this.requestInfo, this.protocolFactory, this.context, this.state)) :
  163.                         ReadTimeoutConfigurationUtils.buildSogliaResponseTimeout(timeout, false, this.protocolFactory);
  164.         }
  165.         else {
  166.             soglia = (this.pd!=null) ?
  167.                     ReadTimeoutConfigurationUtils.buildSogliaResponseTimeout(timeout, configurazioneGlobale, this.pd, this.policyTimeoutConfig,
  168.                             new ReadTimeoutContextParam(this.requestInfo, this.protocolFactory, this.context, this.state)) :
  169.                         ReadTimeoutConfigurationUtils.buildSogliaResponseTimeout(timeout, true, this.protocolFactory);
  170.         }
  171.         boolean saveInContext = !(this.policyTimeoutConfig!=null &&
  172.                                     (this.policyTimeoutConfig.getAttributeAuthority()!=null || this.policyTimeoutConfig.getAttributeAuthorityResponseJwt()!=null)
  173.                                 );
  174.         return new TimeoutNotifier(this.context, this.protocolFactory,
  175.                 soglia, type, this.log, saveInContext);
  176.     }
  177. }