-
Notifications
You must be signed in to change notification settings - Fork 264
Mini runtime release env #3250
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
base: feature/mini-runtime-release
Are you sure you want to change the base?
Mini runtime release env #3250
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package com.akto.hybrid_runtime; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import com.akto.dto.deployment.DeploymentConfig; | ||
| import com.akto.dto.deployment.EnvVariable; | ||
|
|
||
| public final class EnvConfig { | ||
|
|
||
| private static final Map<String, String> envMap = new HashMap<>(); | ||
|
|
||
| private EnvConfig() {} | ||
|
|
||
| public static void hydrate(DeploymentConfig deploymentConfig) { | ||
| envMap.clear(); | ||
| if (deploymentConfig == null) return; | ||
| List<EnvVariable> vars = deploymentConfig.getEnvVars(); | ||
| if (vars == null) return; | ||
| for (EnvVariable v : vars) { | ||
| if (v == null || v.getKey() == null) continue; | ||
| envMap.put(v.getKey(), v.getValue()); | ||
| } | ||
| } | ||
|
|
||
| public static String get(String key, String defaultValue) { | ||
| String v = envMap.get(key); | ||
| if (v != null) return v; | ||
| String sys = System.getenv(key); | ||
| return sys != null ? sys : defaultValue; | ||
| } | ||
|
|
||
| public static Map<String, String> snapshot() { | ||
| return Collections.unmodifiableMap(envMap); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -77,6 +77,23 @@ private static void printL(Object o) { | |||||||||
|
|
||||||||||
| static boolean isDashboardInstance = false; | ||||||||||
|
|
||||||||||
| private static void initEnvConfig() { | ||||||||||
| try { | ||||||||||
| String deploymentId = System.getenv("DEPLOYMENT_ID"); | ||||||||||
| if (deploymentId == null || deploymentId.isEmpty()) return; | ||||||||||
| com.akto.dto.deployment.DeploymentConfig dc = dataActor.fetchDeploymentConfig(deploymentId); | ||||||||||
| EnvConfig.hydrate(dc); | ||||||||||
| try { | ||||||||||
| Map<String, String> snapshot = EnvConfig.snapshot(); | ||||||||||
| dataActor.sendDeploymentConfig(deploymentId, snapshot); | ||||||||||
| } catch (Exception e) { | ||||||||||
| loggerMaker.errorAndAddToDb(e, "Failed to send deployment config after hydrate"); | ||||||||||
| } | ||||||||||
| } catch (Exception e) { | ||||||||||
| loggerMaker.errorAndAddToDb(e, "initEnvConfig failed: " + e.getMessage()); | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| public static boolean tryForCollectionName(String message) { | ||||||||||
| boolean ret = false; | ||||||||||
| try { | ||||||||||
|
|
@@ -253,6 +270,7 @@ public static String getLogTopicName() { | |||||||||
|
|
||||||||||
| // REFERENCE: https://www.oreilly.com/library/view/kafka-the-definitive/9781491936153/ch04.html (But how do we Exit?) | ||||||||||
| public static void main(String[] args) { | ||||||||||
|
||||||||||
| public static void main(String[] args) { | |
| public static void main(String[] args) { | |
| // Initialize dataActor before calling initEnvConfig | |
| dataActor = DataActorFactory.createDataActor(); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package com.akto.dto.deployment; | ||
|
|
||
| import org.bson.codecs.pojo.annotations.BsonId; | ||
| import com.akto.dao.context.Context; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
| import lombok.Setter; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Getter | ||
| @Setter | ||
| @NoArgsConstructor | ||
| public class DeploymentConfig { | ||
|
|
||
| @BsonId | ||
| private String id; | ||
| private String name; | ||
| private String type; | ||
| private List<EnvVariable> envVars; | ||
| private int createdTs; | ||
| private int lastUpdatedTs; | ||
|
|
||
| public static final String ID = "_id"; | ||
| public static final String NAME = "name"; | ||
| public static final String TYPE = "type"; | ||
| public static final String ENV_VARS = "envVars"; | ||
| public static final String CREATED_TS = "createdTs"; | ||
| public static final String LAST_UPDATED_TS = "lastUpdatedTs"; | ||
|
|
||
| public DeploymentConfig(String id, String name, String type, List<EnvVariable> envVars) { | ||
| this.id = id; | ||
| this.name = name; | ||
| this.type = type; | ||
| this.envVars = envVars; | ||
| this.createdTs = Context.now(); | ||
| this.lastUpdatedTs = Context.now(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package com.akto.dto.deployment; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
| import lombok.Setter; | ||
|
|
||
| @Getter | ||
| @Setter | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| public class EnvVariable { | ||
| private String key; | ||
| private String value; | ||
| private boolean editable; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -114,6 +114,44 @@ public static String buildDbAbstractorUrl() { | |
| return dbAbsHost + "/api"; | ||
| } | ||
|
|
||
| @Override | ||
| public DeploymentConfig fetchDeploymentConfig(String deploymentId) { | ||
| Map<String, List<String>> headers = buildHeaders(); | ||
| String endpoint = url + "/fetchDeploymentConfig?deploymentId=" + deploymentId; | ||
|
||
| OriginalHttpRequest request = new OriginalHttpRequest(endpoint, "", "GET", null, headers, ""); | ||
| try { | ||
| OriginalHttpResponse response = ApiExecutor.sendRequestBackOff(request, true, null, false, null); | ||
| if (response.getStatusCode() != 200 || response.getBody() == null) { | ||
| loggerMaker.errorAndAddToDb("non 2xx in fetchDeploymentConfig", LoggerMaker.LogDb.RUNTIME); | ||
| return null; | ||
| } | ||
| BasicDBObject payloadObj = BasicDBObject.parse(response.getBody()); | ||
| BasicDBObject dcObj = (BasicDBObject) payloadObj.get("deploymentConfig"); | ||
| if (dcObj == null) return null; | ||
| return objectMapper.readValue(dcObj.toJson(), DeploymentConfig.class); | ||
| } catch (Exception e) { | ||
| loggerMaker.errorAndAddToDb("error in fetchDeploymentConfig " + e, LoggerMaker.LogDb.RUNTIME); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void sendDeploymentConfig(String deploymentId, Map<String, String> envVars) { | ||
| Map<String, List<String>> headers = buildHeaders(); | ||
| BasicDBObject obj = new BasicDBObject(); | ||
| obj.put("deploymentId", deploymentId); | ||
| obj.put("envVars", envVars); | ||
| OriginalHttpRequest request = new OriginalHttpRequest(url + "/sendDeploymentConfig", "", "POST", obj.toString(), headers, ""); | ||
| try { | ||
| OriginalHttpResponse response = ApiExecutor.sendRequestBackOff(request, true, null, false, null); | ||
| if (response.getStatusCode() != 200) { | ||
| loggerMaker.errorAndAddToDb("non 2xx in sendDeploymentConfig", LoggerMaker.LogDb.RUNTIME); | ||
| } | ||
| } catch (Exception e) { | ||
| loggerMaker.errorAndAddToDb("error in sendDeploymentConfig " + e, LoggerMaker.LogDb.RUNTIME); | ||
| } | ||
| } | ||
|
|
||
| public AccountSettings fetchAccountSettings() { | ||
| AccountSettings acc = null; | ||
| for (int i=0; i < 5; i++) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The envMap is not thread-safe and could cause race conditions when accessed concurrently with hydrate() method. Consider using ConcurrentHashMap or adding synchronization.