Skip to content

Commit

Permalink
[ISSUE #12257] client auth plugin support refreshing server list (#12920
Browse files Browse the repository at this point in the history
)
  • Loading branch information
misakacoder authored Dec 5, 2024
1 parent 5c4bdb4 commit 7651eb3
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public ConfigTransportClient(NacosClientProperties properties, ConfigServerListM
this.tenant = properties.getProperty(PropertyKeyConst.NAMESPACE);
this.serverListManager = serverListManager;
this.properties = properties.asProperties();
this.securityProxy = new SecurityProxy(serverListManager.getServerList(),
this.securityProxy = new SecurityProxy(serverListManager,
ConfigHttpClientManager.getInstance().getNacosRestTemplate());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private void init(Properties properties) throws NacosException {
InitUtils.initWebRootContext(nacosClientProperties);
serverListManager = new NamingServerListManager(nacosClientProperties, namespace);
serverListManager.start();
securityProxy = new SecurityProxy(serverListManager.getServerList(),
securityProxy = new SecurityProxy(serverListManager,
NamingHttpClientManager.getInstance().getNacosRestTemplate());
initSecurityProxy(properties);
serverProxy = new NamingHttpClientProxy(namespace, securityProxy, serverListManager, nacosClientProperties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public NamingClientProxyDelegate(String namespace, ServiceInfoHolder serviceInfo
this.serverListManager = new NamingServerListManager(properties, namespace);
this.serverListManager.start();
this.serviceInfoHolder = serviceInfoHolder;
this.securityProxy = new SecurityProxy(this.serverListManager.getServerList(),
this.securityProxy = new SecurityProxy(this.serverListManager,
NamingHttpClientManager.getInstance().getNacosRestTemplate());
initSecurityProxy(properties);
this.httpClientProxy = new NamingHttpClientProxy(namespace, securityProxy, serverListManager, properties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,22 @@
package com.alibaba.nacos.client.security;

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.client.address.AbstractServerListManager;
import com.alibaba.nacos.client.address.ServerListChangeEvent;
import com.alibaba.nacos.client.auth.impl.NacosAuthLoginConstant;
import com.alibaba.nacos.plugin.auth.spi.client.ClientAuthPluginManager;
import com.alibaba.nacos.plugin.auth.api.LoginIdentityContext;
import com.alibaba.nacos.plugin.auth.spi.client.ClientAuthService;
import com.alibaba.nacos.plugin.auth.api.RequestResource;
import com.alibaba.nacos.common.http.client.NacosRestTemplate;
import com.alibaba.nacos.common.lifecycle.Closeable;
import com.alibaba.nacos.common.notify.Event;
import com.alibaba.nacos.common.notify.NotifyCenter;
import com.alibaba.nacos.common.notify.listener.Subscriber;
import com.alibaba.nacos.plugin.auth.api.LoginIdentityContext;
import com.alibaba.nacos.plugin.auth.api.RequestResource;
import com.alibaba.nacos.plugin.auth.spi.client.ClientAuthPluginManager;
import com.alibaba.nacos.plugin.auth.spi.client.ClientAuthService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

Expand All @@ -45,15 +49,25 @@ public class SecurityProxy implements Closeable {
private ClientAuthPluginManager clientAuthPluginManager;

/**
* Construct from serverList, nacosRestTemplate, init client auth plugin.
* // TODO change server list to serverListManager after serverListManager refactor and unite.
* Construct from serverListManager, nacosRestTemplate, init client auth plugin.
*
* @param serverList a server list that client request to.
* @param serverListManager a server list manager that client request to.
* @Param nacosRestTemplate http request template.
*/
public SecurityProxy(List<String> serverList, NacosRestTemplate nacosRestTemplate) {
public SecurityProxy(AbstractServerListManager serverListManager, NacosRestTemplate nacosRestTemplate) {
clientAuthPluginManager = new ClientAuthPluginManager();
clientAuthPluginManager.init(serverList, nacosRestTemplate);
clientAuthPluginManager.init(serverListManager.getServerList(), nacosRestTemplate);
NotifyCenter.registerSubscriber(new Subscriber<ServerListChangeEvent>() {
@Override
public void onEvent(ServerListChangeEvent event) {
clientAuthPluginManager.refreshServerList(serverListManager.getServerList());
}

@Override
public Class<? extends Event> subscribeType() {
return ServerListChangeEvent.class;
}
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.client.address.AbstractServerListManager;
import com.alibaba.nacos.client.auth.impl.NacosAuthLoginConstant;
import com.alibaba.nacos.client.env.NacosClientProperties;
import com.alibaba.nacos.common.http.HttpRestResult;
import com.alibaba.nacos.common.http.client.NacosRestTemplate;
import com.alibaba.nacos.common.http.param.Header;
Expand Down Expand Up @@ -68,7 +70,34 @@ void setUp() throws Exception {

List<String> serverList = new ArrayList<>();
serverList.add("localhost");
securityProxy = new SecurityProxy(serverList, nacosRestTemplate);
NacosClientProperties properties = NacosClientProperties.PROTOTYPE.derive(new Properties());
AbstractServerListManager serverListManager = new AbstractServerListManager(properties) {
@Override
protected String getModuleName() {
return "Test";
}

@Override
protected NacosRestTemplate getNacosRestTemplate() {
return nacosRestTemplate;
}

@Override
public String genNextServer() {
return serverList.get(0);
}

@Override
public String getCurrentServer() {
return serverList.get(0);
}

@Override
public List<String> getServerList() {
return serverList;
}
};
securityProxy = new SecurityProxy(serverListManager, nacosRestTemplate);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
*/
public abstract class AbstractClientAuthService implements ClientAuthService {

protected List<String> serverList;
protected volatile List<String> serverList;

protected NacosRestTemplate nacosRestTemplate;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ public void init(List<String> serverList, NacosRestTemplate nacosRestTemplate) {
}
}

/**
* refresh ClientAuthService server list.
*
* @param serverList the new server list.
*/
public void refreshServerList(List<String> serverList) {
for (ClientAuthService clientAuthService : clientAuthServiceHashSet) {
clientAuthService.setServerList(serverList);
}
}

/**
* get all ClientAuthService instance.
*
Expand Down

0 comments on commit 7651eb3

Please sign in to comment.