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

[Bug] Unable to interact with short form custom target #1867

Open
cmah88 opened this issue Jan 25, 2024 · 7 comments
Open

[Bug] Unable to interact with short form custom target #1867

cmah88 opened this issue Jan 25, 2024 · 7 comments
Labels
bug Something isn't working

Comments

@cmah88
Copy link

cmah88 commented Jan 25, 2024

Current Behavior

java.net.URISyntaxException: Expected scheme-specific part at index 10: localhost: caused by URISyntaxException: Expected scheme-specific part at index 10: localhost:

image

image

Expected Behavior

No response

Steps To Reproduce

No response

Environment

- OS: 
- Environment: 
- Version:

Anything else?

No response

@cmah88 cmah88 added bug Something isn't working needs-triage Needs thorough attention from code reviewers labels Jan 25, 2024
@andrewazores
Copy link
Member

Creating a custom target with localhost:0 results in a target discovery notification, but also immediately an error notification with the 500 response.

It may actually be that the 500 response is occurring in response to the request to list event templates, not the target creation. So perhaps the exception is really happening when Cryostat tries to use this custom target definition to open a JMX connection - is it no longer looking at these "short form" connection URLs and trying to format them into service:jmx:rmi: JMX URLs?

@andrewazores andrewazores added question Further information is requested and removed needs-triage Needs thorough attention from code reviewers labels Jan 25, 2024
@andrewazores
Copy link
Member

@Josh-Matsuoka @aali309 @mwangggg could one of you take a look at this next week?

@aali309
Copy link
Contributor

aali309 commented Jan 31, 2024

Creating a custom target with localhost:0 results in a target discovery notification, but also immediately an error notification with the 500 response.

It may actually be that the 500 response is occurring in response to the request to list event templates, not the target creation. So perhaps the exception is really happening when Cryostat tries to use this custom target definition to open a JMX connection - is it no longer looking at these "short form" connection URLs and trying to format them into service:jmx:rmi: JMX URLs?

Yes, that could be the reason. Target is being created and discovered but listing event templates produces
Failed to load resource: the server responded with a status of 500 (Internal Server Error) https://localhost:8181/api/v1/targets/localhost%3A0/templates

@andrewazores
Copy link
Member

Looks like the root cause is here. This should catch URISyntaxException, not only MalformedURLException. This way if the connection attempt using the raw short-form string fails for that reason it will try to create the full service:jmx:rmi URL using the short form.

@andrewazores
Copy link
Member

Here's the server stack trace from when this happens:

SEVERE: HTTP 500: java.net.URISyntaxException: Expected scheme-specific part at index 10: localhost:
io.vertx.ext.web.handler.HttpException: Internal Server Error
Caused by: java.lang.IllegalStateException: java.net.URISyntaxException: Expected scheme-specific part at index 10: localhost:
	at io.cryostat.configuration.CredentialsManager.getCredentialsByTargetId(CredentialsManager.java:186)
	at io.cryostat.net.web.http.AbstractAuthenticatedRequestHandler.getConnectionDescriptorFromContext(AbstractAuthenticatedRequestHandler.java:142)
	at io.cryostat.net.web.http.api.v1.TargetRecordingsGetHandler.handleAuthenticated(TargetRecordingsGetHandler.java:103)
	at io.cryostat.net.web.http.AbstractAuthenticatedRequestHandler.handle(AbstractAuthenticatedRequestHandler.java:81)
	at io.cryostat.net.web.http.AbstractAuthenticatedRequestHandler.handle(AbstractAuthenticatedRequestHandler.java:51)
	at io.vertx.ext.web.impl.BlockingHandlerDecorator.lambda$handle$0(BlockingHandlerDecorator.java:48)
	at io.vertx.core.impl.ContextBase.lambda$null$0(ContextBase.java:137)
	at io.vertx.core.impl.ContextInternal.dispatch(ContextInternal.java:264)
	at io.vertx.core.impl.ContextBase.lambda$executeBlocking$1(ContextBase.java:135)
	at io.vertx.core.impl.TaskQueue.run(TaskQueue.java:76)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
	at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
	at java.base/java.lang.Thread.run(Thread.java:840)
Caused by: java.net.URISyntaxException: Expected scheme-specific part at index 10: localhost:
	at java.base/java.net.URI$Parser.fail(URI.java:2976)
	at java.base/java.net.URI$Parser.failExpecting(URI.java:2982)
	at java.base/java.net.URI$Parser.parse(URI.java:3182)
	at java.base/java.net.URI.<init>(URI.java:623)
	at org.apache.http.client.utils.URIBuilder.build(URIBuilder.java:146)
	at io.cryostat.configuration.CredentialsManager.getCredentialsByTargetId(CredentialsManager.java:177)
	... 13 more

@andrewazores
Copy link
Member

That was after defining a custom target with the short-form localhost:0 and then clicking on the Recordings navigation item in the UI. When clicking on Events it's similar:

SEVERE: HTTP 500: java.net.URISyntaxException: Expected scheme-specific part at index 10: localhost:
io.vertx.ext.web.handler.HttpException: Internal Server Error
Caused by: io.cryostat.net.web.http.api.v2.ApiException: java.net.URISyntaxException: Expected scheme-specific part at index 10: localhost:
	at io.cryostat.net.web.http.api.v2.AbstractV2RequestHandler.handle(AbstractV2RequestHandler.java:99)
	at io.cryostat.net.web.http.api.v2.AbstractV2RequestHandler.handle(AbstractV2RequestHandler.java:49)
	at io.vertx.ext.web.impl.BlockingHandlerDecorator.lambda$handle$0(BlockingHandlerDecorator.java:48)
	at io.vertx.core.impl.ContextBase.lambda$null$0(ContextBase.java:137)
	at io.vertx.core.impl.ContextInternal.dispatch(ContextInternal.java:264)
	at io.vertx.core.impl.ContextBase.lambda$executeBlocking$1(ContextBase.java:135)
	at io.vertx.core.impl.TaskQueue.run(TaskQueue.java:76)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
	at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
	at java.base/java.lang.Thread.run(Thread.java:840)
Caused by: java.lang.IllegalStateException: java.net.URISyntaxException: Expected scheme-specific part at index 10: localhost:
	at io.cryostat.configuration.CredentialsManager.getCredentialsByTargetId(CredentialsManager.java:186)
	at io.cryostat.net.web.http.api.v2.AbstractV2RequestHandler.getConnectionDescriptorFromParams(AbstractV2RequestHandler.java:151)
	at io.cryostat.net.web.http.api.v2.TargetProbesGetHandler.handle(TargetProbesGetHandler.java:93)
	at io.cryostat.net.web.http.api.v2.AbstractV2RequestHandler.handle(AbstractV2RequestHandler.java:86)
	... 10 more
Caused by: java.net.URISyntaxException: Expected scheme-specific part at index 10: localhost:
	at java.base/java.net.URI$Parser.fail(URI.java:2976)
	at java.base/java.net.URI$Parser.failExpecting(URI.java:2982)
	at java.base/java.net.URI$Parser.parse(URI.java:3182)
	at java.base/java.net.URI.<init>(URI.java:623)
	at org.apache.http.client.utils.URIBuilder.build(URIBuilder.java:146)
	at io.cryostat.configuration.CredentialsManager.getCredentialsByTargetId(CredentialsManager.java:177)
	... 13 more

@andrewazores
Copy link
Member

Here is where the exception originates:

URI userless = new URIBuilder(uri).setUserInfo(null).build();

                boolean isJmx = URIUtil.isJmxUrl(uri);
                if (isJmx) {
                    match = Objects.equals(uri.toString(), targetId);
                } else {
                    URI in = new URI(targetId);
                    match = Objects.equals(uri, in);
                    URI userless = new URIBuilder(uri).setUserInfo(null).build();
                    match |= Objects.equals(userless, in);
                }

URIUtil.isJmxUrl() is for checking whether the given URI is a valid JMX Service URL, ex. a service:jmx:rmi: one. The implicit assumption in this code block is that the given URI is either of that form, or it is an Agent http:// URL. It does not account for the possibility of localhost:0 or other "short-form" target identifier.

Immediately apparent options:

  1. Check if the targetId looks like a short-form (with a regex?) in this block and if it is, convert it to a JMX Service URL
  2. Catch the URISyntaxException and assume that when caught this means, or might mean, that the targetId was a short-form. Then do the same thing as option 1.
  3. Catch the IllegalStateException somewhere higher up in the stack, do the short-form-to-JMX conversion, and retry
  4. Add logic like below:
boolean isJmx = URIUtil.isJmxUrl(targetId);
boolean isHttp = URIUtil.isHttpUrl(targetId);
if (isJmx) {
  ...
} else if (isHttp) {
  ...
} else {
  // assume it is a short form
  var u = connectionToolkit.createServiceUrl(targetId);
  ... // do the same thing as in the isJmx branch
}

Options 1 and 4 are probably the best since they aren't relying on exception throwing/catching for control flow. Option 4 is probably simpler since it first checks whether the targetId looks like an http:// URL, which is better-defined than the "short-form" and therefore easier to check for.

@andrewazores andrewazores changed the title [Bug] Unable to create short form custom target [Bug] Unable to interact with short form custom target Feb 15, 2024
@andrewazores andrewazores removed the question Further information is requested label Feb 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: In Progress
Development

Successfully merging a pull request may close this issue.

3 participants