VaultEncDecConfig.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.vault.cli;

  21. import java.io.File;

  22. import org.apache.commons.lang.StringUtils;
  23. import org.openspcoop2.core.commons.CoreException;
  24. import org.openspcoop2.utils.certificate.byok.BYOKManager;

  25. /**
  26. * VaultEncDecConfig
  27. *
  28. * @author Poli Andrea (apoli@link.it)
  29. * @author $Author$
  30. * @version $Rev$, $Date$
  31. */
  32. public class VaultEncDecConfig {
  33.    
  34.     public static final String SYSTEM_IN="-system_in";
  35.     public static final String SYSTEM_OUT="-system_out";
  36.    
  37.     public static final String FILE_IN="-file_in";
  38.     public static final String FILE_OUT="-file_out";
  39.    
  40.     public static final String SECURITY="-sec";
  41.     public static final String KMS="-kms";
  42.     public static final String KSM_DEPRECATED="-ksm";
  43.        
  44.     public static String getUsage() {
  45.         return SYSTEM_IN+"|"+FILE_IN+"=text|path "+SYSTEM_OUT+"|"+FILE_OUT+"=path ["+SECURITY+"|"+KMS+"=id]";
  46.     }
  47.    
  48.     private static final String UNKNOW_OPTION = "(unknown option '";
  49.    
  50.     private boolean encodingMode = false;
  51.    
  52.     private boolean inSystemMode = false;
  53.     private String inText = null;
  54.     private boolean inFileMode = false;
  55.     private String inFilePath = null;
  56.        
  57.     private boolean outSystemMode = false;
  58.     private boolean outFileMode = false;
  59.     private String outFilePath = null;
  60.    
  61.     private boolean securityMode = true; // default
  62.     private boolean kmsMode = false;
  63.     private String id= null;
  64.    
  65.     public VaultEncDecConfig(String[] args, String utilizzoErrato, boolean encodingMode) throws CoreException{
  66.         if(args.length<2 || args[0]==null || args[1]==null) {
  67.             throw new CoreException(utilizzoErrato);
  68.         }
  69.            
  70.         this.encodingMode = encodingMode;
  71.        
  72.         parseFirstArgument(args, utilizzoErrato);
  73.         parseSecondArgument(args, utilizzoErrato);
  74.        
  75.         if(args.length>2 && args[2]!=null) {
  76.             parseThirdArgument(args, utilizzoErrato);
  77.         }
  78.     }
  79.    
  80.     private void parseFirstArgument(String[] args, String utilizzoErrato) throws CoreException {
  81.         if(!(args[0].contains("="))) {
  82.             throw new CoreException("(= not found in first param '"+args[0]+"') "+utilizzoErrato);
  83.         }
  84.         else {
  85.             if(args[0].startsWith(SYSTEM_IN+"=") && args[0].length()>(SYSTEM_IN+"=").length()) {
  86.                 this.inSystemMode=true;
  87.                 this.inText = args[0].substring((SYSTEM_IN+"=").length());
  88.             }
  89.             else if(args[0].startsWith(FILE_IN+"=") && args[0].length()>(FILE_IN+"=").length()) {
  90.                 parseFirstArgumentFile(args, utilizzoErrato);
  91.             }
  92.             else {
  93.                 throw new CoreException(UNKNOW_OPTION+args[0]+"') "+utilizzoErrato);
  94.             }
  95.         }
  96.     }
  97.     private void parseFirstArgumentFile(String[] args, String utilizzoErrato) throws CoreException {
  98.         this.inFileMode=true;
  99.         this.inFilePath = args[0].substring((FILE_IN+"=").length());
  100.        
  101.         File fFilePath = new File(this.inFilePath);
  102.         String prefix = "(File '"+fFilePath.getAbsolutePath()+"' ";
  103.         if(!fFilePath.exists()) {
  104.             throw new CoreException(prefix+"not exists) "+utilizzoErrato);
  105.         }
  106.         if(!fFilePath.canRead()) {
  107.             throw new CoreException(prefix+"cannot read) "+utilizzoErrato);
  108.         }
  109.         if(!fFilePath.isFile()) {
  110.             throw new CoreException(prefix+"is not a file) "+utilizzoErrato);
  111.         }
  112.     }
  113.    
  114.     private void parseSecondArgument(String[] args, String utilizzoErrato) throws CoreException {
  115.         if(!(args[1].contains("="))) {
  116.             if(SYSTEM_OUT.equals(args[1])) {
  117.                 this.outSystemMode=true;
  118.             }
  119.             else {
  120.                 throw new CoreException("(= not found in second param '"+args[1]+"') "+utilizzoErrato);
  121.             }
  122.         }
  123.         else {
  124.             if(args[1].startsWith(FILE_OUT+"=") && args[1].length()>(FILE_OUT+"=").length()) {
  125.                 this.outFileMode=true;
  126.                 this.outFilePath = args[1].substring((FILE_OUT+"=").length());
  127.                
  128.                 File fFilePath = new File(this.outFilePath);
  129.                 String prefix = "(File '"+fFilePath.getAbsolutePath()+"' ";
  130.                 if(fFilePath.exists()) {
  131.                     throw new CoreException(prefix+"already exists) "+utilizzoErrato);
  132.                 }
  133.             }
  134.             else {
  135.                 throw new CoreException(UNKNOW_OPTION+args[1]+"') "+utilizzoErrato);
  136.             }
  137.         }
  138.     }
  139.    
  140.     private void parseThirdArgument(String[] args, String utilizzoErrato) throws CoreException {
  141.         if(!(args[2].contains("="))) {
  142.             throw new CoreException("(= not found in third param '"+args[2]+"') "+utilizzoErrato);
  143.         }
  144.         else {
  145.             if(args[2].startsWith(SECURITY+"=") && args[2].length()>(SECURITY+"=").length()) {
  146.                 this.securityMode=true;
  147.                 this.id = args[2].substring((SECURITY+"=").length());
  148.             }
  149.             else if(args[2].startsWith(KMS+"=") && args[2].length()>(KMS+"=").length()) {
  150.                 this.securityMode=false;
  151.                 this.kmsMode=true;
  152.                 this.id = args[2].substring((KMS+"=").length());
  153.             }
  154.             else if(args[2].startsWith(KSM_DEPRECATED+"=") && args[2].length()>(KSM_DEPRECATED+"=").length()) {
  155.                 this.securityMode=false;
  156.                 this.kmsMode=true;
  157.                 this.id = args[2].substring((KSM_DEPRECATED+"=").length());
  158.             }
  159.             else {
  160.                 throw new CoreException(UNKNOW_OPTION+args[2]+"') "+utilizzoErrato);
  161.             }
  162.         }
  163.     }
  164.    
  165.     public boolean isEncode() {
  166.         return this.encodingMode;
  167.     }

  168.     public boolean isInSystemMode() {
  169.         return this.inSystemMode;
  170.     }

  171.     public String getInText() {
  172.         return this.inText;
  173.     }

  174.     public boolean isInFileMode() {
  175.         return this.inFileMode;
  176.     }

  177.     public String getInFilePath() {
  178.         return this.inFilePath;
  179.     }

  180.     public boolean isOutSystemMode() {
  181.         return this.outSystemMode;
  182.     }

  183.     public boolean isOutFileMode() {
  184.         return this.outFileMode;
  185.     }

  186.     public String getOutFilePath() {
  187.         return this.outFilePath;
  188.     }  

  189.     public boolean isSecurityMode() {
  190.         return this.securityMode;
  191.     }

  192.     public boolean isKmsMode() {
  193.         return this.kmsMode;
  194.     }

  195.     public String getId() {
  196.         return this.id;
  197.     }
  198.    
  199.     public void validate(BYOKManager byokManager) throws CoreException {
  200.        
  201.         String kmsPrefix = "Kms '";
  202.        
  203.         if(this.securityMode){
  204.             validateSecurityMode(byokManager);
  205.         }
  206.         if(this.kmsMode && !byokManager.existsKMSConfigByType(this.id)) {
  207.             throw new CoreException(kmsPrefix+this.id+"' not exists");
  208.         }
  209.         else if(this.kmsMode && !this.encodingMode && !byokManager.getUnwrapTypes().contains(this.id)) {
  210.             throw new CoreException(kmsPrefix+this.id+"' unusable for unwrap operation");
  211.         }
  212.         else if(this.kmsMode && this.encodingMode && !byokManager.getWrapTypes().contains(this.id)) {
  213.             throw new CoreException(kmsPrefix+this.id+"' unusable for wrap operation");
  214.         }
  215.     }
  216.     public void validateSecurityMode(BYOKManager byokManager) throws CoreException {
  217.         String policy = this.id;
  218.         if(policy==null || StringUtils.isEmpty(policy)) {
  219.             policy = BYOKManager.getSecurityRemoteEngineGovWayPolicy();
  220.         }
  221.         if(policy==null || StringUtils.isEmpty(policy)) {
  222.             policy = BYOKManager.getSecurityEngineGovWayPolicy();
  223.         }
  224.         if(policy==null || StringUtils.isEmpty(policy)) {
  225.             throw new CoreException("Security policy default undefined (BYOK Disabled?)");
  226.         }
  227.         if(!byokManager.existsSecurityEngineByType(policy)) {
  228.             throw new CoreException("Security policy '"+policy+"' not found");
  229.         }
  230.     }
  231. }