forked from eclipse-kura/kura
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Session REST API improvements (eclipse-kura#4947)
* feat: Session REST API improvements Signed-off-by: Nicola Timeus <[email protected]> * Return all certificate auth ports Signed-off-by: Nicola Timeus <[email protected]> * Addressed comments --------- Signed-off-by: Nicola Timeus <[email protected]>
- Loading branch information
1 parent
1fb4c42
commit 087b214
Showing
24 changed files
with
620 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
....provider/src/main/java/org/eclipse/kura/internal/rest/auth/ConfigurationAdminHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023 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 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Eurotech | ||
*******************************************************************************/ | ||
package org.eclipse.kura.internal.rest.auth; | ||
|
||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.Dictionary; | ||
import java.util.Enumeration; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
import org.osgi.service.cm.Configuration; | ||
import org.osgi.service.cm.ConfigurationAdmin; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class ConfigurationAdminHelper { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(ConfigurationAdminHelper.class); | ||
|
||
private ConfigurationAdminHelper() { | ||
} | ||
|
||
public static Map<String, Object> loadConfigurationProperties(final ConfigurationAdmin configurationAdmin, | ||
final String pid) { | ||
|
||
return getConfiguration(configurationAdmin, pid) | ||
.map(Configuration::getProperties).map(ConfigurationAdminHelper::dictionaryToMap) | ||
.orElseGet(() -> new HashMap<>()); | ||
} | ||
|
||
public static Map<String, Object> loadConsoleConfigurationProperties(final ConfigurationAdmin configurationAdmin) { | ||
|
||
return loadConfigurationProperties(configurationAdmin, "org.eclipse.kura.web.Console"); | ||
} | ||
|
||
public static Map<String, Object> loadHttpServiceConfigurationProperties( | ||
final ConfigurationAdmin configurationAdmin) { | ||
|
||
return loadConfigurationProperties(configurationAdmin, "org.eclipse.kura.http.server.manager.HttpService"); | ||
} | ||
|
||
public static Optional<String> getLoginMessage(final Map<String, Object> properties) { | ||
final Object messageEnabled = properties.get("access.banner.enabled"); | ||
final Object message = properties.get("access.banner.content"); | ||
|
||
if (!Boolean.TRUE.equals(messageEnabled)) { | ||
return Optional.empty(); | ||
} | ||
|
||
if (!(message instanceof String)) { | ||
return Optional.empty(); | ||
} | ||
|
||
return Optional.of((String) message); | ||
} | ||
|
||
public static Set<Integer> getHttpsMutualAuthPorts(final Map<String, Object> properties) { | ||
final Object rawPortList = properties.get("https.client.auth.ports"); | ||
|
||
if (!(rawPortList instanceof Integer[])) { | ||
return Collections.emptySet(); | ||
} | ||
|
||
final Integer[] portList = (Integer[]) rawPortList; | ||
|
||
return Arrays.stream(portList).filter(Objects::nonNull).collect(Collectors.toSet()); | ||
} | ||
|
||
private static Optional<Configuration> getConfiguration(final ConfigurationAdmin configurationAdmin, | ||
final String pid) { | ||
try { | ||
return Optional | ||
.ofNullable(configurationAdmin.getConfiguration(pid, "?")); | ||
} catch (final IOException e) { | ||
logger.warn("Failed to retrieve configuration for {}", pid, e); | ||
return Optional.empty(); | ||
} | ||
} | ||
|
||
private static final Map<String, Object> dictionaryToMap(final Dictionary<String, Object> dict) { | ||
final Map<String, Object> result = new HashMap<>(dict.size()); | ||
|
||
final Enumeration<String> keys = dict.keys(); | ||
|
||
while (keys.hasMoreElements()) { | ||
final String key = keys.nextElement(); | ||
|
||
result.put(key, dict.get(key)); | ||
} | ||
|
||
return result; | ||
} | ||
} |
Oops, something went wrong.