Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions pkg/console/handler/mesh.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ import (
"github.com/apache/dubbo-admin/pkg/console/model"
)

// ListMeshes list all meshes(discoveries + engine) defined in config
// ListMeshes lists all registry meshes defined in config.
// When no discovery is configured, it falls back to a non-mock engine so
// runtime-only deployments remain selectable in the UI.
func ListMeshes(ctx consolectx.Context) gin.HandlerFunc {
return func(c *gin.Context) {
discoveries := ctx.Config().Discovery
Expand All @@ -41,9 +43,8 @@ func ListMeshes(ctx consolectx.Context) gin.HandlerFunc {
}
})

// Include the engine as a mesh so K8s resources are discoverable in the UI
engineCfg := ctx.Config().Engine
if engineCfg != nil && engineCfg.Type != enginecfg.Mock {
if len(meshes) == 0 && engineCfg != nil && engineCfg.Type != enginecfg.Mock {
meshes = append(meshes, model.MeshResp{
ID: engineCfg.ID,
Name: engineCfg.Name,
Expand Down
119 changes: 119 additions & 0 deletions pkg/console/handler/mesh_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package handler

import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/require"

appcfg "github.com/apache/dubbo-admin/pkg/config/app"
discoverycfg "github.com/apache/dubbo-admin/pkg/config/discovery"
enginecfg "github.com/apache/dubbo-admin/pkg/config/engine"
"github.com/apache/dubbo-admin/pkg/console/model"
)

func TestListMeshes(t *testing.T) {
tests := []struct {
name string
cfg appcfg.AdminConfig
expected []model.MeshResp
}{
{
name: "excludes kubernetes engine when discovery is configured",
cfg: appcfg.AdminConfig{
Discovery: []*discoverycfg.Config{
{ID: "nacos2.5", Name: "nacos2.5-standalone", Type: discoverycfg.Nacos2},
},
Engine: &enginecfg.Config{
ID: "default",
Name: "dubbo-admin-rke2",
Type: enginecfg.Kubernetes,
},
},
expected: []model.MeshResp{
{ID: "nacos2.5", Name: "nacos2.5-standalone", Type: string(discoverycfg.Nacos2)},
},
},
{
name: "preserves multiple discoveries and excludes engine",
cfg: appcfg.AdminConfig{
Discovery: []*discoverycfg.Config{
{ID: "nacos2.5", Name: "nacos-main", Type: discoverycfg.Nacos2},
{ID: "zk3.6", Name: "zk-main", Type: discoverycfg.Zookeeper},
},
Engine: &enginecfg.Config{
ID: "default",
Name: "k8s-prod",
Type: enginecfg.Kubernetes,
},
},
expected: []model.MeshResp{
{ID: "nacos2.5", Name: "nacos-main", Type: string(discoverycfg.Nacos2)},
{ID: "zk3.6", Name: "zk-main", Type: string(discoverycfg.Zookeeper)},
},
},
{
name: "falls back to non-mock engine when no discovery is configured",
cfg: appcfg.AdminConfig{
Engine: &enginecfg.Config{
ID: "default",
Name: "k8s-only",
Type: enginecfg.Kubernetes,
},
},
expected: []model.MeshResp{
{ID: "default", Name: "k8s-only", Type: string(enginecfg.Kubernetes)},
},
},
{
name: "returns empty list when only mock engine is configured",
cfg: appcfg.AdminConfig{
Engine: &enginecfg.Config{
ID: "mock",
Name: "mock",
Type: enginecfg.Mock,
},
},
expected: []model.MeshResp{},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := observabilityHandlerTestContext{cfg: tt.cfg}
c, recorder := testGinContext()
c.Request = httptest.NewRequest(http.MethodGet, "/api/v1/meshes", nil)

ListMeshes(ctx)(c)

require.Equal(t, http.StatusOK, recorder.Code)
var resp struct {
Code string `json:"code"`
Message string `json:"message"`
Data []model.MeshResp `json:"data"`
}
require.NoError(t, json.Unmarshal(recorder.Body.Bytes(), &resp))
require.Equal(t, "Success", resp.Code)
require.Equal(t, tt.expected, resp.Data)
})
}
}
52 changes: 47 additions & 5 deletions pkg/console/handler/overview.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package handler

import (
"net/http"
"net/url"
"strings"

"github.com/gin-gonic/gin"

Expand Down Expand Up @@ -55,17 +57,57 @@ func AdminMetadata(ctx consolectx.Context) gin.HandlerFunc {
grafanaURL = cfg.Observability.GrafanaBaseURL.String()
}
metadata := model.AdminMetadata{
Registry: registryAddr,
Metadata: metadataAddr,
Config: configAddr,
Prometheus: prometheusURL,
Grafana: grafanaURL,
Registry: sanitizeMetadataURL(registryAddr),
Metadata: sanitizeMetadataURL(metadataAddr),
Config: sanitizeMetadataURL(configAddr),
Prometheus: sanitizeMetadataURL(prometheusURL),
Grafana: sanitizeMetadataURL(grafanaURL),
Tracing: "",
}
c.JSON(http.StatusOK, model.NewSuccessResp(metadata))
}
}

func isSensitiveQueryKey(key string) bool {
lower := strings.ToLower(strings.TrimSpace(key))
for _, token := range []string{"password", "passwd", "pwd", "secret", "token", "apikey", "api_key", "accesskey", "access_key", "secretkey", "secret_key", "auth"} {
if lower == token || strings.Contains(lower, token) {
return true
}
}
return false
}

func sanitizeMetadataURL(raw string) string {
trimmed := strings.TrimSpace(raw)
if trimmed == "" {
return ""
}
parsed, err := url.Parse(trimmed)
if err != nil {
return trimmed
}
if parsed.User != nil {
if _, hasPassword := parsed.User.Password(); hasPassword {
parsed.User = nil
}
}
if parsed.RawQuery != "" {
query := parsed.Query()
changed := false
for key := range query {
if isSensitiveQueryKey(key) {
query.Del(key)
changed = true
}
}
if changed {
parsed.RawQuery = query.Encode()
}
}
return parsed.String()
}
Comment on lines +81 to +109

// ClusterOverview get cluster overview
func ClusterOverview(ctx consolectx.Context) gin.HandlerFunc {
return func(c *gin.Context) {
Expand Down
119 changes: 119 additions & 0 deletions pkg/console/handler/overview_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package handler

import (
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
"testing"

"github.com/stretchr/testify/require"

appcfg "github.com/apache/dubbo-admin/pkg/config/app"
discoverycfg "github.com/apache/dubbo-admin/pkg/config/discovery"
observabilitycfg "github.com/apache/dubbo-admin/pkg/config/observability"
"github.com/apache/dubbo-admin/pkg/console/model"
)

func TestSanitizeMetadataURL(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
name: "empty string",
input: "",
expected: "",
},
{
name: "plain url unchanged",
input: "http://127.0.0.1:9090",
expected: "http://127.0.0.1:9090",
},
{
name: "strips userinfo password",
input: "https://admin:s3cr3t@grafana.internal:3000/grafana",
expected: "https://grafana.internal:3000/grafana",
},
{
name: "strips nacos.password and keeps non-sensitive params",
input: "nacos://127.0.0.1:8848?nacos.username=nacos&nacos.password=nacos123&namespace=public",
expected: "nacos://127.0.0.1:8848?nacos.username=nacos&namespace=public",
},
{
name: "strips token and secret query params",
input: "http://prom.internal:9090/api?token=abc&api_key=xyz&secret=123&env=prod",
expected: "http://prom.internal:9090/api?env=prod",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.expected, sanitizeMetadataURL(tt.input))
})
}
}

func TestAdminMetadataSanitizesCredentials(t *testing.T) {
promURL, err := url.Parse("http://user:pass@127.0.0.1:9090?token=secret-token")
require.NoError(t, err)
grafanaURL, err := url.Parse("http://127.0.0.1:3000/grafana?api_key=abc")
require.NoError(t, err)

ctx := observabilityHandlerTestContext{
cfg: appcfg.AdminConfig{
Discovery: []*discoverycfg.Config{
{
ID: "nacos2.5",
Name: "nacos2.5",
Type: discoverycfg.Nacos2,
Address: discoverycfg.AddressConfig{
Registry: "nacos://127.0.0.1:8848?nacos.username=nacos&nacos.password=topsecret",
MetadataReport: "nacos://127.0.0.1:8848?password=meta-secret",
ConfigCenter: "nacos://user:cfg-pass@127.0.0.1:8848?group=DEFAULT_GROUP",
},
},
},
Observability: &observabilitycfg.Config{
PrometheusBaseURL: promURL,
GrafanaBaseURL: grafanaURL,
},
},
}

c, recorder := testGinContext()
c.Request = httptest.NewRequest(http.MethodGet, "/api/v1/metadata?mesh=nacos2.5", nil)

AdminMetadata(ctx)(c)

require.Equal(t, http.StatusOK, recorder.Code)
var resp struct {
Code string `json:"code"`
Data model.AdminMetadata `json:"data"`
}
require.NoError(t, json.Unmarshal(recorder.Body.Bytes(), &resp))
require.Equal(t, "Success", resp.Code)
require.Equal(t, "nacos://127.0.0.1:8848?nacos.username=nacos", resp.Data.Registry)
require.Equal(t, "nacos://127.0.0.1:8848", resp.Data.Metadata)
require.Equal(t, "nacos://127.0.0.1:8848?group=DEFAULT_GROUP", resp.Data.Config)
require.Equal(t, "http://127.0.0.1:9090", resp.Data.Prometheus)
require.Equal(t, "http://127.0.0.1:3000/grafana", resp.Data.Grafana)
}
7 changes: 4 additions & 3 deletions ui-vue3/src/Login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { login, type AuthProvider } from '@/api/service/login'
import { useRoute, useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
import { i18n } from '@/base/i18n'
import { useMeshStore } from '@/stores/mesh'
import { resolveActiveMesh, useMeshStore } from '@/stores/mesh'
import { meshesSearch } from '@/api/service/globalSearch'
import { loadAuthConfiguration, providerLoginURL, syncAuthenticatedPrincipal } from '@/auth/session'

Expand Down Expand Up @@ -63,8 +63,9 @@ async function loginHandle() {
await login(formData)
await syncAuthenticatedPrincipal()
const { data } = await meshesSearch()
if (!meshStore.mesh || !data.some((item: any) => item.id === meshStore.mesh)) {
meshStore.mesh = data[0]?.id
const resolvedMesh = resolveActiveMesh(meshStore.mesh, data)
if (resolvedMesh !== meshStore.mesh) {
meshStore.mesh = resolvedMesh
}
await router.replace(redirect)
} catch {
Expand Down
Loading
Loading