feat:add zero protection.

pull/1278/head
Haotian Zhang 1 year ago
parent 5b7acf92b6
commit fdac64c285

@ -63,7 +63,9 @@ public class ConfigurationModifier implements PolarisConfigurationConfigModifier
@Override
public void modify(ConfigurationImpl configuration) {
if (!polarisConfigProperties.isEnabled()) {
configuration.getGlobal().getAPI().setReportEnable(false);
if (!polarisContextProperties.getEnabled() || !polarisConfigProperties.isEnabled()) {
return;
}
if (StringUtils.equalsIgnoreCase(polarisConfigProperties.getDataSource(), DATA_SOURCE_POLARIS)) {

@ -89,9 +89,16 @@ public class PolarisConfigDataLocationResolver implements
if (!location.hasPrefix(PREFIX)) {
return false;
}
return context.getBinder()
boolean contextEnabled = context.getBinder()
.bind("spring.cloud.polaris.enabled", Boolean.class)
.orElse(true);
boolean configEnabled = context.getBinder()
.bind("spring.cloud.polaris.config.enabled", Boolean.class)
.orElse(true);
return contextEnabled && configEnabled;
}
@Override
@ -138,6 +145,10 @@ public class PolarisConfigDataLocationResolver implements
polarisContextProperties = new PolarisContextProperties();
}
if (!polarisContextProperties.getEnabled() || !polarisConfigProperties.isEnabled()) {
return Collections.emptyList();
}
// prepare and init earlier Polaris SDKContext to pull config files from remote.
try {
prepareAndInitEarlierPolarisSdkContext(resolverContext, polarisConfigProperties, polarisCryptoConfigProperties, polarisContextProperties);

@ -22,6 +22,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.tencent.cloud.common.util.GzipUtil;
import com.tencent.cloud.common.util.JacksonUtils;
import com.tencent.cloud.polaris.PolarisDiscoveryProperties;
import com.tencent.cloud.polaris.contract.config.PolarisContractProperties;
@ -97,13 +98,16 @@ public class PolarisContractReporter implements ApplicationListener<ApplicationR
request.setVersion(polarisDiscoveryProperties.getVersion());
List<InterfaceDescriptor> interfaceDescriptorList = getInterfaceDescriptorFromSwagger(openAPI);
request.setInterfaceDescriptors(interfaceDescriptorList);
String jsonValue = JacksonUtils.serialize2Json(openAPI);
String serviceApiMeta = GzipUtil.compressBase64Encode(jsonValue, "utf-8");
request.setContent(serviceApiMeta);
ReportServiceContractResponse response = providerAPI.reportServiceContract(request);
LOG.info("Service contract [Namespace: {}. Name: {}. Service: {}. Protocol:{}. Version: {}. API counter: {}] is reported.",
request.getNamespace(), request.getName(), request.getService(), request.getProtocol(),
request.getVersion(), request.getInterfaceDescriptors().size());
if (LOG.isDebugEnabled()) {
String jsonValue = JacksonUtils.serialize2Json(openAPI);
LOG.debug("OpenApi json data: {}", jsonValue);
LOG.debug("OpenApi json base64 data: {}", serviceApiMeta);
}
}
else {

@ -85,8 +85,8 @@ public class PolarisSwaggerAutoConfiguration {
public OpenAPI polarisOpenAPI() {
return new OpenAPI()
.info(new Info()
.title("Polaris Swagger API")
.description("This is to show polaris api description.")
.title("Polaris Contract")
.description("This is to show polaris contract description.")
.license(new License().name("BSD-3-Clause").url("https://opensource.org/licenses/BSD-3-Clause"))
.version("1.0.0"));
}

@ -1,4 +1,4 @@
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
com.tencent.cloud.polaris.contract.config.PolarisContractPropertiesAutoConfiguration
com.tencent.cloud.polaris.contract.config.PolarisContractPropertiesBootstrapConfiguration
org.springframework.context.ApplicationListener=\
com.tencent.cloud.polaris.contract.PolarisSwaggerApplicationListener

@ -1,2 +1,2 @@
com.tencent.cloud.polaris.contract.config.PolarisSwaggerAutoConfiguration
com.tencent.cloud.polaris.contract.config.PolarisContractProperties
com.tencent.cloud.polaris.contract.config.PolarisContractPropertiesAutoConfiguration

@ -67,6 +67,7 @@ public class PolarisRegistration implements Registration {
private final List<PolarisRegistrationCustomizer> customizers;
private boolean registerEnabled = false;
private Map<String, String> metadata;
private Map<String, Map<String, String>> extendedMetadata;
private int port;
private String instanceId;
@ -132,6 +133,8 @@ public class PolarisRegistration implements Registration {
this.metadata = instanceMetadata;
}
this.extendedMetadata = new HashMap<>();
// generate registerEnabled
if (null != polarisDiscoveryProperties) {
registerEnabled = polarisDiscoveryProperties.isRegisterEnabled();
@ -217,6 +220,13 @@ public class PolarisRegistration implements Registration {
return metadata;
}
public Map<String, Map<String, String>> getExtendedMetadata() {
if (extendedMetadata == null) {
extendedMetadata = new HashMap<>();
}
return extendedMetadata;
}
@Override
public String getInstanceId() {
return instanceId;

@ -117,6 +117,7 @@ public class PolarisServiceRegistry implements ServiceRegistry<PolarisRegistrati
instanceRegisterRequest.setCampus(staticMetadataManager.getCampus());
instanceRegisterRequest.setTtl(polarisDiscoveryProperties.getHeartbeatInterval());
instanceRegisterRequest.setMetadata(registration.getMetadata());
instanceRegisterRequest.setExtendedMetadata(registration.getExtendedMetadata());
instanceRegisterRequest.setProtocol(polarisDiscoveryProperties.getProtocol());
instanceRegisterRequest.setVersion(polarisDiscoveryProperties.getVersion());
instanceRegisterRequest.setInstanceId(polarisDiscoveryProperties.getInstanceId());

@ -0,0 +1,84 @@
/*
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* 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 com.tencent.cloud.common.util;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
/**
* @author kysonli
* @date 2018/9/7 15:35
*/
public final class GzipUtil {
private GzipUtil() {
}
public static byte[] compress(String data, String charsetName) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(bos);
gzip.write(data.getBytes(charsetName));
gzip.finish();
gzip.close();
byte[] ret = bos.toByteArray();
bos.close();
return ret;
}
public static String compressBase64Encode(String data, String charsetName) throws IOException {
byte[] compressData = compress(data, charsetName);
return new String(Base64.getEncoder().encode(compressData), charsetName);
}
public static String compressBase64Encode(byte[] byteData, String charsetName) throws IOException {
byte[] compressData = compress(new String(byteData, charsetName), charsetName);
return Base64.getEncoder().encodeToString(compressData);
}
public static byte[] decompress(byte[] zipData) throws IOException {
ByteArrayInputStream bis = new ByteArrayInputStream(zipData);
GZIPInputStream gzip = new GZIPInputStream(bis);
byte[] buf = new byte[256];
int num = -1;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while ((num = gzip.read(buf, 0, buf.length)) != -1) {
bos.write(buf, 0, num);
}
gzip.close();
bis.close();
byte[] ret = bos.toByteArray();
bos.flush();
bos.close();
return ret;
}
public static String base64DecodeDecompress(String data, String charsetName) throws IOException {
byte[] base64DecodeData = Base64.getDecoder().decode(data);
return new String(decompress(base64DecodeData), charsetName);
}
public static String base64DecodeDecompress(String data) throws IOException {
return base64DecodeDecompress(data, "utf-8");
}
}

@ -65,7 +65,7 @@ public class PolarisContextProperties {
* If polaris enabled.
*/
@Value("${spring.cloud.polaris.enabled:#{'true'}}")
private Boolean enabled;
private Boolean enabled = true;
/**
* polaris namespace.

Loading…
Cancel
Save