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

fix: crash due to config packages key with missing value #289

Merged
merged 2 commits into from
Aug 21, 2024

Conversation

zermelo-wisen
Copy link
Contributor

@zermelo-wisen zermelo-wisen commented Aug 18, 2024

Fixes #285.

This PR addresses an issue where the AppMap agent crashes due to a malformed configuration file, specifically when the packages key value is missing. The changes ensure that such scenarios are handled gracefully, avoiding null pointer exceptions.

  • Treat empty 'packages:' entry as empty array to prevent NPE. Ensures consistent behavior with when no 'packages:' entry or 'packages: []' is specified.
  • Log and return null instead of System.exit(1) on 'load', since System.exit(1) is already done in 'initialize.'

Added tests to verify:

  • Loading config with 'packages' key having no value.
  • Error message for scalar 'packages' value.

Changes in AppMapConfig.java

ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
try {
  singleton = mapper.readValue(inputStream, AppMapConfig.class);
  // If an empty "package:" entry is specified, treat it as an empty
  // array to make the behavior consistent with cases where there is no
  // "package:" entry or where "package: []" is specified.
  if (singleton.packages == null)
      singleton.packages = new AppMapPackage[0];
} catch (IOException e) {
  logger.error("AppMap: encountered syntax error in appmap.yml {}", e.getMessage());
  return null;
}
singleton.configFile = configFile;
logger.debug("config: {}", singleton);
  1. Handling Empty packages: Entry

    • The code now checks if the packages field is null after reading the appmap.yml file.
    • If packages is null, it is set to an empty array (AppMapPackage[0]).
    • This ensures consistent behavior when there is no packages: entry or when packages: [] is specified explicitly, preventing null pointer exceptions.
  2. Error Handling Improvement

    • Instead of calling System.exit(1) 'load', the method now logs the error and returns null, since 'System.exit(1)' is already called in 'initialize' when the load returns null.

Changes in AppMapConfigTest.java

  1. New Tests for packages Key
    • Added a test loadPackagesKeyWithMissingValue to verify that the configuration loads correctly when the packages key is present but has no value.
    • Added a test loadPackagesKeyWithScalarValue to verify that an appropriate error message is logged when the packages key has a scalar value instead of an array.
  • Test for Missing Values:

@Test
public void loadPackagesKeyWithMissingValue() throws Exception {
  Path configFile = tmpdir.resolve("appmap.yml");
  final String contents = "name: test\npackages:\npath: xyz";
  Files.write(configFile, contents.getBytes());
  AppMapConfig.load(configFile, true);
}
  • Test for Scalar Values:

@Test
public void loadPackagesKeyWithScalarValue() throws Exception {
  Path configFile = Files.createTempFile("appmap", ".yml");
  final String contents = "name:q test\npackages: abc\n";
  Files.write(configFile, contents.getBytes());
  String actualErr = tapSystemErr(() -> AppMapConfig.load(configFile, false));
  assertTrue(actualErr.contains("AppMap: encountered syntax error in appmap.yml"));
}

These tests ensure that the configuration loading behaves as expected for various malformed packages key scenarios, preventing crashes similar to the one reported.

@zermelo-wisen zermelo-wisen force-pushed the fix/config-packages-key-missing-value branch 3 times, most recently from 80f3d3a to 31b3961 Compare August 18, 2024 10:50
@apotterri
Copy link
Contributor

I don't think this is the right way to handle this case. Specifying a null value for package is an error. Instead of crashing with an NPE, the agent should detect this error and show the user an informative message.

Copy link
Contributor

@apotterri apotterri left a comment

Choose a reason for hiding this comment

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

See my comment.

- Treat empty 'packages:' entry as empty array to prevent NPE.
  Ensures consistent behavior with when no 'package:' entry or
  'package: []' is specified.
- Log and return null instead of System.exit(1) on 'load',
  since System.exit(1) is already done in 'initialize'

Added tests to verify:
- Loading config with 'packages' key having no value.
- Error message for scalar 'packages' value.
@zermelo-wisen zermelo-wisen force-pushed the fix/config-packages-key-missing-value branch from 31b3961 to 26714d8 Compare August 20, 2024 12:58
@zermelo-wisen
Copy link
Contributor Author

zermelo-wisen commented Aug 20, 2024

I don't think this is the right way to handle this case. Specifying a null value for package is an error. Instead of crashing with an NPE, the agent should detect this error and show the user an informative message.

Actually, I reviewed the behavior and made it consistent with how the agent handles the absence of a packages entry or when it is specified as an empty array (packages: []). Now I changed it to show an informative error message and quit.

@apotterri apotterri merged commit 34059ae into master Aug 21, 2024
6 checks passed
@apotterri apotterri deleted the fix/config-packages-key-missing-value branch August 21, 2024 13:10
@appland-release
Copy link

🎉 This issue has been resolved in version 1.26.10 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Better handling of malformed config files
3 participants