Skip to content

Commit ec92625

Browse files
mtoffl01mcculls
andauthored
Enforce size limit on application_monitoring.yaml files (#8789)
* migrate CLIHelper to use a Map, parse system properties in key-vals * Fix processTemplateVar logic to remove superfluous ':' * nits: javadocs * Move writeFileRaw helper to testutils * Add YamlParser tests * Revert CLIHelper to use List<String>; lazily load a Map cache for querying key-vals * reuse logic for adding to the vm args cache * apply github suggestions * Move processTemplate yaml helper fns into StableConfigParser, along with tests * Remove changes to CLIHelper; rely on System.getProperty instead * optimize: return from processTemplate early if no {{ found * Add more test coverage to StableConfigParserTest * Optimize template processing to reduce use of substrings * Introduce constants for repeated strings * Change UNDEFINED_VALUE to empty string * Add log messages for empty environment_variable and process_argument values in template variable * Remove FileUtils and all its references * initial file size limit implementation: phase 1 limit only * Implement file size limit + tests * remove unrelated changes * Merge master & remove getMaxFileSizeBytes helper; give MAX_FILE_SIZE_BYTES default access --------- Co-authored-by: Stuart McCulloch <stuart.mcculloch@datadoghq.com>
1 parent f19b3dc commit ec92625

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

internal-api/src/main/java/datadog/trace/bootstrap/config/provider/StableConfigParser.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.io.IOException;
88
import java.nio.charset.StandardCharsets;
99
import java.nio.file.Files;
10+
import java.nio.file.Path;
1011
import java.nio.file.Paths;
1112
import java.util.Collections;
1213
import java.util.LinkedHashMap;
@@ -21,6 +22,7 @@ public class StableConfigParser {
2122

2223
private static final String ENVIRONMENT_VARIABLES_PREFIX = "environment_variables['";
2324
private static final String PROCESS_ARGUMENTS_PREFIX = "process_arguments['";
25+
static final int MAX_FILE_SIZE_BYTES = 256 * 1024; // 256 KB in bytes;
2426
private static final String UNDEFINED_VALUE = "";
2527

2628
/**
@@ -39,7 +41,19 @@ public class StableConfigParser {
3941
*/
4042
public static StableConfigSource.StableConfig parse(String filePath) throws IOException {
4143
try {
42-
String content = new String(Files.readAllBytes(Paths.get(filePath)), StandardCharsets.UTF_8);
44+
Path path = Paths.get(filePath);
45+
46+
// If file is over size limit, drop
47+
if (Files.size(path) > MAX_FILE_SIZE_BYTES) {
48+
log.warn(
49+
"Configuration file {} exceeds max size {} bytes; dropping.",
50+
filePath,
51+
MAX_FILE_SIZE_BYTES);
52+
return StableConfigSource.StableConfig.EMPTY;
53+
}
54+
55+
String content = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
56+
4357
String processedContent = processTemplate(content);
4458
Object parsedYaml = YamlParser.parse(processedContent);
4559
StableConfig data = new StableConfig(parsedYaml);

internal-api/src/test/groovy/datadog/trace/bootstrap/config/provider/StableConfigParserTest.groovy

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,50 @@ apm_configuration_rules:
184184
Files.delete(filePath)
185185
}
186186

187+
def "test file over max size"() {
188+
when:
189+
Path filePath = Files.createTempFile("testFile_", ".yaml")
190+
if (filePath == null) {
191+
throw new AssertionError("Failed to create test file")
192+
}
193+
194+
// Create a file with valid contents, but bigger than MAX_FILE_SIZE_BYTES
195+
String baseYaml = """
196+
config_id: 12345
197+
apm_configuration_default:
198+
KEY_ONE: "value_one"
199+
apm_configuration_rules:
200+
"""
201+
String builderYaml = """
202+
- selectors:
203+
- origin: language
204+
matches: ["Java"]
205+
operator: equals
206+
configuration:
207+
KEY_TWO: "value_two"
208+
"""
209+
String bigYaml = baseYaml
210+
while(bigYaml.size() < StableConfigParser.MAX_FILE_SIZE_BYTES) {
211+
bigYaml += builderYaml
212+
}
213+
214+
try {
215+
Files.write(filePath, bigYaml.getBytes())
216+
} catch (IOException e) {
217+
throw new AssertionError("Failed to write to file: ${e.message}")
218+
}
219+
220+
StableConfigSource.StableConfig cfg
221+
try {
222+
cfg = StableConfigParser.parse(filePath.toString())
223+
} catch (Exception e) {
224+
throw new AssertionError("Failed to parse the file: ${e.message}")
225+
}
226+
227+
then:
228+
cfg == StableConfigSource.StableConfig.EMPTY
229+
}
230+
187231
def "test processTemplate valid cases"() {
188232
when:
189233
if (envKey != null) {

0 commit comments

Comments
 (0)