BYOKStore.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.security.keystore;

  21. import java.io.Serializable;

  22. import org.openspcoop2.security.SecurityException;
  23. import org.openspcoop2.utils.LoggerWrapperFactory;
  24. import org.openspcoop2.utils.UtilsException;
  25. import org.openspcoop2.utils.certificate.byok.BYOKConfig;
  26. import org.openspcoop2.utils.certificate.byok.BYOKInstance;
  27. import org.openspcoop2.utils.certificate.byok.BYOKMode;
  28. import org.openspcoop2.utils.certificate.byok.BYOKRemoteUtils;
  29. import org.openspcoop2.utils.transport.http.HttpResponse;
  30. import org.openspcoop2.utils.transport.http.HttpUtilities;

  31. /**
  32.  * BYOKStore
  33.  *
  34.  * @author Andrea Poli (apoli@link.it)
  35.  * @author $Author$
  36.  * @version $Rev$, $Date$
  37.  */
  38. public class BYOKStore implements Serializable {

  39.     /**
  40.      *
  41.      */
  42.     private static final long serialVersionUID = 1L;
  43.    
  44.     private byte[] storeBytes;
  45.    
  46.     private BYOKConfig config;
  47.     private String key;
  48.    
  49.     @Override
  50.     public String toString() {
  51.         StringBuilder bf = new StringBuilder();
  52.         bf.append("BYOKStore (").append(this.config.getLabel()).append(") ").append(this.key);
  53.         return bf.toString();
  54.     }
  55.    
  56.     public BYOKStore(String key,
  57.             BYOKInstance instance) throws SecurityException{
  58.        
  59.         this.key = key;
  60.        
  61.         try{
  62.             if(instance==null){
  63.                 throw new SecurityException("Instance non fornita");
  64.             }
  65.             this.config = instance.getConfig();
  66.            
  67.             if(instance.getHttpRequest()!=null) {
  68.                 remoteProcess(instance);
  69.             }
  70.             else {
  71.                 BYOKLocalEncrypt localEncrypt = new BYOKLocalEncrypt();
  72.                 if(BYOKMode.WRAP.equals(this.config.getMode())) {
  73.                     this.storeBytes = localEncrypt.wrap(instance.getLocalConfigResolved(), instance.getLocalKey()).getBytes();
  74.                 }
  75.                 else {
  76.                     this.storeBytes = localEncrypt.unwrap(instance.getLocalConfigResolved(), instance.getLocalKey());
  77.                 }
  78.             }

  79.         }catch(Exception e){
  80.             throw new SecurityException(e.getMessage(),e);
  81.         }
  82.        
  83.     }
  84.     private void remoteProcess(BYOKInstance instance) throws UtilsException, SecurityException{
  85.         HttpResponse httpResponse = HttpUtilities.httpInvoke(instance.getHttpRequest());
  86.         if(httpResponse==null || httpResponse.getContent()==null) {
  87.             throw new SecurityException("Store '"+this.config.getLabel()+"' (endpoint:"+instance.getHttpRequest().getUrl()+") unavailable");
  88.         }
  89.         if(httpResponse.getResultHTTPOperation()!=200) {
  90.             throw new SecurityException("Retrieve store '"+this.config.getLabel()+"' (endpoint:"+instance.getHttpRequest().getUrl()+") failed (returnCode:"+httpResponse.getResultHTTPOperation()+")");
  91.         }
  92.         this.storeBytes = httpResponse.getContent();
  93.        
  94.         if(this.storeBytes!=null && this.storeBytes.length>0) {
  95.             this.storeBytes = BYOKRemoteUtils.normalizeResponse(instance, this.storeBytes, LoggerWrapperFactory.getLogger(BYOKStore.class));
  96.         }
  97.     }
  98.    
  99.     public byte[] getStoreBytes() {
  100.         return this.storeBytes;
  101.     }

  102. }