fix: init user just one time (#140)

* fix: init user just one time

* chore: init system cluster metrics default settings

---------

Co-authored-by: hardy <luohf@infinilabs.com>
This commit is contained in:
Hardy 2025-02-15 21:11:50 +08:00 committed by GitHub
parent fd2746a210
commit 71d291fdf1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 53 additions and 16 deletions

View File

@ -69,8 +69,11 @@ import (
"infini.sh/framework/plugins/replay"
)
// Easysearch auto create ingest user password
const ingestUser = "infini_ingest"
var ingestPassword = util.GenerateRandomString(20)
type Module struct {
api.Handler
}
@ -503,6 +506,42 @@ func (module *Module) initialize(w http.ResponseWriter, r *http.Request, ps http
//保存默认集群
t := time.Now()
toSaveCfg.MetadataConfigs = &elastic.MetadataConfig{
HealthCheck: elastic.TaskConfig{
Enabled: true,
Interval: "10s",
},
ClusterSettingsCheck: elastic.TaskConfig{
Enabled: true,
Interval: "10s",
},
MetadataRefresh: elastic.TaskConfig{
Enabled: true,
Interval: "10s",
},
NodeAvailabilityCheck: elastic.TaskConfig{
Enabled: true,
Interval: "10s",
},
}
toSaveCfg.MonitorConfigs = &elastic.MonitorConfig{
ClusterStats: elastic.TaskConfig{
Enabled: true,
Interval: "10s",
},
NodeStats: elastic.TaskConfig{
Enabled: true,
Interval: "10s",
},
ClusterHealth: elastic.TaskConfig{
Enabled: true,
Interval: "10s",
},
IndexStats: elastic.TaskConfig{
Enabled: true,
Interval: "10s",
},
}
toSaveCfg.Created = &t
err = orm.Save(nil, &toSaveCfg)
if err != nil {
@ -683,6 +722,7 @@ func getYamlData(filename string) []byte {
escapedContent = bytes.ReplaceAll(escapedContent, []byte("\""), []byte("\\\""))
return escapedContent
}
func (module *Module) initializeTemplate(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if !global.Env().SetupRequired() {
module.WriteError(w, "setup not permitted", 500)
@ -730,6 +770,17 @@ func (module *Module) initializeTemplate(w http.ResponseWriter, r *http.Request,
case "view":
dslTplFileName = "view.tpl"
case "agent":
if ver.Distribution == elastic.Easysearch {
err = keystore.SetValue("SYSTEM_CLUSTER_INGEST_PASSWORD", []byte(ingestPassword))
if err != nil {
panic(err)
}
client := elastic.GetClient(GlobalSystemElasticsearchID)
err = initIngestUser(client, cfg1.IndexPrefix, ingestUser, ingestPassword)
if err != nil {
panic(err)
}
}
dslTplFileName = "agent.tpl"
default:
panic(fmt.Sprintf("unsupport template name [%s]", request.InitializeTemplate))
@ -786,20 +837,6 @@ func (module *Module) initializeTemplate(w http.ResponseWriter, r *http.Request,
return
}
// Easysearch auto create ingest user
ingestPassword := util.GenerateRandomString(20)
if ver.Distribution == elastic.Easysearch {
err = keystore.SetValue("SYSTEM_CLUSTER_INGEST_PASSWORD", []byte(ingestPassword))
if err != nil {
panic(err)
}
client := elastic.GetClient(GlobalSystemElasticsearchID)
err = initIngestUser(client, cfg1.IndexPrefix, ingestUser, ingestPassword)
if err != nil {
panic(err)
}
}
output := tpl.ExecuteFuncString(func(w io.Writer, tag string) (int, error) {
switch tag {
case "SETUP_SYSTEM_INGEST_CONFIG":
@ -925,7 +962,7 @@ func initIngestUser(client elastic.API, indexPrefix string, username, password s
}]
}`
roleBody := fmt.Sprintf(roleTpl, indexPrefix, indexPrefix)
err := client.PutRole(ingestUser, []byte(roleBody))
err := client.PutRole(username, []byte(roleBody))
if err != nil {
return fmt.Errorf("failed to create ingest role: %w", err)
}
@ -935,7 +972,7 @@ func initIngestUser(client elastic.API, indexPrefix string, username, password s
],
"password": "%s"}`
userBody := fmt.Sprintf(userTpl, ingestUser, password)
userBody := fmt.Sprintf(userTpl, username, password)
err = client.PutUser(username, []byte(userBody))
if err != nil {
return fmt.Errorf("failed to create ingest user: %w", err)