-
Notifications
You must be signed in to change notification settings - Fork 42
Add uploaded file validation #1649
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| package org.cloudfoundry.multiapps.controller.core.validators.parameters; | ||
|
|
||
| import java.io.BufferedInputStream; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.text.MessageFormat; | ||
| import java.util.function.Consumer; | ||
|
|
||
| import jakarta.inject.Inject; | ||
| import jakarta.inject.Named; | ||
| import org.apache.tika.Tika; | ||
| import org.cloudfoundry.multiapps.common.ParsingException; | ||
| import org.cloudfoundry.multiapps.common.SLException; | ||
| import org.cloudfoundry.multiapps.controller.core.Messages; | ||
| import org.cloudfoundry.multiapps.controller.core.helpers.DescriptorParserFacadeFactory; | ||
| import org.cloudfoundry.multiapps.controller.persistence.services.FileService; | ||
| import org.cloudfoundry.multiapps.controller.persistence.services.FileStorageException; | ||
|
|
||
| @Named | ||
| public class FileMimeTypeValidator { | ||
|
|
||
| private static final String APPLICATION_OCTET_STREAM_MIME_TYPE = "application/octet-stream"; | ||
| private static final String APPLICATION_ZIP_MIME_TYPE = "application/zip"; | ||
| private static final String TEXT_PLAIN_MIME_TYPE = "text/plain"; | ||
| private static final Tika mimeTypeDetector = new Tika(); | ||
| private final DescriptorParserFacadeFactory descriptorParserFactory; | ||
| private final FileService fileService; | ||
|
|
||
| @Inject | ||
| public FileMimeTypeValidator(FileService fileService, DescriptorParserFacadeFactory descriptorParserFactory) { | ||
| this.fileService = fileService; | ||
| this.descriptorParserFactory = descriptorParserFactory; | ||
| } | ||
|
|
||
| public void validateFileType(String spaceGuid, String appArchiveId, Consumer<String> stepLogger) { | ||
| try (InputStream fileInputStream = fileService.openInputStream(spaceGuid, appArchiveId); | ||
| InputStream bufferedFileInputStream = new BufferedInputStream(fileInputStream)) { | ||
| validateInputStreamMimeType(bufferedFileInputStream, stepLogger); | ||
| } catch (FileStorageException | IOException e) { | ||
| throw new SLException(e); | ||
| } | ||
| } | ||
|
|
||
| private void validateInputStreamMimeType(InputStream uploadedFileInputStream, Consumer<String> stepLogger) throws IOException { | ||
Yavor16 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| String detectedType = getFileMimeType(uploadedFileInputStream); | ||
|
|
||
| switch (detectedType) { | ||
| case TEXT_PLAIN_MIME_TYPE -> validateYamlFile(uploadedFileInputStream, stepLogger); | ||
| case APPLICATION_ZIP_MIME_TYPE, APPLICATION_OCTET_STREAM_MIME_TYPE -> { | ||
| } | ||
vkalapov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| default -> stepLogger.accept(MessageFormat.format(Messages.UNSUPPORTED_FILE_FORMAT, detectedType)); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| private String getFileMimeType(InputStream uploadedFileInputStream) throws IOException { | ||
| return mimeTypeDetector.detect(uploadedFileInputStream); | ||
| } | ||
|
|
||
| private void validateYamlFile(InputStream uploadedFileInputStream, Consumer<String> stepLogger) { | ||
| try { | ||
| descriptorParserFactory.getInstanceWithDisabledDuplicateKeys() | ||
| .parseExtensionDescriptor(uploadedFileInputStream); | ||
| } catch (ParsingException e) { | ||
| stepLogger.accept(Messages.EXTENSION_DESCRIPTORS_COULD_NOT_BE_PARSED_TO_VALID_YAML); | ||
| stepLogger.accept(e.getMessage()); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
...oudfoundry/multiapps/controller/core/validators/parameters/FileMimeTypeValidatorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| package org.cloudfoundry.multiapps.controller.core.validators.parameters; | ||
|
|
||
| import java.io.InputStream; | ||
| import java.util.UUID; | ||
|
|
||
| import org.cloudfoundry.multiapps.controller.core.Messages; | ||
| import org.cloudfoundry.multiapps.controller.core.helpers.DescriptorParserFacadeFactory; | ||
| import org.cloudfoundry.multiapps.controller.core.util.ApplicationConfiguration; | ||
| import org.cloudfoundry.multiapps.controller.persistence.services.FileService; | ||
| import org.cloudfoundry.multiapps.controller.persistence.services.FileStorageException; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.mockito.Mock; | ||
| import org.mockito.Mockito; | ||
| import org.mockito.MockitoAnnotations; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| class FileMimeTypeValidatorTest { | ||
|
|
||
| @Mock | ||
| private FileService fileService; | ||
|
|
||
| private FileMimeTypeValidator fileMimeTypeValidator; | ||
|
|
||
| private final String TEST_SPACE_ID = UUID.randomUUID() | ||
| .toString(); | ||
| private final String TEST_FILE_ID = UUID.randomUUID() | ||
| .toString(); | ||
|
|
||
| private DescriptorParserFacadeFactory descriptorParserFacadeFactory; | ||
|
|
||
| @BeforeEach | ||
| void setUp() throws Exception { | ||
| MockitoAnnotations.openMocks(this) | ||
| .close(); | ||
| int maxAliases = 5; | ||
| ApplicationConfiguration applicationConfiguration = Mockito.mock(ApplicationConfiguration.class); | ||
| when(applicationConfiguration.getSnakeyamlMaxAliasesForCollections()) | ||
| .thenReturn(maxAliases); | ||
| descriptorParserFacadeFactory = new DescriptorParserFacadeFactory(applicationConfiguration); | ||
| fileMimeTypeValidator = new FileMimeTypeValidator(fileService, descriptorParserFacadeFactory); | ||
| } | ||
|
|
||
| @Test | ||
| void testValidationWithCorrectYaml() throws FileStorageException { | ||
| InputStream mtadYaml = getClass().getResourceAsStream("valid-format.yaml"); | ||
| when(fileService.openInputStream(TEST_SPACE_ID, TEST_FILE_ID)).thenReturn(mtadYaml); | ||
| fileMimeTypeValidator = new FileMimeTypeValidator(fileService, descriptorParserFacadeFactory); | ||
| fileMimeTypeValidator.validateFileType(TEST_SPACE_ID, TEST_FILE_ID, message -> { | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| void testValidationWithDuplicateKeys() throws FileStorageException { | ||
| InputStream mtadYaml = getClass().getResourceAsStream("duplicate-key.yaml"); | ||
| when(fileService.openInputStream(TEST_SPACE_ID, TEST_FILE_ID)).thenReturn(mtadYaml); | ||
| fileMimeTypeValidator = new FileMimeTypeValidator(fileService, descriptorParserFacadeFactory); | ||
| fileMimeTypeValidator.validateFileType(TEST_SPACE_ID, TEST_FILE_ID, message -> { | ||
| boolean doesMessageContainWarningMessage = message.contains(Messages.EXTENSION_DESCRIPTORS_COULD_NOT_BE_PARSED_TO_VALID_YAML); | ||
| boolean doesMessageContainException = message.contains("Error while parsing YAML string"); | ||
| assertTrue(doesMessageContainException || doesMessageContainWarningMessage); | ||
| }); | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
...src/test/resources/org/cloudfoundry/multiapps/controller/core/helpers/duplicate-keys.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| _schema-version: "3.1" | ||
| ID: com.test.1 | ||
| version: 1.0.0 | ||
|
|
||
| modules: | ||
| - name: spark-service-admin | ||
| type: abc | ||
| properties: | ||
| MAX_SIZE: а | ||
| MAX_SIZE: а |
9 changes: 9 additions & 0 deletions
9
...urces/org/cloudfoundry/multiapps/controller/core/validators/parameters/duplicate-key.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| _schema-version: "3.3" | ||
| ID: com.test | ||
| extends: com.test | ||
|
|
||
| modules: | ||
| - name: spark-service-admin | ||
| properties: | ||
| MAX_SIZE: а | ||
| MAX_SIZE: а |
8 changes: 8 additions & 0 deletions
8
...ources/org/cloudfoundry/multiapps/controller/core/validators/parameters/valid-format.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| _schema-version: "3.3" | ||
| ID: com.test | ||
| extends: com.test | ||
|
|
||
| modules: | ||
| - name: spark-service-admin | ||
| properties: | ||
| MAX_SIZE: а |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.