Skip to content

Commit 12f4321

Browse files
lucas-a-martinslucas.martins.sccloudsGutoVeronezierikbocks
authored
Changes error message when using invalid endpoint.url (#8603)
Co-authored-by: lucas.martins.scclouds <lucas.martins@scclouds.com.br> Co-authored-by: Daniel Augusto Veronezi Salvador <38945620+GutoVeronezi@users.noreply.github.com> Co-authored-by: erikbocks <erik.bock@outlook.com>
1 parent a0ba2aa commit 12f4321

File tree

6 files changed

+122
-22
lines changed

6 files changed

+122
-22
lines changed

api/src/main/java/org/apache/cloudstack/config/ApiServiceConfiguration.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,15 @@
1616
// under the License.
1717
package org.apache.cloudstack.config;
1818

19+
import com.cloud.exception.InvalidParameterValueException;
1920
import org.apache.cloudstack.framework.config.ConfigKey;
2021
import org.apache.cloudstack.framework.config.Configurable;
22+
import org.apache.commons.lang3.StringUtils;
23+
import org.apache.logging.log4j.LogManager;
24+
import org.apache.logging.log4j.Logger;
2125

2226
public class ApiServiceConfiguration implements Configurable {
27+
protected static Logger LOGGER = LogManager.getLogger(ApiServiceConfiguration.class);
2328
public static final ConfigKey<String> ManagementServerAddresses = new ConfigKey<>(String.class, "host", "Advanced", "localhost", "The ip address of management server. This can also accept comma separated addresses.", true, ConfigKey.Scope.Global, null, null, null, null, null, ConfigKey.Kind.CSV, null);
2429
public static final ConfigKey<String> ApiServletPath = new ConfigKey<String>("Advanced", String.class, "endpoint.url", "http://localhost:8080/client/api",
2530
"API end point. Can be used by CS components/services deployed remotely, for sending CS API requests", true);
@@ -29,6 +34,20 @@ public class ApiServiceConfiguration implements Configurable {
2934
"true", "Are the source checks on API calls enabled (true) or not (false)? See api.allowed.source.cidr.list", true, ConfigKey.Scope.Global);
3035
public static final ConfigKey<String> ApiAllowedSourceCidrList = new ConfigKey<>(String.class, "api.allowed.source.cidr.list", "Advanced",
3136
"0.0.0.0/0,::/0", "Comma separated list of IPv4/IPv6 CIDRs from which API calls can be performed. Can be set on Global and Account levels.", true, ConfigKey.Scope.Account, null, null, null, null, null, ConfigKey.Kind.CSV, null);
37+
38+
39+
public static void validateEndpointUrl() {
40+
String csUrl = getApiServletPathValue();
41+
if (StringUtils.isBlank(csUrl) || StringUtils.containsAny(csUrl, "localhost", "127.0.0.1", "[::1]")) {
42+
LOGGER.error("Global setting [{}] cannot contain localhost or be blank. Current value: {}", ApiServletPath.key(), csUrl);
43+
throw new InvalidParameterValueException("Unable to complete this operation. Contact your cloud admin.");
44+
}
45+
}
46+
47+
public static String getApiServletPathValue() {
48+
return ApiServletPath.value();
49+
}
50+
3251
@Override
3352
public String getConfigComponentName() {
3453
return ApiServiceConfiguration.class.getSimpleName();
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.apache.cloudstack.config;
19+
20+
import com.cloud.exception.InvalidParameterValueException;
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
import org.mockito.MockedStatic;
24+
import org.mockito.Mockito;
25+
import org.mockito.junit.MockitoJUnitRunner;
26+
27+
@RunWith(MockitoJUnitRunner.class)
28+
public class ApiServiceConfigurationTest {
29+
30+
private static final String LOCALHOST = "http://localhost";
31+
32+
private static final String ENDPOINT_URL = "https://acs.clouds.com/client/api";
33+
34+
private static final String WHITE_SPACE = " ";
35+
36+
private static final String BLANK_STRING = "";
37+
38+
private static final String NULL_STRING = null;
39+
40+
private static final String LOCALHOST_IP = "127.0.0.1";
41+
42+
@Test(expected = InvalidParameterValueException.class)
43+
public void validateEndpointUrlTestIfEndpointUrlContainLocalhostShouldThrowInvalidParameterValueException() {
44+
try (MockedStatic<ApiServiceConfiguration> apiServiceConfigurationMockedStatic = Mockito.mockStatic(ApiServiceConfiguration.class)) {
45+
apiServiceConfigurationMockedStatic.when(ApiServiceConfiguration::getApiServletPathValue).thenReturn(LOCALHOST);
46+
apiServiceConfigurationMockedStatic.when(ApiServiceConfiguration::validateEndpointUrl).thenCallRealMethod();
47+
ApiServiceConfiguration.validateEndpointUrl();
48+
}
49+
}
50+
51+
@Test
52+
public void validateEndpointUrlTestIfEndpointUrlContainLocalhostShouldNotThrowInvalidParameterValueException() {
53+
try (MockedStatic<ApiServiceConfiguration> apiServiceConfigurationMockedStatic = Mockito.mockStatic(ApiServiceConfiguration.class)) {
54+
apiServiceConfigurationMockedStatic.when(ApiServiceConfiguration::getApiServletPathValue).thenReturn(ENDPOINT_URL);
55+
apiServiceConfigurationMockedStatic.when(ApiServiceConfiguration::validateEndpointUrl).thenCallRealMethod();
56+
ApiServiceConfiguration.validateEndpointUrl();
57+
}
58+
}
59+
60+
@Test(expected = InvalidParameterValueException.class)
61+
public void validateEndpointUrlTestIfEndpointUrlIsNullShouldThrowInvalidParameterValueException() {
62+
try (MockedStatic<ApiServiceConfiguration> apiServiceConfigurationMockedStatic = Mockito.mockStatic(ApiServiceConfiguration.class)) {
63+
apiServiceConfigurationMockedStatic.when(ApiServiceConfiguration::getApiServletPathValue).thenReturn(NULL_STRING);
64+
apiServiceConfigurationMockedStatic.when(ApiServiceConfiguration::validateEndpointUrl).thenCallRealMethod();
65+
ApiServiceConfiguration.validateEndpointUrl();
66+
}
67+
}
68+
69+
@Test(expected = InvalidParameterValueException.class)
70+
public void validateEndpointUrlTestIfEndpointUrlIsBlankShouldThrowInvalidParameterValueException() {
71+
try (MockedStatic<ApiServiceConfiguration> apiServiceConfigurationMockedStatic = Mockito.mockStatic(ApiServiceConfiguration.class)) {
72+
apiServiceConfigurationMockedStatic.when(ApiServiceConfiguration::getApiServletPathValue).thenReturn(BLANK_STRING);
73+
apiServiceConfigurationMockedStatic.when(ApiServiceConfiguration::validateEndpointUrl).thenCallRealMethod();
74+
ApiServiceConfiguration.validateEndpointUrl();
75+
}
76+
}
77+
78+
@Test(expected = InvalidParameterValueException.class)
79+
public void validateEndpointUrlTestIfEndpointUrlIsWhiteSpaceShouldThrowInvalidParameterValueException() {
80+
try (MockedStatic<ApiServiceConfiguration> apiServiceConfigurationMockedStatic = Mockito.mockStatic(ApiServiceConfiguration.class)) {
81+
apiServiceConfigurationMockedStatic.when(ApiServiceConfiguration::getApiServletPathValue).thenReturn(WHITE_SPACE);
82+
apiServiceConfigurationMockedStatic.when(ApiServiceConfiguration::validateEndpointUrl).thenCallRealMethod();
83+
ApiServiceConfiguration.validateEndpointUrl();
84+
}
85+
}
86+
87+
@Test(expected = InvalidParameterValueException.class)
88+
public void validateEndpointUrlTestIfEndpointUrlContainLocalhostIpShouldThrowInvalidParameterValueException() {
89+
try (MockedStatic<ApiServiceConfiguration> apiServiceConfigurationMockedStatic = Mockito.mockStatic(ApiServiceConfiguration.class)) {
90+
apiServiceConfigurationMockedStatic.when(ApiServiceConfiguration::getApiServletPathValue).thenReturn(LOCALHOST_IP);
91+
apiServiceConfigurationMockedStatic.when(ApiServiceConfiguration::validateEndpointUrl).thenCallRealMethod();
92+
ApiServiceConfiguration.validateEndpointUrl();
93+
}
94+
}
95+
}

plugins/integrations/kubernetes-service/src/main/java/com/cloud/kubernetes/cluster/KubernetesClusterManagerImpl.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -905,15 +905,6 @@ public KubernetesClusterResponse createKubernetesClusterResponse(long kubernetes
905905
return response;
906906
}
907907

908-
private void validateEndpointUrl() {
909-
String csUrl = ApiServiceConfiguration.ApiServletPath.value();
910-
if (csUrl == null || csUrl.contains("localhost")) {
911-
String error = String.format("Global setting %s has to be set to the Management Server's API end point",
912-
ApiServiceConfiguration.ApiServletPath.key());
913-
throw new InvalidParameterValueException(error);
914-
}
915-
}
916-
917908
private DataCenter validateAndGetZoneForKubernetesCreateParameters(Long zoneId, Long networkId) {
918909
DataCenter zone = dataCenterDao.findById(zoneId);
919910
if (zone == null) {
@@ -1008,7 +999,7 @@ public boolean isCommandSupported(KubernetesCluster cluster, String cmdName) {
1008999
}
10091000

10101001
private void validateManagedKubernetesClusterCreateParameters(final CreateKubernetesClusterCmd cmd) throws CloudRuntimeException {
1011-
validateEndpointUrl();
1002+
ApiServiceConfiguration.validateEndpointUrl();
10121003
final String name = cmd.getName();
10131004
final Long zoneId = cmd.getZoneId();
10141005
final Long kubernetesVersionId = cmd.getKubernetesVersionId();
@@ -1308,7 +1299,7 @@ private void validateKubernetesClusterScaleParameters(ScaleKubernetesClusterCmd
13081299
KubernetesVersionManagerImpl.MINIMUN_AUTOSCALER_SUPPORTED_VERSION ));
13091300
}
13101301

1311-
validateEndpointUrl();
1302+
ApiServiceConfiguration.validateEndpointUrl();
13121303

13131304
if (minSize == null || maxSize == null) {
13141305
throw new InvalidParameterValueException("Autoscaling requires minsize and maxsize to be passed");
@@ -1413,7 +1404,7 @@ protected boolean isAnyNodeOfferingEmpty(Map<String, Long> map) {
14131404

14141405
private void validateKubernetesClusterUpgradeParameters(UpgradeKubernetesClusterCmd cmd) {
14151406
// Validate parameters
1416-
validateEndpointUrl();
1407+
ApiServiceConfiguration.validateEndpointUrl();
14171408

14181409
final Long kubernetesClusterId = cmd.getId();
14191410
final Long upgradeVersionId = cmd.getKubernetesVersionId();

plugins/integrations/kubernetes-service/src/main/java/com/cloud/kubernetes/cluster/actionworkers/KubernetesClusterActionWorker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ protected boolean createCloudStackSecret(String[] keys) {
685685

686686
try {
687687
String command = String.format("sudo %s/%s -u '%s' -k '%s' -s '%s'",
688-
scriptPath, deploySecretsScriptFilename, ApiServiceConfiguration.ApiServletPath.value(), keys[0], keys[1]);
688+
scriptPath, deploySecretsScriptFilename, ApiServiceConfiguration.getApiServletPathValue(), keys[0], keys[1]);
689689
Account account = accountDao.findById(kubernetesCluster.getAccountId());
690690
if (account != null && account.getType() == Account.Type.PROJECT) {
691691
String projectId = projectService.findByProjectAccountId(account.getId()).getUuid();

server/src/main/java/com/cloud/network/as/AutoScaleManagerImpl.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,6 @@ public void checkAutoScaleUser(Long autoscaleUserId, long accountId) {
512512

513513
String apiKey = user.getApiKey();
514514
String secretKey = user.getSecretKey();
515-
String csUrl = ApiServiceConfiguration.ApiServletPath.value();
516515

517516
if (apiKey == null) {
518517
throw new InvalidParameterValueException("apiKey for user: " + user.getUsername() + " is empty. Please generate it");
@@ -522,9 +521,7 @@ public void checkAutoScaleUser(Long autoscaleUserId, long accountId) {
522521
throw new InvalidParameterValueException("secretKey for user: " + user.getUsername() + " is empty. Please generate it");
523522
}
524523

525-
if (csUrl == null || csUrl.contains("localhost")) {
526-
throw new InvalidParameterValueException(String.format("Global setting %s has to be set to the Management Server's API end point", ApiServiceConfiguration.ApiServletPath.key()));
527-
}
524+
ApiServiceConfiguration.validateEndpointUrl();
528525
}
529526
@Override
530527
@ActionEvent(eventType = EventTypes.EVENT_AUTOSCALEVMPROFILE_CREATE, eventDescription = "creating autoscale vm profile", create = true)

server/src/main/java/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,6 @@ private LbAutoScaleVmGroup getLbAutoScaleVmGroup(AutoScaleVmGroupVO vmGroup, Aut
337337

338338
String apiKey = null;
339339
String secretKey = null;
340-
String csUrl = ApiServiceConfiguration.ApiServletPath.value();
341340
Network.Provider provider = getLoadBalancerServiceProvider(lb);
342341
if (Network.Provider.Netscaler.equals(provider)) {
343342
Long autoscaleUserId = autoScaleVmProfile.getAutoScaleUserId();
@@ -358,13 +357,12 @@ private LbAutoScaleVmGroup getLbAutoScaleVmGroup(AutoScaleVmGroupVO vmGroup, Aut
358357
throw new InvalidParameterValueException("secretKey for user: " + user.getUsername() + " is empty. Please generate it");
359358
}
360359

361-
if (csUrl == null || csUrl.contains("localhost")) {
362-
throw new InvalidParameterValueException(String.format("Global setting %s has to be set to the Management Server's API end point", ApiServiceConfiguration.ApiServletPath.key()));
363-
}
360+
ApiServiceConfiguration.validateEndpointUrl();
364361
}
365362

366363
LbAutoScaleVmProfile lbAutoScaleVmProfile =
367-
new LbAutoScaleVmProfile(autoScaleVmProfile, apiKey, secretKey, csUrl, zoneId, domainId, serviceOfferingId, templateId, vmName, lbNetworkUuid);
364+
new LbAutoScaleVmProfile(autoScaleVmProfile, apiKey, secretKey, ApiServiceConfiguration.getApiServletPathValue(), zoneId, domainId, serviceOfferingId, templateId,
365+
vmName, lbNetworkUuid);
368366
return new LbAutoScaleVmGroup(vmGroup, autoScalePolicies, lbAutoScaleVmProfile, currentState);
369367
}
370368

0 commit comments

Comments
 (0)