Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Set default web ports for console and rest #5624

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2011, 2022 Eurotech and/or its affiliates and others
* Copyright (c) 2011, 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
Expand Down Expand Up @@ -1247,8 +1247,8 @@ private ComponentConfiguration getSelfConfiguringComponentConfiguration(String p
cc = selfConfigComp.getConfiguration();
if (cc.getPid() == null || !cc.getPid().equals(pid)) {
logger.error(
"Invalid pid for returned Configuration of SelfConfiguringComponent with pid: "
+ pid + ". Ignoring it.");
"Invalid pid for returned Configuration of SelfConfiguringComponent with pid: {}. Ignoring it.",
pid);
return null;
}

Expand Down Expand Up @@ -1278,31 +1278,22 @@ private ComponentConfiguration getSelfConfiguringComponentConfiguration(String p
if (props != null) {
Object value = props.get(adId);
if (value != null) {
String propType;
if (!value.getClass().isArray()) {
propType = value.getClass().getSimpleName();
} else {
propType = value.getClass().getComponentType()
.getSimpleName();
}

try {
logger.debug(
"pid: {}, property name: {}, type: {}, value: {}",
pid, adId, propType, value);
Scalar propertyScalar = Scalar.fromValue(propType);
logger.debug("pid: {}, property name: {}, value: {}", pid,
adId, value);
Scalar propertyScalar = getScalarFromObject(value);
Scalar adScalar = Scalar.fromValue(adType);
if (propertyScalar != adScalar) {
logger.error(
"Type: {} for property named: {} does not match the AD type: {} for returned Configuration of SelfConfiguringComponent with pid: {}",
new Object[] { propType, adId, adType, pid });
propertyScalar.name(), adId, adType, pid);
return null;
}
} catch (IllegalArgumentException e) {
logger.error(
"Invalid class: {} for property named: {} for returned Configuration of SelfConfiguringComponent with pid: "
+ pid,
propType, adId);
"Invalid class for property named: {} for returned Configuration of SelfConfiguringComponent with pid: {}",
adId, pid);
return null;
}
}
Expand All @@ -1329,6 +1320,19 @@ private ComponentConfiguration getSelfConfiguringComponentConfiguration(String p
return cc;
}

private Scalar getScalarFromObject(Object p) {
Class<?> clazz = p.getClass();
if (clazz.isArray()) {
Object[] tempArray = (Object[]) p;
if (tempArray.length > 0 && tempArray[0] != null) {
clazz = tempArray[0].getClass();
} else {
clazz = clazz.getComponentType();
}
}
return Scalar.fromValue(clazz.getSimpleName());
}

private TreeSet<Long> getSnapshotsInternal() {
// keeps the list of snapshots ordered
TreeSet<Long> ids = new TreeSet<>();
Expand Down Expand Up @@ -1901,10 +1905,7 @@ public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
if (obj == null || getClass() != obj.getClass()) {
return false;
}
TrackedComponentFactory other = (TrackedComponentFactory) obj;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*******************************************************************************
* Copyright (c) 2021, 2022 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
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
*
* SPDX-License-Identifier: EPL-2.0
*
*
* Contributors:
* Eurotech
*******************************************************************************/
Expand Down Expand Up @@ -45,10 +45,10 @@ public Object getValue() {
public void validate() {
FailureHandler.requireParameter(this.type, "type");

if (value instanceof List<?>) {
validateArrayProperty(type, (List<?>) value);
if (this.value instanceof List<?>) {
validateArrayProperty(this.type, (List<?>) this.value);
} else {
validateSingletonProperty(type, value);
validateSingletonProperty(this.type, this.value);
}
}

Expand Down Expand Up @@ -96,22 +96,35 @@ private static void validateSingletonProperty(final Scalar type, final Object va

public static Optional<PropertyDTO> fromConfigurationProperty(final Object property) {

return Optional.ofNullable(property).flatMap(p -> scalarFromClass(p.getClass()))
return Optional.ofNullable(property).flatMap(PropertyDTO::getScalarFromObject)
.map(type -> new PropertyDTO(configurationPropertyToDTOProperty(property), type));
}

private static Optional<Scalar> getScalarFromObject(Object p) {
Class<?> clazz = p.getClass();
if (clazz.isArray()) {
Object[] tempArray = (Object[]) p;
if (tempArray.length > 0 && tempArray[0] != null) {
clazz = tempArray[0].getClass();
} else {
clazz = clazz.getComponentType();
}
}
return scalarFromSingletonClass(clazz);
}

public Optional<Object> toConfigurationProperty() {
if (value == null) {
if (this.value == null) {
return Optional.empty();
}

final Optional<Object> asSingleton = singletonToProperty(value, type);
final Optional<Object> asSingleton = singletonToProperty(this.value, this.type);

if (asSingleton.isPresent()) {
return asSingleton;
}

return arrayToProperty(value, type);
return arrayToProperty(this.value, this.type);
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -233,7 +246,7 @@ private static Optional<Object> arrayToProperty(final Object propertyValue, fina
}
}

public static Optional<Scalar> scalarFormSingletonClass(final Class<?> clazz) {
public static Optional<Scalar> scalarFromSingletonClass(final Class<?> clazz) {
final Scalar result;

if (clazz == Boolean.class) {
Expand Down Expand Up @@ -263,14 +276,6 @@ public static Optional<Scalar> scalarFormSingletonClass(final Class<?> clazz) {
return Optional.of(result);
}

public static Optional<Scalar> scalarFromClass(final Class<?> clazz) {
if (clazz.isArray()) {
return scalarFormSingletonClass(clazz.getComponentType());
} else {
return scalarFormSingletonClass(clazz);
}
}

private static Object configurationPropertyToDTOProperty(final Object property) {
if (property instanceof Password) {
return new String(((Password) property).getPassword());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
type="Integer"
cardinality="3"
required="false"
default="443,4443"
min="1"
max="65535"
description="If set to a non empty list, REST API access will be allowed only on the specified ports. If set to an empty list, access will be allowed on all ports. Please make sure that the allowed ports are open in HttpService and Firewall configuration.">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public class ConsoleOptions {
new AdBuilder("allowed.ports", "Allowed ports", Tscalar.INTEGER) //
.setRequired(false) //
.setCardinality(3) //
.setDefault("443,4443") //
.setDescription(
"If set to a non empty list, Web Console access will be allowed only on the specified ports. If set to an empty list, access will be allowed on all ports. Please make sure that the allowed ports are open in HttpService and Firewall configuration.") //
.build(), //
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*******************************************************************************
* Copyright (c) 2020 Eurotech and/or its affiliates and others
*
* Copyright (c) 2020, 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
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
*
* SPDX-License-Identifier: EPL-2.0
*
*
* Contributors:
* Eurotech
*******************************************************************************/
Expand Down Expand Up @@ -52,15 +52,15 @@ private SelfConfiguringComponentProperty(final Tad ad, final Class<T> classz,
}

public Tad getAd() {
return ad;
return this.ad;
}

public void fillValue(final Map<String, Object> properties) {
if (value.isPresent() && !properties.containsKey(ad.getId())) {
if (ad.getType() == Scalar.PASSWORD) {
properties.put(this.ad.getId(), new Password(value.get().toString().toCharArray()));
if (this.value.isPresent() && !properties.containsKey(this.ad.getId())) {
if (this.ad.getType() == Scalar.PASSWORD) {
properties.put(this.ad.getId(), new Password(this.value.get().toString().toCharArray()));
} else {
properties.put(this.ad.getId(), value.get());
properties.put(this.ad.getId(), this.value.get());
}
}
}
Expand All @@ -74,16 +74,16 @@ public void update(final Map<String, Object> properties) {
} else if (this.ad.getType() == Scalar.PASSWORD && providedValue instanceof Password) {
final Password providedPassword = (Password) providedValue;

this.value = Optional.of((T) new String((providedPassword.getPassword())));
this.value = Optional.of((T) new String(providedPassword.getPassword()));
}
}

public T get() {
return value.orElseThrow(() -> new IllegalStateException("property value has not been set"));
return this.value.orElseThrow(() -> new IllegalStateException("property value has not been set"));
}

public Optional<T> getOptional() {
return value;
return this.value;
}

private static void check(final Scalar scalar, final int cardinality, final Class<?> clazz) {
Expand Down Expand Up @@ -130,40 +130,64 @@ private static void check(final Scalar scalar, final int cardinality, final Clas

private Object extractScalar(final Scalar scalar, final String value) {
if (scalar == Scalar.BOOLEAN) {
return (Boolean) Boolean.parseBoolean(value);
return Boolean.parseBoolean(value);
} else if (scalar == Scalar.BYTE) {
return (Boolean) Boolean.parseBoolean(value);
return Byte.parseByte(value);
} else if (scalar == Scalar.CHAR) {
return (Character) value.charAt(0);
return value.charAt(0);
} else if (scalar == Scalar.DOUBLE) {
return (Double) Double.parseDouble(value);
return Double.parseDouble(value);
} else if (scalar == Scalar.FLOAT) {
return (Float) Float.parseFloat(value);
return Float.parseFloat(value);
} else if (scalar == Scalar.INTEGER) {
return (Integer) Integer.parseInt(value);
return Integer.parseInt(value);
} else if (scalar == Scalar.LONG) {
return (Long) Long.parseLong(value);
return Long.parseLong(value);
} else if (scalar == Scalar.PASSWORD) {
try {
return new String(unwrapCryptoService().encryptAes(value.toCharArray()));
} catch (KuraException e) {
throw new IllegalStateException("failed to encrypt password", e);
}
} else if (scalar == Scalar.SHORT) {
return (Short) Short.parseShort(value);
return Short.parseShort(value);
} else if (scalar == Scalar.STRING) {
return (String) value;
return value;
} else {
throw new IllegalArgumentException(scalar == null ? null : scalar.toString());
}
}

private Object createScalarArray(final Scalar scalar) {
if (scalar == Scalar.BOOLEAN) {
return new Boolean[0];
} else if (scalar == Scalar.BYTE) {
return new Byte[0];
} else if (scalar == Scalar.CHAR) {
return new Character[0];
} else if (scalar == Scalar.DOUBLE) {
return new Double[0];
} else if (scalar == Scalar.FLOAT) {
return new Float[0];
} else if (scalar == Scalar.INTEGER) {
return new Integer[0];
} else if (scalar == Scalar.LONG) {
return new Long[0];
} else if (scalar == Scalar.SHORT) {
return new Short[0];
} else if (scalar == Scalar.STRING || scalar == Scalar.PASSWORD) {
return new String[0];
} else {
throw new IllegalArgumentException(scalar == null ? null : scalar.toString());
}
}

private CryptoService unwrapCryptoService() {
if (!cryptoService.isPresent()) {
if (!this.cryptoService.isPresent()) {
throw new IllegalArgumentException("CryptoService is required for defining a password property");
}

return cryptoService.get();
return this.cryptoService.get();
}

private Optional<T> extractDefault(final AD ad) {
Expand All @@ -180,10 +204,10 @@ private Optional<T> extractDefault(final AD ad) {
if (cardinality == 0) {
return Optional.of((T) extractScalar(scalar, defaultValue));
} else {
final List<?> result = COMMA.splitAsStream(defaultValue).map(String::trim).filter(String::isEmpty)
final List<?> result = COMMA.splitAsStream(defaultValue).map(String::trim).filter(s -> !s.isEmpty())
.map(s -> extractScalar(scalar, s)).collect(Collectors.toList());

return Optional.of((T) result.toArray());
return Optional.of((T) result.toArray((T[]) createScalarArray(scalar)));
}

}
Expand Down
Loading
Loading