Skip to content

Commit

Permalink
Adding a custom vertx config store
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasm-ttd committed Aug 2, 2024
1 parent a928bcd commit f849d6f
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/uid2/shared/attest/UidCoreClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,12 @@ public InputStream download(String path) throws CloudStorageException {
return this.internalDownload(path);
}

protected String getJWT() {
public String getJWT() {
return this.getAttestationResponseHandler().getCoreJWT();
}

private InputStream internalDownload(String path) throws CloudStorageException {
LOGGER.info("UidCoreClient download: {}", path);
try {
InputStream inputStream;
if (allowContentFromLocalFileSystem && path.startsWith("file:/tmp/uid2")) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/uid2/shared/attest/UidOptOutClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public UidOptOutClient(String userToken,
}

@Override
protected String getJWT() {
public String getJWT() {
return this.getAttestationResponseHandler().getOptOutJWT();
}

Expand Down
66 changes: 66 additions & 0 deletions src/main/java/com/uid2/shared/vertx/CoreConfigStore.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.uid2.shared.vertx;

import com.uid2.shared.ApplicationVersion;
import com.uid2.shared.Utils;
import com.uid2.shared.attest.AttestationResponseHandler;
import com.uid2.shared.attest.NoAttestationProvider;
import com.uid2.shared.attest.UidCoreClient;
import com.uid2.shared.cloud.CloudStorageException;
import com.uid2.shared.cloud.CloudUtils;
import io.vertx.config.spi.ConfigStore;
import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.impl.VertxInternal;
import io.vertx.core.impl.future.FailedFuture;
import io.vertx.core.json.Json;
import io.vertx.core.json.JsonObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;

public class CoreConfigStore implements ConfigStore {
private static final Logger LOGGER = LoggerFactory.getLogger(CoreConfigStore.class);
private final VertxInternal vertx;
private final UidCoreClient uidCoreClient;

public CoreConfigStore(Vertx vertx, JsonObject configuration) {
this.vertx = (VertxInternal) vertx;
String token = configuration.getString("core_api_token");
String coreUrl = configuration.getString("core_attest_url");

ApplicationVersion appVersion = null;
try {
appVersion = ApplicationVersion.load("uid2-operator", "uid2-shared", "uid2-attestation-api");
AttestationResponseHandler handler = new AttestationResponseHandler(vertx, coreUrl, token, appVersion, new NoAttestationProvider(), null, CloudUtils.defaultProxy);
this.uidCoreClient = new UidCoreClient(token, CloudUtils.defaultProxy, handler);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@Override
public Future<Void> close() {
return this.vertx.getOrCreateContext().succeededFuture();
}

@Override
public Future<Buffer> get() {
try {
Future<JsonObject> future;
InputStream stream = this.uidCoreClient.download("/config");
String jsonString = Utils.readToEnd(stream);
if (jsonString != null && !jsonString.isEmpty()) {
return Future.succeededFuture(Buffer.buffer(jsonString));
} else {
return Future.failedFuture("Unable to get config from Core");
}
} catch (CloudStorageException e) {
return Future.failedFuture(e);
} catch (IOException e) {
return Future.failedFuture(e);
}
}
}
18 changes: 18 additions & 0 deletions src/main/java/com/uid2/shared/vertx/CoreConfigStoreFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.uid2.shared.vertx;

import io.vertx.config.spi.ConfigStore;
import io.vertx.config.spi.ConfigStoreFactory;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;

public class CoreConfigStoreFactory implements ConfigStoreFactory {
@Override
public String name() {
return "core";
}

@Override
public ConfigStore create(Vertx vertx, JsonObject configuration) {
return new CoreConfigStore(vertx, configuration);
}
}
17 changes: 11 additions & 6 deletions src/main/java/com/uid2/shared/vertx/VertxUtils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.uid2.shared.vertx;

import com.uid2.shared.Const;
import com.uid2.shared.attest.UidCoreClient;
import io.vertx.config.ConfigRetriever;
import io.vertx.config.ConfigRetrieverOptions;
import io.vertx.config.ConfigStoreOptions;
Expand Down Expand Up @@ -45,6 +46,10 @@ public static JsonObject getJsonConfig(Vertx vertx) throws ExecutionException, I
// see https://vertx.io/blog/vert-x-application-configuration/
//
public static ConfigRetriever createConfigRetriever(Vertx vertx) {
return ConfigRetriever.create(vertx, createConfigRetrieverOptions(vertx));
}

public static ConfigRetrieverOptions createConfigRetrieverOptions(Vertx vertx) {
ConfigRetrieverOptions retrieverOptions = new ConfigRetrieverOptions();

{
Expand All @@ -53,14 +58,14 @@ public static ConfigRetriever createConfigRetriever(Vertx vertx) {
String format = extractFormatFromFileExtension(defaultConfigPath);
LOGGER.info("Default config file path: " + defaultConfigPath + ", format:" + format);
ConfigStoreOptions defaultStore = new ConfigStoreOptions()
.setType("file").setFormat(format)
.setConfig(new JsonObject().put("path", defaultConfigPath));
.setType("file").setFormat(format)
.setConfig(new JsonObject().put("path", defaultConfigPath));
retrieverOptions.addStore(defaultStore);
}
}

ConfigStoreOptions vertxConfig = new ConfigStoreOptions().setType("json")
.setConfig(vertx.getOrCreateContext().config());
.setConfig(vertx.getOrCreateContext().config());
ConfigStoreOptions sysConfig = new ConfigStoreOptions().setType("sys");
ConfigStoreOptions envConfig = new ConfigStoreOptions().setType("env");
retrieverOptions.addStore(vertxConfig).addStore(sysConfig).addStore(envConfig);
Expand All @@ -71,13 +76,13 @@ public static ConfigRetriever createConfigRetriever(Vertx vertx) {
String format = extractFormatFromFileExtension(overrideConfigPath);
LOGGER.info("Override config file path: " + overrideConfigPath + ", format:" + format);
ConfigStoreOptions overrideStore = new ConfigStoreOptions()
.setType("file").setFormat(format)
.setConfig(new JsonObject().put("path", overrideConfigPath));
.setType("file").setFormat(format)
.setConfig(new JsonObject().put("path", overrideConfigPath));
retrieverOptions.addStore(overrideStore);
}
}

return ConfigRetriever.create(vertx, retrieverOptions);
return retrieverOptions;
}

public static Map.Entry<String, String> parseClientAppVersion(String appVersions) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.uid2.shared.vertx.CoreConfigStoreFactory

0 comments on commit f849d6f

Please sign in to comment.