Skip to content

Commit

Permalink
Adding https support for backend service
Browse files Browse the repository at this point in the history
  • Loading branch information
Gurleen Kaur committed Sep 1, 2022
1 parent 765c510 commit e2cad9a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.text.DecimalFormat;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.pulsar.client.admin.Brokers;
import org.apache.pulsar.client.admin.PulsarAdminException;
import org.apache.pulsar.manager.controller.exception.PulsarAdminOperationException;
Expand Down Expand Up @@ -53,6 +54,7 @@
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.util.UriComponentsBuilder;

@Service
@Configuration
Expand All @@ -67,6 +69,9 @@ public class BrokerStatsServiceImpl implements BrokerStatsService {
@Value("${clear.stats.interval}")
private Long clearStatsInterval;

@Value("${tls.enabled}")
private boolean tlsEnabled;

private final EnvironmentsRepository environmentsRepository;
private final ClustersService clustersService;
private final BrokersService brokersService;
Expand Down Expand Up @@ -124,13 +129,19 @@ private void scheduleCollectStats() {
clusterLists.forEach((clusterMap) -> {
String cluster = (String) clusterMap.get("cluster");
Pair<String, String> envCluster = Pair.of(env.getName(), cluster);
String webServiceUrl = (String) clusterMap.get("serviceUrl");

String webServiceUrl = tlsEnabled && StringUtils.isNotBlank(clusterMap.get("serviceUrlTls")) ? clusterMap.get("serviceUrlTls") : clusterMap.get("serviceUrl");

if (webServiceUrl.contains(",")) {
String[] webServiceUrlList = webServiceUrl.split(",");
for (String url : webServiceUrlList) {
if (!url.contains("http://")) {
// making sure the protocol is appended in case the env was added without the protocol
if (!tlsEnabled && !url.contains("http://")) {
url = "http://" + url;
}
if (tlsEnabled && StringUtils.isNotBlank(clusterMap.get("serviceUrlTls") && !url.contains("https://")) {
url = "https://" + url;
}
try {
Brokers brokers = pulsarAdminService.brokers(url);
brokers.healthcheck();
Expand Down Expand Up @@ -158,9 +169,16 @@ public void collectStatsToDB(long unixTime, String env, String cluster, String s
Map<String, Object> brokerObject = brokersService.getBrokersList(0, 0, cluster, serviceUrl);
List<HashMap<String, Object>> brokerLists = (List<HashMap<String, Object>>) brokerObject.get("data");
brokerLists.forEach((brokerMap) -> {
// returns [Broker Hostname]:[Broker non Tls port]
String tempBroker = (String) brokerMap.get("broker");
// TODO: handle other protocols
//default to http
String broker = "http://" + tempBroker;
// if tls enabled the protocol and port is extracted from service url
if (tlsEnabled && tempBroker.contains(":")) {
String brokerHost = tempBroker.substring(0, tempBroker.indexOf(":"));
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(serviceUrl);
broker = builder.host(brokerHost).toUriString();
}
JsonObject result;
try {
result = pulsarAdminService.brokerStats(broker).getTopics();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.pulsar.common.policies.data.ClusterData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

Expand All @@ -51,6 +52,9 @@ public class EnvironmentCacheServiceImpl implements EnvironmentCacheService {

private final PulsarAdminService pulsarAdminService;

@Value("${tls.enabled}")
private boolean tlsEnabled;

@Autowired
public EnvironmentCacheServiceImpl(EnvironmentsRepository environmentsRepository, PulsarAdminService pulsarAdminService) {
this.environmentsRepository = environmentsRepository;
Expand Down Expand Up @@ -125,7 +129,7 @@ private String getServiceUrl(String environment, String cluster, int numReloads)
throw new RuntimeException(
"No cluster '" + cluster + "' found in environment '" + environment + "'");
}
return clusterData.getServiceUrl();
return tlsEnabled && StringUtils.isNotBlank(clusterData.getServiceUrlTls()) ? clusterData.getServiceUrlTls() : clusterData.getServiceUrl();
}

@Scheduled(
Expand Down

0 comments on commit e2cad9a

Please sign in to comment.