-
-
Notifications
You must be signed in to change notification settings - Fork 592
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Improve automated containerized deployment
See #2443. Allow configuration by environment variables. Signed-off-by: syalioune <[email protected]>
- Loading branch information
Showing
3 changed files
with
172 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,14 +18,18 @@ | |
*/ | ||
package org.dependencytrack.persistence; | ||
|
||
import alpine.model.ConfigProperty; | ||
import alpine.server.auth.PasswordService; | ||
import org.dependencytrack.PersistenceCapableTest; | ||
import org.dependencytrack.auth.Permissions; | ||
import org.dependencytrack.model.ConfigPropertyConstants; | ||
import org.dependencytrack.notification.publisher.DefaultNotificationPublishers; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Method; | ||
import java.util.Map; | ||
|
||
public class DefaultObjectGeneratorTest extends PersistenceCapableTest { | ||
|
||
|
@@ -60,10 +64,39 @@ public void testLoadDefaultPermissions() throws Exception { | |
@Test | ||
public void testLoadDefaultPersonas() throws Exception { | ||
DefaultObjectGenerator generator = new DefaultObjectGenerator(); | ||
clearEnvironmentVariable(DefaultObjectGenerator.ADMIN_USERNAME_ENV_VARIABLE); | ||
clearEnvironmentVariable(DefaultObjectGenerator.ADMIN_PASSWORD_ENV_VARIABLE); | ||
clearEnvironmentVariable(DefaultObjectGenerator.ADMIN_FULL_NAME_ENV_VARIABLE); | ||
clearEnvironmentVariable(DefaultObjectGenerator.ADMIN_EMAIL_ENV_VARIABLE); | ||
Method method = generator.getClass().getDeclaredMethod("loadDefaultPersonas"); | ||
method.setAccessible(true); | ||
method.invoke(generator); | ||
Assert.assertEquals(3, qm.getTeams().size()); | ||
var users = qm.getManagedUsers(); | ||
Assert.assertEquals(1, users.size()); | ||
Assert.assertEquals(DefaultObjectGenerator.DEFAULT_ADMIN_USERNAME, users.get(0).getUsername()); | ||
Assert.assertTrue(PasswordService.matches(DefaultObjectGenerator.DEFAULT_ADMIN_PASSWORD.toCharArray(), users.get(0))); | ||
Assert.assertEquals(DefaultObjectGenerator.DEFAULT_ADMIN_FULL_NAME, users.get(0).getFullname()); | ||
Assert.assertEquals(DefaultObjectGenerator.DEFAULT_ADMIN_EMAIL, users.get(0).getEmail()); | ||
} | ||
|
||
@Test | ||
public void testLoadDefaultPersonasWithUserProvidedCredentials() throws Exception { | ||
DefaultObjectGenerator generator = new DefaultObjectGenerator(); | ||
setEnvironmentVariable(DefaultObjectGenerator.ADMIN_USERNAME_ENV_VARIABLE, "test"); | ||
setEnvironmentVariable(DefaultObjectGenerator.ADMIN_PASSWORD_ENV_VARIABLE, "testPassword"); | ||
setEnvironmentVariable(DefaultObjectGenerator.ADMIN_FULL_NAME_ENV_VARIABLE, "test test"); | ||
setEnvironmentVariable(DefaultObjectGenerator.ADMIN_EMAIL_ENV_VARIABLE, "[email protected]"); | ||
Method method = generator.getClass().getDeclaredMethod("loadDefaultPersonas"); | ||
method.setAccessible(true); | ||
method.invoke(generator); | ||
Assert.assertEquals(3, qm.getTeams().size()); | ||
var users = qm.getManagedUsers(); | ||
Assert.assertEquals(1, users.size()); | ||
Assert.assertEquals("test", users.get(0).getUsername()); | ||
Assert.assertTrue(PasswordService.matches("testPassword".toCharArray(), users.get(0))); | ||
Assert.assertEquals("test test", users.get(0).getFullname()); | ||
Assert.assertEquals("[email protected]", users.get(0).getEmail()); | ||
} | ||
|
||
@Test | ||
|
@@ -78,10 +111,14 @@ public void testLoadDefaultRepositories() throws Exception { | |
@Test | ||
public void testLoadDefaultConfigProperties() throws Exception { | ||
DefaultObjectGenerator generator = new DefaultObjectGenerator(); | ||
String nvdToggleEnvVariableName = generator.generateEnvVariableName(ConfigPropertyConstants.VULNERABILITY_SOURCE_NVD_ENABLED); | ||
setEnvironmentVariable(nvdToggleEnvVariableName, "false"); | ||
Method method = generator.getClass().getDeclaredMethod("loadDefaultConfigProperties"); | ||
method.setAccessible(true); | ||
method.invoke(generator); | ||
Assert.assertEquals(ConfigPropertyConstants.values().length, qm.getConfigProperties().size()); | ||
ConfigProperty nvdEnabled = qm.getConfigProperty(ConfigPropertyConstants.VULNERABILITY_SOURCE_NVD_ENABLED.getGroupName(), ConfigPropertyConstants.VULNERABILITY_SOURCE_NVD_ENABLED.getPropertyName()); | ||
Assert.assertEquals("false", nvdEnabled.getPropertyValue()); | ||
} | ||
|
||
@Test | ||
|
@@ -92,4 +129,63 @@ public void testLoadDefaultNotificationPublishers() throws Exception { | |
method.invoke(generator); | ||
Assert.assertEquals(DefaultNotificationPublishers.values().length, qm.getAllNotificationPublishers().size()); | ||
} | ||
|
||
private static void clearEnvironmentVariable(String key) throws Exception { | ||
Class<?> processEnvironment = Class.forName("java.lang.ProcessEnvironment"); | ||
|
||
Field unmodifiableMapField = getAccessibleField(processEnvironment, "theUnmodifiableEnvironment"); | ||
Object unmodifiableMap = unmodifiableMapField.get(null); | ||
clearUnmodifiableMap(key, unmodifiableMap); | ||
|
||
Field caseInsensitiveMapField = getAccessibleField(processEnvironment, "theCaseInsensitiveEnvironment"); | ||
Map<String, String> caseInsensitiveMap = (Map<String, String>) caseInsensitiveMapField.get(null); | ||
caseInsensitiveMap.remove(key); | ||
|
||
Field mapField = getAccessibleField(processEnvironment, "theEnvironment"); | ||
Map<String, String> map = (Map<String, String>) mapField.get(null); | ||
map.remove(key); | ||
} | ||
|
||
private static void setEnvironmentVariable(String key, String value) throws Exception { | ||
|
||
Class<?> processEnvironment = Class.forName("java.lang.ProcessEnvironment"); | ||
|
||
Field unmodifiableMapField = getAccessibleField(processEnvironment, "theUnmodifiableEnvironment"); | ||
Object unmodifiableMap = unmodifiableMapField.get(null); | ||
injectIntoUnmodifiableMap(key, value, unmodifiableMap); | ||
|
||
Field caseInsensitiveMapField = getAccessibleField(processEnvironment, "theCaseInsensitiveEnvironment"); | ||
Map<String, String> caseInsensitiveMap = (Map<String, String>) caseInsensitiveMapField.get(null); | ||
caseInsensitiveMap.put(key, value); | ||
|
||
Field mapField = getAccessibleField(processEnvironment, "theEnvironment"); | ||
Map<String, String> map = (Map<String, String>) mapField.get(null); | ||
map.put(key, value); | ||
} | ||
|
||
private static Field getAccessibleField(Class<?> clazz, String fieldName) | ||
throws NoSuchFieldException { | ||
|
||
Field field = clazz.getDeclaredField(fieldName); | ||
field.setAccessible(true); | ||
return field; | ||
} | ||
|
||
private static void injectIntoUnmodifiableMap(String key, String value, Object map) | ||
throws ReflectiveOperationException { | ||
|
||
Class unmodifiableMap = Class.forName("java.util.Collections$UnmodifiableMap"); | ||
Field field = getAccessibleField(unmodifiableMap, "m"); | ||
Object obj = field.get(map); | ||
((Map<String, String>) obj).put(key, value); | ||
} | ||
|
||
private static void clearUnmodifiableMap(String key, Object map) | ||
throws ReflectiveOperationException { | ||
|
||
Class unmodifiableMap = Class.forName("java.util.Collections$UnmodifiableMap"); | ||
Field field = getAccessibleField(unmodifiableMap, "m"); | ||
Object obj = field.get(map); | ||
((Map<String, String>) obj).remove(key); | ||
} | ||
} |