refactor: Improve Elasticsearch query construction and JWT secret retrieval

- Enhanced Elasticsearch query construction by safely building the query body using JSON marshaling, improving error handling for query parsing and marshaling.
- Updated JWT secret retrieval to securely generate a random secret if the environment variable is not set, ensuring better security practices.
This commit is contained in:
wizardchen
2025-11-25 23:58:14 +08:00
parent 016df3a4db
commit a0ada8bcdc
2 changed files with 42 additions and 14 deletions
@@ -885,18 +885,30 @@ func (e *elasticsearchRepository) querySourceBatch(ctx context.Context,
) ([]interface{}, error) {
log := logger.GetLogger(ctx)
// Build query request
filter := e.getBaseConds(retrieveParams)
query := fmt.Sprintf(`{
"query": %s,
"from": %d,
"size": %d
}`, filter, from, batchSize)
// Build query request safely
filterJSON := e.getBaseConds(retrieveParams)
var filter map[string]interface{}
if err := json.Unmarshal([]byte(filterJSON), &filter); err != nil {
log.Errorf("[ElasticsearchV7] Failed to parse base conditions: %v", err)
filter = map[string]interface{}{}
}
queryBody := map[string]interface{}{
"query": filter,
"from": from,
"size": batchSize,
}
queryBytes, err := json.Marshal(queryBody)
if err != nil {
log.Errorf("[ElasticsearchV7] Failed to marshal query body: %v", err)
return nil, err
}
// Execute query
response, err := e.client.Search(
e.client.Search.WithIndex(e.index),
e.client.Search.WithBody(strings.NewReader(query)),
e.client.Search.WithBody(strings.NewReader(string(queryBytes))),
e.client.Search.WithContext(ctx),
)
if err != nil {