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:
@@ -885,18 +885,30 @@ func (e *elasticsearchRepository) querySourceBatch(ctx context.Context,
|
|||||||
) ([]interface{}, error) {
|
) ([]interface{}, error) {
|
||||||
log := logger.GetLogger(ctx)
|
log := logger.GetLogger(ctx)
|
||||||
|
|
||||||
// Build query request
|
// Build query request safely
|
||||||
filter := e.getBaseConds(retrieveParams)
|
filterJSON := e.getBaseConds(retrieveParams)
|
||||||
query := fmt.Sprintf(`{
|
var filter map[string]interface{}
|
||||||
"query": %s,
|
if err := json.Unmarshal([]byte(filterJSON), &filter); err != nil {
|
||||||
"from": %d,
|
log.Errorf("[ElasticsearchV7] Failed to parse base conditions: %v", err)
|
||||||
"size": %d
|
filter = map[string]interface{}{}
|
||||||
}`, filter, from, batchSize)
|
}
|
||||||
|
|
||||||
|
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
|
// Execute query
|
||||||
response, err := e.client.Search(
|
response, err := e.client.Search(
|
||||||
e.client.Search.WithIndex(e.index),
|
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),
|
e.client.Search.WithContext(ctx),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -2,10 +2,13 @@ package service
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"crypto/rand"
|
||||||
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/golang-jwt/jwt/v5"
|
"github.com/golang-jwt/jwt/v5"
|
||||||
@@ -17,14 +20,27 @@ import (
|
|||||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||||
)
|
)
|
||||||
|
|
||||||
// JWT secret key - in production this should be from environment variable
|
var (
|
||||||
|
jwtSecretOnce sync.Once
|
||||||
|
jwtSecret string
|
||||||
|
)
|
||||||
|
|
||||||
|
// getJwtSecret retrieves the JWT secret from the environment, falling back to a securely generated random secret.
|
||||||
func getJwtSecret() string {
|
func getJwtSecret() string {
|
||||||
jwtSecret := os.Getenv("JWT_SECRET")
|
jwtSecretOnce.Do(func() {
|
||||||
if jwtSecret != "" {
|
if envSecret := strings.TrimSpace(os.Getenv("JWT_SECRET")); envSecret != "" {
|
||||||
return jwtSecret
|
jwtSecret = envSecret
|
||||||
}
|
return
|
||||||
return "your-secret-key"
|
}
|
||||||
|
|
||||||
|
randomBytes := make([]byte, 32)
|
||||||
|
if _, err := rand.Read(randomBytes); err != nil {
|
||||||
|
panic(fmt.Sprintf("failed to generate JWT secret: %v", err))
|
||||||
|
}
|
||||||
|
jwtSecret = base64.StdEncoding.EncodeToString(randomBytes)
|
||||||
|
})
|
||||||
|
|
||||||
|
return jwtSecret
|
||||||
}
|
}
|
||||||
|
|
||||||
// userService implements the UserService interface
|
// userService implements the UserService interface
|
||||||
|
|||||||
Reference in New Issue
Block a user