Skip to content

Commit

Permalink
Add plugin handler and configuration classes
Browse files Browse the repository at this point in the history
Signed-off-by: hanbingleixue <[email protected]>
  • Loading branch information
hanbingleixue committed May 31, 2024
1 parent 66958e2 commit d3c8329
Show file tree
Hide file tree
Showing 23 changed files with 1,229 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (C) 2024-2024 Sermant Authors. All rights reserved.
*
* Licensed 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 io.sermant.backend.common.conf;

import lombok.Getter;
import lombok.Setter;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
* Dynamically configured configuration classes
*
* @author zhp
* @since 2024-05-21
*/
@Configuration
@Getter
@Setter
@Component
public class DynamicConfig {
@Value("${dynamic.config.serverAddress}")
private String serverAddress;

@Value("${dynamic.config.dynamicConfigType}")
private String dynamicConfigType;

@Value("${dynamic.config.requestTimeout}")
private long requestTimeout;

@Value("${dynamic.config.connectTimeout}")
private long connectTimeout;

@Value("${dynamic.config.timeout}")
private int timeout;

@Value("${dynamic.config.enableAuth}")
private boolean enableAuth;

@Value("${dynamic.config.userName}")
private String userName;

@Value("${dynamic.config.password}")
private String password;

@Value("${dynamic.config.secretKey}")
private String secretKey;

@Value("${dynamic.config.namespace}")
private String namespace = "default";

@Value("${dynamic.config.dynamicConfigEnable}")
private boolean dynamicConfigEnable;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (C) 2024-2024 Sermant Authors. All rights reserved.
*
* Licensed 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 io.sermant.backend.handler.config;

import io.sermant.backend.entity.config.ConfigInfo;
import io.sermant.backend.entity.config.PluginType;

import org.apache.commons.lang.StringUtils;

import java.util.Map;

/**
* Database Write Prohibition plugin handler
*
* @author zhp
* @since 2024-05-16
*/
public class DatabaseWriteProhibitionPluginHandler extends PluginConfigHandler {
private static final String GLOBAL_CONFIGURATION_NAME = "sermant.database.write.globalConfig";

private static final String SERVICE_CONFIGURATION_NAME_PREFIX = "sermant.database.write.";

private static final String PATTERN = "^(app=[^&]*(&environment=[^&]*)?(&zone=[^&]*)?)?$";

@Override
public ConfigInfo parsePluginInfo(String key, String group) {
Map<String, String> map = parseGroup(group);
ConfigInfo configInfo = new ConfigInfo();
if (!StringUtils.equals(key, GLOBAL_CONFIGURATION_NAME) && key.startsWith(SERVICE_CONFIGURATION_NAME_PREFIX)) {
configInfo.setServiceName(key.replace(SERVICE_CONFIGURATION_NAME_PREFIX, ""));
}
configInfo.setAppName(map.get(APP_KEY));
configInfo.setEnvironment(map.get(ENVIRONMENT_KEY));
configInfo.setZone(map.get(ZONE_KEY));
configInfo.setKey(key);
configInfo.setGroup(group);
configInfo.setPluginType(PluginType.DATABASE_WRITE_PROHIBITION.getPluginName());
return configInfo;
}

@Override
public boolean verifyConfiguration(String key, String group) {
if (StringUtils.isBlank(key) || StringUtils.isBlank(group)) {
return false;
}
if (!group.matches(PATTERN)) {
return false;
}
return key.equals(GLOBAL_CONFIGURATION_NAME) || key.startsWith(SERVICE_CONFIGURATION_NAME_PREFIX);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (C) 2024-2024 Sermant Authors. All rights reserved.
*
* Licensed 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 io.sermant.backend.handler.config;

import io.sermant.backend.entity.config.ConfigInfo;
import io.sermant.backend.entity.config.PluginType;

import org.apache.commons.lang.StringUtils;

import java.util.Map;

/**
* Flow control plugin handler
*
* @author zhp
* @since 2024-05-16
*/
public class FlowControlPluginHandler extends PluginConfigHandler {
private static final String[] CONFIGURATION_NAME_PREFIX_ARRAY = {"servicecomb.rateLimiting.",
"servicecomb.matchGroup.", "servicecomb.circuitBreaker.", "servicecomb.bulkhead.",
"servicecomb.faultInjection.", "servicecomb.retry.", "servicecomb.system."};

private static final String PATTERN = "^service=[^&]*$";

@Override
public ConfigInfo parsePluginInfo(String key, String group) {
Map<String, String> map = parseGroup(group);
ConfigInfo configInfo = new ConfigInfo();
configInfo.setServiceName(map.get(SERVICE_KEY));
configInfo.setKey(key);
configInfo.setGroup(group);
configInfo.setPluginType(PluginType.FLOW_CONTROL.getPluginName());
return configInfo;
}

@Override
public boolean verifyConfiguration(String key, String group) {
if (StringUtils.isBlank(key) || StringUtils.isBlank(group)) {
return false;
}
if (!group.matches(PATTERN)) {
return false;
}
for (String name : CONFIGURATION_NAME_PREFIX_ARRAY) {
if (key.startsWith(name)) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (C) 2024-2024 Sermant Authors. All rights reserved.
*
* Licensed 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 io.sermant.backend.handler.config;

import io.sermant.backend.entity.config.ConfigInfo;
import io.sermant.backend.entity.config.PluginType;

import org.apache.commons.lang.StringUtils;

/**
* Spring boot registry plugin handler
*
* @author zhp
* @since 2024-05-16
*/
public class LoadbalancerPluginHandler extends PluginConfigHandler {
private static final String TRAFFIC_MARKERS_CONFIGURATION_NAME_PREFIX = "servicecomb.matchGroup.";

private static final String LOADBALANCER_RULES_CONFIGURATION_NAME_PREFIX = "servicecomb.loadbalance.";

private static final String PATTERN = "^(app=[^&]*(&environment=[^&]*)?(&service=[^&]*)?)?$";

@Override
public ConfigInfo parsePluginInfo(String key, String group) {
ConfigInfo configInfo = super.parsePluginInfo(key, group);
configInfo.setPluginType(PluginType.LOADBALANCER.getPluginName());
return configInfo;
}

@Override
public boolean verifyConfiguration(String key, String group) {
if (StringUtils.isBlank(key) || StringUtils.isBlank(group)) {
return false;
}
if (!group.matches(PATTERN)) {
return false;
}
return key.startsWith(TRAFFIC_MARKERS_CONFIGURATION_NAME_PREFIX)
|| key.startsWith(LOADBALANCER_RULES_CONFIGURATION_NAME_PREFIX);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (C) 2024-2024 Sermant Authors. All rights reserved.
*
* Licensed 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 io.sermant.backend.handler.config;

import io.sermant.backend.entity.config.ConfigInfo;
import io.sermant.backend.entity.config.PluginType;

import org.apache.commons.lang.StringUtils;

import java.util.Map;

/**
* Mq Consume Prohibition plugin handler
*
* @author zhp
* @since 2024-05-16
*/
public class MqConsumeProhibitionPluginHandler extends PluginConfigHandler {
private static final String GLOBAL_CONFIGURATION_NAME = "sermant.mq.consume.globalConfig";

private static final String SERVICE_CONFIGURATION_NAME_PREFIX = "sermant.mq.consume.";

private static final String PATTERN = "^(app=[^&]*(&environment=[^&]*)?(&zone=[^&]*)?)?$";

@Override
public ConfigInfo parsePluginInfo(String key, String group) {
ConfigInfo configInfo = new ConfigInfo();
Map<String, String> map = parseGroup(group);
configInfo.setAppName(map.get(APP_KEY));
configInfo.setEnvironment(map.get(ENVIRONMENT_KEY));
configInfo.setKey(key);
configInfo.setGroup(group);
configInfo.setZone(map.get(ZONE_KEY));
configInfo.setPluginType(PluginType.MQ_CONSUME_PROHIBITION.getPluginName());
if (!StringUtils.equals(key, GLOBAL_CONFIGURATION_NAME) && key.startsWith(SERVICE_CONFIGURATION_NAME_PREFIX)) {
configInfo.setServiceName(key.replace(SERVICE_CONFIGURATION_NAME_PREFIX, ""));
}
return configInfo;
}

@Override
public boolean verifyConfiguration(String key, String group) {
if (StringUtils.isBlank(key) || StringUtils.isBlank(group)) {
return false;
}
if (!group.matches(PATTERN)) {
return false;
}
return key.equals(GLOBAL_CONFIGURATION_NAME) || key.startsWith(SERVICE_CONFIGURATION_NAME_PREFIX);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (C) 2024-2024 Sermant Authors. All rights reserved.
*
* Licensed 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 io.sermant.backend.handler.config;

import io.sermant.backend.entity.config.ConfigInfo;
import io.sermant.backend.entity.config.PluginType;

import org.apache.commons.lang.StringUtils;

/**
* Other plugin handler
*
* @author zhp
* @since 2024-05-16
*/
public class OtherPluginHandler extends PluginConfigHandler {
@Override
public ConfigInfo parsePluginInfo(String key, String group) {
ConfigInfo configInfo = new ConfigInfo();
configInfo.setKey(key);
configInfo.setGroup(group);
configInfo.setPluginType(PluginType.OTHER.getPluginName());
return configInfo;
}

@Override
public boolean verifyConfiguration(String key, String group) {
return StringUtils.isNotBlank(key) && StringUtils.isNotBlank(group);
}
}
Loading

0 comments on commit d3c8329

Please sign in to comment.