RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
Add audit log stub (#21712)
Jin Haic81b2f005263
diff --git a/cmd/ragflow_server.go b/cmd/ragflow_server.goindex 230f62f086..f71bdacd0a 100644--- a/cmd/ragflow_server.go+++ b/cmd/ragflow_server.go@@ -460,6 +460,13 @@ func main() {os.Exit(1)}+ auditConfig := globalConfig.GetAuditConfig()+ err = common.InitAudit(auditConfig.Type, auditConfig.GetFileOutput(), serverName)+ if err != nil {+ common.Error("Failed to initialize audit logger", err)+ os.Exit(1)+ }+// set server name and log file pathserver.SetServerName(serverName)diff --git a/conf/service_conf.yaml b/conf/service_conf.yamlindex 7801da4c7d..8f52d3b26a 100644--- a/conf/service_conf.yaml+++ b/conf/service_conf.yaml@@ -124,6 +124,13 @@ ingestor:# unset (recommended), N is derived from K so each Run stays single-threaded# by default (the prior behaviour); an explicit 0 opts into "all cores".# inference_cpu_cores: 0+ log:+ type: file # file | database+ format: json # text | json (reserved; text only today)+ path: ingest_log.log # empty/missing = binary default (server_main.log / admin_server.log / ingestion_server.log)+ max_size: 1000 # MB before rotation+ max_backups: 10 # retained rotated files+ max_age: 30 # daysfile_syncer:max_concurrent_syncs: 5user_default_llm:@@ -137,6 +144,13 @@ gcs:bucket: '${GCS_BUCKET:-ragflow}'prefix_path: '${GCS_PREFIX_PATH:-}'endpoint_url: '${GCS_ENDPOINT:-}'+audit:+ type: file # file | database+ format: json # text | json (reserved; text only today)+ path: audit_log.log # empty/missing = binary default (server_main.log / admin_server.log / ingestion_server.log)+ max_size: 100 # MB before rotation+ max_backups: 10 # retained rotated files+ max_age: 30 # days# postgres:# name: 'rag_flow'# user: 'rag_flow'diff --git a/docker/service_conf.yaml.template b/docker/service_conf.yaml.templateindex 3c98118371..9e59d8583b 100644--- a/docker/service_conf.yaml.template+++ b/docker/service_conf.yaml.template@@ -136,6 +136,13 @@ ingestor:# (highest precedence). Valid range [1, 16]. Default 2.page_concurrency: 2compiler_pool_size: 0+ # log:+ # type: file # file | database+ # format: json # text | json (reserved; text only today)+ # path: ingest_log.log # empty/missing = binary default (server_main.log / admin_server.log / ingestion_server.log)+ # max_size: 100 # MB before rotation+ # max_backups: 10 # retained rotated files+ # max_age: 30 # daysfile_syncer:max_concurrent_syncs: 5user_default_llm:@@ -243,3 +250,10 @@ user_default_llm:# max_backups: 10 # retained rotated files# max_age: 30 # days# compress: true # gzip rotated files; default true. Set false to disable.+# audit:+# type: file # file | database+# format: json # text | json (reserved; text only today)+# path: audit_log.log # empty/missing = binary default (server_main.log / admin_server.log / ingestion_server.log)+# max_size: 100 # MB before rotation+# max_backups: 10 # retained rotated files+# max_age: 30 # days\ No newline at end of filediff --git a/internal/common/model.go b/internal/common/model.goindex 01c1938893..8d9408db91 100644--- a/internal/common/model.go+++ b/internal/common/model.go@@ -62,8 +62,8 @@ type OperationLog struct {ResourceID string `json:"resource_id"`ResourceName string `json:"resource_name"`RequestParams string `json:"request_params"`- ErrorCode uint16 `json:"error_code"`- Message string `json:"message"`+ Code int `json:"code"`+ Error error `json:"error"`DurationMS int64 `json:"duration_ms"`}diff --git a/internal/common/stub.go b/internal/common/stub.goindex dbf96f3f8c..0dacb0b53b 100644--- a/internal/common/stub.go+++ b/internal/common/stub.go@@ -18,6 +18,10 @@package common+import (+ "go.uber.org/zap"+)+var (OpenSourceVersion = "open source"EnterpriseEdition = "enterprise edition"@@ -27,3 +31,18 @@ var (func GetRAGFlowType() string {return OpenSourceVersion}++func InitAudit(logType string, file FileOutput, serviceName string) error {+ return nil+}++func Audit(operationLog *OperationLog, fields ...zap.Field) {+}++func InitIngestLog(logType string, file FileOutput, serviceName string) error {+ return nil+}++func IngestLog(fields ...zap.Field) {++}diff --git a/internal/handler/user.go b/internal/handler/user.goindex e9f1436d65..cdbc2df6aa 100644--- a/internal/handler/user.go+++ b/internal/handler/user.go@@ -17,10 +17,10 @@package handlerimport (+ "errors""fmt""net/http""ragflow/internal/common"- "ragflow/internal/engine/clickhouse""ragflow/internal/engine/kvrocks""ragflow/internal/server""ragflow/internal/server/local"@@ -57,11 +57,6 @@ const oauthAuthCookie = "ragflow_auth"// Authorization header on subsequent fetches. The server checks the token// signature and expiry on each request.func setOAuthAuthCookie(c *gin.Context, token string) {- // the SPA's bootstrap credential after the OAuth redirect. The- // SPA reads it via document.cookie and copies it into the- // Authorization header. Setting HttpOnly would break the login- // flow. The API validates the token signature and expiry on each request.- // codeql[go/cookie-httponly-not-set] Intentional: this cookie ishttp.SetCookie(c.Writer, &http.Cookie{Name: oauthAuthCookie,Value: token,@@ -139,20 +134,14 @@ func (h *UserHandler) Login(c *gin.Context) {}defer func() {operationLog.DurationMS = time.Since(startAt).Milliseconds()- clickhouseDriver := clickhouse.GetDriver()- err := clickhouseDriver.SaveOperationLog(operationLog)- if err != nil {- common.ResponseWithCodeData(c, common.CodeServerError, false, err.Error())- return- }+ common.Audit(operationLog)}()var req service.LoginRequestif err := c.ShouldBindJSON(&req); err != nil {common.ResponseWithCodeData(c, common.CodeBadRequest, false, err.Error())- operationLog.ErrorCode = uint16(common.CodeBadRequest)- operationLog.Message = err.Error()- operationLog.DurationMS = time.Since(startAt).Milliseconds()+ operationLog.Code = int(common.CodeBadRequest)+ operationLog.Error = errreturn}operationLog.ResourceName = req.Username@@ -160,9 +149,8 @@ func (h *UserHandler) Login(c *gin.Context) {user, code, err := h.userService.Login(ctx, &req)if err != nil {common.ResponseWithCodeData(c, code, false, err.Error())- operationLog.ErrorCode = uint16(code)- operationLog.Message = err.Error()- operationLog.DurationMS = time.Since(startAt).Milliseconds()+ operationLog.Code = int(code)+ operationLog.Error = errreturn}operationLog.UserID = user.ID@@ -170,19 +158,17 @@ func (h *UserHandler) Login(c *gin.Context) {// Sign the access token with the active tenant context.secretKey, err := server.GetSecretKey(ctx, kvrocks.Get())if err != nil {- errMessage := fmt.Sprintf("Failed to get secret key: %s", err.Error())- common.ResponseWithCodeData(c, common.CodeServerError, false, errMessage)- operationLog.ErrorCode = uint16(common.CodeServerError)- operationLog.Message = errMessage- operationLog.DurationMS = time.Since(startAt).Milliseconds()+ errMessage := fmt.Errorf("failed to get secret key: %s", err.Error())+ common.ResponseWithCodeData(c, common.CodeServerError, false, errMessage.Error())+ operationLog.Code = int(common.CodeServerError)+ operationLog.Error = errMessagereturn}authToken, err := issueUserAccessToken(user, secretKey)if err != nil {common.ResponseWithCodeData(c, common.CodeServerError, false, "Failed to generate auth token")- operationLog.ErrorCode = uint16(common.CodeServerError)- operationLog.Message = "Failed to generate auth token"- operationLog.DurationMS = time.Since(startAt).Milliseconds()+ operationLog.Code = int(common.CodeServerError)+ operationLog.Error = errreturn}@@ -218,20 +204,14 @@ func (h *UserHandler) LoginByEmail(c *gin.Context) {}defer func() {operationLog.DurationMS = time.Since(startAt).Milliseconds()- clickhouseDriver := clickhouse.GetDriver()- err := clickhouseDriver.SaveOperationLog(operationLog)- if err != nil {- common.ResponseWithCodeData(c, common.CodeServerError, false, err.Error())- return- }+ common.Audit(operationLog)}()var req service.EmailLoginRequestif err := c.ShouldBindJSON(&req); err != nil {common.ResponseWithCodeData(c, common.CodeBadRequest, false, err.Error())- operationLog.ErrorCode = uint16(common.CodeBadRequest)- operationLog.Message = err.Error()- operationLog.DurationMS = time.Since(startAt).Milliseconds()+ operationLog.Code = int(common.CodeBadRequest)+ operationLog.Error = errreturn}operationLog.ResourceName = req.Email@@ -239,44 +219,40 @@ func (h *UserHandler) LoginByEmail(c *gin.Context) {if !local.IsAdminAvailable() {adminStatus := local.GetAdminStatus()common.ResponseWithCodeData(c, common.CodeAuthenticationError, "No", adminStatus.Reason)- operationLog.ErrorCode = uint16(common.CodeAuthenticationError)- operationLog.Message = adminStatus.Reason- operationLog.DurationMS = time.Since(startAt).Milliseconds()+ operationLog.Code = int(common.CodeAuthenticationError)+ operationLog.Error = errors.New(adminStatus.Reason)return}user, code, err := h.userService.LoginByEmail(ctx, &req)if err != nil {common.ResponseWithCodeData(c, code, false, err.Error())- operationLog.ErrorCode = uint16(code)- operationLog.Message = err.Error()- operationLog.DurationMS = time.Since(startAt).Milliseconds()+ operationLog.Code = int(code)+ operationLog.Error = errreturn}operationLog.UserID = user.IDif user.IsSuperuser != nil && *user.IsSuperuser {common.ResponseWithCodeData(c, common.CodeForbidden, false, "super admin should not login to this page.")- operationLog.ErrorCode = uint16(common.CodeForbidden)- operationLog.Message = "super admin should not login to this page."+ operationLog.Code = int(common.CodeForbidden)+ operationLog.Error = fmt.Errorf("super admin should not login to this page")return}secretKey, err := server.GetSecretKey(ctx, kvrocks.Get())if err != nil {- errorMessage := fmt.Sprintf("Failed to get secret key: %s", err.Error())- common.ResponseWithCodeData(c, common.CodeServerError, false, errorMessage)- operationLog.ErrorCode = uint16(common.CodeServerError)- operationLog.Message = errorMessage- operationLog.DurationMS = time.Since(startAt).Milliseconds()+ errorMessage := fmt.Errorf("failed to get secret key: %s", err.Error())+ common.ResponseWithCodeData(c, common.CodeServerError, false, errorMessage.Error())+ operationLog.Code = int(common.CodeServerError)+ operationLog.Error = errorMessagereturn}authToken, err := issueUserAccessToken(user, secretKey)if err != nil {common.ResponseWithCodeData(c, common.CodeServerError, false, "Failed to generate auth token")- operationLog.ErrorCode = uint16(common.CodeServerError)- operationLog.Message = "Failed to generate auth token"- operationLog.DurationMS = time.Since(startAt).Milliseconds()+ operationLog.Code = int(common.CodeServerError)+ operationLog.Error = errreturn}setOAuthAuthCookie(c, authToken)@@ -334,12 +310,7 @@ func (h *UserHandler) Logout(c *gin.Context) {}defer func() {operationLog.DurationMS = time.Since(startAt).Milliseconds()- clickhouseDriver := clickhouse.GetDriver()- err := clickhouseDriver.SaveOperationLog(operationLog)- if err != nil {- common.ResponseWithCodeData(c, common.CodeServerError, false, err.Error())- return- }+ common.Audit(operationLog)}()http.SetCookie(c.Writer, &http.Cookie{@@ -355,11 +326,10 @@ func (h *UserHandler) Logout(c *gin.Context) {// Same as [email protected]token := c.GetHeader("Authorization")if token == "" {- common.ResponseWithHttpCodeData(c, http.StatusUnauthorized, common.CodeUnauthorized, nil, "Missing Authorization header")+ common.ResponseWithHttpCodeData(c, http.StatusUnauthorized, common.CodeUnauthorized, nil, "missing authorization header")c.Abort()- operationLog.ErrorCode = uint16(common.CodeUnauthorized)- operationLog.Message = "Missing Authorization header"- operationLog.DurationMS = time.Since(startAt).Milliseconds()+ operationLog.Code = int(common.CodeUnauthorized)+ operationLog.Error = fmt.Errorf("missing authorization header")return}@@ -368,9 +338,8 @@ func (h *UserHandler) Logout(c *gin.Context) {if err != nil {common.ResponseWithHttpCodeData(c, http.StatusUnauthorized, code, nil, "Invalid access token")c.Abort()- operationLog.ErrorCode = uint16(code)- operationLog.Message = "Invalid access token"- operationLog.DurationMS = time.Since(startAt).Milliseconds()+ operationLog.Code = int(code)+ operationLog.Error = errreturn}user := identity.User@@ -380,9 +349,8 @@ func (h *UserHandler) Logout(c *gin.Context) {code, err = h.userService.Logout(ctx, user)if err != nil {common.ResponseWithCodeData(c, code, false, err.Error())- operationLog.ErrorCode = uint16(code)- operationLog.Message = err.Error()- operationLog.DurationMS = time.Since(startAt).Milliseconds()+ operationLog.Code = int(code)+ operationLog.Error = errreturn}@@ -594,8 +562,6 @@ type forgotCaptchaRequest struct {// pixels — so a scripted client can't regex it out (fixes the// SVG-text leak from the previous iteration, per PR #15290 review).// @Tags auth-// @Accept json-// @Produce json// @Param email query string false "user email (also accepted in JSON body)"// @Success 200 {object} map[string]interface{}// @Router /api/v1/auth/password/forgot/captcha [post]@@ -631,7 +597,6 @@ type forgotSendOTPRequest struct {// @Description Validates the captcha (looked up by captcha_id), then// mints a one-time code, stores a salted hash in Redis (5 min TTL,// attempt cap, resend cooldown), and emails the OTP to the user.-// @Tags auth// @Param request body forgotSendOTPRequest true "email + captcha_id + captcha"// @Success 200 {object} map[string]interface{}// @Router /api/v1/auth/password/forgot/otp [post]@@ -685,8 +650,6 @@ func (h *UserHandler) ForgotVerifyOTP(c *gin.Context) {// set in Redis). Updates the password hash and rotates the access// token so the response can auto-login the user.// @Tags auth-// @Accept json-// @Produce json// @Param request body service.ForgotResetPasswordRequest true "email + new password"// @Success 200 {object} map[string]interface{}// @Router /api/v1/auth/password/reset [post]diff --git a/internal/server/config.go b/internal/server/config.goindex a610d96b09..ee3b790097 100644--- a/internal/server/config.go+++ b/internal/server/config.go@@ -161,6 +161,11 @@ func Init(configPath string) error {return fmt.Errorf("parse OAuth config error: %w", err)}+ err = globalConfig.ParseAuditConfig(v)+ if err != nil {+ return fmt.Errorf("parse audit log config error: %w", err)+ }+return nil}diff --git a/internal/server/config/audit_config.go b/internal/server/config/audit_config.gonew file mode 100644index 0000000000..6621cc12d3--- /dev/null+++ b/internal/server/config/audit_config.go@@ -0,0 +1,26 @@+//+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.+//+// Licensed under the Apache License, Version 2.0 (the "License");+// you may not use this file except in compliance with the License.+// You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing, software+// distributed under the License is distributed on an "AS IS" BASIS,+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+// See the License for the specific language governing permissions and+// limitations under the License.+//++package config++type AuditConfig struct {+ Type string `mapstructure:"type"` // file, database; default "file"+ Format string `mapstructure:"format"` // json, text (reserved for future use); default "json"+ Path string `mapstructure:"path"` // per-binary file override; empty = use cmd/* hardcoded default+ MaxSize int `mapstructure:"max_size"` // MB before rotation; default 100+ MaxBackups int `mapstructure:"max_backups"` // retained rotated files; default 10+ MaxAge int `mapstructure:"max_age"` // days; default 30+}diff --git a/internal/server/config/base.go b/internal/server/config/base.goindex 1ffacd2164..5ff588ac7b 100644--- a/internal/server/config/base.go+++ b/internal/server/config/base.go@@ -44,4 +44,5 @@ type Config struct {defaultModels DefaultModelsConfigbilling BillingConfigoAuth OAuthConfig+ audit AuditConfig}diff --git a/internal/server/config/ingestor_config.go b/internal/server/config/ingestor_config.goindex 255134f0b4..6f9e3a58a3 100644--- a/internal/server/config/ingestor_config.go+++ b/internal/server/config/ingestor_config.go@@ -28,6 +28,15 @@ import ("github.com/spf13/viper")+type IngestLogConfig struct {+ Type string `mapstructure:"type"` // file, database; default "file"+ Format string `mapstructure:"format"` // json, text (reserved for future use); default "json"+ Path string `mapstructure:"path"` // per-binary file override; empty = use cmd/* hardcoded default+ MaxSize int `mapstructure:"max_size"` // MB before rotation; default 1000+ MaxBackups int `mapstructure:"max_backups"` // retained rotated files; default 20+ MaxAge int `mapstructure:"max_age"` // days; default 60+}+type IngestorConfig struct {// ConcurrentWorkers bounds how many ingestion tasks the ingestor runs in// parallel — it is the NATS consumer worker count. Valid range [1, 256],@@ -77,6 +86,8 @@ type IngestorConfig struct {deepDocInferenceCPUCores int// deepDocInferenceCPUCoresSet is true when inference_cpu_cores was present.deepDocInferenceCPUCoresSet bool++ logConfig IngestLogConfig}// Ingestor concurrency bounds, shared by the CLI/env/config resolver so the@@ -150,6 +161,13 @@ func (c *Config) ParseIngestorConfig(v *viper.Viper) error {c.ingestor.deepDocInferenceCPUCoresSet = true}+ if sub.IsSet("log") {+ err := c.ingestor.logConfig.ParseIngestLogConfig(sub.Sub("log"))+ if err != nil {+ return fmt.Errorf("invalid ingestor.log: %w", err)+ }+ }+return nil}@@ -287,3 +305,7 @@ func (c *Config) GetIngestorPageConcurrency(argValue *int) (int, error) {func (c *Config) GetIngestorConfig() *IngestorConfig {return &c.ingestor}++func (c *Config) GetIngestLogConfig() *IngestLogConfig {+ return &c.ingestor.logConfig+}diff --git a/internal/server/config/stub.go b/internal/server/config/stub.goindex e03c31a5f1..09f76af653 100644--- a/internal/server/config/stub.go+++ b/internal/server/config/stub.go@@ -18,7 +18,11 @@package config-import "github.com/spf13/viper"+import (+ "ragflow/internal/common"++ "github.com/spf13/viper"+)// ModelConfig is one model chosen as a deployment default.type ModelConfig struct {@@ -92,3 +96,19 @@ type OAuthChannel struct{}// ParseOAuthConfig ignores OAuth configuration: the OSS build has no OAuth// login.func (c *Config) ParseOAuthConfig(v *viper.Viper) error { return nil }++func (c *Config) ParseAuditConfig(v *viper.Viper) error { return nil }++func (c *Config) GetAuditConfig() AuditConfig {+ return c.audit+}++func (c *AuditConfig) GetFileOutput() common.FileOutput {+ return common.FileOutput{}+}++func (c *IngestLogConfig) ParseIngestLogConfig(v *viper.Viper) error { return nil }++func (c *IngestLogConfig) GetFileOutput() common.FileOutput {+ return common.FileOutput{}+}diff --git a/internal/server/ingestor/server.go b/internal/server/ingestor/server.goindex cb974cea37..3bd9d5f178 100644--- a/internal/server/ingestor/server.go+++ b/internal/server/ingestor/server.go@@ -63,9 +63,17 @@ func logTokenizerCounters() {// (OS shutdown signal) or the ingestor receives a shutdown command from the// admin, then gracefully stops the ingestor.func Run(ctx context.Context, cancel context.CancelFunc, serverName string, args *bootstrap.ServerArgs) error {+ globalConfig := server.GetConfig()+ ingestLogConfig := globalConfig.GetIngestLogConfig()+ err := common.InitIngestLog(ingestLogConfig.Type, ingestLogConfig.GetFileOutput(), serverName)+ if err != nil {+ common.Fatal("Failed to initialize ingest logger", zap.Error(err))+ }+// Initialize tokenizer (rag_analyzer)// tokenizer.Init handles DictPath fallback: env var → /usr/share/infinity/resource- if err := tokenizer.Init(&tokenizer.PoolConfig{}); err != nil {+ err = tokenizer.Init(&tokenizer.PoolConfig{})+ if err != nil {common.Fatal("Failed to initialize tokenizer", zap.Error(err))}defer tokenizer.Close()@@ -73,7 +81,8 @@ func Run(ctx context.Context, cancel context.CancelFunc, serverName string, args// Fail fast if the cl100k_base BPE table is missing. NumTokensFromString /// TrimContentToTokenLimit now panic rather than degrading silently, so this// trades a mid-request panic for a clear startup failure.- if err := tokenizer.InitCL100KEncoder(); err != nil {+ err = tokenizer.InitCL100KEncoder()+ if err != nil {common.Fatal("Failed to initialize cl100k_base tokenizer", zap.Error(err))}logTokenizerCounters()@@ -86,7 +95,6 @@ func Run(ctx context.Context, cancel context.CancelFunc, serverName string, args// provisioning error is logged by the Ingestor and the pipeline still// writes available_int=0 compiled chunks; they just won't be merged until// the consumer is available.- globalConfig := server.GetConfig()ingestorCfg := globalConfig.GetIngestorConfig()// Resolve the ingestor worker count (K) from CLI > env > config > default(1)// and fail fast on an out-of-range value.