diff options
author | Harald Musum <musum@yahooinc.com> | 2022-04-28 11:06:58 +0200 |
---|---|---|
committer | Harald Musum <musum@yahooinc.com> | 2022-04-28 11:06:58 +0200 |
commit | fe69ccd756fdd4c32be23b4f043b93ecbbd23603 (patch) | |
tree | 31492180a3ebdb7652240f13963382479d6cbd12 /config-application-package/src/main/java/com/yahoo | |
parent | 03f1ea79f5a6ad8d32eaf03b77172e04eab1e0d7 (diff) |
Handle .profile files in subdirs of schemas/
Diffstat (limited to 'config-application-package/src/main/java/com/yahoo')
-rw-r--r-- | config-application-package/src/main/java/com/yahoo/config/model/application/provider/FilesApplicationPackage.java | 63 |
1 files changed, 46 insertions, 17 deletions
diff --git a/config-application-package/src/main/java/com/yahoo/config/model/application/provider/FilesApplicationPackage.java b/config-application-package/src/main/java/com/yahoo/config/model/application/provider/FilesApplicationPackage.java index 6f94ef5454d..08b500911c3 100644 --- a/config-application-package/src/main/java/com/yahoo/config/model/application/provider/FilesApplicationPackage.java +++ b/config-application-package/src/main/java/com/yahoo/config/model/application/provider/FilesApplicationPackage.java @@ -751,18 +751,24 @@ public class FilesApplicationPackage extends AbstractApplicationPackage { /* Validates that files in application dir and subdirectories have a known extension */ public void validateFileExtensions(boolean throwIfInvalid) { - validFileExtensions.forEach((key, value) -> { - java.nio.file.Path path = appDir.toPath().resolve((key.toFile().toPath())); - File subDirectory = path.toFile(); - if ( ! subDirectory.exists() || ! subDirectory.isDirectory()) return; - - try (var filesInPath = Files.list(path)) { - filesInPath.filter(filePath -> filePath.toFile().isFile()) - .forEach(f -> validateFileSuffix(path, f, value, throwIfInvalid)); - } catch (IOException e) { - log.log(Level.WARNING, "Unable to list files in " + subDirectory, e); - } - }); + validFileExtensions.forEach((subDir, __) -> validateInDir(subDir.toFile().toPath(), throwIfInvalid)); + } + + private void validateInDir(java.nio.file.Path subDir, boolean throwIfInvalid) { + java.nio.file.Path path = appDir.toPath().resolve(subDir); + File subDirectory = path.toFile(); + if ( ! subDirectory.exists() || ! subDirectory.isDirectory()) return; + + try (var filesInPath = Files.list(path)) { + filesInPath.forEach(filePath -> { + if (filePath.toFile().isDirectory()) + validateInDir(appDir.toPath().relativize(filePath), throwIfInvalid); + else + validateFileSuffix(filePath, throwIfInvalid); + }); + } catch (IOException e) { + log.log(Level.WARNING, "Unable to list files in " + subDirectory, e); + } } static { @@ -777,10 +783,10 @@ public class FilesApplicationPackage extends AbstractApplicationPackage { Map.entry(QUERY_PROFILE_TYPES_DIR, Set.of(".xml")), Map.entry(Path.fromString(ROUTINGTABLES_DIR), Set.of(".xml")), Map.entry(RULES_DIR, Set.of(RULES_NAME_SUFFIX)), - // TODO: Might have rank profiles in subdirs: schema-name]/[rank-profile].profile + // Note: Might have rank profiles in subdirs: schema-name]/[rank-profile].profile Map.entry(SCHEMAS_DIR, Set.of(SD_NAME_SUFFIX, RANKEXPRESSION_NAME_SUFFIX)), Map.entry(Path.fromString(SEARCHCHAINS_DIR), Set.of(".xml")), - // TODO: Might have rank profiles in subdirs: [schema-name]/[rank-profile].profile + // Note: Might have rank profiles in subdirs: [schema-name]/[rank-profile].profile Map.entry(SEARCH_DEFINITIONS_DIR, Set.of(SD_NAME_SUFFIX, RANKEXPRESSION_NAME_SUFFIX)), Map.entry(SECURITY_DIR, Set.of(".pem"))); @@ -789,11 +795,14 @@ public class FilesApplicationPackage extends AbstractApplicationPackage { // onnx model files (.onnx) } - private void validateFileSuffix(java.nio.file.Path dir, java.nio.file.Path pathToFile, Set<String> allowedSuffixes, boolean throwIfInvalid) { + private void validateFileSuffix(java.nio.file.Path pathToFile, boolean throwIfInvalid) { String fileName = pathToFile.toFile().getName(); - if (allowedSuffixes.stream().noneMatch(fileName::endsWith)) { + java.nio.file.Path relativeDirectory = appDir.toPath().relativize(pathToFile).getParent(); + Set<String> allowedExtensions = findAllowedExtensions(relativeDirectory); + log.log(Level.FINE, "Checking " + pathToFile + " against " + allowedExtensions); + if (allowedExtensions.stream().noneMatch(fileName::endsWith)) { String message = "File in application package with unknown suffix: " + - appDir.toPath().relativize(dir).resolve(fileName) + " Please delete or move file to another directory."; + appDir.toPath().relativize(pathToFile.getParent()).resolve(fileName) + ", please delete or move file to another directory."; if (throwIfInvalid) throw new IllegalArgumentException(message); else @@ -801,4 +810,24 @@ public class FilesApplicationPackage extends AbstractApplicationPackage { } } + private Set<String> findAllowedExtensions(java.nio.file.Path relativeDirectory) { + return (isSubDirInSchemas(relativeDirectory)) + ? Set.of(".profile") // Special case, since subdir in schemas can have any name + : validFileExtensions.entrySet().stream() + .filter(entry -> entry.getKey().equals(Path.fromString(relativeDirectory.toString()))) + .map(Map.Entry::getValue) + .findFirst() + .orElse(Set.of()); + } + + private boolean isSubDirInSchemas(java.nio.file.Path relativeDirectory) { + java.nio.file.Path schemasPath = SCHEMAS_DIR.toFile().toPath().getName(0); + java.nio.file.Path searchDefinitionsPath = SEARCH_DEFINITIONS_DIR.toFile().toPath().getName(0); + if (relativeDirectory.equals(schemasPath) ||relativeDirectory.equals(searchDefinitionsPath)) return false; + + return (relativeDirectory.startsWith(schemasPath + "/") + || relativeDirectory.startsWith(SEARCH_DEFINITIONS_DIR.toFile().toPath().getName(0) + "/")); + } + + } |