|
@ -50,21 +50,27 @@ const tag = "s3.access" |
|
|
var ( |
|
|
var ( |
|
|
Logger *fluent.Fluent |
|
|
Logger *fluent.Fluent |
|
|
hostname = os.Getenv("HOSTNAME") |
|
|
hostname = os.Getenv("HOSTNAME") |
|
|
|
|
|
environment = os.Getenv("ENVIRONMENT") |
|
|
|
|
|
fluentConfig *fluent.Config |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
func InitAuditLog(config string) { |
|
|
func InitAuditLog(config string) { |
|
|
configContent, readErr := os.ReadFile(config) |
|
|
configContent, readErr := os.ReadFile(config) |
|
|
if readErr != nil { |
|
|
if readErr != nil { |
|
|
glog.Fatalf("fail to read fluent config %s : %v", config, readErr) |
|
|
|
|
|
|
|
|
glog.Errorf("fail to read fluent config %s : %v", config, readErr) |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
if err := json.Unmarshal(configContent, fluentConfig); err != nil { |
|
|
|
|
|
glog.Errorf("fail to parse fluent config %s : %v", config, err) |
|
|
|
|
|
return |
|
|
} |
|
|
} |
|
|
var fluentConfig fluent.Config |
|
|
|
|
|
if err := json.Unmarshal(configContent, &fluentConfig); err != nil { |
|
|
|
|
|
glog.Fatalf("fail to parse fluent config %s : %v", config, err) |
|
|
|
|
|
|
|
|
if len(fluentConfig.TagPrefix) == 0 && len(environment) > 0 { |
|
|
|
|
|
fluentConfig.TagPrefix = environment |
|
|
} |
|
|
} |
|
|
var err error |
|
|
var err error |
|
|
Logger, err = fluent.New(fluentConfig) |
|
|
|
|
|
|
|
|
Logger, err = fluent.New(*fluentConfig) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
glog.Fatalf("fail to load fluent config: %v", err) |
|
|
|
|
|
|
|
|
glog.Errorf("fail to load fluent config: %v", err) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -131,16 +137,16 @@ func GetAccessLog(r *http.Request, HTTPStatusCode int, s3errCode ErrorCode) *Acc |
|
|
if len(remoteIP) == 0 { |
|
|
if len(remoteIP) == 0 { |
|
|
remoteIP = r.RemoteAddr |
|
|
remoteIP = r.RemoteAddr |
|
|
} |
|
|
} |
|
|
hostHeader := r.Header.Get("Host") |
|
|
|
|
|
|
|
|
hostHeader := r.Header.Get("X-Forwarded-Host") |
|
|
if len(hostHeader) == 0 { |
|
|
if len(hostHeader) == 0 { |
|
|
hostHeader = r.URL.Hostname() |
|
|
|
|
|
|
|
|
hostHeader = r.Host |
|
|
} |
|
|
} |
|
|
return &AccessLog{ |
|
|
return &AccessLog{ |
|
|
HostHeader: hostHeader, |
|
|
HostHeader: hostHeader, |
|
|
RequestID: r.Header.Get("X-Request-ID"), |
|
|
RequestID: r.Header.Get("X-Request-ID"), |
|
|
RemoteIP: remoteIP, |
|
|
RemoteIP: remoteIP, |
|
|
Requester: r.Header.Get(xhttp.AmzIdentityId), |
|
|
Requester: r.Header.Get(xhttp.AmzIdentityId), |
|
|
UserAgent: r.Header.Get("UserAgent"), |
|
|
|
|
|
|
|
|
UserAgent: r.Header.Get("user-agent"), |
|
|
HostId: hostname, |
|
|
HostId: hostname, |
|
|
Bucket: bucket, |
|
|
Bucket: bucket, |
|
|
HTTPStatus: HTTPStatusCode, |
|
|
HTTPStatus: HTTPStatusCode, |
|
@ -155,16 +161,18 @@ func PostLog(r *http.Request, HTTPStatusCode int, errorCode ErrorCode) { |
|
|
if Logger == nil { |
|
|
if Logger == nil { |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
if err := Logger.Post(tag, *GetAccessLog(r, HTTPStatusCode, errorCode)); err != nil { |
|
|
|
|
|
|
|
|
go func(log *AccessLog) { |
|
|
|
|
|
if err := Logger.Post(tag, *log); err != nil { |
|
|
glog.Warning("Error while posting log: ", err) |
|
|
glog.Warning("Error while posting log: ", err) |
|
|
} |
|
|
} |
|
|
|
|
|
}(GetAccessLog(r, HTTPStatusCode, errorCode)) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func PostAccessLog(log *AccessLog) { |
|
|
|
|
|
if Logger == nil || log == nil { |
|
|
|
|
|
|
|
|
func PostAccessLog(log AccessLog) { |
|
|
|
|
|
if Logger == nil || len(log.Key) == 0 { |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
if err := Logger.Post(tag, *log); err != nil { |
|
|
|
|
|
|
|
|
if err := Logger.Post(tag, log); err != nil { |
|
|
glog.Warning("Error while posting log: ", err) |
|
|
glog.Warning("Error while posting log: ", err) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |