Skip to content
Closed
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
66 changes: 34 additions & 32 deletions src/main/java/com/uid2/core/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import com.uid2.shared.auth.EnclaveIdentifierProvider;
import com.uid2.shared.auth.RotatingOperatorKeyProvider;
import com.uid2.shared.store.reader.RotatingCloudEncryptionKeyProvider;
import com.uid2.shared.model.CloudEncryptionKey;
import com.uid2.shared.cloud.CloudUtils;
import com.uid2.shared.cloud.EmbeddedResourceStorage;
import com.uid2.shared.cloud.ICloudStorage;
Expand All @@ -38,31 +37,32 @@
import io.vertx.core.VertxOptions;
import io.vertx.core.file.FileSystem;
import io.vertx.core.http.HttpServerOptions;
import io.vertx.core.http.impl.HttpUtils;
import io.vertx.core.json.JsonObject;
import io.vertx.micrometer.Label;
import io.vertx.micrometer.MetricsDomain;
import io.vertx.micrometer.MicrometerMetricsOptions;
import io.vertx.micrometer.VertxPrometheusOptions;
import io.vertx.micrometer.backends.BackendRegistries;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.management.*;
import java.lang.management.ManagementFactory;
import java.util.*;

public class Main {

private static final int vertxServiceInstances = 1;
private final static Logger LOGGER = LoggerFactory.getLogger(CoreVerticle.class);
private static final int VERTX_SERVICE_INSTANCES = 6;

public static void main(String[] args) {
final String vertxConfigPath = System.getProperty(Const.Config.VERTX_CONFIG_PATH_PROP);
if (vertxConfigPath != null) {
System.out.format("Running CUSTOM CONFIG mode, config: %s\n", vertxConfigPath);
LOGGER.info("Running CUSTOM CONFIG mode, config: {}", vertxConfigPath);
} else if (!Utils.isProductionEnvironment()) {
System.out.format("Running LOCAL DEBUG mode, config: %s\n", Const.Config.LOCAL_CONFIG_PATH);
LOGGER.info("Running LOCAL DEBUG mode, config: {}", Const.Config.LOCAL_CONFIG_PATH);
System.setProperty(Const.Config.VERTX_CONFIG_PATH_PROP, Const.Config.LOCAL_CONFIG_PATH);
} else {
System.out.format("Running PRODUCTION mode, config: %s\n", Const.Config.OVERRIDE_CONFIG_PATH);
LOGGER.info("Running PRODUCTION mode, config: {}", Const.Config.OVERRIDE_CONFIG_PATH);
}

// create AdminApi instance
Expand All @@ -71,7 +71,7 @@ public static void main(String[] args) {
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
server.registerMBean(AdminApi.instance, objectName);
} catch (InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException | MalformedObjectNameException e) {
System.err.format("%s", e.getMessage());
LOGGER.error(e.getMessage());
System.exit(-1);
}

Expand All @@ -91,7 +91,7 @@ public static void main(String[] args) {

VertxUtils.createConfigRetriever(vertx).getConfig(ar -> {
if (ar.failed()) {
System.out.println("failed to load config: " + ar.cause().toString());
LOGGER.error("failed to load config: {}", ar.cause().toString());
System.exit(-1);
}

Expand All @@ -100,7 +100,7 @@ public static void main(String[] args) {
SecretStore.Global.load(config);

boolean useStorageMock = Optional.ofNullable(ConfigStore.Global.getBoolean("storage_mock")).orElse(false);
ICloudStorage cloudStorage = null;
ICloudStorage cloudStorage;
if (useStorageMock) {
cloudStorage = new EmbeddedResourceStorage(Main.class).withUrlPrefix(ConfigStore.Global.getOrDefault("storage_mock_url_prefix", ""));
} else {
Expand All @@ -110,24 +110,26 @@ public static void main(String[] args) {
cloudStorage.setPreSignedUrlExpiry(expiryInSeconds);
}

RotatingStoreVerticle enclaveRotatingVerticle = null;
RotatingStoreVerticle operatorRotatingVerticle = null;
RotatingStoreVerticle cloudEncryptionKeyRotatingVerticle = null;
CoreVerticle coreVerticle = null;
try {
createVertxInstancesMetric();
createVertxEventLoopsMetric();

CloudPath operatorMetadataPath = new CloudPath(config.getString(Const.Config.OperatorsMetadataPathProp));
GlobalScope operatorScope = new GlobalScope(operatorMetadataPath);
RotatingOperatorKeyProvider operatorKeyProvider = new RotatingOperatorKeyProvider(cloudStorage, cloudStorage, operatorScope);
operatorRotatingVerticle = new RotatingStoreVerticle("operators", 60000, operatorKeyProvider);
RotatingStoreVerticle operatorRotatingVerticle = new RotatingStoreVerticle("operators", 60000, operatorKeyProvider);
vertx.deployVerticle(operatorRotatingVerticle);

String enclaveMetadataPath = SecretStore.Global.get(EnclaveIdentifierProvider.ENCLAVES_METADATA_PATH);
EnclaveIdentifierProvider enclaveIdProvider = new EnclaveIdentifierProvider(cloudStorage, enclaveMetadataPath);
enclaveRotatingVerticle = new RotatingStoreVerticle("enclaves", 60000, enclaveIdProvider);
RotatingStoreVerticle enclaveRotatingVerticle = new RotatingStoreVerticle("enclaves", 60000, enclaveIdProvider);
vertx.deployVerticle(enclaveRotatingVerticle);

CloudPath cloudEncryptionKeyMetadataPath = new CloudPath(config.getString(Const.Config.CloudEncryptionKeysMetadataPathProp));
GlobalScope cloudEncryptionKeyScope = new GlobalScope(cloudEncryptionKeyMetadataPath);
RotatingCloudEncryptionKeyProvider cloudEncryptionKeyProvider = new RotatingCloudEncryptionKeyProvider(cloudStorage, cloudEncryptionKeyScope);
cloudEncryptionKeyRotatingVerticle = new RotatingStoreVerticle("cloud_encryption_keys", 60000, cloudEncryptionKeyProvider);
RotatingStoreVerticle cloudEncryptionKeyRotatingVerticle = new RotatingStoreVerticle("cloud_encryption_keys", 60000, cloudEncryptionKeyProvider);
vertx.deployVerticle(cloudEncryptionKeyRotatingVerticle);

String corePublicUrl = ConfigStore.Global.get(Const.Config.CorePublicUrlProp);
AttestationService attestationService = new AttestationService()
Expand Down Expand Up @@ -155,7 +157,7 @@ public static void main(String[] args) {
attestationService.with("gcp-oidc", new GcpOidcCoreAttestationService(corePublicUrl));

OperatorJWTTokenProvider operatorJWTTokenProvider = new OperatorJWTTokenProvider(config);

IAttestationTokenService attestationTokenService = new AttestationTokenService(
SecretStore.Global.get(Constants.AttestationEncryptionKeyName),
SecretStore.Global.get(Constants.AttestationEncryptionSaltName),
Expand All @@ -164,19 +166,20 @@ public static void main(String[] args) {

JwtService jwtService = new JwtService(config);
FileSystem fileSystem = vertx.fileSystem();
coreVerticle = new CoreVerticle(cloudStorage, operatorKeyProvider, attestationService, attestationTokenService, enclaveIdProvider, operatorJWTTokenProvider, jwtService, cloudEncryptionKeyProvider, fileSystem);

vertx.deployVerticle(() -> {
try {
return new CoreVerticle(cloudStorage, operatorKeyProvider, attestationService, attestationTokenService, enclaveIdProvider, operatorJWTTokenProvider, jwtService, cloudEncryptionKeyProvider, fileSystem);
} catch (Exception e) {
LOGGER.error("failed to deploy core verticle: {}", e.getMessage());
System.exit(-1);
return null;
}
}, new DeploymentOptions().setInstances(VERTX_SERVICE_INSTANCES));
} catch (Exception e) {
System.out.println("failed to initialize core verticle: " + e.getMessage());
LOGGER.error("failed to initialize core verticle: {}", e.getMessage());
System.exit(-1);
}

createVertxInstancesMetric();
createVertxEventLoopsMetric();

vertx.deployVerticle(enclaveRotatingVerticle);
vertx.deployVerticle(operatorRotatingVerticle);
vertx.deployVerticle(cloudEncryptionKeyRotatingVerticle);
vertx.deployVerticle(coreVerticle, new DeploymentOptions().setInstances(vertxServiceInstances));
});
}

Expand All @@ -195,8 +198,8 @@ private static void setupMetrics(MicrometerMetricsOptions metricOptions) {
actualPath -> HTTPPathMetricFilter.filterPath(actualPath, Endpoints.pathSet())))
// Don't record metrics for 404s.
.meterFilter(MeterFilter.deny(id ->
id.getName().startsWith(MetricsDomain.HTTP_SERVER.getPrefix()) &&
Objects.equals(id.getTag(Label.HTTP_CODE.toString()), "404")))
id.getName().startsWith(MetricsDomain.HTTP_SERVER.getPrefix()) &&
Objects.equals(id.getTag(Label.HTTP_CODE.toString()), "404")))
// adding common labels
.commonTags("application", "uid2-core");

Expand All @@ -214,7 +217,7 @@ private static void setupMetrics(MicrometerMetricsOptions metricOptions) {
}

private static void createVertxInstancesMetric() {
Gauge.builder("uid2.vertx_service_instances", () -> vertxServiceInstances)
Gauge.builder("uid2.vertx_service_instances", () -> VERTX_SERVICE_INSTANCES)
.description("gauge for number of vertx service instances requested")
.register(Metrics.globalRegistry);
}
Expand All @@ -225,7 +228,6 @@ private static void createVertxEventLoopsMetric() {
.register(Metrics.globalRegistry);
}


/*
private static CommandLine parseArgs(String[] args) {
final CLI cli = CLI.create("uid2-core")
Expand Down