Skip to content

Commit

Permalink
[ISSUE alibaba#5367]SPI IConfigFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
robberphex committed Apr 26, 2021
1 parent 71a0037 commit 4481e67
Show file tree
Hide file tree
Showing 18 changed files with 471 additions and 209 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import com.alibaba.nacos.api.exception.NacosException;

import java.util.Properties;

/**
* Config Filter Interface.
*
Expand All @@ -30,8 +32,16 @@ public interface IConfigFilter {
*
* @param filterConfig Filter Config
*/
@Deprecated
void init(IFilterConfig filterConfig);

/**
* Init.
*
* @param properties Filter Config
*/
void init(Properties properties);

/**
* do filter.
*
Expand All @@ -43,11 +53,6 @@ public interface IConfigFilter {
void doFilter(IConfigRequest request, IConfigResponse response, IConfigFilterChain filterChain)
throws NacosException;

/**
* deploy.
*/
void deploy();

/**
* Get order.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ public interface IConfigRequest {
*/
Object getParameter(String key);

/**
* put param.
*
* @param key key
* @param value value
*/
void putParameter(String key, Object value);

/**
* get config context.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ public interface IConfigResponse {
*/
Object getParameter(String key);

/**
* put param.
*
* @param key key
* @param value value
*/
void putParameter(String key, Object value);

/**
* Get config context.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*
* @author Nacos
*/
@Deprecated
public interface IFilterConfig {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public class ConfigQueryResponse extends Response {

String content;

String encryptedDataKey;

String contentType;

String md5;
Expand Down Expand Up @@ -120,6 +122,14 @@ public void setContent(String content) {
this.content = content;
}

public void setEncryptedDataKey(String encryptedDataKey) {
this.encryptedDataKey = encryptedDataKey;
}

public String getEncryptedDataKey() {
return encryptedDataKey;
}

/**
* Getter method for property <tt>contentType</tt>.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.alibaba.nacos.client.config.http.ServerHttpAgent;
import com.alibaba.nacos.client.config.impl.ClientWorker;
import com.alibaba.nacos.client.config.impl.LocalConfigInfoProcessor;
import com.alibaba.nacos.client.config.impl.LocalEncryptedDataKeyProcessor;
import com.alibaba.nacos.client.config.impl.ServerListManager;
import com.alibaba.nacos.client.config.utils.ContentUtils;
import com.alibaba.nacos.client.config.utils.ParamUtils;
Expand Down Expand Up @@ -63,12 +64,13 @@ public class NacosConfigService implements ConfigService {

private String namespace;

private final ConfigFilterChainManager configFilterChainManager = new ConfigFilterChainManager();
private final ConfigFilterChainManager configFilterChainManager;

public NacosConfigService(Properties properties) throws NacosException {
ValidatorUtils.checkInitParam(properties);

initNamespace(properties);
this.configFilterChainManager = new ConfigFilterChainManager(properties);
ServerListManager serverListManager = new ServerListManager(properties);
serverListManager.start();

Expand Down Expand Up @@ -148,15 +150,18 @@ private String getConfigInner(String tenant, String dataId, String group, long t
LOGGER.warn("[{}] [get-config] get failover ok, dataId={}, group={}, tenant={}, config={}",
worker.getAgentName(), dataId, group, tenant, ContentUtils.truncateContent(content));
cr.setContent(content);
String encryptedDataKey = LocalEncryptedDataKeyProcessor
.getEncryptDataKeyFailover(agent.getName(), dataId, group, tenant);
cr.setEncryptedDataKey(encryptedDataKey);
configFilterChainManager.doFilter(null, cr);
content = cr.getContent();
return content;
}

try {
String[] ct = worker.getServerConfig(dataId, group, tenant, timeoutMs, false);
cr.setContent(ct[0]);

ConfigResponse response = worker.getServerConfig(dataId, group, tenant, timeoutMs, false);
cr.setContent(response.getContent());
cr.setEncryptedDataKey(response.getEncryptedDataKey());
configFilterChainManager.doFilter(null, cr);
content = cr.getContent();

Expand All @@ -173,6 +178,9 @@ private String getConfigInner(String tenant, String dataId, String group, long t
worker.getAgentName(), dataId, group, tenant, ContentUtils.truncateContent(content));
content = LocalConfigInfoProcessor.getSnapshot(worker.getAgentName(), dataId, group, tenant);
cr.setContent(content);
String encryptedDataKey = LocalEncryptedDataKeyProcessor
.getEncryptDataKeyFailover(agent.getName(), dataId, group, tenant);
cr.setEncryptedDataKey(encryptedDataKey);
configFilterChainManager.doFilter(null, cr);
content = cr.getContent();
return content;
Expand Down Expand Up @@ -201,9 +209,10 @@ private boolean publishConfigInner(String tenant, String dataId, String group, S
cr.setType(type);
configFilterChainManager.doFilter(cr, null);
content = cr.getContent();
String encryptedDataKey = (String) cr.getParameter("encryptedDataKey");

return worker.publishConfig(dataId, group, tenant, appName, tag, betaIps, content, casMd5, type);

return worker
.publishConfig(dataId, group, tenant, appName, tag, betaIps, content, encryptedDataKey, casMd5, type);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.ServiceLoader;

/**
* Config Filter Chain Management.
Expand All @@ -32,7 +34,15 @@
*/
public class ConfigFilterChainManager implements IConfigFilterChain {

private final List<IConfigFilter> filters = new ArrayList<>();
private final List<IConfigFilter> filters = new ArrayList<IConfigFilter>();

public ConfigFilterChainManager(Properties properties) {
ServiceLoader<IConfigFilter> configFilters = ServiceLoader.load(IConfigFilter.class);
for (IConfigFilter configFilter : configFilters) {
configFilter.init(properties);
addFilter(configFilter);
}
}

/**
* Add filter.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ public Object getParameter(String key) {
return param.get(key);
}

@Override
public void putParameter(String key, Object value) {
param.put(key, value);
}

@Override
public IConfigContext getConfigContext() {
return configContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,32 @@ public void setContent(String content) {
param.put("content", content);
}

public String getConfigType() {
return (String) param.get("configType");
}

public void setConfigType(String configType) {
param.put("configType", configType);
}

public String getEncryptedDataKey() {
return (String) param.get("encryptedDataKey");
}

public void setEncryptedDataKey(String encryptedDataKey) {
param.put("encryptedDataKey", encryptedDataKey);
}

@Override
public Object getParameter(String key) {
return param.get(key);
}

@Override
public void putParameter(String key, Object value) {
param.put(key, value);
}

@Override
public IConfigContext getConfigContext() {
return configContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public String toString() {
void checkListenerMd5() {
for (ManagerListenerWrap wrap : listeners) {
if (!md5.equals(wrap.lastCallMd5)) {
safeNotifyListener(dataId, group, content, type, md5, wrap);
safeNotifyListener(dataId, group, content, type, md5, encryptedDataKey, wrap);
}
}
}
Expand All @@ -231,7 +231,7 @@ public boolean checkListenersMd5Consistent() {
}

private void safeNotifyListener(final String dataId, final String group, final String content, final String type,
final String md5, final ManagerListenerWrap listenerWrap) {
final String md5, final String encryptedDataKey, final ManagerListenerWrap listenerWrap) {
final Listener listener = listenerWrap.listener;
if (listenerWrap.inNotifying) {
LOGGER.warn(
Expand All @@ -258,6 +258,7 @@ public void run() {
cr.setDataId(dataId);
cr.setGroup(group);
cr.setContent(content);
cr.setEncryptedDataKey(encryptedDataKey);
configFilterChainManager.doFilter(null, cr);
String contentTmp = cr.getContent();
listenerWrap.inNotifying = true;
Expand Down Expand Up @@ -352,6 +353,7 @@ public CacheData(ConfigFilterChainManager configFilterChainManager, String name,
this.isInitializing = true;
this.content = loadCacheContentFromDiskLocal(name, dataId, group, tenant);
this.md5 = getMd5String(content);
this.encryptedDataKey = loadEncryptedDataKeyFromDiskLocal(name, dataId, group, tenant);
}

public CacheData(ConfigFilterChainManager configFilterChainManager, String name, String dataId, String group,
Expand Down Expand Up @@ -398,6 +400,8 @@ public CacheData(ConfigFilterChainManager configFilterChainManager, String name,

private volatile String content;

private volatile String encryptedDataKey;

private volatile long lastModifiedTs;

private int taskId;
Expand All @@ -411,6 +415,24 @@ public CacheData(ConfigFilterChainManager configFilterChainManager, String name,

private String type;

public String getEncryptedDataKey() {
return encryptedDataKey;
}

public void setEncryptedDataKey(String encryptedDataKey) {
this.encryptedDataKey = encryptedDataKey;
}

private String loadEncryptedDataKeyFromDiskLocal(String name, String dataId, String group, String tenant) {
String encryptedDataKey = LocalEncryptedDataKeyProcessor.getEncryptDataKeyFailover(name, dataId, group, tenant);

if (encryptedDataKey != null) {
return encryptedDataKey;
}

return LocalEncryptedDataKeyProcessor.getEncryptDataKeySnapshot(name, dataId, group, tenant);
}

private static class ManagerListenerWrap {

boolean inNotifying = false;
Expand Down
Loading

0 comments on commit 4481e67

Please sign in to comment.