Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-213: enhance WorkspaceUtil to also search in envrionment variables #214

Merged
merged 3 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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