Skip to content

Commit

Permalink
Merge pull request #353 from conductor-oss/feature/java-orkes-client-…
Browse files Browse the repository at this point in the history
…uses-env-vars

Unify environment variable usage
  • Loading branch information
jmigueprieto authored Jan 6, 2025
2 parents 5ef888a + aabce37 commit 12b483c
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 60 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/java-client-v4-integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ jobs:
./gradlew -p tests test
env:
CONDUCTOR_SERVER_URL: ${{ secrets.CONDUCTOR_SERVER_URL }}
CONDUCTOR_SERVER_AUTH_KEY: ${{ secrets.CONDUCTOR_SERVER_AUTH_KEY }}
CONDUCTOR_SERVER_AUTH_SECRET: ${{ secrets.CONDUCTOR_SERVER_AUTH_SECRET }}
CONDUCTOR_AUTH_KEY: ${{ secrets.CONDUCTOR_AUTH_KEY }}
CONDUCTOR_AUTH_SECRET: ${{ secrets.CONDUCTOR_AUTH_SECRET }}
- name: Publish Test Report
uses: mikepenz/action-junit-report@v3
if: always()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,8 @@ public static class Builder<T extends Builder<T>> {
private Supplier<ObjectMapper> objectMapperSupplier = () -> new ObjectMapperProvider().getObjectMapper();
private final List<HeaderSupplier> headerSuppliers = new ArrayList<>();

private boolean useEnvVariables = false;

protected T self() {
//noinspection unchecked
return (T) this;
Expand Down Expand Up @@ -538,7 +540,19 @@ protected List<HeaderSupplier> headerSupplier() {
return headerSuppliers;
}

public T useEnvVariables(boolean useEnvVariables) {
this.useEnvVariables = useEnvVariables;
return self();
}

protected boolean isUseEnvVariables() {
return this.useEnvVariables;
}

public ConductorClient build() {
if (useEnvVariables) {
applyEnvVariables();
}
return new ConductorClient(this);
}

Expand All @@ -551,5 +565,14 @@ protected void validateAndAssignDefaults() {
basePath = basePath.substring(0, basePath.length() - 1);
}
}

protected void applyEnvVariables() {
String conductorServerUrl = System.getenv("CONDUCTOR_SERVER_URL");
if (conductorServerUrl != null) {
this.basePath(conductorServerUrl);
} else {
throw new RuntimeException("env variable CONDUCTOR_SERVER_URL is not set");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,44 +14,23 @@

import com.netflix.conductor.client.http.ConductorClient;

import io.orkes.conductor.client.ApiClient;
import io.orkes.conductor.client.OrkesClients;
import io.orkes.conductor.client.http.OrkesAuthentication;

import com.google.common.base.Preconditions;

import static java.lang.System.getenv;

public class ClientUtil {
private static final String ENV_ROOT_URI = "CONDUCTOR_SERVER_URL";
private static final String ENV_KEY_ID = "CONDUCTOR_SERVER_AUTH_KEY";
private static final String ENV_SECRET = "CONDUCTOR_SERVER_AUTH_SECRET";
private static final ConductorClient CLIENT = getClient();

private static final ConductorClient CLIENT = ApiClient.builder()
.useEnvVariables(true)
.readTimeout(10_000)
.connectTimeout(10_000)
.writeTimeout(10_000)
.build();;

public static OrkesClients getOrkesClients() {
return new OrkesClients(CLIENT);
}

public static ConductorClient getClient() {
if (CLIENT != null) {
return CLIENT;
}

var basePath = getenv(ENV_ROOT_URI);
Preconditions.checkNotNull(basePath, ENV_ROOT_URI + " env not set");

ConductorClient.Builder builder = ConductorClient.builder()
.basePath(basePath)
.readTimeout(10_000)
.connectTimeout(10_000)
.writeTimeout(10_000);

var keyId = getenv(ENV_KEY_ID);
var keySecret = getenv(ENV_SECRET);

if (keyId != null && keySecret != null) {
builder.addHeaderSupplier(new OrkesAuthentication(keyId, keySecret));
}

return builder.build();
return CLIENT;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public ApiClient(String rootUri) {
super(rootUri);
}

public ApiClient() {
this(builder().useEnvVariables(true));
}

private ApiClient(ApiClientBuilder builder) {
super(builder);
}
Expand Down Expand Up @@ -161,8 +165,30 @@ public ApiClientBuilder credentials(String key, String secret) {

@Override
public ApiClient build() {
if (isUseEnvVariables()) {
applyEnvVariables();
}

return new ApiClient(this);
}

protected void applyEnvVariables() {
super.applyEnvVariables();

String conductorAuthKey = System.getenv("CONDUCTOR_AUTH_KEY");
if (conductorAuthKey == null) {
conductorAuthKey = System.getenv("CONDUCTOR_SERVER_AUTH_KEY"); // for backwards compatibility
}

String conductorAuthSecret = System.getenv("CONDUCTOR_AUTH_SECRET");
if (conductorAuthSecret == null) {
conductorAuthSecret = System.getenv("CONDUCTOR_SERVER_AUTH_SECRET"); // for backwards compatibility
}

if (conductorAuthKey != null && conductorAuthSecret != null) {
this.credentials(conductorAuthKey, conductorAuthSecret);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,24 @@
*/
package io.orkes.conductor.client.util;

import org.junit.jupiter.api.Assertions;

import com.netflix.conductor.client.http.ConductorClient;

import io.orkes.conductor.client.ApiClient;
import io.orkes.conductor.client.OrkesClients;

public class ClientTestUtil {
private static final String ENV_ROOT_URI = "CONDUCTOR_SERVER_URL";
private static final String ENV_KEY_ID = "CONDUCTOR_SERVER_AUTH_KEY";
private static final String ENV_SECRET = "CONDUCTOR_SERVER_AUTH_SECRET";
private static final ConductorClient CLIENT = getClient();
private static final ConductorClient CLIENT = ApiClient.builder()
.useEnvVariables(true)
.readTimeout(30_000)
.connectTimeout(30_000)
.writeTimeout(30_000)
.build();

public static OrkesClients getOrkesClients() {
return new OrkesClients(CLIENT);
}

public static ConductorClient getClient() {
if (CLIENT != null) {
return CLIENT;
}

String basePath = getEnv(ENV_ROOT_URI);
Assertions.assertNotNull(basePath, ENV_ROOT_URI + " env not set");
String keyId = getEnv(ENV_KEY_ID);
Assertions.assertNotNull(keyId, ENV_KEY_ID + " env not set");
String keySecret = getEnv(ENV_SECRET);
Assertions.assertNotNull(keySecret, ENV_SECRET + " env not set");

return ApiClient.builder()
.basePath(basePath)
.credentials(keyId, keySecret)
.readTimeout(30_000)
.connectTimeout(30_000)
.writeTimeout(30_000)
.build();
}

private static String getEnv(String key) {
return System.getenv(key);
return CLIENT;
}
}

0 comments on commit 12b483c

Please sign in to comment.