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

BE: Chore: Dynamic config file checks adjustments #229

Merged
merged 3 commits into from
Jan 16, 2025
Merged
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
45 changes: 25 additions & 20 deletions api/src/main/java/io/kafbat/ui/util/DynamicConfigOperations.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.yaml.snakeyaml.nodes.Tag;
import org.yaml.snakeyaml.representer.Representer;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;

@Slf4j
@RequiredArgsConstructor
Expand Down Expand Up @@ -125,26 +126,30 @@ public Mono<Path> uploadConfigRelatedFile(FilePart file) {
String targetDirStr = ctx.getEnvironment()
.getProperty(CONFIG_RELATED_UPLOADS_DIR_PROPERTY, CONFIG_RELATED_UPLOADS_DIR_DEFAULT);

Path targetDir = Path.of(targetDirStr);
if (!Files.exists(targetDir)) {
try {
Files.createDirectories(targetDir);
} catch (IOException e) {
return Mono.error(
new FileUploadException("Error creating directory for uploads %s".formatted(targetDir), e));
Mono<Path> directoryCreationMono = Mono.fromCallable(() -> {
Path targetDir = Path.of(targetDirStr);
if (!Files.exists(targetDir)) {
try {
Files.createDirectories(targetDir);
} catch (IOException e) {
throw new FileUploadException("Error creating directory for uploads %s".formatted(targetDir), e);
}
}
return targetDir;
}).subscribeOn(Schedulers.boundedElastic());

return directoryCreationMono.flatMap(dir -> {
Path targetFilePath = dir.resolve(file.filename() + "-" + Instant.now().getEpochSecond());
log.info("Uploading config-related file {}", targetFilePath);
if (Files.exists(targetFilePath)) {
log.info("File {} already exists, it will be overwritten", targetFilePath);
}
}

Path targetFilePath = targetDir.resolve(file.filename() + "-" + Instant.now().getEpochSecond());
log.info("Uploading config-related file {}", targetFilePath);
if (Files.exists(targetFilePath)) {
log.info("File {} already exists, it will be overwritten", targetFilePath);
}

return file.transferTo(targetFilePath)
.thenReturn(targetFilePath)
.doOnError(th -> log.error("Error uploading file {}", targetFilePath, th))
.onErrorMap(th -> new FileUploadException(targetFilePath, th));
return file.transferTo(targetFilePath)
.thenReturn(targetFilePath)
.doOnError(th -> log.error("Error uploading file {}", targetFilePath, th))
.onErrorMap(th -> new FileUploadException(targetFilePath, th));
});
}

private void checkIfDynamicConfigEnabled() {
Expand All @@ -163,8 +168,8 @@ private void writeYamlToFile(String yaml, Path path) {
if (!Files.exists(path.getParent())) {
Files.createDirectories(path.getParent());
}
if (Files.exists(path) && !Files.isWritable(path)) {
throw new ValidationException("File already exists and is not writable");
if (Files.exists(path) && (!Files.isReadable(path) || !Files.isWritable(path))) {
throw new ValidationException("File already exists and is not readable or writable");
}
try {
Files.writeString(
Expand Down
Loading