]> git.basschouten.com Git - openhab-addons.git/blob
590a71b48a3abed66a83238926b961278ec3046a
[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.transform.exec.internal;
14
15 import java.io.IOException;
16 import java.nio.file.Files;
17 import java.nio.file.Path;
18 import java.util.HashSet;
19 import java.util.Set;
20 import java.util.stream.Stream;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.openhab.core.service.WatchService;
24 import org.osgi.service.component.annotations.Activate;
25 import org.osgi.service.component.annotations.Component;
26 import org.osgi.service.component.annotations.Deactivate;
27 import org.osgi.service.component.annotations.Reference;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * The {@link ExecTransformationWhitelistWatchService} provides a whitelist check for exec commands
33  *
34  * @author Jan N. Klug - Initial contribution
35  */
36 @Component(service = ExecTransformationWhitelistWatchService.class)
37 @NonNullByDefault
38 public class ExecTransformationWhitelistWatchService implements WatchService.WatchEventListener {
39     private static final Path COMMAND_WHITELIST_FILE = Path.of("misc", "exec.whitelist");
40
41     private final Logger logger = LoggerFactory.getLogger(ExecTransformationWhitelistWatchService.class);
42     private final Set<String> commandWhitelist = new HashSet<>();
43     private final WatchService watchService;
44     private final Path watchFile;
45
46     @Activate
47     public ExecTransformationWhitelistWatchService(
48             final @Reference(target = WatchService.CONFIG_WATCHER_FILTER) WatchService watchService) {
49         this.watchService = watchService;
50         this.watchFile = watchService.getWatchPath().resolve(COMMAND_WHITELIST_FILE);
51         watchService.registerListener(this, COMMAND_WHITELIST_FILE, false);
52
53         // read initial content
54         processWatchEvent(WatchService.Kind.CREATE, COMMAND_WHITELIST_FILE);
55     }
56
57     @Deactivate
58     public void deactivate() {
59         watchService.unregisterListener(this);
60     }
61
62     @Override
63     public void processWatchEvent(WatchService.Kind kind, Path path) {
64         commandWhitelist.clear();
65         if (kind != WatchService.Kind.DELETE) {
66             try (Stream<String> lines = Files.lines(watchFile)) {
67                 lines.filter(line -> !line.trim().startsWith("#")).forEach(commandWhitelist::add);
68                 logger.debug("Updated command whitelist: {}", commandWhitelist);
69             } catch (IOException e) {
70                 logger.warn("Cannot read whitelist file, exec transformations won't be processed: {}", e.getMessage());
71             }
72         }
73     }
74
75     /**
76      * Check if a command is whitelisted
77      *
78      * @param command the command to check alias
79      * @return true if whitelisted, false if not
80      */
81     public boolean isWhitelisted(String command) {
82         return commandWhitelist.contains(command);
83     }
84 }