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

fix(deviceManagementConfiguration): fixed serialization of DeviceConfiguration of Password properties #3965

Merged
Merged
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
Expand Up @@ -32,6 +32,11 @@
return Password.class.equals(objectClass);
}

@Override
public boolean doesEncrypt() {
return true;

Check warning on line 37 in commons/src/main/java/org/eclipse/kapua/commons/configuration/metatype/PasswordPropertyAdapter.java

View check run for this annotation

Codecov / codecov/patch

commons/src/main/java/org/eclipse/kapua/commons/configuration/metatype/PasswordPropertyAdapter.java#L37

Added line #L37 was not covered by tests
}

@Override
public String marshallValue(Object value) {
return cryptoUtil.encodeBase64(value.toString());
Expand All @@ -42,6 +47,7 @@
return new Password(cryptoUtil.decodeBase64(value));
}

@Override
public Object unmarshallValues(XmlPropertyAdapted<?> property) {
if (!property.getArray()) {
return property.isEncrypted() ? unmarshallValue(property.getValues()[0]) : new Password(property.getValues()[0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,20 @@
return objectClass.isArray() ? clazz.equals(objectClass.getComponentType()) : clazz.equals(objectClass);
}

@Override
public boolean doesEncrypt() {
return false;
}

@Override
public void marshallValues(XmlPropertyAdapted<?> property, Object value) {
if (!value.getClass().isArray()) {
property.setArray(false);
property.setEncrypted(false);
property.setEncrypted(doesEncrypt());

Check warning on line 44 in service/api/src/main/java/org/eclipse/kapua/model/xml/adapters/ClassBasedXmlPropertyAdapterBase.java

View check run for this annotation

Codecov / codecov/patch

service/api/src/main/java/org/eclipse/kapua/model/xml/adapters/ClassBasedXmlPropertyAdapterBase.java#L44

Added line #L44 was not covered by tests
property.setValues(new String[]{marshallValue(value)});
} else {
property.setArray(true);
property.setEncrypted(doesEncrypt());
Object[] nativeValues = (Object[]) value;
String[] stringValues = new String[nativeValues.length];
for (int i = 0; i < nativeValues.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
public interface XmlPropertyAdapter {
boolean canMarshall(Class<?> objectClass);

boolean doesEncrypt();

void marshallValues(XmlPropertyAdapted<?> property, Object value);

Object unmarshallValues(XmlPropertyAdapted<?> property);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*******************************************************************************
* Copyright (c) 2016, 2022 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 - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.service.device.call.kura.model.configuration.xml;

import org.eclipse.kapua.commons.crypto.CryptoUtil;
import org.eclipse.kapua.model.xml.XmlPropertyAdapted;
import org.eclipse.kapua.model.xml.adapters.ClassBasedXmlPropertyAdapterBase;
import org.eclipse.kapua.service.device.call.kura.model.configuration.KuraPassword;

import java.util.Arrays;
import java.util.stream.Collectors;

public class KuraPasswordPropertyAdapter extends ClassBasedXmlPropertyAdapterBase<KuraPassword> {
private final CryptoUtil cryptoUtil;

public KuraPasswordPropertyAdapter(CryptoUtil cryptoUtil) {
super(KuraPassword.class);
this.cryptoUtil = cryptoUtil;
}

@Override
public boolean canMarshall(Class objectClass) {
return KuraPassword.class.equals(objectClass);

Check warning on line 33 in service/device/call/kura/src/main/java/org/eclipse/kapua/service/device/call/kura/model/configuration/xml/KuraPasswordPropertyAdapter.java

View check run for this annotation

Codecov / codecov/patch

service/device/call/kura/src/main/java/org/eclipse/kapua/service/device/call/kura/model/configuration/xml/KuraPasswordPropertyAdapter.java#L33

Added line #L33 was not covered by tests
}

@Override
public boolean doesEncrypt() {
return true;

Check warning on line 38 in service/device/call/kura/src/main/java/org/eclipse/kapua/service/device/call/kura/model/configuration/xml/KuraPasswordPropertyAdapter.java

View check run for this annotation

Codecov / codecov/patch

service/device/call/kura/src/main/java/org/eclipse/kapua/service/device/call/kura/model/configuration/xml/KuraPasswordPropertyAdapter.java#L38

Added line #L38 was not covered by tests
}

@Override
public String marshallValue(Object value) {
return cryptoUtil.encodeBase64(value.toString());

Check warning on line 43 in service/device/call/kura/src/main/java/org/eclipse/kapua/service/device/call/kura/model/configuration/xml/KuraPasswordPropertyAdapter.java

View check run for this annotation

Codecov / codecov/patch

service/device/call/kura/src/main/java/org/eclipse/kapua/service/device/call/kura/model/configuration/xml/KuraPasswordPropertyAdapter.java#L43

Added line #L43 was not covered by tests
}

@Override
public KuraPassword unmarshallValue(String value) {
return new KuraPassword(cryptoUtil.decodeBase64(value));

Check warning on line 48 in service/device/call/kura/src/main/java/org/eclipse/kapua/service/device/call/kura/model/configuration/xml/KuraPasswordPropertyAdapter.java

View check run for this annotation

Codecov / codecov/patch

service/device/call/kura/src/main/java/org/eclipse/kapua/service/device/call/kura/model/configuration/xml/KuraPasswordPropertyAdapter.java#L48

Added line #L48 was not covered by tests
}

@Override
public Object unmarshallValues(XmlPropertyAdapted<?> property) {
if (!property.getArray()) {
return property.isEncrypted() ? unmarshallValue(property.getValues()[0]) : new KuraPassword(property.getValues()[0]);
} else {
return Arrays
.stream(property.getValues())

Check warning on line 57 in service/device/call/kura/src/main/java/org/eclipse/kapua/service/device/call/kura/model/configuration/xml/KuraPasswordPropertyAdapter.java

View check run for this annotation

Codecov / codecov/patch

service/device/call/kura/src/main/java/org/eclipse/kapua/service/device/call/kura/model/configuration/xml/KuraPasswordPropertyAdapter.java#L56-L57

Added lines #L56 - L57 were not covered by tests
.map(value -> property.isEncrypted() ? unmarshallValue(value) : new KuraPassword(value))
.collect(Collectors.toList()).toArray();

Check warning on line 59 in service/device/call/kura/src/main/java/org/eclipse/kapua/service/device/call/kura/model/configuration/xml/KuraPasswordPropertyAdapter.java

View check run for this annotation

Codecov / codecov/patch

service/device/call/kura/src/main/java/org/eclipse/kapua/service/device/call/kura/model/configuration/xml/KuraPasswordPropertyAdapter.java#L59

Added line #L59 was not covered by tests
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*******************************************************************************/
package org.eclipse.kapua.service.device.call.kura.model.configuration.xml;

import org.eclipse.kapua.commons.configuration.metatype.PasswordPropertyAdapter;
import org.eclipse.kapua.commons.crypto.CryptoUtil;
import org.eclipse.kapua.locator.KapuaLocator;
import org.eclipse.kapua.model.xml.adapters.BooleanPropertyAdapter;
Expand Down Expand Up @@ -50,7 +49,7 @@ public class KuraXmlConfigPropertiesAdapter extends XmlAdapter<KuraXmlConfigProp
put(ConfigPropertyType.charType, new CharPropertyAdapter());
put(ConfigPropertyType.booleanType, new BooleanPropertyAdapter());
put(ConfigPropertyType.shortType, new ShortPropertyAdapter());
put(ConfigPropertyType.passwordType, new PasswordPropertyAdapter(KapuaLocator.getInstance().getComponent(CryptoUtil.class)));
put(ConfigPropertyType.passwordType, new KuraPasswordPropertyAdapter(KapuaLocator.getInstance().getComponent(CryptoUtil.class)));
}
});

Expand Down
Loading