Version 2.3.0 incompatible with SpringBoot - KmsMasterKey class does not load
Problem:
VersionInfo#loadUserAgent tries to load a version from a file, this fails catastrophically when this library is packed with the spring-boot-maven-plugin: the result of ClassLoader.getSystemResourceAsStream("project.properties") is null, triggering a NullPointerException which is not handled, which results in the following error for me.
java.lang.NoClassDefFoundError: Could not initialize class com.amazonaws.encryptionsdk.kms.KmsMasterKey
| public final class KmsMasterKey extends MasterKey<KmsMasterKey> implements KmsMethods { | |
| private static final String USER_AGENT = VersionInfo.loadUserAgent(); |
| public static String loadUserAgent() { | |
| try { | |
| final Properties properties = new Properties(); | |
| properties.load(ClassLoader.getSystemResourceAsStream("project.properties")); | |
| return USER_AGENT_PREFIX + properties.getProperty("version"); | |
| } catch (final IOException ex) { | |
| return USER_AGENT_PREFIX + UNKNOWN_VERSION; | |
| } | |
| } |
Works fine directly (IntelliJ run), but debugging the app within my docker-build clearly shows the issue:
application.jar packed with
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.4.2</version> <!-- EDIT: also tested latest = 2.5.1 --> </plugin>
Solution(s):
- Handle a null resource stream, and/or catch NullPointerExceptions here as well as IOExceptions.
- Or simply hard-code the version directly in this file rather than delegating to a properties resource?
- Use a classLoader from the same jar, e.g., the following works in my debugger:
properties.load(VersionInfo.class.getClassLoader().getResourceAsStream("project.properties"));

