Skip to content

Commit

Permalink
fix(alibaba#7039): Temporary fix encryptedContent bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
CherishCai committed May 25, 2022
1 parent 7ca069d commit aace9b8
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.api.config.ConfigChangeEvent;
import com.alibaba.nacos.api.config.ConfigChangeItem;
import com.alibaba.nacos.api.config.listener.AbstractSharedListener;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;
Expand Down Expand Up @@ -134,7 +135,7 @@ public String getContent() {

public void setContent(String content) {
this.content = content;
this.md5 = getMd5String(this.content);
this.md5 = getMd5String();
}

/**
Expand Down Expand Up @@ -173,8 +174,8 @@ public void addListener(Listener listener) {
throw new IllegalArgumentException("listener is null");
}
ManagerListenerWrap wrap =
(listener instanceof AbstractConfigChangeListener) ? new ManagerListenerWrap(listener, md5, content)
: new ManagerListenerWrap(listener, md5);
(listener instanceof AbstractConfigChangeListener) ? new ManagerListenerWrap(listener, md5, content,
encryptedDataKey) : new ManagerListenerWrap(listener, md5);

if (listeners.addIfAbsent(wrap)) {
LOGGER.info("[{}] [add-listener] ok, tenant={}, dataId={}, group={}, cnt={}", name, tenant, dataId, group,
Expand Down Expand Up @@ -317,11 +318,22 @@ private void safeNotifyListener(final String dataId, final String group, final S
listener.receiveConfigInfo(contentTmp);
// compare lastContent and content
if (listener instanceof AbstractConfigChangeListener) {
Map data = ConfigChangeHandler.getInstance()
.parseChangeData(listenerWrap.lastContent, content, type);
// FIXME temporary fix https:/alibaba/nacos/issues/7039
ConfigResponse crLast = new ConfigResponse();
crLast.setDataId(dataId);
crLast.setGroup(group);
crLast.setTenant(tenant);
crLast.setContent(listenerWrap.lastContent);
crLast.setEncryptedDataKey(listenerWrap.lastEncryptedDataKey);
configFilterChainManager.doFilter(null, crLast);
String lastContentTmp = crLast.getContent();
Map<String, ConfigChangeItem> data = ConfigChangeHandler.getInstance()
.parseChangeData(lastContentTmp, contentTmp, type);
ConfigChangeEvent event = new ConfigChangeEvent(data);
((AbstractConfigChangeListener) listener).receiveConfigChange(event);
// temporary fix https:/alibaba/nacos/issues/7039, cache original content(which get from nacos server)
listenerWrap.lastContent = content;
listenerWrap.lastEncryptedDataKey = encryptedDataKey;
}

listenerWrap.lastCallMd5 = md5;
Expand Down Expand Up @@ -367,8 +379,31 @@ private void safeNotifyListener(final String dataId, final String group, final S
name, (finishNotify - startNotify), dataId, group, md5, listener);
}

public static String getMd5String(String config) {
return (null == config) ? Constants.NULL : MD5Utils.md5Hex(config, Constants.ENCODE);
/**
* FIXME temporary fix https:/alibaba/nacos/issues/7039
*/
public String getMd5String() {
if (null == this.content) {
return Constants.NULL;
}
if (this.encryptedDataKey == null || this.encryptedDataKey.isEmpty()) {
return MD5Utils.md5Hex(this.content, Constants.ENCODE);
}

try {
ConfigResponse cr = new ConfigResponse();
cr.setDataId(this.dataId);
cr.setGroup(this.group);
cr.setTenant(this.tenant);
cr.setEncryptedDataKey(this.encryptedDataKey);
cr.setContent(this.content);
configFilterChainManager.doFilter(null, cr);
return MD5Utils.md5Hex(cr.getContent(), Constants.ENCODE);
} catch (NacosException e) {
LOGGER.error("[CacheData-getMd5String] error by encryptedDataKey={},config={}", this.encryptedDataKey,
this.content, e);
return Constants.NULL;
}
}

private String loadCacheContentFromDiskLocal(String name, String dataId, String group, String tenant) {
Expand All @@ -392,38 +427,25 @@ public void setSyncWithServer(boolean syncWithServer) {
}

public CacheData(ConfigFilterChainManager configFilterChainManager, String name, String dataId, String group) {
if (null == dataId || null == group) {
throw new IllegalArgumentException("dataId=" + dataId + ", group=" + group);
}
this.name = name;
this.configFilterChainManager = configFilterChainManager;
this.dataId = dataId;
this.group = group;
this.tenant = TenantUtil.getUserTenantForAcm();
listeners = new CopyOnWriteArrayList<>();
this.isInitializing = true;
if (initSnapshot) {
this.content = loadCacheContentFromDiskLocal(name, dataId, group, tenant);
this.md5 = getMd5String(content);
}
this.encryptedDataKey = loadEncryptedDataKeyFromDiskLocal(name, dataId, group, tenant);
this(configFilterChainManager, name, dataId, group, TenantUtil.getUserTenantForAcm());
}

public CacheData(ConfigFilterChainManager configFilterChainManager, String name, String dataId, String group,
String tenant) {
if (null == dataId || null == group) {
throw new IllegalArgumentException("dataId=" + dataId + ", group=" + group);
}
this.name = name;
this.configFilterChainManager = configFilterChainManager;
this.name = name;
this.dataId = dataId;
this.group = group;
this.tenant = tenant;
listeners = new CopyOnWriteArrayList<>();
this.listeners = new CopyOnWriteArrayList<>();
this.isInitializing = true;
if (initSnapshot) {
this.content = loadCacheContentFromDiskLocal(name, dataId, group, tenant);
this.md5 = getMd5String(content);
this.encryptedDataKey = loadEncryptedDataKeyFromDiskLocal(name, dataId, group, tenant);
this.md5 = getMd5String();
}
}

Expand Down Expand Up @@ -453,10 +475,12 @@ private static class ManagerListenerWrap {

final Listener listener;

String lastCallMd5 = CacheData.getMd5String(null);
String lastCallMd5 = Constants.NULL;

String lastContent = null;

String lastEncryptedDataKey = null;

ManagerListenerWrap(Listener listener) {
this.listener = listener;
}
Expand All @@ -466,10 +490,11 @@ private static class ManagerListenerWrap {
this.lastCallMd5 = md5;
}

ManagerListenerWrap(Listener listener, String md5, String lastContent) {
ManagerListenerWrap(Listener listener, String md5, String lastContent, String encryptedDataKey) {
this.listener = listener;
this.lastCallMd5 = md5;
this.lastContent = lastContent;
this.lastEncryptedDataKey = encryptedDataKey;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ public CacheData addCacheDataIfAbsent(String dataId, String group, String tenant
// fix issue # 1317
if (enableRemoteSyncConfig) {
ConfigResponse response = getServerConfig(dataId, group, tenant, 3000L, false);
cache.setEncryptedDataKey(response.getEncryptedDataKey());
cache.setContent(response.getContent());
}
}
Expand Down Expand Up @@ -432,8 +433,8 @@ private void refreshContentAndCheck(CacheData cacheData, boolean notify) {
try {
ConfigResponse response = getServerConfig(cacheData.dataId, cacheData.group, cacheData.tenant, 3000L,
notify);
cacheData.setContent(response.getContent());
cacheData.setEncryptedDataKey(response.getEncryptedDataKey());
cacheData.setContent(response.getContent());
if (null != response.getConfigType()) {
cacheData.setType(response.getConfigType());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.alibaba.nacos.client.config.impl;

import com.alibaba.nacos.api.config.ConfigChangeItem;
import com.alibaba.nacos.api.config.listener.ConfigChangeParser;
import com.alibaba.nacos.common.spi.NacosServiceLoader;

Expand Down Expand Up @@ -65,7 +66,7 @@ public static ConfigChangeHandler getInstance() {
* @return change data map
* @throws IOException io exception
*/
public Map parseChangeData(String oldContent, String newContent, String type) throws IOException {
public Map<String, ConfigChangeItem> parseChangeData(String oldContent, String newContent, String type) throws IOException {
for (ConfigChangeParser changeParser : this.parserList) {
if (changeParser.isResponsibleFor(type)) {
return changeParser.doParse(oldContent, newContent, type);
Expand Down

0 comments on commit aace9b8

Please sign in to comment.