Skip to content

Commit

Permalink
Expose services only on localhost interface when in dev mode, must ov…
Browse files Browse the repository at this point in the history
…erride when testing with LAN clients
  • Loading branch information
Christian Bauer committed Feb 9, 2018
1 parent 1c2e1ae commit 1577f54
Show file tree
Hide file tree
Showing 13 changed files with 112 additions and 78 deletions.
Original file line number Diff line number Diff line change
@@ -1,53 +1,22 @@
package org.openremote.agent.protocol.knx;

import static org.openremote.model.Constants.PROTOCOL_NAMESPACE;
import static org.openremote.model.attribute.MetaItem.isMetaNameEqualTo;
import static org.openremote.model.util.TextUtil.REGEXP_PATTERN_INTEGER_POSITIVE_NON_ZERO;
import static org.openremote.model.util.TextUtil.isNullOrEmpty;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.openremote.agent.protocol.AbstractProtocol;
import org.openremote.model.asset.agent.ConnectionStatus;
import org.openremote.agent.protocol.ProtocolLinkedAttributeImport;
import org.openremote.container.util.Util;
import org.openremote.container.util.CodecUtil;
import org.openremote.model.AbstractValueHolder;
import org.openremote.model.ValidationFailure;
import org.openremote.model.asset.Asset;
import org.openremote.model.asset.AssetAttribute;
import org.openremote.model.asset.AssetMeta;
import org.openremote.model.asset.AssetType;
import org.openremote.model.asset.agent.AgentLink;
import org.openremote.model.attribute.AttributeEvent;
import org.openremote.model.attribute.AttributeRef;
import org.openremote.model.attribute.AttributeState;
import org.openremote.model.attribute.AttributeType;
import org.openremote.model.attribute.AttributeValidationResult;
import org.openremote.model.attribute.MetaItem;
import org.openremote.model.attribute.MetaItemDescriptor;
import org.openremote.model.attribute.MetaItemDescriptorImpl;
import org.openremote.model.asset.agent.ConnectionStatus;
import org.openremote.model.attribute.*;
import org.openremote.model.file.FileInfo;
import org.openremote.model.util.Pair;
import org.openremote.model.value.Value;
import org.openremote.model.value.ValueType;
import org.openremote.model.value.Values;

import tuwien.auto.calimero.GroupAddress;
import tuwien.auto.calimero.KNXException;
import tuwien.auto.calimero.KNXFormatException;
Expand All @@ -59,6 +28,24 @@
import tuwien.auto.calimero.xml.XmlInputFactory;
import tuwien.auto.calimero.xml.XmlReader;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.*;
import java.util.function.Consumer;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import static org.openremote.model.Constants.PROTOCOL_NAMESPACE;
import static org.openremote.model.attribute.MetaItem.isMetaNameEqualTo;
import static org.openremote.model.util.TextUtil.REGEXP_PATTERN_INTEGER_POSITIVE_NON_ZERO;
import static org.openremote.model.util.TextUtil.isNullOrEmpty;

/**
* This protocol is used to connect to a KNX bus via an IP interface.
*/
Expand Down Expand Up @@ -444,7 +431,7 @@ public Asset[] discoverLinkedAssetAttributes(AssetAttribute protocolConfiguratio

try {
boolean fileFound = false;
byte[] data = Util.decodeBase64(fileInfo.getContents());
byte[] data = CodecUtil.decodeBase64(fileInfo.getContents());
zin = new ZipInputStream(new ByteArrayInputStream(data));
ZipEntry zipEntry = zin.getNextEntry();
while (zipEntry != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.openremote.agent.protocol.velbus.device.DevicePropertyValue;
import org.openremote.agent.protocol.velbus.device.FeatureProcessor;
import org.openremote.agent.protocol.velbus.device.VelbusDeviceType;
import org.openremote.container.util.Util;
import org.openremote.container.util.CodecUtil;
import org.openremote.model.asset.Asset;
import org.openremote.model.asset.AssetAttribute;
import org.openremote.model.asset.AssetMeta;
Expand Down Expand Up @@ -263,7 +263,7 @@ public Asset[] discoverLinkedAssetAttributes(AssetAttribute protocolConfiguratio
public Asset[] discoverLinkedAssetAttributes(AssetAttribute protocolConfiguration, FileInfo fileInfo) throws IllegalStateException {
Document xmlDoc;
try {
String xmlStr = fileInfo.isBinary() ? new String(Util.decodeBase64(fileInfo.getContents()), "UTF8") : fileInfo.getContents();
String xmlStr = fileInfo.isBinary() ? new String(CodecUtil.decodeBase64(fileInfo.getContents()), "UTF8") : fileInfo.getContents();
LOG.info("Parsing VELBUS project file: " + fileInfo.getName());

xmlDoc = DocumentBuilderFactory
Expand Down
4 changes: 3 additions & 1 deletion client/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ task gwtSuperDev(dependsOn: classes, type: JavaExec) {
"-noprecompile",
"-generateJsInteropExports",
"-style", "PRETTY",
"-bindAddress", "0.0.0.0" // Careful! The code server listens on all interfaces!,
// Change this to 0.0.0.0 to bind on all interfaces, enabling
// access of the code server from other devices in your LAN
"-bindAddress", "127.0.0.1"

]
// Make this magic directory easily cleanable by placing it into the build folder
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2018, OpenRemote Inc.
*
* See the CONTRIBUTORS.txt file in the distribution for a
* full listing of individual contributors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.openremote.container.util;

import org.apache.commons.codec.binary.Base64;

public class CodecUtil {

protected CodecUtil() {}

public static byte[] decodeBase64(String base64String) {
if (base64String == null) {
return null;
}

if (base64String.length() == 0) {
return new byte[0];
}

// Could be data URL encoded so look for comma in first 50 chars
int searchLength = Math.min(50, base64String.length());
String str = base64String.substring(0, searchLength-1);
int commaIndex = str.indexOf(',');
if (commaIndex >=0) {
base64String = base64String.substring(commaIndex+1);
}

return Base64.decodeBase64(base64String);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.openremote.manager.util;
package org.openremote.container.util;

import com.fasterxml.jackson.databind.ObjectMapper;

Expand All @@ -27,11 +27,6 @@ public class JsonUtil {

protected JsonUtil() {}

/**
* For some reason the GWT compiler no longer likes this being in the {@link org.openremote.model.util.JsonUtil}
* class; no idea why as it used to compile fine with this in there???
*/
// TODO: Figure out why this won't compile when located in the model JsonUtil class and then remove this class
@SuppressWarnings("unchecked")
public static <T> T convert(ObjectMapper objectMapper, Class<T> targetType, Object object) {
Map<String, Object> props = objectMapper.convertValue(object, Map.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public class LogUtil {

public static final String LOGGING_CONFIG_FILE = "LOGGING_CONFIG_FILE";

protected LogUtil() {
}

/**
* If system property <code>java.util.logging.config.file</code> has not been set, try to load the
* logging configuration specified in environment variable <code>LOGGING_CONFIG_FILE</code> as a file.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
*/
public class MapAccess {

protected MapAccess() {
}

static public String getString(Map<String, String> map, String key, String defaultValue) {
return map.containsKey(key) ? map.get(key) : defaultValue;
}
Expand Down
21 changes: 0 additions & 21 deletions container/src/main/java/org/openremote/container/util/Util.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.openremote.container.util;

import org.apache.commons.codec.binary.Base64;

import java.lang.reflect.Array;
import java.text.ParseException;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -141,23 +139,4 @@ public static String shiftDouble(Object o, double shift, String suffix) {
return (String.format("%.1f", d) + suffix);
}

public static byte[] decodeBase64(String base64String) {
if (base64String == null) {
return null;
}

if (base64String.length() == 0) {
return new byte[0];
}

// Could be data URL encoded so look for comma in first 50 chars
int searchLength = Math.min(50, base64String.length());
String str = base64String.substring(0, searchLength-1);
int commaIndex = str.indexOf(',');
if (commaIndex >=0) {
base64String = base64String.substring(commaIndex+1);
}

return Base64.decodeBase64(base64String);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,11 @@ public abstract class WebService implements ContainerService {

private static final Logger LOG = Logger.getLogger(WebService.class.getName());

// Change this to 0.0.0.0 to bind on all interfaces, enabling
// access of the manager service from other devices in your LAN
public static final String WEBSERVER_LISTEN_HOST = "WEBSERVER_LISTEN_HOST";
public static final String WEBSERVER_LISTEN_HOST_DEFAULT = "0.0.0.0";
public static final String WEBSERVER_LISTEN_HOST_DEFAULT = "127.0.0.1";

public static final String WEBSERVER_LISTEN_PORT = "WEBSERVER_LISTEN_PORT";
public static final int WEBSERVER_LISTEN_PORT_DEFAULT = 8080;
public static final String WEBSERVER_DUMP_REQUESTS = "WEBSERVER_DUMP_REQUESTS";
Expand Down
15 changes: 15 additions & 0 deletions manager/src/main/java/org/openremote/manager/rules/RulesClock.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
import org.openremote.model.rules.TemporaryFact;
import org.openremote.model.util.TimeUtil;

import java.time.DayOfWeek;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit;
import java.util.function.Predicate;

/**
Expand Down Expand Up @@ -58,6 +61,18 @@ public <T extends TemporaryFact> Predicate<T> last(String timeWindow) {
return fact -> timeWindowStart <= fact.getTimestamp();
}

public DayOfWeek getDayOfWeek() {
return getTime().getDayOfWeek();
}

public LocalDateTime getYesterday() {
return getTime().minus(1, ChronoUnit.DAYS);
}

public LocalDateTime getTomorrow() {
return getTime().plus(1, ChronoUnit.DAYS);
}

@Override
public String toString() {
return getClass().getSimpleName() + "{" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
import java.util.List;
import java.util.logging.Logger;

import static org.openremote.manager.util.JsonUtil.convert;
import static org.openremote.container.util.JsonUtil.convert;
import static org.openremote.model.Constants.*;

public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider implements ManagerIdentityProvider {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,13 +362,13 @@ AssetAttribute[] createTimerAttributes(ServerAsset apartment) {
List<AssetAttribute> attributes = new ArrayList<>();
for (DayOfWeek dayOfWeek : DayOfWeek.values()) {
// "MONDAY" => "Monday"
String dayName = dayOfWeek.name().substring(0, 1) + dayOfWeek.name().substring(1).toLowerCase(Locale.ROOT);
String dayOfWeekLabel = dayOfWeek.name().substring(0, 1) + dayOfWeek.name().substring(1).toLowerCase(Locale.ROOT);
// "0 0 7 ? *" => "0 0 7 ? * MON *"
String timePattern = startTime + " " + dayOfWeek.name().substring(0, 3).toUpperCase(Locale.ROOT) + " *";
attributes.add(
initTimerConfiguration(new AssetAttribute(attributeName + dayName), timePattern,
initTimerConfiguration(new AssetAttribute(attributeName + dayOfWeek.name()), timePattern,
new AttributeState(apartment.getId(), attributeName, Values.create("REQUEST_START")))
.addMeta(new MetaItem(LABEL, Values.create(attributeLabel + " trigger " + dayName)))
.addMeta(new MetaItem(LABEL, Values.create(attributeLabel + " trigger " + dayOfWeekLabel)))
);
}
return attributes.toArray(new AssetAttribute[attributes.size()]);
Expand Down Expand Up @@ -452,24 +452,24 @@ protected void linkDemoApartmentWithSceneAgent(ServerAsset apartment, ServerAsse
);
for (DayOfWeek dayOfWeek : DayOfWeek.values()) {
// "MONDAY" => "Monday"
String dayName = dayOfWeek.name().substring(0, 1) + dayOfWeek.name().substring(1).toLowerCase(Locale.ROOT);
String dayOfWeekLabel = dayOfWeek.name().substring(0, 1) + dayOfWeek.name().substring(1).toLowerCase(Locale.ROOT);
apartment.addAttributes(
new AssetAttribute(scene.attributeName + "Time" + dayOfWeek.name(), AttributeType.STRING)
.setMeta(
new MetaItem(LABEL, Values.create(scene.attributeLabel + " time " + dayName)),
new MetaItem(LABEL, Values.create(scene.attributeLabel + " time " + dayOfWeekLabel)),
new MetaItem(ACCESS_RESTRICTED_READ, Values.create(true)),
new MetaItem(ACCESS_RESTRICTED_WRITE, Values.create(true)),
new MetaItem(RULE_STATE, Values.create(true)),
new MetaItem(META_TIMER_VALUE_LINK, Values.create(TimerValue.TIME.toString())),
new MetaItem(AGENT_LINK, new AttributeRef(agent.getId(), scene.attributeName + dayName).toArrayValue())
new MetaItem(AGENT_LINK, new AttributeRef(agent.getId(), scene.attributeName + dayOfWeek.name()).toArrayValue())
),
new AssetAttribute(scene.attributeName + "Enabled" + dayOfWeek.name(), AttributeType.BOOLEAN)
.setMeta(
new MetaItem(LABEL, Values.create(scene.attributeLabel + " enabled " + dayName)),
new MetaItem(LABEL, Values.create(scene.attributeLabel + " enabled " + dayOfWeekLabel)),
new MetaItem(ACCESS_RESTRICTED_READ, Values.create(true)),
new MetaItem(ACCESS_RESTRICTED_WRITE, Values.create(true)),
new MetaItem(META_TIMER_VALUE_LINK, Values.create(TimerValue.ENABLED.toString())),
new MetaItem(AGENT_LINK, new AttributeRef(agent.getId(), scene.attributeName + dayName).toArrayValue())
new MetaItem(AGENT_LINK, new AttributeRef(agent.getId(), scene.attributeName + dayOfWeek.name()).toArrayValue())
)
);
}
Expand Down
6 changes: 3 additions & 3 deletions profile/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,9 @@ services:
# the rules engine.
# SCHEDULED_TASKS_THREADS_MAX: 4

# When asset attributes are enabled for rule events, each value update will be an event fact
# in a rules. You can set the global default expiration time span, after which rule event
# facts are retracted automatically. Default is 1 hour.
# When asset attributes are enabled for rule events, each value update will be a temporary
# fact in rules. You can set the global default expiration time span used when no other
# attribute-level configuration exists. Default is 1 hour.
# RULE_EVENT_EXPIRES: '1h'

# The identity provider implementation to use for managing user identities, roles, and
Expand Down

0 comments on commit 1577f54

Please sign in to comment.