Skip to content

Commit

Permalink
feat: Session REST API improvements (eclipse-kura#4947)
Browse files Browse the repository at this point in the history
* 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
nicolatimeus authored Nov 6, 2023
1 parent 1fb4c42 commit 087b214
Show file tree
Hide file tree
Showing 24 changed files with 620 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Import-Package: javax.annotation.security;version="1.2.0",
org.eclipse.kura.rest.configuration.api;version="[1.0,2.0)",
org.eclipse.kura.util.configuration;version="[1.0,2.0)",
org.eclipse.kura.util.useradmin;version="[1.0,2.0)",
org.eclipse.kura.util.validation;version="[1.0,2.0)",
org.osgi.framework;version="1.8.0",
org.osgi.service.cm;version="1.6.0",
org.osgi.service.component;version="[1.3,2.0)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@
import org.eclipse.kura.internal.rest.identity.provider.dto.UserConfigDTO;
import org.eclipse.kura.internal.rest.identity.provider.dto.UserDTO;
import org.eclipse.kura.internal.rest.identity.provider.dto.ValidatorOptionsDTO;
import org.eclipse.kura.internal.rest.identity.provider.validator.ValidatorOptions;
import org.eclipse.kura.request.handler.jaxrs.DefaultExceptionHandler;
import org.eclipse.kura.request.handler.jaxrs.JaxRsRequestHandlerProxy;
import org.eclipse.kura.util.validation.ValidatorOptions;
import org.osgi.service.useradmin.Role;
import org.osgi.service.useradmin.UserAdmin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SuppressWarnings("restriction")
@Path("identity/v1")
public class IdentityRestService {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
import org.eclipse.kura.configuration.ConfigurationService;
import org.eclipse.kura.crypto.CryptoService;
import org.eclipse.kura.internal.rest.identity.provider.dto.UserDTO;
import org.eclipse.kura.internal.rest.identity.provider.validator.PasswordStrengthValidators;
import org.eclipse.kura.internal.rest.identity.provider.validator.Validator;
import org.eclipse.kura.internal.rest.identity.provider.validator.ValidatorOptions;
import org.eclipse.kura.util.useradmin.UserAdminHelper;
import org.eclipse.kura.util.useradmin.UserAdminHelper.FallibleConsumer;
import org.eclipse.kura.util.validation.PasswordStrengthValidators;
import org.eclipse.kura.util.validation.Validator;
import org.eclipse.kura.util.validation.ValidatorOptions;
import org.osgi.service.useradmin.Role;
import org.osgi.service.useradmin.User;
import org.osgi.service.useradmin.UserAdmin;
Expand Down
2 changes: 2 additions & 0 deletions kura/org.eclipse.kura.rest.provider/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ Import-Package: com.eclipsesource.jaxrs.provider.security;version="2.2.0",
org.eclipse.kura.request.handler.jaxrs;version="[1.0,2.0)",
org.eclipse.kura.util.configuration;version="[1.0,2.0)",
org.eclipse.kura.util.useradmin;version="[1.0,2.0)",
org.eclipse.kura.util.validation;version="[1.0,2.0)",
org.osgi.annotation.versioning;version="[1.0.0,2.0.0)";resolution:=optional,
org.osgi.framework;version="1.10.0",
org.osgi.service.cm;version="1.6.0",
org.osgi.service.useradmin;version="1.1.0",
org.osgi.util.tracker;version="1.5.2",
org.slf4j;version="1.7.21"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@
<reference bind="setCryptoService" cardinality="1..1" interface="org.eclipse.kura.crypto.CryptoService" name="CryptoService" policy="static" />
<reference bind="setUserAdmin" cardinality="1..1" interface="org.osgi.service.useradmin.UserAdmin" name="UserAdmin" policy="static" />
<reference bind="bindAuthenticationProvider" cardinality="0..n" interface="org.eclipse.kura.rest.auth.AuthenticationProvider" name="AuthenticationProvider" policy="dynamic" unbind="unbindAuthenticationProvider" />
</scr:component>
<reference bind="setConfigurationAdmin" cardinality="1..1" interface="org.osgi.service.cm.ConfigurationAdmin" name="ConfigurationAdmin" policy="static"/>
</scr:component>
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;
}
}
Loading

0 comments on commit 087b214

Please sign in to comment.