|
| 1 | +package utilities.pgpBatchUpload; |
| 2 | + |
| 3 | +import java.io.BufferedInputStream; |
| 4 | +import java.io.File; |
| 5 | +import java.io.FileInputStream; |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.InputStream; |
| 8 | +import java.nio.file.Files; |
| 9 | +import java.nio.file.Path; |
| 10 | +import java.nio.file.Paths; |
| 11 | +import java.security.PrivateKey; |
| 12 | +import java.security.cert.CertificateException; |
| 13 | +import java.security.cert.CertificateFactory; |
| 14 | +import java.security.cert.X509Certificate; |
| 15 | +import java.util.Iterator; |
| 16 | + |
| 17 | +import org.apache.commons.lang3.StringUtils; |
| 18 | +import org.apache.logging.log4j.LogManager; |
| 19 | +import org.apache.logging.log4j.Logger; |
| 20 | +import org.bouncycastle.openpgp.PGPException; |
| 21 | +import org.bouncycastle.openpgp.PGPPublicKey; |
| 22 | +import org.bouncycastle.openpgp.PGPPublicKeyRing; |
| 23 | +import org.bouncycastle.openpgp.PGPPublicKeyRingCollection; |
| 24 | +import org.bouncycastle.openpgp.PGPUtil; |
| 25 | + |
| 26 | +import com.cybersource.authsdk.util.GlobalLabelParameters; |
| 27 | + |
| 28 | +/** |
| 29 | + * Utility class for batch upload operations, including certificate and PGP key loading, |
| 30 | + * endpoint URL construction, and input validation for batch API operations. |
| 31 | + */ |
| 32 | +public class BatchUploadUtility { |
| 33 | + |
| 34 | + private static final Logger logger = LogManager.getLogger(BatchUploadUtility.class); |
| 35 | + private static final long MAX_FILE_SIZE_BYTES = 75 * 1024 * 1024; |
| 36 | + |
| 37 | + /** |
| 38 | + * Loads an X509 certificate from a PEM file. |
| 39 | + * |
| 40 | + * @param certFilePath The file path to the PEM certificate file. |
| 41 | + * @return The loaded X509Certificate object. |
| 42 | + * @throws CertificateException If the certificate cannot be parsed or is invalid. |
| 43 | + * @throws IOException If the file cannot be read or does not exist. |
| 44 | + */ |
| 45 | + public static X509Certificate loadCertificateFromPemFile(String certFilePath) throws CertificateException, IOException { |
| 46 | + try (FileInputStream inStream = new FileInputStream(certFilePath)) { |
| 47 | + CertificateFactory cf = CertificateFactory.getInstance("X.509"); |
| 48 | + return (X509Certificate) cf.generateCertificate(inStream); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Reads a PGP public key from the specified file. |
| 54 | + * |
| 55 | + * @param filePath The file path to the PGP public key. |
| 56 | + * @return The first encryption-capable PGPPublicKey found in the file. |
| 57 | + * @throws IOException If an I/O error occurs. |
| 58 | + * @throws PGPException If a PGP error occurs or no encryption key is found. |
| 59 | + * @throws IllegalArgumentException If the file path is null or empty. |
| 60 | + */ |
| 61 | + public static PGPPublicKey readPGPPublicKey(String filePath) throws IOException, PGPException { |
| 62 | + validatePathAndFile(filePath, "pgp public key path"); |
| 63 | + logger.debug("Reading pgp public key from file: {}", filePath); |
| 64 | + try (InputStream keyIn = new BufferedInputStream(new FileInputStream(filePath))) { |
| 65 | + PGPPublicKeyRingCollection pgpPub = new PGPPublicKeyRingCollection(PGPUtil.getDecoderStream(keyIn), |
| 66 | + new org.bouncycastle.openpgp.operator.jcajce.JcaKeyFingerprintCalculator()); |
| 67 | + |
| 68 | + Iterator<PGPPublicKeyRing> keyRingIter = pgpPub.getKeyRings(); |
| 69 | + while (keyRingIter.hasNext()) { |
| 70 | + PGPPublicKeyRing keyRing = keyRingIter.next(); |
| 71 | + Iterator<PGPPublicKey> keyIter = keyRing.getPublicKeys(); |
| 72 | + while (keyIter.hasNext()) { |
| 73 | + PGPPublicKey key = keyIter.next(); |
| 74 | + if (key.isEncryptionKey()) { |
| 75 | + return key; |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + throw new PGPException("No encryption key found in the provided key ring: " + filePath); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Constructs the full endpoint URL for the given environment hostname and endpoint path. |
| 85 | + * |
| 86 | + * @param environmentHostname The environment hostname (with or without protocol prefix). |
| 87 | + * @param endpoint The endpoint path to append. |
| 88 | + * @return The full endpoint URL. |
| 89 | + */ |
| 90 | + public static String getEndpointUrl(String environmentHostname , String endpoint) { |
| 91 | + String baseUrl; |
| 92 | + if(environmentHostname.startsWith(GlobalLabelParameters.URL_PREFIX)) |
| 93 | + baseUrl=environmentHostname.trim(); |
| 94 | + else |
| 95 | + baseUrl= GlobalLabelParameters.URL_PREFIX + environmentHostname.trim(); |
| 96 | + return baseUrl + endpoint; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Validates the input parameters for batch API using JKS keystore. |
| 101 | + * |
| 102 | + * @param inputFile The input CSV file for batch upload. |
| 103 | + * @param environmentHostname The environment hostname. |
| 104 | + * @param pgpEncryptionCertPath The path to the PGP encryption certificate. |
| 105 | + * @param keystorePath The path to the keystore file. |
| 106 | + * @param truststorePath The path to the truststore file. |
| 107 | + * @throws Exception If any validation fails. |
| 108 | + */ |
| 109 | + public static void validateBatchApiJKSInputs(File inputFile, String environmentHostname, String pgpEncryptionCertPath, String keystorePath, String truststorePath) throws Exception{ |
| 110 | + validateInputFile(inputFile); |
| 111 | + if(StringUtils.isEmpty(environmentHostname)) { |
| 112 | + logger.error("Environment Host Name for Batch Upload API cannot be null or empty."); |
| 113 | + throw new IllegalArgumentException("Environment Host Name for Batch Upload API cannot be null or empty."); |
| 114 | + } |
| 115 | + validatePathAndFile(pgpEncryptionCertPath, "PGP Encryption Cert Path"); |
| 116 | + validatePathAndFile(keystorePath, "Keystore Path"); |
| 117 | + validatePathAndFile(truststorePath, "Truststore Path"); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Validates the input parameters for batch API using P12 client certificate. |
| 122 | + * |
| 123 | + * @param inputFile The input CSV file for batch upload. |
| 124 | + * @param environmentHostname The environment hostname. |
| 125 | + * @param pgpEncryptionCertPath The path to the PGP encryption certificate. |
| 126 | + * @param clientCertP12FilePath The path to the client certificate P12 file. |
| 127 | + * @param serverTrustCertPath The path to the server trust certificate. |
| 128 | + * @throws Exception If any validation fails. |
| 129 | + */ |
| 130 | + public static void validateBatchApiP12Inputs(File inputFile, String environmentHostname, String pgpEncryptionCertPath, String clientCertP12FilePath, String serverTrustCertPath) throws Exception{ |
| 131 | + validateInputFile(inputFile); |
| 132 | + if(StringUtils.isEmpty(environmentHostname)) { |
| 133 | + logger.error("Environment Host Name for Batch Upload API cannot be null or empty."); |
| 134 | + throw new IllegalArgumentException("Environment Host Name for Batch Upload API cannot be null or empty."); |
| 135 | + } |
| 136 | + validatePathAndFile(pgpEncryptionCertPath, "PGP Encryption Cert Path"); |
| 137 | + validatePathAndFile(clientCertP12FilePath, "Client Cert P12 File Path"); |
| 138 | + validatePathAndFile(serverTrustCertPath, "Server Trust Cert Path"); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Validates the input parameters for batch API using direct key and certificate objects. |
| 143 | + * |
| 144 | + * @param inputFile The input CSV file for batch upload. |
| 145 | + * @param environmentHostname The environment hostname. |
| 146 | + * @param pgpPublicKey The PGP public key object. |
| 147 | + * @param clientPrivateKey The client private key. |
| 148 | + * @param clientCert The client X509 certificate. |
| 149 | + * @param serverTrustCert The server trust X509 certificate. |
| 150 | + * @throws Exception If any validation fails. |
| 151 | + */ |
| 152 | + public static void validateBatchApiKeysInputs(File inputFile, String environmentHostname, PGPPublicKey pgpPublicKey, PrivateKey clientPrivateKey, X509Certificate clientCert , X509Certificate serverTrustCert) throws Exception{ |
| 153 | + validateInputFile(inputFile); |
| 154 | + if(StringUtils.isEmpty(environmentHostname)) { |
| 155 | + logger.error("Environment Host Name for Batch Upload API cannot be null or empty."); |
| 156 | + throw new IllegalArgumentException("Environment Host Name for Batch Upload API cannot be null or empty."); |
| 157 | + } |
| 158 | + if (pgpPublicKey == null) throw new IllegalArgumentException("PGP Public Key is null"); |
| 159 | + if (clientPrivateKey == null) throw new IllegalArgumentException("Client Private Key is null"); |
| 160 | + if (clientCert == null) throw new IllegalArgumentException("Client Certificate is null"); |
| 161 | + if (serverTrustCert == null) throw new IllegalArgumentException("Server Trust Certificate is null"); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Validates the input file for batch upload. |
| 166 | + * Checks for existence, file type (CSV), and maximum file size (75MB). |
| 167 | + * |
| 168 | + * @param inputFile The input file to validate. |
| 169 | + * @throws IllegalArgumentException If the file is invalid, not a CSV, or exceeds size limit. |
| 170 | + */ |
| 171 | + private static void validateInputFile(File inputFile) { |
| 172 | + if (inputFile == null || !inputFile.exists() || !inputFile.isFile()) { |
| 173 | + logger.error("Input file is invalid or does not exist: " + (inputFile != null ? inputFile : "null")); |
| 174 | + throw new IllegalArgumentException("Input file is invalid or does not exist: " + inputFile); |
| 175 | + } |
| 176 | + //only csv files are allowed for batch api |
| 177 | + if (!inputFile.getName().toLowerCase().endsWith(".csv")) { |
| 178 | + logger.error("Only CSV file type is allowed: " + inputFile.getName()); |
| 179 | + throw new IllegalArgumentException("Only CSV file type is allowed: " + inputFile.getName()); |
| 180 | + } |
| 181 | + //maximum file size allowed is 75MB |
| 182 | + if (inputFile.length() > MAX_FILE_SIZE_BYTES) { |
| 183 | + logger.error("Input file size exceeds the maximum allowed size of 75MB: " + inputFile.length() + " fileName=" + inputFile.getName()); |
| 184 | + throw new IllegalArgumentException("Input file size exceeds the maximum allowed size of 75MB: " + inputFile.length()); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * Validates that the given file path exists and is not empty. |
| 190 | + * |
| 191 | + * @param path The file path to validate. |
| 192 | + * @param pathType A description of the path type (e.g., "Input file"). |
| 193 | + * @throws IOException If the file does not exist. |
| 194 | + * @throws IllegalArgumentException If the path is null or empty. |
| 195 | + */ |
| 196 | + private static void validatePathAndFile(String filePath, String pathType) throws IOException { |
| 197 | + if (filePath == null || filePath.trim().isEmpty()) { |
| 198 | + logger.error(pathType + " path cannot be null or empty"); |
| 199 | + throw new IllegalArgumentException(pathType + " path cannot be null or empty"); |
| 200 | + } |
| 201 | + |
| 202 | + // Normalize Windows-style paths that start with a slash before the drive letter |
| 203 | + String normalizedPath = filePath; |
| 204 | + if (File.separatorChar == '\\' && normalizedPath.matches("^/[A-Za-z]:.*")) { |
| 205 | + normalizedPath = normalizedPath.substring(1); |
| 206 | + } |
| 207 | + |
| 208 | + Path path = Paths.get(normalizedPath); |
| 209 | + if (!Files.exists(path)) { |
| 210 | + logger.error(pathType + " does not exist: " + path); |
| 211 | + throw new IOException(pathType + " does not exist: " + path); |
| 212 | + } |
| 213 | + if (!Files.isRegularFile(path)) { |
| 214 | + logger.error(pathType + " does not have valid file: " + path); |
| 215 | + throw new IOException(pathType + " does not have valid file: " + path); |
| 216 | + } |
| 217 | + if (!Files.isReadable(path)) { |
| 218 | + logger.error(pathType + " is not readable: " + path); |
| 219 | + throw new IOException(pathType + " is not readable: " + path); |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | +} |
0 commit comments