Skip to content

Convert malformed opaque URLs to hierarchical form - #4820

Merged
am312 merged 11 commits into
masterfrom
andrew.munn/uri-opaque
Mar 6, 2023
Merged

am312 merged 11 commits into
masterfrom
andrew.munn/uri-opaque

Conversation

@am312

@am312 am312 commented Mar 1, 2023 •

Copy link
Copy Markdown
Contributor

What Does This Do

if telemetry.convert-opaque-uri.enabled is set, opaque URLs will be converted to hierarchal by prepending the missing '/'.

Handle opaque URIs in telemetry.

Motivation

This work-around addresses malformed Windows URLs which can cause errors like:
datadog.telemetry.dependency.DependencyResolver - Failed to determine dependency for uri file:C:/lib.jar. java.lang.IllegalArgumentException: URI is not hierarchical

@am312
am312 requested a review from a team as a code owner March 1, 2023 07:06
@am312 am312 changed the title Andrew.munn/uri opaque Convert malformed opaque URLs to hierarchical form Mar 1, 2023
public static final String TELEMETRY_DEPENDENCY_COLLECTION_ENABLED =
"telemetry.dependency-collection.enabled";
public static final String TELEMETRY_CONVERT_OPAQUE_URI_ENABLED =
"telemetry.convert-opaque-uri.enabled";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm not sure this warrants its own feature-flag, given that we know the opaque case leads to an exception.

BTW, have you considered an alternative fix to the DependencyResolver - when it tries to turn the URI into a file it could fall back to using the path for opaque URLs?

@am312 am312 Mar 1, 2023 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed about the flag if the opaque case is never valid. My concern with going without a flag was that if the path was relative and everything was working fine, prepending an unneeded "/" to make URIs hierarchical might break something for the user. The anticipated usage is just selectively when there is a problem and the behavior should not roll out as a new default for users where everything is working ok right now.

In DependencyResolver I could just catch any IllegalArgumentException containing URI is not hierarchical in its message and then convert+retry the new File op again using the updated URI. If that also does not work then rethrow the original exception. Sound ok using that default behavior without a flag around it?

@am312 am312 Mar 1, 2023 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Actually I think the flag should stay because if "fixing" it for everybody causes some previously ignored opaque URL to suddenly start working it could override a dep and change the behavior in some unexpected, undesired way.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't think that's a valid case though - not handling opaque file URLs properly and causing an exception is a bug, it's not something that we need a flag for. If an opaque file URL can be mapped to a file by using its path then I see that as improving the resolver logic. That's why I think the fix really should be in the resolver when it tries to convert the URL to a file.

@mcculls mcculls left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you remove the flag and also move this fix to DependencyResolver - ie. when it tries to convert the URI to a file, handle opaque URIs by using the path. If that resolves to a file then we know that conversion is ok. (You can either check the URI is opaque upfront or use a try...catch with the path conversion as a fallback for any situation where the whole URI doesn't map to a file.)

@ValentinZakharov

Copy link
Copy Markdown
Contributor

I like this approach, but may be we can just get path from uri instead of manipulating with the string directly in the resolver?
Something like this:

static List<Dependency> extractDependenciesFromURI(URI uri) {
    ...
    File f;
    if ("file".equals(uri.getScheme())) {
        if (uri.isOpaque()) {
            f = new File(uri.getPath());
        } else {
            f = new File(uri);
        }
    }
    dependencies = extractDependenciesFromJar(f);
    ...
}

Also, according to rfc8089 the windows local file representation like file:c:/path/to/file is valid. So that looks like a bug in the File, which brings me to idea that we should not touch uri.

@am312
am312 marked this pull request as draft March 3, 2023 04:45
@am312

am312 commented Mar 3, 2023 •

Copy link
Copy Markdown
Contributor Author

if uri.isOpaque() == true then uri.getPath() == null

but we could do this which I agree is better than manipulating the string:

if (uri.isOpaque()) {
    f = new File(uri.getSchemeSpecificPart());
}...

but doing this without a flag means potentially changing runtime behavior of users' deployed applications. I think it's correcting the behavior but skipping opaque URIs could be relied on as a feature by someone.

@mcculls

mcculls commented Mar 3, 2023 •

Copy link
Copy Markdown
Contributor

@am312 using getSchemeSpecificPart in the DependencyResolver makes sense for opaque URLs.

Re. adding a flag - the dependency resolution feature is to provide users with more information about their deployments - it only reports dependencies that resolve to something, such as a file. If they have an opaque code-location URL that's opaque and doesn't resolve to a file then that will not be reported, so that behaviour has not changed. If they have an opaque code-source location that can be resolved to a file then previously that information was missing, by fixing this we're now providing more complete information - that's why I don't think we need a flag to revert to the previous incomplete behaviour. (Since if they're not interested in dependency information then there's already a flag to turn it off completely.)

@am312

am312 commented Mar 6, 2023

Copy link
Copy Markdown
Contributor Author

The updated version works but it's tricky to add really meaningful tests for Windows files when we don't build on Windows.

@am312
am312 marked this pull request as ready for review March 6, 2023 17:47
@am312
am312 merged commit 6f4b40f into master Mar 6, 2023
@am312
am312 deleted the andrew.munn/uri-opaque branch March 6, 2023 17:48
@github-actions github-actions Bot added this to the 1.11.0 milestone Mar 6, 2023
@bantonsson bantonsson modified the milestones: 1.11.0, new 1.11.0 Mar 17, 2023
@bantonsson bantonsson added the comp: telemetry Telemetry label Mar 28, 2023
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.

4 participants