Skip to content

Bug: UserAgentConfigurator does not detect Powertools version when packaged as JAR #1812

Closed
@phipag

Description

@phipag

The UserAgentConfigurator does not correctly detect the Powertools version from the version.properties file when packaged as a .jar.

Expected Behavior

https://github.com/aws-powertools/powertools-lambda-java/blob/main/powertools-core/src/main/java/software/amazon/lambda/powertools/core/internal/UserAgentConfigurator.java#L67-L84

    static String getVersionFromProperties(String propertyFileName, String versionKey) {


        URL propertiesFileURI = Thread.currentThread().getContextClassLoader().getResource(propertyFileName);
        if (propertiesFileURI != null) {
            try (FileInputStream fis = new FileInputStream(propertiesFileURI.getPath())) {
                Properties properties = new Properties();
                properties.load(fis);
                String version = properties.getProperty(versionKey);
                if (version != null && !version.isEmpty()) {
                    return version;
                }
            } catch (IOException e) {
                LOG.warn("Unable to read {} file. Using default version.", propertyFileName);
                LOG.debug("Exception:", e);
            }
        }
        return NA;
    }

This code should return the Powertools version according to the transformed version.properties file. But it returns "NA". The reason for this is that files from a packaged .jar cannot be read as regular files. They need to be read using

Possible Solution

This was fixed in v2 as part of another PR to add GraalVM support. The code should use .getResourceAsStream().

    static String getVersionFromProperties(String propertyFileName, String versionKey) {


        InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(propertyFileName);


        if (is != null) {
            try {
                Properties properties = new Properties();
                properties.load(is);
                String version = properties.getProperty(versionKey);
                if (version != null && !version.isEmpty()) {
                    return version;
                }
            } catch (IOException e) {
                LOG.warn("Unable to read {} file. Using default version.", propertyFileName);
                LOG.debug("Exception:", e);
            }
        }
        return NA;
    }

Steps to Reproduce (for bugs)

See discussion: #1809

Environment

  • Powertools for AWS Lambda (Java) version used: 1.20.0
  • Packaging format (Layers, Maven/Gradle): Both maven and gradle
  • AWS Lambda function runtime: Java17, Java21

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Status

Coming soon

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions