Fix exec transformation service whitelisting (#14884)

Reported on the german openHAB forum.

Signed-off-by: Jan N. Klug <github@klug.nrw>
This commit is contained in:
J-N-K 2023-04-26 21:52:00 +02:00 committed by GitHub
parent d7b94b6bd3
commit dba7fcc697
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -41,12 +41,14 @@ public class ExecTransformationWhitelistWatchService implements WatchService.Wat
private final Logger logger = LoggerFactory.getLogger(ExecTransformationWhitelistWatchService.class);
private final Set<String> commandWhitelist = new HashSet<>();
private final WatchService watchService;
private final Path watchFile;
@Activate
public ExecTransformationWhitelistWatchService(
final @Reference(target = WatchService.CONFIG_WATCHER_FILTER) WatchService watchService) {
this.watchService = watchService;
watchService.registerListener(this, COMMAND_WHITELIST_FILE);
this.watchFile = watchService.getWatchPath().resolve(COMMAND_WHITELIST_FILE);
watchService.registerListener(this, COMMAND_WHITELIST_FILE, false);
// read initial content
processWatchEvent(WatchService.Kind.CREATE, COMMAND_WHITELIST_FILE);
@ -59,9 +61,9 @@ public class ExecTransformationWhitelistWatchService implements WatchService.Wat
@Override
public void processWatchEvent(WatchService.Kind kind, Path path) {
if (path.endsWith(COMMAND_WHITELIST_FILE)) {
commandWhitelist.clear();
try (Stream<String> lines = Files.lines(path)) {
commandWhitelist.clear();
if (kind != WatchService.Kind.DELETE) {
try (Stream<String> lines = Files.lines(watchFile)) {
lines.filter(line -> !line.trim().startsWith("#")).forEach(commandWhitelist::add);
logger.debug("Updated command whitelist: {}", commandWhitelist);
} catch (IOException e) {