]> git.basschouten.com Git - openhab-addons.git/blob
6220459f6bc1355c199077b3a27c4303d81abe02
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.exec.internal;
14
15 import static java.nio.file.StandardWatchEventKinds.*;
16
17 import java.io.IOException;
18 import java.nio.file.Files;
19 import java.nio.file.Path;
20 import java.util.HashSet;
21 import java.util.Set;
22 import java.util.stream.Stream;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.openhab.core.service.WatchService;
26 import org.osgi.service.component.annotations.Activate;
27 import org.osgi.service.component.annotations.Component;
28 import org.osgi.service.component.annotations.Deactivate;
29 import org.osgi.service.component.annotations.Reference;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * The {@link ExecWhitelistWatchService} provides a whitelist check for exec commands
35  *
36  * @author Jan N. Klug - Initial contribution
37  */
38 @Component(service = ExecWhitelistWatchService.class)
39 @NonNullByDefault
40 public class ExecWhitelistWatchService implements WatchService.WatchEventListener {
41     private static final Path COMMAND_WHITELIST_FILE = Path.of("misc", "exec.whitelist");
42
43     private final Logger logger = LoggerFactory.getLogger(ExecWhitelistWatchService.class);
44     private final Set<String> commandWhitelist = new HashSet<>();
45     private final WatchService watchService;
46     private final Path watchFile;
47
48     @Activate
49     public ExecWhitelistWatchService(
50             final @Reference(target = WatchService.CONFIG_WATCHER_FILTER) WatchService watchService) {
51         this.watchService = watchService;
52         this.watchFile = watchService.getWatchPath().resolve(COMMAND_WHITELIST_FILE);
53         watchService.registerListener(this, COMMAND_WHITELIST_FILE, false);
54
55         // read initial content
56         processWatchEvent(WatchService.Kind.CREATE, COMMAND_WHITELIST_FILE);
57     }
58
59     @Deactivate
60     public void deactivate() {
61         watchService.unregisterListener(this);
62     }
63
64     @Override
65     public void processWatchEvent(WatchService.Kind kind, Path path) {
66         commandWhitelist.clear();
67         if (kind != WatchService.Kind.DELETE) {
68             try (Stream<String> lines = Files.lines(watchFile)) {
69                 lines.filter(line -> !line.trim().startsWith("#")).forEach(commandWhitelist::add);
70                 logger.debug("Updated command whitelist: {}", commandWhitelist);
71             } catch (IOException e) {
72                 logger.warn("Cannot read whitelist file, exec binding commands won't be processed: {}", e.getMessage());
73             }
74         }
75     }
76
77     /**
78      * Check if a command is whitelisted
79      *
80      * @param command the command to check alias
81      * @return true if whitelisted, false if not
82      */
83     public boolean isWhitelisted(String command) {
84         return commandWhitelist.contains(command);
85     }
86 }