Skip to content

Commit

Permalink
Address review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
bbende committed Aug 6, 2024
1 parent c11242a commit 41c2958
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ public static long getContainerUsableSpace(final Path path) {
* @param filename The filename to clean
* @return sanitized filename
*/
public static String sanitizeFilename(String filename) {
public static String getSanitizedFilename(String filename) {
if (filename == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ public class ParameterContextResource extends AbstractParameterResource {
private static final String FILENAME_HEADER = "Filename";
private static final String CONTENT_TYPE_HEADER = "Content-Type";
private static final String UPLOAD_CONTENT_TYPE = "application/octet-stream";
private static final long MAX_ASSET_SIZE_BYTES = (long) DataUnit.GB.toB(1);

private NiFiServiceFacade serviceFacade;
private Authorizer authorizer;
Expand Down Expand Up @@ -395,6 +396,11 @@ public Response createAsset(
throw new IllegalArgumentException("Asset contents must be specified.");
}

final String sanitizedAssetName = FileUtils.getSanitizedFilename(assetName);
if (!assetName.equals(sanitizedAssetName)) {
throw new IllegalArgumentException(FILENAME_HEADER + " header contains an invalid file name");
}

// If clustered and not all nodes are connected, do not allow creating an asset.
// Generally, we allow the flow to be modified when nodes are disconnected, but we do not allow creating an asset because
// the cluster has no mechanism for synchronizing those assets after the upload.
Expand Down Expand Up @@ -430,16 +436,16 @@ public Response createAsset(
// different request for each of the two phases.

final long startTime = System.currentTimeMillis();
final InputStream maxLengthInputStream = new MaxLengthInputStream(assetContents, (long) DataUnit.GB.toB(1));
final InputStream maxLengthInputStream = new MaxLengthInputStream(assetContents, MAX_ASSET_SIZE_BYTES);

final AssetEntity assetEntity;
if (isReplicateRequest()) {
final UploadRequest<AssetEntity> uploadRequest = new UploadRequest.Builder<AssetEntity>()
.user(NiFiUserUtils.getNiFiUser())
.filename(assetName)
.filename(sanitizedAssetName)
.identifier(UUID.randomUUID().toString())
.contents(maxLengthInputStream)
.header(FILENAME_HEADER, assetName)
.header(FILENAME_HEADER, sanitizedAssetName)
.header(CONTENT_TYPE_HEADER, UPLOAD_CONTENT_TYPE)
.exampleRequestUri(getAbsolutePath())
.responseClass(AssetEntity.class)
Expand All @@ -448,7 +454,6 @@ public Response createAsset(
assetEntity = uploadRequestReplicator.upload(uploadRequest);
} else {
final String existingContextId = contextEntity.getId();
final String sanitizedAssetName = FileUtils.sanitizeFilename(assetName);
final Asset asset = assetManager.createAsset(existingContextId, sanitizedAssetName, maxLengthInputStream);
assetEntity = dtoFactory.createAssetEntity(asset);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;

public interface ParamContextClient {

Expand All @@ -47,7 +48,7 @@ public interface ParamContextClient {

AssetsEntity getAssets(String contextId) throws NiFiClientException, IOException;

File getAssetContent(String contextId, String assetId, File outputDirectory) throws NiFiClientException, IOException;
Path getAssetContent(String contextId, String assetId, File outputDirectory) throws NiFiClientException, IOException;

AssetEntity deleteAsset(String contextId, String assetId) throws NiFiClientException, IOException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

public class JerseyParamContextClient extends AbstractJerseyClient implements ParamContextClient {
Expand Down Expand Up @@ -217,7 +218,7 @@ public AssetsEntity getAssets(final String contextId) throws NiFiClientException
}

@Override
public File getAssetContent(final String contextId, final String assetId, final File outputDirectory)
public Path getAssetContent(final String contextId, final String assetId, final File outputDirectory)
throws NiFiClientException, IOException {
if (StringUtils.isBlank(contextId)) {
throw new IllegalArgumentException("Parameter context id cannot be null or blank");
Expand All @@ -239,7 +240,7 @@ public File getAssetContent(final String contextId, final String assetId, final

try (final InputStream responseInputStream = response.readEntity(InputStream.class)) {
Files.copy(responseInputStream, assetFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
return assetFile;
return assetFile.toPath();
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Properties;

public class GetAsset extends AbstractNiFiCommand<VoidResult> {
Expand Down Expand Up @@ -55,9 +56,9 @@ public VoidResult doExecute(final NiFiClient client, final Properties properties
final String paramContextId = getRequiredArg(properties, CommandOption.PARAM_CONTEXT_ID);
final String assetId = getRequiredArg(properties, CommandOption.ASSET_ID);
final File outputDir = new File(getRequiredArg(properties, CommandOption.OUTPUT_DIR));
final File assetFile = client.getParamContextClient().getAssetContent(paramContextId, assetId, outputDir);
final Path assetFile = client.getParamContextClient().getAssetContent(paramContextId, assetId, outputDir);
if (isInteractive()) {
println(assetFile.getAbsolutePath());
println(assetFile.toFile().getAbsolutePath());
}
return VoidResult.getInstance();
}
Expand Down

0 comments on commit 41c2958

Please sign in to comment.