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

NIFI-13611 Reuse shared proxy header handling for Content Viewer #9134

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@
<artifactId>jakarta.ws.rs-api</artifactId>
<scope>provided</scope> <!-- expected to be provided by parent classloader -->
</dependency>
<dependency>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-web-servlet-shared</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.apache.nifi.authorization.AccessDeniedException;
import org.apache.nifi.stream.io.StreamUtils;
import org.apache.nifi.web.ViewableContent.DisplayMode;
import org.apache.nifi.web.servlet.shared.RequestUriBuilder;
import org.apache.tika.detect.DefaultDetector;
import org.apache.tika.io.TikaInputStream;
import org.apache.tika.metadata.Metadata;
Expand All @@ -57,11 +58,7 @@ public class ContentViewerController extends HttpServlet {
// 1.5kb - multiple of 12 (3 bytes = 4 base 64 encoded chars)
private final static int BUFFER_LENGTH = 1536;

private static final String PROXY_CONTEXT_PATH_HTTP_HEADER = "X-ProxyContextPath";
private static final String FORWARDED_CONTEXT_HTTP_HEADER = "X-Forwarded-Context";
private static final String FORWARDED_PREFIX_HTTP_HEADER = "X-Forwarded-Prefix";

/**
/**
* Gets the content and defers to registered viewers to generate the markup.
*
* @param request servlet request
Expand Down Expand Up @@ -263,23 +260,11 @@ public String getRawContentType() {
*/
private ContentRequestContext getContentRequest(final HttpServletRequest request) {
final String ref = request.getParameter("ref");
final String clientId = request.getParameter("clientId");

final UriBuilder refUriBuilder = UriBuilder.fromUri(ref);

// base the data ref on the request parameter but ensure the scheme is based off the incoming request...
// this is necessary for scenario's where the NiFi instance is behind a proxy running a different scheme
refUriBuilder.scheme(request.getScheme());
final URI inputRefUri = UriBuilder.fromUri(ref).build();
final URI dataUri = RequestUriBuilder.fromHttpServletRequest(request).path(inputRefUri.getPath()).build();
Copy link
Contributor

Choose a reason for hiding this comment

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

These changes do not work when clustered and behind a proxy. The incoming ref is the user facing URI. The previous behavior converted that URI into an "internal" URI so that a node could directly GET the content from another node in the cluster.

Interestingly, this behavior in this PR exposes an issue in handling the response of this GET because the response does not contain a content-disposition header and fails with an NPE.

3296 [APP] 2024-08-05 13:20:45,807 WARN [NiFi Web Server-191] org.apache.nifi.web.ContentViewerController Content retrieval failed
3297 java.lang.NullPointerException: Cannot invoke "String.indexOf(int)" because "headerValue" is null
3298     at org.springframework.http.ContentDisposition.tokenize(ContentDisposition.java:454)
3299     at org.springframework.http.ContentDisposition.parse(ContentDisposition.java:344)
3300     at org.apache.nifi.web.StandardNiFiContentAccess.getContent(StandardNiFiContentAccess.java:120)
3301     at org.apache.nifi.web.ContentViewerController.doGet(ContentViewerController.java:95)
3302     at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:527)
3303     at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:614)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the feedback @mcgilman, I see the issue now with the URL changes for clustered requests. I will correct that issue and review the Content-Disposition handling.


// If there is path context from a proxy, remove it since this request will be used inside the cluster
final String proxyContextPath = getFirstHeaderValue(request, PROXY_CONTEXT_PATH_HTTP_HEADER, FORWARDED_CONTEXT_HTTP_HEADER, FORWARDED_PREFIX_HTTP_HEADER);
if (StringUtils.isNotBlank(proxyContextPath)) {
refUriBuilder.replacePath(StringUtils.substringAfter(UriBuilder.fromUri(ref).build().getPath(), proxyContextPath));
}

final URI refUri = refUriBuilder.build();

final String query = refUri.getQuery();
final String clientId = request.getParameter("clientId");
final String query = inputRefUri.getQuery();

String rawClusterNodeId = null;
if (query != null) {
Expand All @@ -296,7 +281,7 @@ private ContentRequestContext getContentRequest(final HttpServletRequest request
return new ContentRequestContext() {
@Override
public String getDataUri() {
return refUri.toString();
return dataUri.toString();
}

@Override
Expand All @@ -310,29 +295,4 @@ public String getClientId() {
}
};
}

/**
* Returns the value for the first key discovered when inspecting the current request. Will
* return null if there are no keys specified or if none of the specified keys are found.
*
* @param keys http header keys
* @return the value for the first key found
*/
private String getFirstHeaderValue(HttpServletRequest httpServletRequest, final String... keys) {
if (keys == null) {
return null;
}

for (final String key : keys) {
final String value = httpServletRequest.getHeader(key);

// if we found an entry for this key, return the value
if (value != null) {
return value;
}
}

// unable to find any matching keys
return null;
}
}