From da1feb689a3a20541b0e0d4fb35e1ff6c3e6036f Mon Sep 17 00:00:00 2001 From: Pierantonio Merlino Date: Mon, 11 Nov 2024 16:43:33 +0100 Subject: [PATCH 1/2] fix(rest.network.configuration): Fix net interfaces property management again (#5535) * Added net.interfaces property management in NetworkConfiguration Rest Signed-off-by: pierantoniomerlino * Changed parsing logic Signed-off-by: pierantoniomerlino * Improved string parsing Signed-off-by: pierantoniomerlino * Added tests for net.interfaces property Signed-off-by: pierantoniomerlino * Update kura/org.eclipse.kura.rest.network.configuration.provider/src/main/java/org/eclipse/kura/internal/rest/network/configuration/NetworkConfigurationRestService.java Co-authored-by: Mattia Dal Ben * Update kura/org.eclipse.kura.rest.network.configuration.provider/src/main/java/org/eclipse/kura/internal/rest/network/configuration/NetworkConfigurationRestService.java Co-authored-by: Mattia Dal Ben --------- Signed-off-by: pierantoniomerlino Co-authored-by: Mattia Dal Ben --- .../META-INF/MANIFEST.MF | 1 + .../NetworkConfigurationRestService.java | 34 +++++++++++++++++-- .../META-INF/MANIFEST.MF | 4 ++- .../NetworkConfigurationRestServiceTest.java | 30 ++++++++++++++-- .../RestNetworkConfigurationJson.java | 3 +- 5 files changed, 64 insertions(+), 8 deletions(-) diff --git a/kura/org.eclipse.kura.rest.network.configuration.provider/META-INF/MANIFEST.MF b/kura/org.eclipse.kura.rest.network.configuration.provider/META-INF/MANIFEST.MF index 30ef91678c1..a4a3aec6128 100644 --- a/kura/org.eclipse.kura.rest.network.configuration.provider/META-INF/MANIFEST.MF +++ b/kura/org.eclipse.kura.rest.network.configuration.provider/META-INF/MANIFEST.MF @@ -11,6 +11,7 @@ Import-Package: javax.annotation.security;version="1.2.0", javax.ws.rs;version="2.0.1", javax.ws.rs.core;version="2.0.1", org.apache.commons.io;version="2.4.0", + org.apache.commons.lang3;version="3.12.0", org.eclipse.kura;version="[1.3,2.0)", org.eclipse.kura.cloudconnection.request;version="[1.0,2.0)", org.eclipse.kura.configuration;version="[1.2,2.0)", diff --git a/kura/org.eclipse.kura.rest.network.configuration.provider/src/main/java/org/eclipse/kura/internal/rest/network/configuration/NetworkConfigurationRestService.java b/kura/org.eclipse.kura.rest.network.configuration.provider/src/main/java/org/eclipse/kura/internal/rest/network/configuration/NetworkConfigurationRestService.java index 2bd0ebfa7ef..f0873274ead 100644 --- a/kura/org.eclipse.kura.rest.network.configuration.provider/src/main/java/org/eclipse/kura/internal/rest/network/configuration/NetworkConfigurationRestService.java +++ b/kura/org.eclipse.kura.rest.network.configuration.provider/src/main/java/org/eclipse/kura/internal/rest/network/configuration/NetworkConfigurationRestService.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2021, 2023 Eurotech and/or its affiliates and others + * Copyright (c) 2021, 2024 Eurotech and/or its affiliates and others * * This program and the accompanying materials are made * available under the terms of the Eclipse Public License 2.0 @@ -17,8 +17,10 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; @@ -33,6 +35,7 @@ import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; +import org.apache.commons.lang3.StringUtils; import org.eclipse.kura.KuraErrorCode; import org.eclipse.kura.KuraException; import org.eclipse.kura.configuration.ComponentConfiguration; @@ -58,8 +61,8 @@ public class NetworkConfigurationRestService { private static final Logger logger = LoggerFactory.getLogger(NetworkConfigurationRestService.class); private static final String KURA_PERMISSION_REST_CONFIGURATION_ROLE = "kura.permission.rest.network.configuration"; - private static final List NETWORK_CONFIGURATION_PIDS = Arrays.asList( - "org.eclipse.kura.net.admin.NetworkConfigurationService", + private static final String NETWORK_CONFIGURATION_SERVICE_PID = "org.eclipse.kura.net.admin.NetworkConfigurationService"; + private static final List NETWORK_CONFIGURATION_PIDS = Arrays.asList(NETWORK_CONFIGURATION_SERVICE_PID, "org.eclipse.kura.net.admin.FirewallConfigurationService", "org.eclipse.kura.net.admin.ipv6.FirewallConfigurationServiceIPv6"); private static final String SUBTASK_SNAPSHOT_TAG = "snapshot"; @@ -169,6 +172,7 @@ public Response updateNetworkComponentConfigurations(UpdateComponentConfiguratio failureHandler.runFallibleSubtask("update:" + componentConfig.getPid(), () -> { final Map configurationProperties = DTOUtil .dtosToConfigurationProperties(componentConfig.getProperties()); + updateNetInterfaces(configurationProperties, componentConfig.getPid()); this.configurationService.updateConfiguration(componentConfig.getPid(), configurationProperties, false); }); @@ -273,4 +277,28 @@ private boolean isNetworkConfigurationPid(String pid) { } return result; } + + private void updateNetInterfaces(Map configurationProperties, String pid) throws KuraException { + if (pid.equals(NETWORK_CONFIGURATION_SERVICE_PID)) { + Set interfaceNames = new HashSet<>(); + configurationProperties.keySet().forEach(key -> { + Optional interfaceName = parseInterfaceName(key); + interfaceName.ifPresent(interfaceNames::add); + }); + Map properties = this.configurationService.getComponentConfiguration(pid) + .getConfigurationProperties(); + String netInterfaces = (String) properties.get("net.interfaces"); + List netInterfacesList = Arrays.asList(netInterfaces.split(",")); + for (String name : interfaceNames) { + if (!netInterfacesList.contains(name)) { + netInterfaces += "," + name; + } + } + configurationProperties.put("net.interfaces", netInterfaces); + } + } + + private Optional parseInterfaceName(String key) { + return Optional.ofNullable(StringUtils.substringBetween(key, "net.interface.", ".config.")); + } } diff --git a/kura/test/org.eclipse.kura.rest.network.configuration.provider.test/META-INF/MANIFEST.MF b/kura/test/org.eclipse.kura.rest.network.configuration.provider.test/META-INF/MANIFEST.MF index da058f5694a..60cdc627438 100644 --- a/kura/test/org.eclipse.kura.rest.network.configuration.provider.test/META-INF/MANIFEST.MF +++ b/kura/test/org.eclipse.kura.rest.network.configuration.provider.test/META-INF/MANIFEST.MF @@ -14,8 +14,10 @@ Import-Package: com.eclipsesource.json;version="0.9.5", org.eclipse.kura;version="1.6.0", org.eclipse.kura.configuration, org.eclipse.kura.configuration.metatype;version="1.1.0", + org.eclipse.kura.core.configuration;version="[2.0,3.0)", + org.eclipse.kura.core.configuration.metatype;version="[1.0,2.0)", org.eclipse.kura.core.testutil.json;version="[1.0,2.0)", - org.eclipse.kura.core.testutil.requesthandler;version="[1.1,2.0)", + org.eclipse.kura.core.testutil.requesthandler;version="[1.1,2.0)", org.eclipse.kura.core.testutil.service;version="[1.0,2.0)", org.eclipse.kura.core.util;version="1.3.0", org.eclipse.kura.crypto;version="1.3.0", diff --git a/kura/test/org.eclipse.kura.rest.network.configuration.provider.test/src/main/java/org/eclipse/kura/rest/network/configuration/provider/test/NetworkConfigurationRestServiceTest.java b/kura/test/org.eclipse.kura.rest.network.configuration.provider.test/src/main/java/org/eclipse/kura/rest/network/configuration/provider/test/NetworkConfigurationRestServiceTest.java index 2a79f4e04fc..f907200e1d5 100644 --- a/kura/test/org.eclipse.kura.rest.network.configuration.provider.test/src/main/java/org/eclipse/kura/rest/network/configuration/provider/test/NetworkConfigurationRestServiceTest.java +++ b/kura/test/org.eclipse.kura.rest.network.configuration.provider.test/src/main/java/org/eclipse/kura/rest/network/configuration/provider/test/NetworkConfigurationRestServiceTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2021, 2023 Eurotech and/or its affiliates and others + * Copyright (c) 2021, 2024 Eurotech and/or its affiliates and others * * This program and the accompanying materials are made * available under the terms of the Eclipse Public License 2.0 @@ -28,7 +28,10 @@ import java.util.concurrent.TimeUnit; import org.eclipse.kura.KuraException; +import org.eclipse.kura.configuration.ComponentConfiguration; import org.eclipse.kura.configuration.ConfigurationService; +import org.eclipse.kura.core.configuration.ComponentConfigurationImpl; +import org.eclipse.kura.core.configuration.metatype.ObjectFactory; import org.eclipse.kura.core.testutil.requesthandler.AbstractRequestHandlerTest; import org.eclipse.kura.core.testutil.requesthandler.RestTransport; import org.eclipse.kura.core.testutil.requesthandler.Transport.MethodSpec; @@ -38,7 +41,6 @@ import org.eclipse.kura.rest.network.configuration.provider.test.responses.RestNetworkConfigurationJson; import org.eclipse.kura.util.wire.test.WireTestUtil; import org.junit.BeforeClass; -import org.junit.Ignore; import org.junit.Test; import org.mockito.ArgumentMatchers; import org.mockito.Mockito; @@ -51,7 +53,6 @@ import org.osgi.service.useradmin.User; import org.osgi.service.useradmin.UserAdmin; -@Ignore public class NetworkConfigurationRestServiceTest extends AbstractRequestHandlerTest { private static final String METHOD_SPEC_GET = "GET"; @@ -155,6 +156,21 @@ public void shouldUpdateWithoutErrors() throws KuraException { "1234,tcp,0:0:0:0:0:0:0:0/0,,,,,#"); } + @Test + public void shouldUpdateNetInterfacesProperty() throws KuraException { + givenMockUpdateConfiguration(); + givenMockNetworkConfigurationService(); + givenIdentity("admin", Optional.of("password"), Collections.emptyList()); + givenBasicCredentials(Optional.of("admin:password")); + + whenRequestIsPerformed(new MethodSpec(METHOD_SPEC_PUT), "/configurableComponents/configurations/_update", + RestNetworkConfigurationJson.NETWORK_CONFIGURATION_UPDATE_REQUEST); + + thenRequestSucceeds(); + thenValueIsUpdated(NETWORK_CONF_SERVICE_PID, "net.interface.wlan0.config.ip4.status", "netIPv4StatusDisabled"); + thenValueIsUpdated(NETWORK_CONF_SERVICE_PID, "net.interfaces", "eth0,wlan0"); + } + @Test public void shouldReturnEmptyArray() throws KuraException { givenMockUpdateConfiguration(); @@ -259,6 +275,14 @@ private void givenMockUpdateConfiguration() throws KuraException { .updateConfiguration(ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.anyBoolean()); } + private void givenMockNetworkConfigurationService() throws KuraException { + Map properties = new HashMap<>(); + properties.put("net.interfaces", "eth0"); + ComponentConfiguration componentConfig = new ComponentConfigurationImpl(NETWORK_CONF_SERVICE_PID, + new ObjectFactory().createTocd(), properties); + when(configurationService.getComponentConfiguration(NETWORK_CONF_SERVICE_PID)).thenReturn(componentConfig); + } + /* * Then */ diff --git a/kura/test/org.eclipse.kura.rest.network.configuration.provider.test/src/main/java/org/eclipse/kura/rest/network/configuration/provider/test/responses/RestNetworkConfigurationJson.java b/kura/test/org.eclipse.kura.rest.network.configuration.provider.test/src/main/java/org/eclipse/kura/rest/network/configuration/provider/test/responses/RestNetworkConfigurationJson.java index c833b0d53fd..4af0bf9c946 100644 --- a/kura/test/org.eclipse.kura.rest.network.configuration.provider.test/src/main/java/org/eclipse/kura/rest/network/configuration/provider/test/responses/RestNetworkConfigurationJson.java +++ b/kura/test/org.eclipse.kura.rest.network.configuration.provider.test/src/main/java/org/eclipse/kura/rest/network/configuration/provider/test/responses/RestNetworkConfigurationJson.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2021, 2023 Eurotech and/or its affiliates and others + * Copyright (c) 2021, 2024 Eurotech and/or its affiliates and others * * This program and the accompanying materials are made * available under the terms of the Eclipse Public License 2.0 @@ -25,6 +25,7 @@ private RestNetworkConfigurationJson() { public static final String EMPTY_CONFIGS_REQUEST = "{\"configs\":[]}"; public static final String INVALID_PID_DELETE_REQUEST = "{\"pids\":[\"invalidPid\"]}"; public static final String EMPTY_PIDS_REQUEST = "{\"pids\":[]}"; + public static final String NETWORK_CONFIGURATION_UPDATE_REQUEST = "{\"configs\":[{\"pid\":\"org.eclipse.kura.net.admin.NetworkConfigurationService\",properties: {\"net.interface.wlan0.config.ip4.status\":{\"type\":\"STRING\",\"value\":\"netIPv4StatusDisabled\"}}}]}"; /* * Responses From a659d6e281d70c76cf675bbf2e09856fe3b5e115 Mon Sep 17 00:00:00 2001 From: Mattia Dal Ben Date: Mon, 11 Nov 2024 16:45:05 +0100 Subject: [PATCH 2/2] fix(distrib): revert #5522 (#5533) Revert "fix(distrib): Updated installer script to write all interface names to snapshot file (#5522)" This reverts commit c2ea2ff2cb793a5761b400c7d62f9fa1fabe1b79. --- kura/distrib/src/main/resources/common/customize_snapshot.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kura/distrib/src/main/resources/common/customize_snapshot.py b/kura/distrib/src/main/resources/common/customize_snapshot.py index 292df0e0c7c..22eb56c065d 100644 --- a/kura/distrib/src/main/resources/common/customize_snapshot.py +++ b/kura/distrib/src/main/resources/common/customize_snapshot.py @@ -90,12 +90,12 @@ def main(): interfaces_list = "lo" - for i, eth_name in enumerate(eth_names): + for i, eth_name in enumerate(eth_names[:2]): snapshot_content = snapshot_content.replace('ETH_INTERFACE_' + str(i), eth_name) interfaces_list += "," + eth_name logging.info("%s : replaced ETH_INTERFACE_%s with %s", SNAPSHOT_FILENAME, str(i), eth_name) - for i, wlan_name in enumerate(wlan_names): + for i, wlan_name in enumerate(wlan_names[:1]): snapshot_content = snapshot_content.replace('WIFI_INTERFACE_' + str(i), wlan_name) interfaces_list += "," + wlan_name logging.info("%s : replaced WIFI_INTERFACE_%s with %s", SNAPSHOT_FILENAME, str(i), wlan_name)