Skip to content

Enforce size limit on application_monitoring.yaml files #8789

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

Draft
wants to merge 22 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
d112dc7
migrate CLIHelper to use a Map, parse system properties in key-vals
mtoffl01 Apr 10, 2025
3ba99af
Fix processTemplateVar logic to remove superfluous ':'
mtoffl01 Apr 16, 2025
9caeb5d
nits: javadocs
mtoffl01 Apr 17, 2025
d0f194b
Move writeFileRaw helper to testutils
mtoffl01 Apr 21, 2025
457c288
Add YamlParser tests
mtoffl01 Apr 21, 2025
06ae9ac
Revert CLIHelper to use List<String>; lazily load a Map cache for que…
mtoffl01 Apr 29, 2025
06712dc
reuse logic for adding to the vm args cache
mtoffl01 Apr 29, 2025
0a1da8f
apply github suggestions
mtoffl01 May 1, 2025
e106416
Move processTemplate yaml helper fns into StableConfigParser, along w…
mtoffl01 May 1, 2025
48adeaa
Remove changes to CLIHelper; rely on System.getProperty instead
mtoffl01 May 1, 2025
24b8615
optimize: return from processTemplate early if no {{ found
mtoffl01 May 1, 2025
cc76954
Add more test coverage to StableConfigParserTest
mtoffl01 May 1, 2025
afddf95
Merge branch 'master' into mtoff/scfg_fix
mtoffl01 May 1, 2025
fc4652c
Optimize template processing to reduce use of substrings
mcculls May 2, 2025
e633f26
Introduce constants for repeated strings
mcculls May 2, 2025
b5deffd
Change UNDEFINED_VALUE to empty string
mtoffl01 May 2, 2025
9104f0d
Add log messages for empty environment_variable and process_argument …
mtoffl01 May 2, 2025
ecb5a15
Remove FileUtils and all its references
mtoffl01 May 5, 2025
62388b3
initial file size limit implementation: phase 1 limit only
mtoffl01 May 6, 2025
7b7dbdd
Implement file size limit + tests
mtoffl01 May 8, 2025
b7f224b
merge wth main, resolve conflicts
mtoffl01 May 8, 2025
6bcada2
remove unrelated changes
mtoffl01 May 8, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -21,6 +22,7 @@ public class StableConfigParser {

private static final String ENVIRONMENT_VARIABLES_PREFIX = "environment_variables['";
private static final String PROCESS_ARGUMENTS_PREFIX = "process_arguments['";
private static final int MAX_FILE_SIZE_BYTES = 256 * 1024; // 256 KB in bytes;
private static final String UNDEFINED_VALUE = "UNDEFINED";

/**
Expand All @@ -39,7 +41,19 @@ public class StableConfigParser {
*/
public static StableConfigSource.StableConfig parse(String filePath) throws IOException {
try {
String content = new String(Files.readAllBytes(Paths.get(filePath)), StandardCharsets.UTF_8);
Path path = Paths.get(filePath);

// If file is over size limit, drop
if (Files.size(path) > MAX_FILE_SIZE_BYTES) {
log.warn(
"Configuration file {} exceeds max size {} bytes; dropping.",
filePath,
MAX_FILE_SIZE_BYTES);
return StableConfigSource.StableConfig.EMPTY;
}

String content = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);

String processedContent = processTemplate(content);
StableConfigYaml data = YamlParser.parse(processedContent, StableConfigYaml.class);

Expand Down Expand Up @@ -268,4 +282,8 @@ private static String processTemplateVar(String templateVar) throws IOException
return UNDEFINED_VALUE;
}
}

static int getMaxFileSizeBytes() {
return MAX_FILE_SIZE_BYTES;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,50 @@ apm_configuration_rules:
Files.delete(filePath)
}

def "test file over max size"() {
when:
Path filePath = Files.createTempFile("testFile_", ".yaml")
if (filePath == null) {
throw new AssertionError("Failed to create test file")
}

// Create a file with valid contents, but bigger than MAX_FILE_SIZE_BYTES
String baseYaml = """
config_id: 12345
apm_configuration_default:
KEY_ONE: "value_one"
apm_configuration_rules:
"""
String builderYaml = """
- selectors:
- origin: language
matches: ["Java"]
operator: equals
configuration:
KEY_TWO: "value_two"
"""
String bigYaml = baseYaml
while(bigYaml.size() < StableConfigParser.getMaxFileSizeBytes()) {
bigYaml += builderYaml
}

try {
Files.write(filePath, bigYaml.getBytes())
} catch (IOException e) {
throw new AssertionError("Failed to write to file: ${e.message}")
}

StableConfigSource.StableConfig cfg
try {
cfg = StableConfigParser.parse(filePath.toString())
} catch (Exception e) {
throw new AssertionError("Failed to parse the file: ${e.message}")
}

then:
cfg == StableConfigSource.StableConfig.EMPTY
}

def "test processTemplate valid cases"() {
when:
if (envKey != null) {
Expand Down
Loading