Skip to content

Commit

Permalink
[ISSUE #12046] fix console-ui bug when using encryption-plugin (#12061)
Browse files Browse the repository at this point in the history
* #12046 console-ui should not offer encryptedDataKey field to API

* #12046 when containing encryptedDataKey,it should not execute encryption in v2 API (same as the v1 API)
  • Loading branch information
xyohn committed May 7, 2024
1 parent ed64d1f commit 7c461fd
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,13 @@ public void getConfig(HttpServletRequest request, HttpServletResponse response,
public Result<Boolean> publishConfig(ConfigForm configForm, HttpServletRequest request) throws NacosException {
// check required field
configForm.validate();
// encrypted
Pair<String, String> pair = EncryptionHandler.encryptHandler(configForm.getDataId(), configForm.getContent());
configForm.setContent(pair.getSecond());
String encryptedDataKeyFinal = configForm.getEncryptedDataKey();
if (StringUtils.isBlank(encryptedDataKeyFinal)) {
// encrypted
Pair<String, String> pair = EncryptionHandler.encryptHandler(configForm.getDataId(), configForm.getContent());
configForm.setContent(pair.getSecond());
encryptedDataKeyFinal = pair.getFirst();
}
//fix issue #9783
configForm.setNamespaceId(NamespaceUtil.processNamespaceParameter(configForm.getNamespaceId()));
// check param
Expand All @@ -134,16 +138,14 @@ public Result<Boolean> publishConfig(ConfigForm configForm, HttpServletRequest r
if (!ConfigType.isValidType(configForm.getType())) {
configForm.setType(ConfigType.getDefaultType().getType());
}

ConfigRequestInfo configRequestInfo = new ConfigRequestInfo();
configRequestInfo.setSrcIp(RequestUtil.getRemoteIp(request));
configRequestInfo.setRequestIpApp(RequestUtil.getAppName(request));
configRequestInfo.setBetaIps(request.getHeader("betaIps"));
configRequestInfo.setCasMd5(request.getHeader("casMd5"));

String encryptedDataKey = pair.getFirst();

return Result.success(configOperationService.publishConfig(configForm, configRequestInfo, encryptedDataKey));

return Result.success(configOperationService.publishConfig(configForm, configRequestInfo, encryptedDataKeyFinal));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,14 @@ public class ConfigForm implements Serializable {

private String schema;

private String encryptedDataKey;

public ConfigForm() {
}

public ConfigForm(String dataId, String group, String namespaceId, String content, String tag, String appName,
String srcUser, String configTags, String desc, String use, String effect, String type, String schema) {
String srcUser, String configTags, String desc, String use, String effect, String type, String schema,
String encryptedDataKey) {
this.dataId = dataId;
this.group = group;
this.namespaceId = namespaceId;
Expand All @@ -78,6 +81,7 @@ public ConfigForm(String dataId, String group, String namespaceId, String conten
this.effect = effect;
this.type = type;
this.schema = schema;
this.encryptedDataKey = encryptedDataKey;
}

public String getDataId() {
Expand Down Expand Up @@ -184,6 +188,14 @@ public void setSchema(String schema) {
this.schema = schema;
}

public String getEncryptedDataKey() {
return encryptedDataKey;
}

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

@Override
public boolean equals(Object o) {
if (this == o) {
Expand All @@ -196,15 +208,16 @@ public boolean equals(Object o) {
return dataId.equals(configForm.dataId) && group.equals(configForm.group) && Objects.equals(namespaceId, configForm.namespaceId)
&& content.equals(configForm.content) && Objects.equals(tag, configForm.tag) && Objects
.equals(appName, configForm.appName) && Objects.equals(srcUser, configForm.srcUser) && Objects
.equals(configTags, configForm.configTags) && Objects.equals(desc, configForm.desc) && Objects
.equals(use, configForm.use) && Objects.equals(effect, configForm.effect) && Objects
.equals(type, configForm.type) && Objects.equals(schema, configForm.schema);
.equals(configTags, configForm.configTags) && Objects.equals(desc, configForm.desc) && Objects.equals(
use, configForm.use) && Objects.equals(effect, configForm.effect) && Objects.equals(type,
configForm.type) && Objects.equals(schema, configForm.schema) && Objects.equals(encryptedDataKey,
configForm.encryptedDataKey);
}

@Override
public int hashCode() {
return Objects.hash(dataId, group, namespaceId, content, tag, appName, srcUser, configTags, desc, use, effect, type,
schema);
schema, encryptedDataKey);
}

@Override
Expand All @@ -213,7 +226,7 @@ public String toString() {
+ ", content='" + content + '\'' + ", tag='" + tag + '\'' + ", appName='" + appName + '\''
+ ", srcUser='" + srcUser + '\'' + ", configTags='" + configTags + '\'' + ", desc='" + desc + '\''
+ ", use='" + use + '\'' + ", effect='" + effect + '\'' + ", type='" + type + '\'' + ", schema='"
+ schema + '\'' + '}';
+ schema + '\'' + ", encryptedDataKey='" + encryptedDataKey + '\'' + '}';
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ public class ConfigControllerV2Test {

private static final String TEST_CONTENT = "test config";

private static final String TEST_ENCRYPTED_DATA_KEY = "test_encrypted_data_key";

@Before
public void setUp() {
EnvUtil.setEnvironment(new StandardEnvironment());
Expand Down Expand Up @@ -161,6 +163,28 @@ public void testPublishConfig() throws Exception {
assertEquals(true, booleanResult.getData());
}

@Test
public void testPublishConfigWithEncryptedDataKey() throws Exception {

ConfigForm configForm = new ConfigForm();
configForm.setDataId(TEST_DATA_ID);
configForm.setGroup(TEST_GROUP);
configForm.setNamespaceId(TEST_NAMESPACE_ID);
configForm.setContent(TEST_CONTENT);
configForm.setEncryptedDataKey(TEST_ENCRYPTED_DATA_KEY);
MockHttpServletRequest request = new MockHttpServletRequest();

when(configOperationService.publishConfig(any(ConfigForm.class), any(ConfigRequestInfo.class),
eq(TEST_ENCRYPTED_DATA_KEY))).thenReturn(true);

Result<Boolean> booleanResult = configControllerV2.publishConfig(configForm, request);

verify(configOperationService).publishConfig(any(ConfigForm.class), any(ConfigRequestInfo.class), anyString());

assertEquals(ErrorCode.SUCCESS.getCode(), booleanResult.getCode());
assertEquals(true, booleanResult.getData());
}

@Test
public void testPublishConfigWhenNameSpaceIsPublic() throws Exception {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ class ConfigEditor extends React.Component {
if (configTags.length > 0) {
payload.config_tags = configTags.join(',');
}
// #12046 console-ui should not offer encryptedDataKey field to API
payload.encryptedDataKey = '';
const stringify = require('qs/lib/stringify');
this.setState({ loading: true });
return request({
Expand Down
4 changes: 2 additions & 2 deletions console/src/main/resources/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<link rel="stylesheet" type="text/css" href="console-ui/public/css/icon.css">
<link rel="stylesheet" type="text/css" href="console-ui/public/css/font-awesome.css">
<!-- 第三方css结束 -->
<link href="./css/main.css?48bc3a2e5faa78253d8a" rel="stylesheet"></head>
<link href="./css/main.css?c02a3284f12026e72980" rel="stylesheet"></head>

<body>
<div id="root" style="overflow:hidden"></div>
Expand All @@ -56,6 +56,6 @@
<script src="console-ui/public/js/merge.js"></script>
<script src="console-ui/public/js/loader.js"></script>
<!-- 第三方js结束 -->
<script type="text/javascript" src="./js/main.js?48bc3a2e5faa78253d8a"></script></body>
<script type="text/javascript" src="./js/main.js?c02a3284f12026e72980"></script></body>

</html>
4 changes: 2 additions & 2 deletions console/src/main/resources/static/js/main.js

Large diffs are not rendered by default.

0 comments on commit 7c461fd

Please sign in to comment.