vert-x3 / vertx-config

Vert.x Configuration Service
Apache License 2.0
54 stars 64 forks source link

Define list of variables to accept from environment #31

Closed Stwissel closed 6 years ago

Stwissel commented 6 years ago

Problem: Reading all environment variables might "pollute" the configuration with values that are meant for other applications. Most notably the $PORT variable used in containers for the main web server.

Potential solution:

final JsonObject envConfig = new JsonObject().put("keys", new JsonArray("[\"color\",\"taste\"]"));
final ConfigStoreOptions env = new ConfigStoreOptions().setType("env").setConfig(envConfig);

This requires to overwrite https://github.com/vert-x3/vertx-config/blob/master/vertx-config/src/main/java/io/vertx/config/impl/spi/EnvVariablesConfigStore.java like this:

public ConfigStore create(Vertx vertx, JsonObject configuration) {
   return new EnvVariablesConfigStore(
      configuration.getBoolean("raw-data", false),
      configuration.getJsonArray("keys"));
 }

public EnvVariablesConfigStore(boolean rawData, JsonArray keys) {
    this.rawData = rawData;
    this.keys = (keys == null) ? null : keys.getList();
  }

  private static JsonObject all(Map<String, String> env, boolean rawData) {
    JsonObject json = new JsonObject();
    List localKeys = (this.keys == null) ? env.keySet() : this.keys;
    env.entrySet().stream()
        .forEach(entry -> {
              if (localKeys.contains(entry.getKey()) {
                  JsonObjectHelper.put(json, entry.getKey(), entry.getValue(), rawData)
             }
        }
     );
    return json;
  }

Something along that lines

Stwissel commented 6 years ago

Awesome, thx a lot!