Skip to content

Commit

Permalink
Apply CR changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Or-Geva committed Nov 30, 2023
1 parent daf8f3e commit fcd2bd6
Show file tree
Hide file tree
Showing 11 changed files with 270 additions and 184 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import org.jfrog.build.extractor.clientConfiguration.PatternMatcher;
import org.jfrog.build.extractor.clientConfiguration.util.encryption.EncryptionKeyPair;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -28,6 +31,9 @@
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
Expand All @@ -40,7 +46,7 @@
import static org.apache.commons.lang3.StringUtils.removeEnd;
import static org.jfrog.build.extractor.UrlUtils.encodeUrl;
import static org.jfrog.build.extractor.UrlUtils.encodeUrlPathPart;
import static org.jfrog.build.extractor.clientConfiguration.ArtifactoryClientConfiguration.decryptPropertiesFromFile;
import static org.jfrog.build.extractor.clientConfiguration.util.encryption.PropertyEncryptor.decryptPropertiesFromFile;

/**
* @author Noam Y. Tenne
Expand Down Expand Up @@ -113,7 +119,8 @@ private static Properties searchAdditionalPropertiesFile(Properties existingProp
props.load(inputStream);
}
}
} catch (IOException e) {
} catch (IOException | InvalidAlgorithmParameterException | IllegalBlockSizeException | NoSuchPaddingException |
BadPaddingException | NoSuchAlgorithmException | InvalidKeyException e) {
throw new RuntimeException("Unable to load build info properties from file: " + propertiesFile.getAbsolutePath(), e);
}
return props;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ImmutableMap;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.jfrog.build.api.util.CommonUtils;
import org.jfrog.build.api.util.Log;
Expand All @@ -12,12 +11,18 @@
import org.jfrog.build.extractor.clientConfiguration.util.IssuesTrackerUtils;
import org.jfrog.build.extractor.clientConfiguration.util.encryption.EncryptionKeyPair;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
Expand Down Expand Up @@ -138,8 +143,7 @@
import static org.jfrog.build.extractor.clientConfiguration.ClientProperties.PROP_RESOLVE_PREFIX;
import static org.jfrog.build.extractor.clientConfiguration.ClientProperties.PROP_SO_TIMEOUT;
import static org.jfrog.build.extractor.clientConfiguration.ClientProperties.PROP_TIMEOUT;
import static org.jfrog.build.extractor.clientConfiguration.util.encryption.SecurePropertiesEncryption.decryptProperties;
import static org.jfrog.build.extractor.clientConfiguration.util.encryption.SecurePropertiesEncryption.encryptedPropertiesToFile;
import static org.jfrog.build.extractor.clientConfiguration.util.encryption.PropertyEncryptor.encryptedPropertiesToStream;

/**
* @author freds
Expand Down Expand Up @@ -230,8 +234,6 @@ public Log getLog() {
return root.getLog();
}



public void persistToPropertiesFile() {
if (StringUtils.isEmpty(getPropertiesFile())) {
return;
Expand All @@ -243,22 +245,12 @@ public void persistToPropertiesFile() {
}
}

/**
* Decrypts properties from a file using the provided secret key.
*
* @param filePath The path to the file containing encrypted properties.
* @param keyPair The secret key and iv used for decryption.
* @return A Properties object containing the decrypted properties.
*/
public static Properties decryptPropertiesFromFile(String filePath, EncryptionKeyPair keyPair) throws IOException {
return decryptProperties(FileUtils.readFileToByteArray(new File(filePath)), keyPair);
}

public EncryptionKeyPair persistToEncryptedPropertiesFile(OutputStream os) throws IOException {
public EncryptionKeyPair persistToEncryptedPropertiesFile(OutputStream os) throws IOException, InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException {
if (StringUtils.isEmpty(getPropertiesFile())) {
return null;
}
return encryptedPropertiesToFile(os, preparePropertiesToPersist());
return encryptedPropertiesToStream(os, preparePropertiesToPersist());
}

private Properties preparePropertiesToPersist() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@
*/
public class EncryptionKeyPair {
private static final int AES_256_KEY_LENGTH = 256;
private static final int IV_LENGTH = 126;
private static final int IV_LENGTH = 128;
private byte[] secretKey;
private byte[] Iv;
private byte[] iv;

public EncryptionKeyPair() {
this.secretKey = generateRandomKey(AES_256_KEY_LENGTH);
this.Iv = generateRandomKey(IV_LENGTH);
this.iv = generateRandomKey(IV_LENGTH);
}

public EncryptionKeyPair(String secretKey, String Iv) {
if (StringUtils.isNotBlank(secretKey)) {
this.secretKey = Base64.getDecoder().decode(secretKey);
}
if (StringUtils.isNotBlank(Iv)) {
this.Iv = Base64.getDecoder().decode(Iv);
this.iv = Base64.getDecoder().decode(Iv);
}
}

Expand All @@ -45,19 +45,21 @@ public byte[] getSecretKey() {
return secretKey;
}

@SuppressWarnings("unused")
public String getStringSecretKey() {
return Base64.getEncoder().encodeToString(secretKey);
}

public byte[] getIv() {
return Iv;
return iv;
}

@SuppressWarnings("unused")
public String getStringIv() {
return Base64.getEncoder().encodeToString(Iv);
return Base64.getEncoder().encodeToString(iv);
}

public boolean isEmpty() {
return secretKey == null || secretKey.length == 0 || Iv == null || Iv.length == 0;
return secretKey == null || secretKey.length == 0 || iv == null || iv.length == 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.jfrog.build.extractor.clientConfiguration.util.encryption;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class Encryptor {
private static final String ALGORITHM = "AES";
private static final String TRANSFORMATION = "AES/GCM/NoPadding";
private static final int GCM_TAG_LENGTH = 128;

/**
* Decrypts the given data using the provided EncryptionKeyPair.
*
* @param data The encrypted data to be decrypted
* @param keyPair The EncryptionKeyPair containing the secret key and IV for decryption
* @return The decrypted data as a byte array
*/
public static byte[] decrypt(byte[] data, EncryptionKeyPair keyPair) throws IllegalBlockSizeException, BadPaddingException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException {
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
SecretKeySpec secretKeySpec = new SecretKeySpec(keyPair.getSecretKey(), ALGORITHM);
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(GCM_TAG_LENGTH, keyPair.getIv());
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, gcmParameterSpec);
return cipher.doFinal(data);
}

/**
* Encrypts the given data using the provided EncryptionKeyPair.
*
* @param data The data to be encrypted
* @param keyPair The EncryptionKeyPair containing the secret key and IV for encryption
* @return The encrypted data as a byte array
*/
public static byte[] encrypt(byte[] data, EncryptionKeyPair keyPair) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
SecretKeySpec secretKeySpec = new SecretKeySpec(keyPair.getSecretKey(), ALGORITHM);
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(GCM_TAG_LENGTH, keyPair.getIv());
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, gcmParameterSpec);
return cipher.doFinal(data);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package org.jfrog.build.extractor.clientConfiguration.util.encryption;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Properties;

import static org.jfrog.build.extractor.clientConfiguration.util.encryption.Encryptor.decrypt;
import static org.jfrog.build.extractor.clientConfiguration.util.encryption.Encryptor.encrypt;

public class PropertyEncryptor {

/**
* Decrypts properties from a file using the provided secret key.
*
* @param filePath The path to the file containing encrypted properties.
* @param keyPair The secret key and iv used for decryption.
* @return A Properties object containing the decrypted properties.
*/
public static Properties decryptPropertiesFromFile(String filePath, EncryptionKeyPair keyPair) throws IOException, InvalidAlgorithmParameterException, IllegalBlockSizeException, NoSuchPaddingException, BadPaddingException, NoSuchAlgorithmException, InvalidKeyException {
if (StringUtils.isBlank(filePath)) {
return null;
}
if (!new File(filePath).exists()) {
throw new IOException("File " + filePath + " does not exist");
}
return decryptProperties(FileUtils.readFileToByteArray(new File(filePath)), keyPair);
}

/**
* Decrypts properties from an encrypted byte array using the provided secret key.
*
* @param encryptedData The encrypted byte array representing properties.
* @param keyPair The secret key and iv used for decryption.
* @return A Properties object containing the decrypted properties.
*/
private static Properties decryptProperties(byte[] encryptedData, EncryptionKeyPair keyPair) throws IOException, InvalidAlgorithmParameterException, IllegalBlockSizeException, NoSuchPaddingException, BadPaddingException, NoSuchAlgorithmException, InvalidKeyException {
byte[] decryptedBytes = decrypt(encryptedData, keyPair);
String decryptedString = new String(decryptedBytes, StandardCharsets.UTF_8);
return stringToProperties(decryptedString);
}

/**
* Encrypts properties to a file represented as a byte array and returns the secret key used for encryption.
*
* @param os The output stream where the encrypted properties will be written.
* @param properties The Properties object containing the properties to be encrypted.
* @return A byte array representing the secret key used for encryption.
*/
public static EncryptionKeyPair encryptedPropertiesToStream(OutputStream os, Properties properties) throws IOException, InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException {
EncryptionKeyPair keyPair = new EncryptionKeyPair();
os.write(encrypt(propertiesToString(properties).getBytes(StandardCharsets.UTF_8), keyPair));
return keyPair;
}

private static Properties stringToProperties(String propertiesString) throws IOException {
Properties properties = new Properties();
try (InputStream inputStream = new ByteArrayInputStream(propertiesString.getBytes(StandardCharsets.UTF_8))) {
properties.load(inputStream);
}
return properties;
}

private static String propertiesToString(Properties properties) {
StringBuilder stringBuilder = new StringBuilder();
for (String key : properties.stringPropertyNames()) {
stringBuilder.append(key).append("=").append(properties.getProperty(key)).append("\n");
}
return stringBuilder.toString();
}
}

This file was deleted.

Loading

0 comments on commit fcd2bd6

Please sign in to comment.