Skip to content
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

Add support for environment variables in config.pb.json strings #41

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,8 @@ maven_jar(
name = "google_gson_artifact",
artifact = "com.google.code.gson:gson:2.6.2",
)

maven_jar(
name = "apache_commons_lang3_artifact",
artifact = "org.apache.commons:commons-lang3:3.4",
)
1 change: 1 addition & 0 deletions src/main/java/me/dinowernli/grpc/polyglot/config/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ java_library(
"//src/main/proto:config_proto",
"//src/main/java/me/dinowernli/grpc/polyglot/protobuf",
"//third_party/args4j",
"//third_party/commons-lang",
"//third_party/grpc",
"//third_party/guava",
"//third_party/netty",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.protobuf.Descriptors;
import com.google.protobuf.Message;
import com.google.protobuf.util.JsonFormat;

import org.apache.commons.lang3.text.StrSubstitutor;

import polyglot.ConfigProto.Configuration;
import polyglot.ConfigProto.ConfigurationSet;
import polyglot.ConfigProto.OutputConfiguration.Destination;
Expand Down Expand Up @@ -53,13 +58,58 @@ public static ConfigurationLoader forFile(Path configFile) {
ConfigurationSet.Builder configSetBuilder = ConfigurationSet.newBuilder();
String fileContent = Joiner.on('\n').join(Files.readAllLines(configFile));
JsonFormat.parser().merge(fileContent, configSetBuilder);
expandEnvironmentVariables(configSetBuilder);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am a bit worried that this replacing might be... overly thorough. Is there a chance we could just do the replacement on a whitelisted set of fields?

Then, a small indicator in the proto file that this happens would be good.

return ConfigurationLoader.forConfigSet(configSetBuilder.build());
} catch (IOException e) {
throw new RuntimeException("Unable to read config file: " + configFile.toString(), e);
}
}

@VisibleForTesting
/**
* Expand all references to environment variables occurring in string fields (arbitrarily nested)
* in the given Protobuf message builder.
*/
private static void expandEnvironmentVariables(Message.Builder builder) {
expandVariables(builder, System.getenv());
}

/**
* Expand all references to variables (using the dollar and curly braces syntax) occurring in
* string fields (arbitrarily nested) in the given Protobuf message builder, using a given map
* of variable names to values.
*/
private static void expandVariables(Message.Builder builder, Map<String, String> vars) {
for (Map.Entry<Descriptors.FieldDescriptor, Object> field : builder.getAllFields().entrySet()) {
Descriptors.FieldDescriptor desc = field.getKey();
Object value = field.getValue();
switch (desc.getType()) {
case STRING:
if (desc.isRepeated()) {
int index = 0;
for (Object item : (List)value) {
builder.setRepeatedField(desc, index, StrSubstitutor.replace((String)item, vars));
++index;
}
} else {
builder.setField(desc, StrSubstitutor.replace((String)value, vars));
}
break;
case MESSAGE:
case GROUP:
if (desc.isRepeated()) {
int count = builder.getRepeatedFieldCount(desc);
for (int i = 0; i < count; ++i) {
expandVariables(builder.getRepeatedFieldBuilder(desc, i), vars);
}
} else {
expandVariables(builder.getFieldBuilder(desc), vars);
}
break;
}
}
}

@VisibleForTesting
ConfigurationLoader(Optional<ConfigurationSet> configSet, Optional<CommandLineArgs> overrides) {
this.configSet = configSet;
this.overrides = overrides;
Expand Down
9 changes: 9 additions & 0 deletions third_party/commons-lang/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package(default_visibility = ["//visibility:public"])

java_library(
name = "commons-lang",
exports = [
"@apache_commons_lang3_artifact//jar",
],
licenses = ["permissive"],
)