SecretDecoder.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.ctrlstat.servlet.utils;

  21. import java.io.ByteArrayOutputStream;
  22. import java.io.IOException;

  23. import javax.servlet.ServletException;
  24. import javax.servlet.ServletOutputStream;
  25. import javax.servlet.http.HttpServlet;
  26. import javax.servlet.http.HttpServletRequest;
  27. import javax.servlet.http.HttpServletResponse;
  28. import javax.servlet.http.HttpSession;

  29. import org.apache.commons.io.IOUtils;
  30. import org.apache.commons.lang.StringUtils;
  31. import org.openspcoop2.core.byok.BYOKUtilities;
  32. import org.openspcoop2.utils.transport.http.HttpConstants;
  33. import org.openspcoop2.utils.transport.http.HttpRequestMethod;
  34. import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
  35. import org.openspcoop2.web.lib.mvc.MessageType;
  36. import org.openspcoop2.web.lib.mvc.PageData;
  37. import org.openspcoop2.web.lib.mvc.ServletUtils;
  38. import org.openspcoop2.web.lib.mvc.byok.LockUtilities;

  39. /**
  40.  * SecretDecoder
  41.  *
  42.  * @author Andrea Poli (apoli@link.it)
  43.  * @author Giuliano Pintori (giuliano.pintori@link.it)
  44.  * @author $Author$
  45.  * @version $Rev$, $Date$
  46.  *
  47.  */
  48. public class SecretDecoder extends HttpServlet {

  49.     private static final long serialVersionUID = 1L;

  50.     @Override
  51.     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  52.         this.processRequest(req, resp);
  53.     }

  54.     @Override
  55.     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

  56.         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  57.         try {
  58.             IOUtils.copy(req.getInputStream(), baos);
  59.         }catch(Exception e){
  60.             ControlStationCore.logError("Errore durante la ricerca delle informazioni oggetto: "+e.getMessage(), e);
  61.             return;
  62.         }          

  63.         this.processRequest(req, resp);
  64.     }

  65.     private void processRequest(HttpServletRequest request, HttpServletResponse response) {
  66.         String risposta = "";
  67.         String messaggioEsito = null;
  68.         String messageType = null;
  69.         response.setContentType(HttpConstants.CONTENT_TYPE_JSON);
  70.        
  71.         try(ByteArrayOutputStream baosPayload = new ByteArrayOutputStream();){
  72.             HttpRequestMethod httpRequestMethod = HttpRequestMethod.valueOf(request.getMethod().toUpperCase());

  73.             if(httpRequestMethod.equals(HttpRequestMethod.POST)) { // copia del payload
  74.                 IOUtils.copy(request.getInputStream(), baosPayload);
  75.             }

  76.             HttpSession session = request.getSession(true);
  77.             PageData pd = new PageData();
  78.             UtilsHelper registroHelper = new UtilsHelper(request, pd, session);
  79.             ControlStationCore core = registroHelper.getCore();
  80.            
  81.             String secretToUnwrap = registroHelper.getParameter(UtilsCostanti.PARAMETRO_SECRET_TO_UNWRAP);
  82.            
  83.             // Viaggia comunque un valore cifrato, quindi il caso della informazione in chiaro non รจ riconoscibile
  84.             if (core.getDriverBYOKUtilities().isEnabledBYOK()) {
  85.                 StringBuilder sb = new StringBuilder();
  86.                 ControlStationCore.logInfo("SecretDecoder: secretToUnwrap: " + secretToUnwrap);
  87.                 String messaggioInformativoInformazioneNonCifrata = core.getNotaInformativaInformazioneMemorizzataInChiaro();
  88.                 if(BYOKUtilities.isWrappedValue(secretToUnwrap)) {
  89.                     if(!core.getDriverBYOKUtilities().isWrappedWithActivePolicy(secretToUnwrap)) {
  90.                         LockUtilities.appendErrorMessageSecurityPolicyDifferente(core.getNotaInformativaInformazioneCifrataSecurityPolicyDifferente(), sb, secretToUnwrap);
  91.                     }
  92.                 }
  93.                 else if(messaggioInformativoInformazioneNonCifrata!=null && StringUtils.isNotEmpty(messaggioInformativoInformazioneNonCifrata)) {
  94.                     sb.append(messaggioInformativoInformazioneNonCifrata);
  95.                 }
  96.                
  97.                 if(sb.length()>0) {
  98.                     sb.append("\n\nValore in chiaro: ");
  99.                 }
  100.                 messaggioEsito = core.getDriverBYOKUtilities().unwrap(secretToUnwrap);
  101.                 messageType = MessageType.INFO.toString();
  102.             }
  103.             else {
  104.                 messaggioEsito = "ERROR: BYOK Unitialized";
  105.                 messageType = MessageType.ERROR.toString();
  106.             }
  107.         }catch(Exception e){
  108.             ControlStationCore.logError("Errore durante la decodifica: "+e.getMessage(), e);
  109.             messaggioEsito = UtilsCostanti.MESSAGGIO_ERRORE_UNWRAP;
  110.             messageType = MessageType.ERROR.toString();
  111.         } finally {
  112.             risposta = ServletUtils.getJson(ServletUtils.getJsonPair(UtilsCostanti.KEY_ESITO, messageType), ServletUtils.getJsonPair(UtilsCostanti.KEY_DETTAGLIO_ESITO, messaggioEsito));
  113.             try {
  114.                 ServletOutputStream outputStream = response.getOutputStream();
  115.                 outputStream.write(risposta.getBytes());
  116.             }catch(Exception eErr){
  117.                 ControlStationCore.logError("Errore durante la serializzazione dell'errore di decodifica: "+eErr.getMessage(), eErr);
  118.             }
  119.         }
  120.     }
  121. }