Skip to content

Commit

Permalink
fix(alibaba#12333): fixed auth Plugin resource parser can't parser v2…
Browse files Browse the repository at this point in the history
… config openAPI namespaceId.
  • Loading branch information
Bo-Qiu committed Jul 16, 2024
1 parent 2233e65 commit f5af698
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 11 deletions.
10 changes: 7 additions & 3 deletions api/src/main/java/com/alibaba/nacos/api/common/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,14 @@ public class Constants {

public static final String NULL = "";

public static final String DATAID = "dataId";

public static final String DATA_ID = "dataId";

public static final String TENANT = "tenant";

public static final String GROUP = "group";


public static final String NAMESPACE_ID = "namespaceId";

public static final String LAST_MODIFIED = "Last-Modified";

public static final String ACCEPT_ENCODING = "Accept-Encoding";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected String getResourceName(Request request) {
dataId = ((AbstractConfigRequest) request).getDataId();
} else {
dataId = (String) ReflectUtils
.getFieldValue(request, com.alibaba.nacos.api.common.Constants.DATAID, StringUtils.EMPTY);
.getFieldValue(request, com.alibaba.nacos.api.common.Constants.DATA_ID, StringUtils.EMPTY);
}
return StringUtils.isBlank(dataId) ? StringUtils.EMPTY : dataId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.alibaba.nacos.auth.parser.http;

import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.common.utils.NamespaceUtil;
import com.alibaba.nacos.common.utils.StringUtils;

Expand All @@ -31,8 +32,11 @@ public class ConfigHttpResourceParser extends AbstractHttpResourceParser {

@Override
protected String getNamespaceId(HttpServletRequest request) {
return NamespaceUtil.processNamespaceParameter(request.getParameter("tenant"));

String namespaceId = request.getParameter(Constants.NAMESPACE_ID);
if (StringUtils.isBlank(namespaceId)) {
namespaceId = request.getParameter(Constants.TENANT);
}
return NamespaceUtil.processNamespaceParameter(namespaceId);
}

@Override
Expand All @@ -43,7 +47,7 @@ protected String getGroup(HttpServletRequest request) {

@Override
protected String getResourceName(HttpServletRequest request) {
String dataId = request.getParameter(com.alibaba.nacos.api.common.Constants.DATAID);
String dataId = request.getParameter(com.alibaba.nacos.api.common.Constants.DATA_ID);
return StringUtils.isBlank(dataId) ? StringUtils.EMPTY : dataId;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void setUp() throws Exception {
Mockito.when(request.getParameter(eq(CommonParams.SERVICE_NAME))).thenReturn("testS");
Mockito.when(request.getParameter(eq("tenant"))).thenReturn("testCNs");
Mockito.when(request.getParameter(eq(Constants.GROUP))).thenReturn("testCG");
Mockito.when(request.getParameter(eq(Constants.DATAID))).thenReturn("testD");
Mockito.when(request.getParameter(eq(Constants.DATA_ID))).thenReturn("testD");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,49 @@ void testParseWithFullContext() throws NoSuchMethodException {
Secured secured = getMethodSecure();
Mockito.when(request.getParameter(eq("tenant"))).thenReturn("testNs");
Mockito.when(request.getParameter(eq(Constants.GROUP))).thenReturn("testG");
Mockito.when(request.getParameter(eq(Constants.DATAID))).thenReturn("testD");
Mockito.when(request.getParameter(eq(Constants.DATA_ID))).thenReturn("testD");
Resource actual = resourceParser.parse(request, secured);
assertEquals("testNs", actual.getNamespaceId());
assertEquals("testG", actual.getGroup());
assertEquals("testD", actual.getName());
assertEquals(Constants.Config.CONFIG_MODULE, actual.getType());
}

@Test
@Secured(signType = Constants.Config.CONFIG_MODULE)
void testParseWithNamespaceId() throws NoSuchMethodException {
Secured secured = getMethodSecure();
Mockito.when(request.getParameter(eq("namespaceId"))).thenReturn("testNs");
Mockito.when(request.getParameter(eq(Constants.GROUP))).thenReturn("testG");
Mockito.when(request.getParameter(eq(Constants.DATA_ID))).thenReturn("testD");
Resource actual = resourceParser.parse(request, secured);
assertEquals("testNs", actual.getNamespaceId());
assertEquals("testG", actual.getGroup());
assertEquals("testD", actual.getName());
assertEquals(Constants.Config.CONFIG_MODULE, actual.getType());
}

@Test
@Secured(signType = Constants.Config.CONFIG_MODULE)
void testParseWithNamespaceIdFirst() throws NoSuchMethodException {
Secured secured = getMethodSecure();
Mockito.when(request.getParameter(eq("tenant"))).thenReturn("testNs");
Mockito.when(request.getParameter(eq("namespaceId"))).thenReturn("testNsFirst");
Mockito.when(request.getParameter(eq(Constants.GROUP))).thenReturn("testG");
Mockito.when(request.getParameter(eq(Constants.DATA_ID))).thenReturn("testD");
Resource actual = resourceParser.parse(request, secured);
assertEquals("testNsFirst", actual.getNamespaceId());
assertEquals("testG", actual.getGroup());
assertEquals("testD", actual.getName());
assertEquals(Constants.Config.CONFIG_MODULE, actual.getType());
}

@Test
@Secured(signType = Constants.Config.CONFIG_MODULE)
void testParseWithoutNamespace() throws NoSuchMethodException {
Secured secured = getMethodSecure();
Mockito.when(request.getParameter(eq(Constants.GROUP))).thenReturn("testG");
Mockito.when(request.getParameter(eq(Constants.DATAID))).thenReturn("testD");
Mockito.when(request.getParameter(eq(Constants.DATA_ID))).thenReturn("testD");
Resource actual = resourceParser.parse(request, secured);
assertEquals(StringUtils.EMPTY, actual.getNamespaceId());
assertEquals("testG", actual.getGroup());
Expand All @@ -82,7 +111,7 @@ void testParseWithoutNamespace() throws NoSuchMethodException {
void testParseWithoutGroup() throws NoSuchMethodException {
Secured secured = getMethodSecure();
Mockito.when(request.getParameter(eq("tenant"))).thenReturn("testNs");
Mockito.when(request.getParameter(eq(Constants.DATAID))).thenReturn("testD");
Mockito.when(request.getParameter(eq(Constants.DATA_ID))).thenReturn("testD");
Resource actual = resourceParser.parse(request, secured);
assertEquals("testNs", actual.getNamespaceId());
assertEquals(StringUtils.EMPTY, actual.getGroup());
Expand Down

0 comments on commit f5af698

Please sign in to comment.