Skip to content

Commit 561d206

Browse files
authored
Merge property file reading logic (#769)
1 parent 2b9ddae commit 561d206

File tree

3 files changed

+58
-47
lines changed

3 files changed

+58
-47
lines changed

build-info-extractor/src/main/java/org/jfrog/build/extractor/BuildInfoExtractorUtils.java

+1-18
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import com.fasterxml.jackson.databind.ObjectMapper;
99
import com.fasterxml.jackson.databind.SerializationFeature;
1010
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
11-
import org.apache.commons.io.IOUtils;
1211
import org.apache.commons.lang3.StringUtils;
1312
import org.jfrog.build.api.util.CommonUtils;
1413
import org.jfrog.build.api.util.Log;
@@ -180,23 +179,7 @@ public static Properties getEnvProperties(Properties startProps, Log log) {
180179
props.put(varKey, entry.getValue());
181180
}
182181

183-
// TODO: [by FSI] Test if this is needed! Since start props are used now
184-
String propsFilePath = getAdditionalPropertiesFile(startProps, log);
185-
if (StringUtils.isNotBlank(propsFilePath)) {
186-
File propertiesFile = new File(propsFilePath);
187-
InputStream inputStream = null;
188-
try {
189-
inputStream = Files.newInputStream(propertiesFile.toPath());
190-
Properties propertiesFromFile = new Properties();
191-
propertiesFromFile.load(inputStream);
192-
props.putAll(filterDynamicProperties(propertiesFromFile, ENV_PREDICATE));
193-
} catch (IOException e) {
194-
throw new RuntimeException(
195-
"Unable to load build info properties from file: " + propertiesFile.getAbsolutePath(), e);
196-
} finally {
197-
IOUtils.closeQuietly(inputStream);
198-
}
199-
}
182+
props.putAll(filterDynamicProperties(searchAdditionalPropertiesFile(startProps, log), ENV_PREDICATE));
200183
return props;
201184
}
202185

build-info-extractor/src/test/java/org/jfrog/build/extractor/BuildExtractorUtilsTest.java

+56-28
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,8 @@ public void getBuildInfoPropertiesFromSystemProps() {
8484
}
8585

8686
public void getBuildInfoPropertiesFromFile() throws IOException {
87-
Properties props = new Properties();
88-
props.put(POPO_KEY, "buildname");
89-
props.put(MOMO_KEY, "1");
9087
try (FileOutputStream fileOutputStream = new FileOutputStream(tempFile.toFile())) {
91-
props.store(fileOutputStream, "");
88+
createProperties().store(fileOutputStream, "");
9289
}
9390

9491
System.setProperty(BuildInfoConfigProperties.PROP_PROPS_FILE, tempFile.toString());
@@ -103,9 +100,8 @@ public void getBuildInfoPropertiesFromFile() throws IOException {
103100
}
104101

105102
public void getBuildInfoPropertiesFromEncryptedFile() throws IOException, InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException {
106-
EncryptionKeyPair keyPair = setupEncryptedFileTest();
107-
System.setProperty(BuildInfoConfigProperties.PROP_PROPS_FILE_KEY, Base64.getEncoder().encodeToString(keyPair.getSecretKey()));
108-
System.setProperty(BuildInfoConfigProperties.PROP_PROPS_FILE_KEY_IV, Base64.getEncoder().encodeToString(keyPair.getIv()));
103+
setupEncryptedFileTest(createProperties());
104+
109105
Properties fileProps = filterDynamicProperties(
110106
mergePropertiesWithSystemAndPropertyFile(new Properties(), getLog()),
111107
BUILD_INFO_PROP_PREDICATE);
@@ -114,34 +110,35 @@ public void getBuildInfoPropertiesFromEncryptedFile() throws IOException, Invali
114110
assertEquals(fileProps.getProperty(MOMO_KEY), "1", "momo property does not match");
115111
}
116112

117-
public void failToReadEncryptedFileWithNoKey() throws IOException, InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException {
118-
setupEncryptedFileTest();
113+
public void failToReadEncryptedFileWithNoKey() throws InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, IOException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException {
114+
// Create encrypted file with properties
115+
setupEncryptedFileTest(createProperties());
116+
// Remove key
117+
System.setProperty(BuildInfoConfigProperties.PROP_PROPS_FILE_KEY, "");
118+
// Read properties from the encrypted file
119119
Properties fileProps = filterDynamicProperties(
120120
mergePropertiesWithSystemAndPropertyFile(new Properties(), getLog()),
121121
BUILD_INFO_PROP_PREDICATE);
122+
// Check if no properties are read
122123
assertEquals(fileProps.size(), 0, "0 properties should be present, the file is encrypted, and the key is not available");
123124
}
124125

125-
private EncryptionKeyPair setupEncryptedFileTest() throws IOException, InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException {
126-
Properties props = new Properties();
127-
props.put(POPO_KEY, "buildname");
128-
props.put(MOMO_KEY, "1");
126+
private void setupEncryptedFileTest(Properties props) throws IOException, InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException {
129127
props.put(BuildInfoConfigProperties.PROP_PROPS_FILE, tempFile.toString());
130128
System.setProperty(BuildInfoConfigProperties.PROP_PROPS_FILE, tempFile.toString());
131129
ArtifactoryClientConfiguration client = new ArtifactoryClientConfiguration(new NullLog());
132130
client.fillFromProperties(props);
133131

134132
try (FileOutputStream fileOutputStream = new FileOutputStream(tempFile.toFile())) {
135-
return client.persistToEncryptedPropertiesFile(fileOutputStream);
133+
EncryptionKeyPair keyPair = client.persistToEncryptedPropertiesFile(fileOutputStream);
134+
System.setProperty(BuildInfoConfigProperties.PROP_PROPS_FILE_KEY, Base64.getEncoder().encodeToString(keyPair.getSecretKey()));
135+
System.setProperty(BuildInfoConfigProperties.PROP_PROPS_FILE_KEY_IV, Base64.getEncoder().encodeToString(keyPair.getIv()));
136136
}
137137
}
138138

139139
public void getBuildInfoProperties() throws IOException {
140-
Properties props = new Properties();
141-
props.put(POPO_KEY, "buildname");
142-
props.put(MOMO_KEY, "1");
143140
try (FileOutputStream fileOutputStream = new FileOutputStream(tempFile.toFile())) {
144-
props.store(fileOutputStream, "");
141+
createProperties().store(fileOutputStream, "");
145142
}
146143
System.setProperty(BuildInfoConfigProperties.PROP_PROPS_FILE, tempFile.toString());
147144

@@ -166,11 +163,8 @@ public void getBuildInfoProperties() throws IOException {
166163
}
167164

168165
public void getEnvPropertiesFromFile() throws IOException {
169-
Properties props = new Properties();
170-
props.put(ENV_POPO_KEY, "buildname");
171-
props.put(ENV_MOMO_KEY, "1");
172166
try (FileOutputStream fileOutputStream = new FileOutputStream(tempFile.toFile())) {
173-
props.store(fileOutputStream, "");
167+
createPropertiesEnvs().store(fileOutputStream, "");
174168
}
175169
System.setProperty(BuildInfoConfigProperties.PROP_PROPS_FILE, tempFile.toString());
176170

@@ -180,11 +174,8 @@ public void getEnvPropertiesFromFile() throws IOException {
180174
}
181175

182176
public void getEnvAndSysPropertiesFromFile() throws IOException {
183-
Properties props = new Properties();
184-
props.put(ENV_POPO_KEY, "buildname");
185-
props.put(ENV_MOMO_KEY, "1");
186177
try (FileOutputStream fileOutputStream = new FileOutputStream(tempFile.toFile())) {
187-
props.store(fileOutputStream, "");
178+
createPropertiesEnvs().store(fileOutputStream, "");
188179
}
189180
System.setProperty(BuildInfoConfigProperties.PROP_PROPS_FILE, tempFile.toString());
190181

@@ -204,6 +195,29 @@ public void getEnvAndSysPropertiesFromFile() throws IOException {
204195
System.clearProperty(gogoKey);
205196
}
206197

198+
public void getEnvAndSysPropertiesFromEncryptedFile() throws IOException, InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException {
199+
// Put system properties
200+
String kokoKey = "koko";
201+
String gogoKey = "gogo";
202+
System.setProperty(kokoKey, "parent");
203+
System.setProperty(gogoKey, "2");
204+
205+
// Encrypt properties and write to the file
206+
setupEncryptedFileTest(createPropertiesEnvs());
207+
208+
// Read properties from the encrypted file
209+
Properties buildInfoProperties = getEnvProperties(new Properties(), new NullLog());
210+
211+
// Check if decrypted properties are as expected
212+
assertEquals(buildInfoProperties.getProperty(ENV_POPO_KEY), "buildname", "popo property does not match");
213+
assertEquals(buildInfoProperties.getProperty(ENV_MOMO_KEY), "1", "momo number property does not match");
214+
assertEquals(buildInfoProperties.getProperty("koko"), "parent", "koko parent name property does not match");
215+
assertEquals(buildInfoProperties.getProperty("gogo"), "2", "gogo parent number property does not match");
216+
217+
System.clearProperty(kokoKey);
218+
System.clearProperty(gogoKey);
219+
}
220+
207221
public void testExcludePatterns() {
208222
// Put system properties
209223
String kokoKey = "koko";
@@ -215,7 +229,7 @@ public void testExcludePatterns() {
215229

216230
Properties startProps = new Properties();
217231
startProps.put(BuildInfoConfigProperties.PROP_ENV_VARS_EXCLUDE_PATTERNS, "*koko");
218-
Properties buildInfoProperties = getEnvProperties(startProps, null);
232+
Properties buildInfoProperties = getEnvProperties(startProps, new NullLog());
219233
assertNull(buildInfoProperties.getProperty("koko"), "Should not find koko property due to exclude patterns");
220234
assertNull(buildInfoProperties.getProperty("akoko"), "Should not find akoko property due to exclude patterns");
221235
assertEquals(buildInfoProperties.getProperty("gogo"), "2", "gogo parent number property does not match");
@@ -235,7 +249,7 @@ public void testIncludePatterns() {
235249

236250
Properties startProps = new Properties();
237251
startProps.put(BuildInfoConfigProperties.PROP_ENV_VARS_INCLUDE_PATTERNS, "gogo?*");
238-
Properties buildInfoProperties = getEnvProperties(startProps, null);
252+
Properties buildInfoProperties = getEnvProperties(startProps, new NullLog());
239253
assertEquals(buildInfoProperties.getProperty("gogo1"), "1", "gogo1 parent number property does not match");
240254
assertEquals(buildInfoProperties.getProperty("gogo2a"), "2", "gogo2a parent number property does not match");
241255
assertNull(buildInfoProperties.getProperty("koko"), "Should not find koko property due to include patterns");
@@ -311,4 +325,18 @@ public void testCreateBuildInfoUrl(String url, String buildName, String buildNum
311325
boolean encode, boolean platformUrl, String exceptedUrl) {
312326
assertEquals(createBuildInfoUrl(url, buildName, buildNumber, timeStamp, project, encode, platformUrl), exceptedUrl);
313327
}
328+
329+
private Properties createProperties() {
330+
Properties props = new Properties();
331+
props.put(POPO_KEY, "buildname");
332+
props.put(MOMO_KEY, "1");
333+
return props;
334+
}
335+
336+
private Properties createPropertiesEnvs() {
337+
Properties props = new Properties();
338+
props.put(ENV_POPO_KEY, "buildname");
339+
props.put(ENV_MOMO_KEY, "1");
340+
return props;
341+
}
314342
}

build-info-extractor/src/test/java/org/jfrog/build/extractor/packageManager/PackageManagerUtilsTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public void testContainsSuspectedSecrets() {
138138
}
139139

140140
private BuildInfo createBuildInfo(Properties props) {
141-
Properties buildInfoProperties = getEnvProperties(props, null);
141+
Properties buildInfoProperties = getEnvProperties(props, new NullLog());
142142
Properties moduleProps = new Properties();
143143
moduleProps.setProperty(key1, value1);
144144
moduleProps.setProperty("dummy-prefix" + key1, value1);

0 commit comments

Comments
 (0)