Skip to content
Merged
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: 7 additions & 0 deletions pkg/handlers/resources/generic_resource_handler_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ func (h *GenericResourceHandler[T, V]) Search(c *gin.Context, q string, limit in
return nil, nil
}
cs := c.MustGet("cluster").(*cluster.ClientSet)
user := c.MustGet("user").(model.User)
ctx := c.Request.Context()
objectList := reflect.New(h.listType).Interface().(V)
var listOpts []client.ListOption
Expand Down Expand Up @@ -172,6 +173,12 @@ func (h *GenericResourceHandler[T, V]) Search(c *gin.Context, q string, limit in
if !isLabelSearch && !strings.Contains(strings.ToLower(obj.GetName()), strings.ToLower(q)) {
continue
}
if h.Name() == string(common.Namespaces) && !rbac.CanAccessNamespace(user, cs.Name, obj.GetName()) {
continue
}
if obj.GetNamespace() != "" && !rbac.CanAccessNamespace(user, cs.Name, obj.GetNamespace()) {
continue
}
result := common.SearchResult{
ID: string(obj.GetUID()),
Name: obj.GetName(),
Expand Down
11 changes: 7 additions & 4 deletions pkg/handlers/search_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/zxh326/kite/pkg/common"
"github.com/zxh326/kite/pkg/handlers/resources"
"github.com/zxh326/kite/pkg/middleware"
"github.com/zxh326/kite/pkg/model"
"github.com/zxh326/kite/pkg/utils"
"golang.org/x/sync/errgroup"
"k8s.io/klog/v2"
Expand Down Expand Up @@ -52,8 +53,8 @@ func NewSearchHandler() *SearchHandler {
}
}

func (h *SearchHandler) createCacheKey(clusterName, query string, limit int) string {
return fmt.Sprintf("search:%s:%d:%s", clusterName, limit, normalizeSearchQuery(query))
func (h *SearchHandler) createCacheKey(clusterName, userKey, query string, limit int) string {
return fmt.Sprintf("search:%s:%s:%d:%s", clusterName, userKey, limit, normalizeSearchQuery(query))
}

func (h *SearchHandler) Search(c *gin.Context, query string, limit int) ([]common.SearchResult, error) {
Expand Down Expand Up @@ -119,7 +120,8 @@ func (h *SearchHandler) Search(c *gin.Context, query string, limit int) ([]commo
// Only cache results when no failure (panic or error) occurred — avoids
// caching incomplete results that would be served as valid 200 OK for the TTL.
if !hadFailure.Load() {
h.cache.Add(h.createCacheKey(getSearchClusterName(c), query, limit), allResults)
user := c.MustGet("user").(model.User)
h.cache.Add(h.createCacheKey(getSearchClusterName(c), user.Key(), query, limit), allResults)
}
return allResults, nil
}
Expand All @@ -140,7 +142,8 @@ func (h *SearchHandler) GlobalSearch(c *gin.Context) {
}
limit = normalizeSearchLimit(limit)

cacheKey := h.createCacheKey(getSearchClusterName(c), query, limit)
user := c.MustGet("user").(model.User)
cacheKey := h.createCacheKey(getSearchClusterName(c), user.Key(), query, limit)

if cachedResults, found := h.cache.Get(cacheKey); found {
response := SearchResponse{
Expand Down
4 changes: 4 additions & 0 deletions pkg/handlers/search_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/zxh326/kite/pkg/common"
"github.com/zxh326/kite/pkg/handlers/resources"
"github.com/zxh326/kite/pkg/middleware"
"github.com/zxh326/kite/pkg/model"
)

func TestNormalizeSearchQuery(t *testing.T) {
Expand Down Expand Up @@ -115,6 +116,7 @@ func TestGlobalSearchNegativeLimitDoesNotPanic(t *testing.T) {
rec := httptest.NewRecorder()
ctx, _ := gin.CreateTestContext(rec)
ctx.Request = httptest.NewRequest(http.MethodGet, "/search?q=po&limit=-1", nil)
ctx.Set("user", model.AnonymousUser)

handler := NewSearchHandler()

Expand Down Expand Up @@ -188,6 +190,7 @@ func newSearchContext(t *testing.T, clusterName string) *gin.Context {
if clusterName != "" {
ctx.Set(middleware.ClusterNameKey, clusterName)
}
ctx.Set("user", model.AnonymousUser)
return ctx
}

Expand All @@ -212,6 +215,7 @@ func performGlobalSearch(t *testing.T, handler *SearchHandler, clusterName, targ
if clusterName != "" {
ctx.Set(middleware.ClusterNameKey, clusterName)
}
ctx.Set("user", model.AnonymousUser)

handler.GlobalSearch(ctx)

Expand Down
Loading