From 63cd503c38e0eec618775c09bb68397ea4c51225 Mon Sep 17 00:00:00 2001 From: liugq Date: Tue, 26 Mar 2024 12:20:44 +0800 Subject: [PATCH] feat: make certain system indices accessible in visualization data api --- plugin/api/insight/metadata.go | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/plugin/api/insight/metadata.go b/plugin/api/insight/metadata.go index 013eaa57..c36afb07 100644 --- a/plugin/api/insight/metadata.go +++ b/plugin/api/insight/metadata.go @@ -8,15 +8,20 @@ import ( "bytes" "github.com/Knetic/govaluate" log "github.com/cihub/seelog" - "text/template" + "infini.sh/console/model/alerting" "infini.sh/console/model/insight" httprouter "infini.sh/framework/core/api/router" "infini.sh/framework/core/elastic" + "infini.sh/framework/core/event" + "infini.sh/framework/core/global" "infini.sh/framework/core/orm" + "infini.sh/framework/core/radix" "infini.sh/framework/core/util" "math" "net/http" "strings" + "sync" + "text/template" ) func (h *InsightAPI) HandleGetPreview(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { @@ -181,8 +186,11 @@ func (h *InsightAPI) HandleGetMetricData(w http.ResponseWriter, req *http.Reques } clusterID := ps.MustGetParameter("id") if !h.IsIndexAllowed(req, clusterID, reqBody.IndexPattern){ - h.WriteError(w, http.StatusText(http.StatusForbidden), http.StatusForbidden) - return + allowedSystemIndices := getAllowedSystemIndices() + if clusterID != global.MustLookupString(elastic.GlobalSystemElasticsearchID) || !radix.Compile(allowedSystemIndices...).Match(reqBody.IndexPattern){ + h.WriteError(w, http.StatusText(http.StatusForbidden), http.StatusForbidden) + return + } } reqBody.ClusterId = clusterID metricData, err := getMetricData(&reqBody) @@ -195,6 +203,21 @@ func (h *InsightAPI) HandleGetMetricData(w http.ResponseWriter, req *http.Reques h.WriteJSON(w, metricData, http.StatusOK) } +var ( + allowedSystemIndicesOnce sync.Once + allowedSystemIndices []string +) +func getAllowedSystemIndices() []string { + allowedSystemIndicesOnce.Do(func() { + metricIndexName := orm.GetWildcardIndexName(event.Event{}) + activityIndexName := orm.GetIndexName(event.Activity{}) + clusterIndexName := orm.GetIndexName(elastic.ElasticsearchConfig{}) + alertMessageIndexName := orm.GetIndexName(alerting.AlertMessage{}) + allowedSystemIndices = []string{metricIndexName, activityIndexName, clusterIndexName, alertMessageIndexName} + }) + return allowedSystemIndices +} + func getMetricData(metric *insight.Metric) (interface{}, error) { query, err := GenerateQuery(metric) if err != nil {