Skip to content

Commit

Permalink
GH-213: enhance WorkspaceUtil to also search in envrionment variables (
Browse files Browse the repository at this point in the history
  • Loading branch information
nicdard authored Feb 1, 2024
1 parent 2948283 commit 4a53673
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import com.adobe.aio.event.management.model.EventsOfInterestInputModel;
import com.adobe.aio.event.management.model.Registration;
import com.adobe.aio.event.management.model.RegistrationCreateModel;
import com.adobe.aio.event.management.model.RegistrationUpdateModel;
import com.adobe.aio.util.WorkspaceUtil;
import com.adobe.aio.workspace.Workspace;
import java.net.MalformedURLException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@
*/
package com.adobe.aio.event.management;

import com.adobe.aio.event.management.model.ProviderInputModel;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import com.adobe.aio.event.management.feign.ConflictException;
import com.adobe.aio.event.management.model.EventMetadata;
import com.adobe.aio.event.management.model.Provider;
import com.adobe.aio.util.WorkspaceUtil;
Expand Down
55 changes: 49 additions & 6 deletions ims/src/main/java/com/adobe/aio/util/WorkspaceUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ public class WorkspaceUtil {
private WorkspaceUtil() {
}

/**
* Loads configurations for a Workspace from either one and only one of the following
* sources, probing them first to check that all the required properties are given,
* in order:
* <ol>
* <li>System Properties</li>
* <li>Environment Variables</li>
* <li>classpath:{@link WorkspaceUtil#DEFAULT_TEST_PROPERTIES}</li>
* </ol>
* @return a Workspace.Builder loaded with the provided config
*/
public static Workspace.Builder getSystemWorkspaceBuilder() {
if (StringUtils.isNoneBlank(
System.getProperty(PrivateKeyBuilder.AIO_ENCODED_PKCS_8),
Expand All @@ -47,6 +58,23 @@ public static Workspace.Builder getSystemWorkspaceBuilder() {
return Workspace.builder()
.properties(System.getProperties())
.privateKey(privateKey);
} else if (StringUtils.isNoneBlank(
System.getenv(PrivateKeyBuilder.AIO_ENCODED_PKCS_8),
System.getenv(Workspace.API_KEY),
System.getenv(Workspace.WORKSPACE_ID),
System.getenv(Workspace.CLIENT_SECRET),
System.getenv(Workspace.CONSUMER_ORG_ID),
System.getenv(Workspace.CREDENTIAL_ID),
System.getenv(Workspace.IMS_ORG_ID),
System.getenv(Workspace.META_SCOPES),
System.getenv(Workspace.PROJECT_ID),
System.getenv(Workspace.TECHNICAL_ACCOUNT_ID))) {
logger.debug("loading test Workspace from JVM System Properties");
PrivateKey privateKey =
new PrivateKeyBuilder()
.encodedPkcs8Key(System.getenv(PrivateKeyBuilder.AIO_ENCODED_PKCS_8))
.build();
return Workspace.builder().systemEnv().privateKey(privateKey);
} else {
/**
* WARNING: don't push back your workspace secrets to github
Expand All @@ -60,15 +88,30 @@ public static String getSystemProperty(String key) {
return getSystemProperty(key,DEFAULT_TEST_PROPERTIES);
}

/**
* Loads a property from either one of the following sources, probing it first to
* check that the required property is given, in order:
* <ol>
* <li>System Properties</li>
* <li>Environment Variables</li>
* <li>classpath:{@code propertyClassPath}</li>
* </ol>
*
* @param key the property name
* @param propertyClassPath the classpath of the property file
* @return the value of the property
*/
public static String getSystemProperty(String key, String propertyClassPath) {
String value = System.getProperty(key);
if (StringUtils.isBlank(value)) {
logger.debug("loading property `{}` from classpath `{}`", key, propertyClassPath);
value = FileUtil.readPropertiesFromClassPath(propertyClassPath).getProperty(key);
} else {
if (StringUtils.isNotBlank(System.getProperty(key))) {
logger.debug("loading property `{}`from JVM System Properties", key);
return System.getProperty(key);
} if (StringUtils.isNotBlank(System.getenv(key))) {
logger.debug("loading property `{}` from Environment Variables", key);
return System.getenv(key);
} else {
logger.debug("loading property `{}` from classpath `{}`", key, propertyClassPath);
return FileUtil.readPropertiesFromClassPath(propertyClassPath).getProperty(key);
}
return value;
}

private static Workspace.Builder getWorkspaceBuilder(String propertyFileClassPath) {
Expand Down

0 comments on commit 4a53673

Please sign in to comment.