Skip to content

Commit 1f76048

Browse files
committed
fix(pulsar): validate WebServiceURL in config
1 parent 52541a0 commit 1f76048

4 files changed

Lines changed: 76 additions & 5 deletions

File tree

pkg/mcp/builders/pulsar/status.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,6 @@ func (b *PulsarAdminStatusToolBuilder) buildStatusHandler() func(context.Context
9595
return b.handleError("get Pulsar configuration", err), nil
9696
}
9797

98-
if cfg.WebServiceURL == "" {
99-
cfg.WebServiceURL = admin.DefaultWebServiceURL
100-
}
101-
10298
authProvider, err := pulsaradminauth.GetAuthProvider((*pulsaradminconfig.Config)(cfg))
10399
if err != nil {
104100
return b.handleError("build status auth provider", err), nil

pkg/mcp/builders/pulsar/status_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ import (
1919
"testing"
2020

2121
"github.com/mark3labs/mcp-go/mcp"
22+
"github.com/streamnative/pulsarctl/pkg/cmdutils"
2223
"github.com/streamnative/streamnative-mcp-server/pkg/mcp/builders"
24+
mcpCtx "github.com/streamnative/streamnative-mcp-server/pkg/mcp/internal/context"
25+
pulsarsession "github.com/streamnative/streamnative-mcp-server/pkg/pulsar"
2326
"github.com/stretchr/testify/assert"
2427
"github.com/stretchr/testify/require"
2528
)
@@ -56,4 +59,26 @@ func TestPulsarAdminStatusToolBuilder(t *testing.T) {
5659
require.NotNil(t, result)
5760
assert.True(t, result.IsError)
5861
})
62+
63+
t.Run("Handler_EmptyWebServiceURL", func(t *testing.T) {
64+
tools, err := builder.BuildTools(context.Background(), builders.ToolBuildConfig{
65+
Features: []string{"pulsar-admin-brokers-status"},
66+
})
67+
require.NoError(t, err)
68+
require.Len(t, tools, 1)
69+
70+
ctx := mcpCtx.WithPulsarSession(context.Background(), &pulsarsession.Session{
71+
PulsarCtlConfig: &cmdutils.ClusterConfig{},
72+
})
73+
74+
result, callErr := tools[0].Handler(ctx, mcp.CallToolRequest{})
75+
require.NoError(t, callErr)
76+
require.NotNil(t, result)
77+
assert.True(t, result.IsError)
78+
require.Len(t, result.Content, 1)
79+
80+
text, ok := result.Content[0].(mcp.TextContent)
81+
require.True(t, ok)
82+
assert.Contains(t, text.Text, "Please set the cluster context first")
83+
})
5984
}

pkg/pulsar/connection.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ func (s *Session) GetPulsarCtlConfig() (*cmdutils.ClusterConfig, error) {
191191
s.mutex.RLock()
192192
defer s.mutex.RUnlock()
193193

194-
if s.PulsarCtlConfig == nil {
194+
if s.PulsarCtlConfig == nil || s.PulsarCtlConfig.WebServiceURL == "" {
195195
return nil, fmt.Errorf("err: ContextNotSetErr: Please set the cluster context first")
196196
}
197197

pkg/pulsar/connection_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2025 StreamNative
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package pulsar
16+
17+
import (
18+
"testing"
19+
20+
"github.com/streamnative/pulsarctl/pkg/cmdutils"
21+
"github.com/stretchr/testify/require"
22+
)
23+
24+
func TestSessionGetPulsarCtlConfigRequiresWebServiceURL(t *testing.T) {
25+
session := &Session{}
26+
27+
_, err := session.GetPulsarCtlConfig()
28+
require.EqualError(t, err, "err: ContextNotSetErr: Please set the cluster context first")
29+
30+
session.PulsarCtlConfig = &cmdutils.ClusterConfig{}
31+
32+
_, err = session.GetPulsarCtlConfig()
33+
require.EqualError(t, err, "err: ContextNotSetErr: Please set the cluster context first")
34+
}
35+
36+
func TestSessionGetPulsarCtlConfigReturnsCopy(t *testing.T) {
37+
session := &Session{
38+
PulsarCtlConfig: &cmdutils.ClusterConfig{
39+
WebServiceURL: "http://pulsar.example.com:8080",
40+
},
41+
}
42+
43+
cfg, err := session.GetPulsarCtlConfig()
44+
require.NoError(t, err)
45+
require.Equal(t, "http://pulsar.example.com:8080", cfg.WebServiceURL)
46+
47+
cfg.WebServiceURL = "http://mutated.example.com:8080"
48+
49+
require.Equal(t, "http://pulsar.example.com:8080", session.PulsarCtlConfig.WebServiceURL)
50+
}

0 commit comments

Comments
 (0)