VaultUpdateConfig.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.openspcoop2.core.commons.CoreException;
  23. import org.openspcoop2.utils.certificate.byok.BYOKManager;

  24. /**
  25. * VaultUpdateConfig
  26. *
  27. * @author Poli Andrea (apoli@link.it)
  28. * @author $Author$
  29. * @version $Rev$, $Date$
  30. */
  31. public class VaultUpdateConfig {

  32.     public static final String SECURITY_IN="-sec_in";
  33.     public static final String SECURITY_OUT="-sec_out";
  34.    
  35.     /** NO! I secreti interni a GovWay sono protetti con security public static final String KMS_IN="-kms_in";
  36.     public static final String KMS_OUT="-kms_out";*/
  37.    
  38.     public static final String PLAIN_IN="-plain_in";
  39.     public static final String PLAIN_OUT="-plain_out";
  40.    
  41.     public static final String REPORT="-report";
  42.    
  43.     private static final String UNKNOW_OPTION = "(unknown option '";
  44.    
  45.     public static String getUsage() {
  46.         return SECURITY_IN+"|"+
  47.                 /**KMS_IN+"|"+*/
  48.                 PLAIN_IN+
  49.                 "[=id] "+
  50.                 SECURITY_OUT+"|"+
  51.                 /**KMS_OUT+"|"+*/
  52.                 PLAIN_OUT+")[=id] "+
  53.                 "["+REPORT+"=path]";
  54.     }
  55.    
  56.     private boolean inSecurityMode = false;
  57.     /**private boolean inKmsMode = false;*/
  58.     private boolean inPlainMode = false;
  59.     private String inId = null;
  60.    
  61.     private boolean outSecurityMode = false;
  62.     /**private boolean outKmsMode = false;*/
  63.     private boolean outPlainMode = false;
  64.     private String outId = null;
  65.    
  66.     private String reportPath = null;
  67.    
  68.     public VaultUpdateConfig(String[] args, String utilizzoErrato) throws CoreException{
  69.         if(args.length<2 || args[0]==null || args[1]==null) {
  70.             throw new CoreException(utilizzoErrato);
  71.         }
  72.            
  73.         parseFirstArgument(args, utilizzoErrato);
  74.         parseSecondArgument(args, utilizzoErrato);
  75.        
  76.         if(args.length>2 && args[2]!=null) {
  77.             parseThirdArgument(args, utilizzoErrato);
  78.         }
  79.     }
  80.    
  81.     private void parseFirstArgument(String[] args, String utilizzoErrato) throws CoreException {
  82.         if(!(args[0].contains("="))) {
  83.             if(PLAIN_IN.equals(args[0])) {
  84.                 this.inPlainMode=true;
  85.             }
  86.             else {
  87.                 throw new CoreException("(= not found in first param '"+args[0]+"') "+utilizzoErrato);
  88.             }
  89.         }
  90.         else {
  91.             if(args[0].startsWith(SECURITY_IN+"=") && args[0].length()>(SECURITY_IN+"=").length()) {
  92.                 this.inSecurityMode=true;
  93.                 this.inId = args[0].substring((SECURITY_IN+"=").length());
  94.             }
  95.             /**else if(args[0].startsWith(KMS_IN+"=") && args[0].length()>(KMS_IN+"=").length()) {
  96.                 this.inKmsMode=true;
  97.                 this.inId = args[0].substring((KMS_IN+"=").length());
  98.             }*/
  99.             else {
  100.                 throw new CoreException(UNKNOW_OPTION+args[0]+"') "+utilizzoErrato);
  101.             }
  102.         }
  103.     }
  104.    
  105.     private void parseSecondArgument(String[] args, String utilizzoErrato) throws CoreException {
  106.         if(!(args[1].contains("="))) {
  107.             if(PLAIN_OUT.equals(args[1])) {
  108.                 this.outPlainMode=true;
  109.             }
  110.             else {
  111.                 throw new CoreException("(= not found in second param '"+args[1]+"') "+utilizzoErrato);
  112.             }
  113.         }
  114.         else {
  115.             if(args[1].startsWith(SECURITY_OUT+"=") && args[1].length()>(SECURITY_OUT+"=").length()) {
  116.                 this.outSecurityMode=true;
  117.                 this.outId = args[1].substring((SECURITY_OUT+"=").length());
  118.             }
  119.             /**else if(args[1].startsWith(KMS_OUT+"=") && args[1].length()>(KMS_OUT+"=").length()) {
  120.                 this.outKmsMode=true;
  121.                 this.outId = args[1].substring((KMS_OUT+"=").length());
  122.             }*/
  123.             else {
  124.                 throw new CoreException(UNKNOW_OPTION+args[1]+"') "+utilizzoErrato);
  125.             }
  126.         }
  127.     }
  128.    
  129.     private void parseThirdArgument(String[] args, String utilizzoErrato) throws CoreException {
  130.         if(!(args[2].contains("="))) {
  131.             throw new CoreException("(= not found in third param '"+args[2]+"') "+utilizzoErrato);
  132.         }
  133.         else {
  134.             if(args[2].startsWith(REPORT+"=") && args[2].length()>(REPORT+"=").length()) {
  135.                 this.reportPath = args[2].substring((REPORT+"=").length());
  136.             }
  137.             else {
  138.                 throw new CoreException(UNKNOW_OPTION+args[2]+"') "+utilizzoErrato);
  139.             }
  140.            
  141.             File fFilePath = new File(this.reportPath);
  142.             String prefix = "(File '"+fFilePath.getAbsolutePath()+"' ";
  143.             if(fFilePath.exists()) {
  144.                 throw new CoreException(prefix+"already exists) "+utilizzoErrato);
  145.             }
  146.         }
  147.     }
  148.    
  149.     public boolean isInSecurityMode() {
  150.         return this.inSecurityMode;
  151.     }

  152.     /**public boolean isInKmsMode() {
  153.         return this.inKmsMode;
  154.     }*/

  155.     public boolean isInPlainMode() {
  156.         return this.inPlainMode;
  157.     }

  158.     public String getInId() {
  159.         return this.inId;
  160.     }

  161.     public boolean isOutSecurityMode() {
  162.         return this.outSecurityMode;
  163.     }

  164.     /**public boolean isOutKmsMode() {
  165.         return this.outKmsMode;
  166.     }*/

  167.     public boolean isOutPlainMode() {
  168.         return this.outPlainMode;
  169.     }

  170.     public String getOutId() {
  171.         return this.outId;
  172.     }

  173.     public String getReportPath() {
  174.         return this.reportPath;
  175.     }
  176.    
  177.     public void validate(BYOKManager byokManager) throws CoreException {
  178.        
  179.         /**String kmsPrefix = "Kms '";*/
  180.        
  181.         if(this.inSecurityMode && !byokManager.existsSecurityEngineByType(this.inId)){
  182.             throw new CoreException("Security policy '"+this.inId+"' not found");
  183.         }
  184.         /**if(this.inKmsMode && !byokManager.existsKMSConfigByType(this.inId)) {
  185.             throw new CoreException(kmsPrefix+this.inId+"' not exists");
  186.         }
  187.         else if(this.inKmsMode && !byokManager.isKMSUsedInSecurityUnwrapConfig(this.inId, new StringBuilder())) {
  188.             throw new CoreException(kmsPrefix+this.inId+"' unusable for unwrap operation");
  189.         }*/
  190.        
  191.         if(this.outSecurityMode && !byokManager.existsSecurityEngineByType(this.outId)){
  192.             throw new CoreException("Security policy '"+this.outId+"' not found");
  193.         }
  194.         /**if(this.outKmsMode && !byokManager.existsKMSConfigByType(this.outId)) {
  195.             throw new CoreException(kmsPrefix+this.outId+"' not exists");
  196.         }
  197.         else if(this.outKmsMode && !byokManager.isKMSUsedInSecurityWrapConfig(this.outId, new StringBuilder())) {
  198.             throw new CoreException(kmsPrefix+this.outId+"' unusable for wrap operation");
  199.         }*/
  200.     }
  201. }