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

fix: Installed App resource rules [DHIS2-18185] (2.41) #20022

Open
wants to merge 1 commit into
base: 2.41
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions dhis-2/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
# Datasource local storage ignored files
/.idea/dataSources/
dataSources.local.xml
dhis-support/dhis-support-test/src/main/resources/db/init-db.sql


# Editor-based HTTP Client requests
/.idea/httpRequests/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,9 @@ public interface AppManager {
*
* @param app the app to look up files for
* @param pageName the page requested
* @return the Resource representing the file, or null if no file was found
* @return the {@link ResourceResult}
*/
Resource getAppResource(App app, String pageName) throws IOException;
ResourceResult getAppResource(App app, String pageName) throws IOException;

/**
* Sets the app status to DELETION_IN_PROGRESS.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@
import java.io.File;
import java.io.IOException;
import java.util.Map;
import org.hisp.dhis.appmanager.ResourceResult.Redirect;
import org.hisp.dhis.appmanager.ResourceResult.ResourceFound;
import org.hisp.dhis.appmanager.ResourceResult.ResourceNotFound;
import org.hisp.dhis.cache.Cache;
import org.springframework.core.io.Resource;
import org.springframework.scheduling.annotation.Async;

/**
Expand Down Expand Up @@ -71,12 +73,18 @@ public interface AppStorageService {
void deleteApp(App app);

/**
* Looks up and returns a resource representing the page for the app requested. If the resource is
* not found, return null.
* Try to retrieve the requested app resource. The returned {@link ResourceResult} value will be
* one of :
*
* <ul>
* <li>{@link ResourceFound} - when the resource exists
* <li>{@link ResourceNotFound} - when no resource found
* <li>{@link Redirect} - when a directory is found without a trailing '/'
* </ul>
*
* @param app the app to look up
* @param pageName the name of the page to look up
* @return The resource representing the page, or null if not found
* @param resource the name of the resource to look up (can be directory or file)
* @return {@link ResourceResult}
*/
Resource getAppResource(App app, String pageName) throws IOException;
ResourceResult getAppResource(App app, String resource) throws IOException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2004-2025, University of Oslo
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither the name of the HISP project nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.hisp.dhis.appmanager;

import javax.annotation.Nonnull;
import org.springframework.core.io.Resource;

/**
* Models the potential results when trying to retrieve a Resource. <br>
* Can be one of:
*
* <ul>
* <li>ResourceFound
* <li>ResourceNotFound
* <li>Redirect
* </ul>
*
* <p>This enables:
*
* <ul>
* <li>clearer understanding of control flow & intent
* <li>easier handling of multiple scenarios without having to deal with nulls or exceptions
* <li>easier extension potential
* </ul>
*/
public sealed interface ResourceResult {
record ResourceFound(@Nonnull Resource resource) implements ResourceResult {}

record ResourceNotFound(@Nonnull String path) implements ResourceResult {}

record Redirect(@Nonnull String path) implements ResourceResult {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import java.util.UUID;
import java.util.function.Predicate;
import java.util.stream.Stream;
import javax.annotation.Nonnull;
import javax.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
Expand Down Expand Up @@ -386,19 +387,29 @@ public boolean isAccessible(App app) {
}

@Override
public Resource getAppResource(App app, String pageName) throws IOException {
public ResourceResult getAppResource(App app, String pageName) throws IOException {
return getAppStorageServiceByApp(app).getAppResource(app, pageName);
}

/**
* We need to handle scenarios when the Resource is a File (knowing the content length) or when
* it's URL (not knowing the content length and having to make a call, e.g. remote web link in AWS
* S3/MinIO) - otherwise content length can be set to 0 which causes issues at the front-end,
* returning an empty body. If it's a URL resource, an underlying HEAD request is made to get the
* content length.
*
* @param resource resource to check content length
* @return the content length or -1 (unknown size) if exception caught
*/
@Override
public int getUriContentLength(Resource resource) {
public int getUriContentLength(@Nonnull Resource resource) {
try {
URLConnection urlConnection = resource.getURL().openConnection();
return urlConnection.getContentLength();
if (resource.isFile()) {
return (int) resource.contentLength();
} else {
URLConnection urlConnection = resource.getURL().openConnection();
return urlConnection.getContentLength();
}
} catch (IOException e) {
log.error("Error trying to retrieve content length of Resource: {}", e.getMessage());
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -45,20 +46,20 @@
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import javax.annotation.Nonnull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hisp.dhis.appmanager.ResourceResult.Redirect;
import org.hisp.dhis.appmanager.ResourceResult.ResourceFound;
import org.hisp.dhis.appmanager.ResourceResult.ResourceNotFound;
import org.hisp.dhis.cache.Cache;
import org.hisp.dhis.external.location.LocationManager;
import org.hisp.dhis.external.location.LocationManagerException;
import org.hisp.dhis.fileresource.FileResourceContentStore;
import org.hisp.dhis.jclouds.JCloudsStore;
import org.hisp.dhis.util.ZipFileUtils;
import org.jclouds.blobstore.BlobRequestSigner;
import org.jclouds.blobstore.LocalBlobRequestSigner;
import org.jclouds.blobstore.domain.Blob;
import org.jclouds.blobstore.domain.StorageMetadata;
import org.jclouds.blobstore.internal.RequestSigningUnsupported;
import org.jclouds.blobstore.options.ListContainerOptions;
import org.jclouds.http.HttpRequest;
import org.joda.time.Minutes;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
Expand All @@ -80,6 +81,7 @@ public class JCloudsAppStorageService implements AppStorageService {
private final LocationManager locationManager;

private final ObjectMapper jsonMapper;
private final FileResourceContentStore fileResourceContentStore;

private void discoverInstalledApps(Consumer<App> handler) {
ObjectMapper mapper = new ObjectMapper();
Expand Down Expand Up @@ -252,13 +254,10 @@ public App installApp(File file, String filename, Cache<App> appCache) {
String namespace = app.getActivities().getDhis().getNamespace();

log.info(
String.format(
"New app '%s' installed"
+ "\n\tInstall path: %s"
+ (namespace != null && !namespace.isEmpty() ? "\n\tNamespace reserved: %s" : ""),
app.getName(),
dest,
namespace));
"New app {} installed, Install path: {}, Namespace reserved: {}",
app.getName(),
dest,
(namespace != null && !namespace.isEmpty() ? namespace : "no namespace reserved"));

// -----------------------------------------------------------------
// Installation complete.
Expand Down Expand Up @@ -304,64 +303,68 @@ public void deleteApp(App app) {
}

@Override
public Resource getAppResource(App app, String pageName) throws IOException {
public ResourceResult getAppResource(App app, @Nonnull String resource) throws IOException {
if (app == null || !app.getAppStorageSource().equals(AppStorageSource.JCLOUDS)) {
log.warn(
"Can't look up resource {}. The specified app was not found in JClouds storage.",
pageName);
return null;
resource);
return new ResourceNotFound(resource);
}

String key = (app.getFolderName() + ("/" + pageName)).replaceAll("//", "/");
URI uri = getSignedGetContentUri(key);

if (uri == null) {

String filepath = jCloudsStore.getBlobContainer() + "/" + key;
filepath = filepath.replaceAll("//", "/");
File res;

try {
res = locationManager.getFileForReading(filepath);
} catch (LocationManagerException e) {
return null;
}

if (res.isDirectory()) {
String indexPath = pageName.replaceAll("/+$", "") + "/index.html";
log.info("Resource {} ({} is a directory, serving {}", pageName, filepath, indexPath);
return getAppResource(app, indexPath);
} else if (res.exists()) {
return new FileSystemResource(res);
} else {
return null;
}
if (resource.isBlank()) {
return new Redirect("/");
}

return new UrlResource(uri);
}
String resolvedFileResource = useIndexHtmlIfDirCall(resource);
String key = app.getFolderName() + ("/" + resolvedFileResource);
String cleanedKey = key.replaceAll("/+", "/");

public URI getSignedGetContentUri(String key) {
BlobRequestSigner signer = jCloudsStore.getBlobRequestSigner();

if (!requestSigningSupported(signer)) {
return null;
log.debug("Checking if blob exists {} for App {}", cleanedKey, app.getName());
if (jCloudsStore.blobExists(cleanedKey)) {
return new ResourceFound(getResource(cleanedKey));
}
if (keyExistsAsDirectory(cleanedKey)) {
return new Redirect(resource + "/");
}
log.debug("ResourceNotFound {} for App {}", cleanedKey, app.getName());
return new ResourceNotFound(resource);
}

HttpRequest httpRequest;
private boolean keyExistsAsDirectory(String cleanedKey) {
return !jCloudsStore.getBlobList(prefix(cleanedKey)).isEmpty();
}

try {
httpRequest =
signer.signGetBlob(jCloudsStore.getBlobContainer(), key, FIVE_MINUTES_IN_SECONDS);
} catch (UnsupportedOperationException uoe) {
return null;
private Resource getResource(@Nonnull String filePath) throws MalformedURLException {
if (jCloudsStore.isUsingFileSystem()) {
String cleanedFilepath = jCloudsStore.getBlobContainer() + "/" + filePath;
return new FileSystemResource(
locationManager.getFileForReading(cleanedFilepath.replaceAll("/+", "/")));
} else {
URI uri = fileResourceContentStore.getSignedGetContentUri(filePath);
return new UrlResource(uri);
}

return httpRequest.getEndpoint();
}

private boolean requestSigningSupported(BlobRequestSigner signer) {
return !(signer instanceof RequestSigningUnsupported)
&& !(signer instanceof LocalBlobRequestSigner);
/**
* The server is expected to return the 'index.html' for calls made to resources ending in '/'<br>
*
* <p>Examples: <br>
* <li>'' -> ''
* <li>'index.html' ->'index.html'
* <li>'subDir/index.html' ->'subDir/index.html'
* <li>'baseDir/' ->'baseDir/index.html'
* <li>'baseDir/subDir/' ->'baseDir/subDir/index.html'
* <li>'subDir' ->'subDir'
* <li>'static/js/138.af8b0ff6.chunk.js' ->'static/js/138.af8b0ff6.chunk.js'
*
* @param resource app resource to resolve
* @return potentially-updated app resource (file)
*/
private String useIndexHtmlIfDirCall(@Nonnull String resource) {
if (resource.endsWith("/")) {
log.debug("Resource ends with '/', appending 'index.html' to {}", resource);
return resource + "index.html";
}
// any other resource, no special handling required, return as is
return resource;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.hisp.dhis.appmanager.ResourceResult.ResourceFound;
import org.hisp.dhis.cache.Cache;
import org.hisp.dhis.external.location.LocationManager;
import org.hisp.dhis.external.location.LocationManagerException;
Expand Down Expand Up @@ -184,7 +185,7 @@ private String getAppFolderPath() {
}

@Override
public Resource getAppResource(App app, String pageName) throws IOException {
public ResourceResult getAppResource(App app, String pageName) throws IOException {
List<Resource> locations =
Lists.newArrayList(
resourceLoader.getResource(
Expand All @@ -199,7 +200,7 @@ public Resource getAppResource(App app, String pageName) throws IOException {

// Make sure that file resolves into path app folder
if (file != null && file.toPath().startsWith(getAppFolderPath())) {
return resource;
return new ResourceFound(resource);
}
}
}
Expand Down
Loading
Loading