@@ -84,11 +84,8 @@ public void getBuildInfoPropertiesFromSystemProps() {
84
84
}
85
85
86
86
public void getBuildInfoPropertiesFromFile () throws IOException {
87
- Properties props = new Properties ();
88
- props .put (POPO_KEY , "buildname" );
89
- props .put (MOMO_KEY , "1" );
90
87
try (FileOutputStream fileOutputStream = new FileOutputStream (tempFile .toFile ())) {
91
- props .store (fileOutputStream , "" );
88
+ createProperties () .store (fileOutputStream , "" );
92
89
}
93
90
94
91
System .setProperty (BuildInfoConfigProperties .PROP_PROPS_FILE , tempFile .toString ());
@@ -103,9 +100,8 @@ public void getBuildInfoPropertiesFromFile() throws IOException {
103
100
}
104
101
105
102
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
+
109
105
Properties fileProps = filterDynamicProperties (
110
106
mergePropertiesWithSystemAndPropertyFile (new Properties (), getLog ()),
111
107
BUILD_INFO_PROP_PREDICATE );
@@ -114,34 +110,35 @@ public void getBuildInfoPropertiesFromEncryptedFile() throws IOException, Invali
114
110
assertEquals (fileProps .getProperty (MOMO_KEY ), "1" , "momo property does not match" );
115
111
}
116
112
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
119
119
Properties fileProps = filterDynamicProperties (
120
120
mergePropertiesWithSystemAndPropertyFile (new Properties (), getLog ()),
121
121
BUILD_INFO_PROP_PREDICATE );
122
+ // Check if no properties are read
122
123
assertEquals (fileProps .size (), 0 , "0 properties should be present, the file is encrypted, and the key is not available" );
123
124
}
124
125
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 {
129
127
props .put (BuildInfoConfigProperties .PROP_PROPS_FILE , tempFile .toString ());
130
128
System .setProperty (BuildInfoConfigProperties .PROP_PROPS_FILE , tempFile .toString ());
131
129
ArtifactoryClientConfiguration client = new ArtifactoryClientConfiguration (new NullLog ());
132
130
client .fillFromProperties (props );
133
131
134
132
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 ()));
136
136
}
137
137
}
138
138
139
139
public void getBuildInfoProperties () throws IOException {
140
- Properties props = new Properties ();
141
- props .put (POPO_KEY , "buildname" );
142
- props .put (MOMO_KEY , "1" );
143
140
try (FileOutputStream fileOutputStream = new FileOutputStream (tempFile .toFile ())) {
144
- props .store (fileOutputStream , "" );
141
+ createProperties () .store (fileOutputStream , "" );
145
142
}
146
143
System .setProperty (BuildInfoConfigProperties .PROP_PROPS_FILE , tempFile .toString ());
147
144
@@ -166,11 +163,8 @@ public void getBuildInfoProperties() throws IOException {
166
163
}
167
164
168
165
public void getEnvPropertiesFromFile () throws IOException {
169
- Properties props = new Properties ();
170
- props .put (ENV_POPO_KEY , "buildname" );
171
- props .put (ENV_MOMO_KEY , "1" );
172
166
try (FileOutputStream fileOutputStream = new FileOutputStream (tempFile .toFile ())) {
173
- props .store (fileOutputStream , "" );
167
+ createPropertiesEnvs () .store (fileOutputStream , "" );
174
168
}
175
169
System .setProperty (BuildInfoConfigProperties .PROP_PROPS_FILE , tempFile .toString ());
176
170
@@ -180,11 +174,8 @@ public void getEnvPropertiesFromFile() throws IOException {
180
174
}
181
175
182
176
public void getEnvAndSysPropertiesFromFile () throws IOException {
183
- Properties props = new Properties ();
184
- props .put (ENV_POPO_KEY , "buildname" );
185
- props .put (ENV_MOMO_KEY , "1" );
186
177
try (FileOutputStream fileOutputStream = new FileOutputStream (tempFile .toFile ())) {
187
- props .store (fileOutputStream , "" );
178
+ createPropertiesEnvs () .store (fileOutputStream , "" );
188
179
}
189
180
System .setProperty (BuildInfoConfigProperties .PROP_PROPS_FILE , tempFile .toString ());
190
181
@@ -204,6 +195,29 @@ public void getEnvAndSysPropertiesFromFile() throws IOException {
204
195
System .clearProperty (gogoKey );
205
196
}
206
197
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
+
207
221
public void testExcludePatterns () {
208
222
// Put system properties
209
223
String kokoKey = "koko" ;
@@ -215,7 +229,7 @@ public void testExcludePatterns() {
215
229
216
230
Properties startProps = new Properties ();
217
231
startProps .put (BuildInfoConfigProperties .PROP_ENV_VARS_EXCLUDE_PATTERNS , "*koko" );
218
- Properties buildInfoProperties = getEnvProperties (startProps , null );
232
+ Properties buildInfoProperties = getEnvProperties (startProps , new NullLog () );
219
233
assertNull (buildInfoProperties .getProperty ("koko" ), "Should not find koko property due to exclude patterns" );
220
234
assertNull (buildInfoProperties .getProperty ("akoko" ), "Should not find akoko property due to exclude patterns" );
221
235
assertEquals (buildInfoProperties .getProperty ("gogo" ), "2" , "gogo parent number property does not match" );
@@ -235,7 +249,7 @@ public void testIncludePatterns() {
235
249
236
250
Properties startProps = new Properties ();
237
251
startProps .put (BuildInfoConfigProperties .PROP_ENV_VARS_INCLUDE_PATTERNS , "gogo?*" );
238
- Properties buildInfoProperties = getEnvProperties (startProps , null );
252
+ Properties buildInfoProperties = getEnvProperties (startProps , new NullLog () );
239
253
assertEquals (buildInfoProperties .getProperty ("gogo1" ), "1" , "gogo1 parent number property does not match" );
240
254
assertEquals (buildInfoProperties .getProperty ("gogo2a" ), "2" , "gogo2a parent number property does not match" );
241
255
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
311
325
boolean encode , boolean platformUrl , String exceptedUrl ) {
312
326
assertEquals (createBuildInfoUrl (url , buildName , buildNumber , timeStamp , project , encode , platformUrl ), exceptedUrl );
313
327
}
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
+ }
314
342
}
0 commit comments