Skip to content
Draft
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 @@ -150,7 +150,7 @@ public void execute() {
ConfigurationResponse response = _responseGenerator.createConfigurationResponse(cfg);
response.setResponseName(getCommandName());
response = setResponseScopes(response);
response = setResponseValue(response, cfg);
setResponseValue(response, cfg);
this.setResponseObject(response);
} else {
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to update config");
Expand All @@ -161,15 +161,13 @@ public void execute() {
* Sets the configuration value in the response. If the configuration is in the `Hidden` or `Secure` categories, the value is encrypted before being set in the response.
* @param response to be set with the configuration `cfg` value
* @param cfg to be used in setting the response value
* @return the response with the configuration's value
*/
public ConfigurationResponse setResponseValue(ConfigurationResponse response, Configuration cfg) {
public void setResponseValue(ConfigurationResponse response, Configuration cfg) {
String value = cfg.getValue();
if (cfg.isEncrypted()) {
response.setValue(DBEncryptionUtil.encrypt(getValue()));
} else {
response.setValue(getValue());
value = DBEncryptionUtil.encrypt(value);
}
return response;
response.setValue(value);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.apache.cloudstack.api.command.admin.config;

import org.apache.cloudstack.api.response.ConfigurationResponse;
import org.apache.cloudstack.config.Configuration;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

import com.cloud.utils.crypt.DBEncryptionUtil;

@RunWith(MockitoJUnitRunner.class)
public class UpdateCfgCmdTest {

private UpdateCfgCmd updateCfgCmd;

private MockedStatic<DBEncryptionUtil> mockedStatic;

@Before
public void setUp() {
updateCfgCmd = new UpdateCfgCmd();
mockedStatic = Mockito.mockStatic(DBEncryptionUtil.class);
}

@After
public void tearDown() {
mockedStatic.close();
}

@Test
public void setResponseValueSetsEncryptedValueWhenConfigurationIsEncrypted() {
ConfigurationResponse response = new ConfigurationResponse();
Configuration cfg = Mockito.mock(Configuration.class);
Mockito.when(cfg.isEncrypted()).thenReturn(true);
Mockito.when(cfg.getValue()).thenReturn("testValue");
Mockito.when(DBEncryptionUtil.encrypt("testValue")).thenReturn("encryptedValue");
updateCfgCmd.setResponseValue(response, cfg);
Assert.assertEquals("encryptedValue", response.getValue());
}

@Test
public void setResponseValueSetsPlainValueWhenConfigurationIsNotEncrypted() {
ConfigurationResponse response = new ConfigurationResponse();
Configuration cfg = Mockito.mock(Configuration.class);
Mockito.when(cfg.isEncrypted()).thenReturn(false);
Mockito.when(cfg.getValue()).thenReturn("testValue");
updateCfgCmd.setResponseValue(response, cfg);
Assert.assertEquals("testValue", response.getValue());
}

@Test
public void setResponseValueHandlesNullConfigurationValueGracefully() {
ConfigurationResponse response = new ConfigurationResponse();
Configuration cfg = Mockito.mock(Configuration.class);
Mockito.when(cfg.isEncrypted()).thenReturn(false);
Mockito.when(cfg.getValue()).thenReturn(null);
updateCfgCmd.setResponseValue(response, cfg);
Assert.assertNull(response.getValue());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,19 @@ private void updateCustomDisplayNameOnHypervisorsList(String previousValue, Stri
}
}

protected String getNormalizedEmptyValueForConfig(final String name, final String inputValue,
final Long configStorageId) {
String value = inputValue.trim();
if (!value.isEmpty() && !value.equals("null")) {
return value;
}
if (configStorageId != null) {
return "";
}
ConfigKey<?> key = _configDepot.get(name);
return (key != null && key.type() == String.class) ? "" : null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

edge case: can the default value of a String be NULL?
we have the key here so we could as well check the default value for that. However, when “” is passed is it meant to be a reset or an explicit set to “”…?
I think this is the best we can do now, except maybe for making the value field non-null completely.

}

@Override
@ActionEvent(eventType = EventTypes.EVENT_CONFIGURATION_VALUE_EDIT, eventDescription = "updating configuration")
public Configuration updateConfiguration(final UpdateCfgCmd cmd) throws InvalidParameterValueException {
Expand Down Expand Up @@ -1076,11 +1089,7 @@ public Configuration updateConfiguration(final UpdateCfgCmd cmd) throws InvalidP
throw new InvalidParameterValueException("cannot handle multiple IDs, provide only one ID corresponding to the scope");
}

value = value.trim();

if (value.isEmpty() || value.equals("null")) {
value = (id == null) ? null : "";
}
value = getNormalizedEmptyValueForConfig(name, value, id);

String currentValueInScope = getConfigurationValueInScope(config, name, scope, id);
final String updatedValue = updateConfiguration(userId, name, category, value, scope, id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1072,4 +1072,42 @@ public void serviceOfferingExternalDetailsNeedUpdateReturnsFalseWhenDetailsMatch

Assert.assertFalse(result);
}

@Test
public void normalizedEmptyValueForConfigReturnsTrimmedValueWhenInputIsValid() {
String result = configurationManagerImplSpy.getNormalizedEmptyValueForConfig("someConfig", " validValue ", null);
Assert.assertEquals("validValue", result);
}

@Test
public void normalizedEmptyValueForConfigReturnsNullWhenInputIsNullAndNoConfigStorageId() {
String result = configurationManagerImplSpy.getNormalizedEmptyValueForConfig("someConfig", "null", null);
Assert.assertNull(result);
}

@Test
public void normalizedEmptyValueForConfigReturnsEmptyStringWhenInputIsNullAndConfigStorageIdProvided() {
String result = configurationManagerImplSpy.getNormalizedEmptyValueForConfig("someConfig", "null", 123L);
Assert.assertEquals("", result);
}

@Test
public void normalizedEmptyValueForConfigReturnsEmptyStringWhenKeyTypeIsStringAndInputIsEmpty() {
ConfigKey<String> mockKey = Mockito.mock(ConfigKey.class);
Mockito.when(mockKey.type()).thenReturn(String.class);
Mockito.doReturn(mockKey).when(configDepot).get("someConfig");

String result = configurationManagerImplSpy.getNormalizedEmptyValueForConfig("someConfig", "", null);
Assert.assertEquals("", result);
}

@Test
public void normalizedEmptyValueForConfigReturnsNullWhenKeyTypeIsNotStringAndInputIsEmpty() {
ConfigKey<Integer> mockKey = Mockito.mock(ConfigKey.class);
Mockito.when(mockKey.type()).thenReturn(Integer.class);
Mockito.doReturn(mockKey).when(configDepot).get("someConfig");

String result = configurationManagerImplSpy.getNormalizedEmptyValueForConfig("someConfig", "", null);
Assert.assertNull(result);
}
}
Loading