18
18
import org .jfrog .build .extractor .clientConfiguration .ClientProperties ;
19
19
import org .jfrog .build .extractor .clientConfiguration .IncludeExcludePatterns ;
20
20
import org .jfrog .build .extractor .clientConfiguration .PatternMatcher ;
21
+ import org .jfrog .build .extractor .clientConfiguration .util .encryption .EncryptionKeyPair ;
21
22
23
+ import javax .crypto .BadPaddingException ;
24
+ import javax .crypto .IllegalBlockSizeException ;
25
+ import javax .crypto .NoSuchPaddingException ;
22
26
import java .io .File ;
23
27
import java .io .IOException ;
24
28
import java .io .InputStream ;
27
31
import java .io .StringWriter ;
28
32
import java .nio .charset .StandardCharsets ;
29
33
import java .nio .file .Files ;
34
+ import java .security .InvalidAlgorithmParameterException ;
35
+ import java .security .InvalidKeyException ;
36
+ import java .security .NoSuchAlgorithmException ;
30
37
import java .util .HashMap ;
31
38
import java .util .Map ;
32
39
import java .util .Properties ;
39
46
import static org .apache .commons .lang3 .StringUtils .removeEnd ;
40
47
import static org .jfrog .build .extractor .UrlUtils .encodeUrl ;
41
48
import static org .jfrog .build .extractor .UrlUtils .encodeUrlPathPart ;
49
+ import static org .jfrog .build .extractor .clientConfiguration .util .encryption .PropertyEncryptor .decryptPropertiesFromFile ;
42
50
43
51
/**
44
52
* @author Noam Y. Tenne
@@ -64,33 +72,57 @@ public abstract class BuildInfoExtractorUtils {
64
72
public static final Predicate <Object > MATRIX_PARAM_PREDICATE =
65
73
new PrefixPredicate (ClientProperties .PROP_DEPLOY_PARAM_PROP_PREFIX );
66
74
67
- public static Properties mergePropertiesWithSystemAndPropertyFile (Properties existingProps ) {
68
- return mergePropertiesWithSystemAndPropertyFile (existingProps , null );
75
+ public static Properties mergePropertiesWithSystemAndPropertyFile (Properties existingProps , Log log ) {
76
+ Properties mergedProps = new Properties ();
77
+ mergedProps .putAll (addSystemProperties (existingProps ));
78
+ mergedProps .putAll (searchAdditionalPropertiesFile (mergedProps , log ));
79
+ return mergedProps ;
69
80
}
70
81
71
- public static Properties mergePropertiesWithSystemAndPropertyFile (Properties existingProps , Log log ) {
82
+
83
+ private static Properties addSystemProperties (Properties existingProps ) {
84
+ Properties props = new Properties ();
85
+ props .putAll (existingProps );
86
+ props .putAll (System .getProperties ());
87
+ return props ;
88
+ }
89
+
90
+ /**
91
+ * Retrieves additional properties from a specified build info properties file path.
92
+ *
93
+ * @param existingProps Existing properties object.
94
+ * @param log Logger instance for logging debug information.
95
+ * @return Properties object containing additional properties if found; otherwise, an empty properties object.
96
+ */
97
+ private static Properties searchAdditionalPropertiesFile (Properties existingProps , Log log ) {
72
98
Properties props = new Properties ();
73
- addPropsFromCommandSystemProp (existingProps , log );
74
99
String propsFilePath = getAdditionalPropertiesFile (existingProps , log );
75
- if (StringUtils .isNotBlank (propsFilePath )) {
76
- File propertiesFile = new File (propsFilePath );
77
- InputStream inputStream = null ;
78
- try {
79
- if (propertiesFile .exists ()) {
80
- inputStream = Files .newInputStream (propertiesFile .toPath ());
100
+
101
+ if (StringUtils .isBlank (propsFilePath )) {
102
+ log .debug ("[buildinfo] BuildInfo properties file path is not found." );
103
+ return props ;
104
+ }
105
+
106
+ File propertiesFile = new File (propsFilePath );
107
+ if (!propertiesFile .exists ()) {
108
+ log .debug ("[buildinfo] BuildInfo properties file is not exists." );
109
+ return props ;
110
+ }
111
+
112
+ try {
113
+ EncryptionKeyPair keyPair = new EncryptionKeyPair (getPropertiesFileEncryptionKey (existingProps ), getPropertiesFileEncryptionKeyIv (existingProps ));
114
+ if (!keyPair .isEmpty ()) {
115
+ log .debug ("[buildinfo] Found an encryption for buildInfo properties file for this build." );
116
+ props .putAll (decryptPropertiesFromFile (propertiesFile .getPath (), keyPair ));
117
+ } else {
118
+ try (InputStream inputStream = Files .newInputStream (propertiesFile .toPath ())) {
81
119
props .load (inputStream );
82
120
}
83
- } catch (IOException e ) {
84
- throw new RuntimeException (
85
- "Unable to load build info properties from file: " + propertiesFile .getAbsolutePath (), e );
86
- } finally {
87
- IOUtils .closeQuietly (inputStream );
88
121
}
122
+ } catch (IOException | InvalidAlgorithmParameterException | IllegalBlockSizeException | NoSuchPaddingException |
123
+ BadPaddingException | NoSuchAlgorithmException | InvalidKeyException e ) {
124
+ throw new RuntimeException ("Unable to load build info properties from file: " + propertiesFile .getAbsolutePath (), e );
89
125
}
90
-
91
- props .putAll (existingProps );
92
- props .putAll (System .getProperties ());
93
-
94
126
return props ;
95
127
}
96
128
@@ -237,11 +269,45 @@ public static void saveBuildInfoToFile(BuildInfo buildInfo, File toFile) throws
237
269
CommonUtils .writeByCharset (buildInfoJson , toFile , StandardCharsets .UTF_8 );
238
270
}
239
271
272
+ /**
273
+ * @param additionalProps Additional properties containing the encryption key.
274
+ * @return The encryption key obtained from system properties or additional properties.
275
+ */
276
+ private static String getPropertiesFileEncryptionKey (Properties additionalProps ) {
277
+ return getPropertiesFileEncryption (additionalProps , BuildInfoConfigProperties .PROP_PROPS_FILE_KEY );
278
+ }
279
+
280
+ /**
281
+ * @param additionalProps Additional properties containing the encryption IV.
282
+ * @return The encryption IV obtained from system properties or additional properties.
283
+ */
284
+ private static String getPropertiesFileEncryptionKeyIv (Properties additionalProps ) {
285
+ return getPropertiesFileEncryption (additionalProps , BuildInfoConfigProperties .PROP_PROPS_FILE_KEY_IV );
286
+ }
287
+
288
+ private static String getPropertiesFileEncryption (Properties additionalProps , String key ) {
289
+ // Check if the encryption key is set in system properties
290
+ if (StringUtils .isNotBlank (System .getProperty (key ))) {
291
+ return System .getProperty (key );
292
+ }
293
+ if (additionalProps != null ) {
294
+ // Check for the encryption key directly in additional properties
295
+ if (StringUtils .isNotBlank (additionalProps .getProperty (key ))) {
296
+ return additionalProps .getProperty (key );
297
+ }
298
+ // Jenkins prefixes these variables with "env." so let's try that
299
+ if (StringUtils .isNotBlank (additionalProps .getProperty ("env." + key ))) {
300
+ return additionalProps .getProperty ("env." + key );
301
+ }
302
+ }
303
+ return null ;
304
+ }
305
+
240
306
private static String getAdditionalPropertiesFile (Properties additionalProps , Log log ) {
241
307
String key = BuildInfoConfigProperties .PROP_PROPS_FILE ;
242
308
String filePath = System .getProperty (key );
243
309
String propFoundPath = "System.getProperty(" + key + ")" ;
244
- if (StringUtils .isBlank (filePath ) && additionalProps != null ) {
310
+ if (StringUtils .isBlank (filePath )) {
245
311
filePath = additionalProps .getProperty (key );
246
312
propFoundPath = "additionalProps.getProperty(" + key + ")" ;
247
313
}
0 commit comments