Skip to content

Commit

Permalink
fix: auth login failed in address mode. (nacos-group#728)
Browse files Browse the repository at this point in the history
  • Loading branch information
huangjikun committed Mar 3, 2024
1 parent 389f6b6 commit 7fbf59a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
5 changes: 3 additions & 2 deletions common/nacos_server/nacos_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ func NewNacosServer(ctx context.Context, serverList []constant.ServerConfig, cli
ns.initRefreshSrvIfNeed(ctx)
}

_, err := securityLogin.Login()
_, err := ns.securityLogin.Login()

if err != nil {
logger.Errorf("login in err:%v", err)
}

securityLogin.AutoRefresh(ctx)
ns.securityLogin.AutoRefresh(ctx)
return &ns, nil
}

Expand Down Expand Up @@ -344,6 +344,7 @@ func (server *NacosServer) refreshServerSrvIfNeed(urlString string, header map[s
server.ServerSrcChangeSignal <- struct{}{}
}
server.lastSrvRefTime = util.CurrentMillis()
server.securityLogin.UpdateServerList(servers)
server.Unlock()
}

Expand Down
24 changes: 24 additions & 0 deletions common/nacos_server/nacos_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,27 @@ func TestNacosServer_InjectSignForNamingHttp_WithoutServiceNameAndGroup(t *testi
_, has := param["signature"]
assert.True(t, has)
}

func TestNacosServer_UpdateServerListForSecurityLogin(t *testing.T) {
endpoint := "console.nacos.io:80"
clientConfig := constant.ClientConfig{
Username: "nacos",
Password: "nacos",
NamespaceId: "namespace_1",
Endpoint: endpoint,
EndpointContextPath: "nacos",
ClusterName: "serverlist",
AppendToStdout: true,
}
server, err := NewNacosServer(context.Background(),
nil,
clientConfig,
&http_agent.HttpAgent{},
1000,
endpoint,
nil)
if err != nil {
t.FailNow()
}
assert.Equal(t, server.GetServerList(), server.securityLogin.GetServerList())
}
8 changes: 8 additions & 0 deletions common/security/security_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ func (ac *AuthClient) Login() (bool, error) {
return false, throwable
}

func (ac *AuthClient) UpdateServerList(serverList []constant.ServerConfig) {
ac.serverCfgs = serverList
}

func (ac *AuthClient) GetServerList() []constant.ServerConfig {
return ac.serverCfgs
}

func (ac *AuthClient) login(server constant.ServerConfig) (bool, error) {
if ac.username != "" {
contextPath := server.ContextPath
Expand Down

0 comments on commit 7fbf59a

Please sign in to comment.