diff --git a/cmd/serv.go b/cmd/serv.go index 56167f63a87..1c9f5dc44e9 100644 --- a/cmd/serv.go +++ b/cmd/serv.go @@ -81,6 +81,10 @@ func fail(userMessage, logMessage string, args ...interface{}) { } } + if len(logMessage) > 0 { + _ = private.SSHLog(true, fmt.Sprintf(logMessage+": ", args...)) + } + os.Exit(1) } diff --git a/custom/conf/app.example.ini b/custom/conf/app.example.ini index b1fe95e6e20..f37157c3b10 100644 --- a/custom/conf/app.example.ini +++ b/custom/conf/app.example.ini @@ -444,6 +444,11 @@ ROUTER = console ;ACCESS_LOG_TEMPLATE = {{.Ctx.RemoteAddr}} - {{.Identity}} {{.Start.Format "[02/Jan/2006:15:04:05 -0700]" }} "{{.Ctx.Req.Method}} {{.Ctx.Req.URL.RequestURI}} {{.Ctx.Req.Proto}}" {{.ResponseWriter.Status}} {{.ResponseWriter.Size}} "{{.Ctx.Req.Referer}}\" \"{{.Ctx.Req.UserAgent}}" ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; +;; SSH log (Creates log from ssh git request) +;; +;ENABLE_SSH_LOG = false +;; ;; Other Settings ;; ;; Print Stacktraces with logs. (Rarely helpful.) Either "Trace", "Debug", "Info", "Warn", "Error", "Critical", default is "None" diff --git a/docs/content/doc/advanced/config-cheat-sheet.en-us.md b/docs/content/doc/advanced/config-cheat-sheet.en-us.md index 9bb790f4c9c..ee701b65724 100644 --- a/docs/content/doc/advanced/config-cheat-sheet.en-us.md +++ b/docs/content/doc/advanced/config-cheat-sheet.en-us.md @@ -657,6 +657,7 @@ Default templates for project boards: - `ROUTER`: **console**: The mode or name of the log the router should log to. (If you set this to `,` it will log to default gitea logger.) NB: You must have `DISABLE_ROUTER_LOG` set to `false` for this option to take effect. Configure each mode in per mode log subsections `\[log.modename.router\]`. - `ENABLE_ACCESS_LOG`: **false**: Creates an access.log in NCSA common log format, or as per the following template +- `ENABLE_SSH_LOG`: **false**: save ssh log to log file - `ACCESS`: **file**: Logging mode for the access logger, use a comma to separate values. Configure each mode in per mode log subsections `\[log.modename.access\]`. By default the file mode will log to `$ROOT_PATH/access.log`. (If you set this to `,` it will log to the default gitea logger.) - `ACCESS_LOG_TEMPLATE`: **`{{.Ctx.RemoteAddr}} - {{.Identity}} {{.Start.Format "[02/Jan/2006:15:04:05 -0700]" }} "{{.Ctx.Req.Method}} {{.Ctx.Req.URL.RequestURI}} {{.Ctx.Req.Proto}}" {{.ResponseWriter.Status}} {{.ResponseWriter.Size}} "{{.Ctx.Req.Referer}}\" \"{{.Ctx.Req.UserAgent}}"`**: Sets the template used to create the access log. - The following variables are available: diff --git a/modules/private/hook.go b/modules/private/hook.go index 178500f7362..cb8fe25708a 100644 --- a/modules/private/hook.go +++ b/modules/private/hook.go @@ -5,6 +5,7 @@ package private import ( + "encoding/json" "fmt" "net/http" "net/url" @@ -57,6 +58,12 @@ type HookOptions struct { IsDeployKey bool } +// SSHLogOption ssh log options +type SSHLogOption struct { + IsError bool + Message string +} + // HookPostReceiveResult represents an individual result from PostReceive type HookPostReceiveResult struct { Results []HookPostReceiveBranchResult @@ -146,3 +153,27 @@ func SetDefaultBranch(ownerName, repoName, branch string) error { } return nil } + +// SSHLog sends ssh error log response +func SSHLog(isErr bool, msg string) error { + reqURL := setting.LocalURL + "api/internal/ssh/log" + req := newInternalRequest(reqURL, "POST") + req = req.Header("Content-Type", "application/json") + + jsonBytes, _ := json.Marshal(&SSHLogOption{ + IsError: isErr, + Message: msg, + }) + req.Body(jsonBytes) + + req.SetTimeout(60*time.Second, 60*time.Second) + resp, err := req.Response() + if err != nil { + return fmt.Errorf("unable to contact gitea: %v", err) + } + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { + return fmt.Errorf("Error returned from gitea: %v", decodeJSONError(resp).Err) + } + return nil +} diff --git a/modules/setting/log.go b/modules/setting/log.go index 44017b11383..0fb108c93dc 100644 --- a/modules/setting/log.go +++ b/modules/setting/log.go @@ -287,6 +287,7 @@ func newLogService() { options := newDefaultLogOptions() options.bufferLength = Cfg.Section("log").Key("BUFFER_LEN").MustInt64(10000) + EnableSSHLog = Cfg.Section("log").Key("ENABLE_SSH_LOG").MustBool(false) description := LogDescription{ Name: log.DEFAULT, diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 4244b55939b..ec8e64578bf 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -319,6 +319,7 @@ var ( DisableRouterLog bool RouterLogLevel log.Level EnableAccessLog bool + EnableSSHLog bool AccessLogTemplate string EnableXORMLog bool diff --git a/routers/private/internal.go b/routers/private/internal.go index c6cc61fc29e..9202e672188 100644 --- a/routers/private/internal.go +++ b/routers/private/internal.go @@ -55,6 +55,7 @@ func Routes() *web.Route { r.Post("/ssh/authorized_keys", AuthorizedPublicKeyByContent) r.Post("/ssh/{id}/update/{repoid}", UpdatePublicKeyInRepo) + r.Post("/ssh/log", bind(private.SSHLogOption{}), SSHLog) r.Post("/hook/pre-receive/{owner}/{repo}", bind(private.HookOptions{}), HookPreReceive) r.Post("/hook/post-receive/{owner}/{repo}", bind(private.HookOptions{}), HookPostReceive) r.Post("/hook/set-default-branch/{owner}/{repo}/{branch}", SetDefaultBranch) diff --git a/routers/private/ssh_log.go b/routers/private/ssh_log.go new file mode 100644 index 00000000000..2f1793a0e0f --- /dev/null +++ b/routers/private/ssh_log.go @@ -0,0 +1,34 @@ +// Copyright 2021 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package private + +import ( + "net/http" + + "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/private" + "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/web" +) + +// SSHLog hook to response ssh log +func SSHLog(ctx *context.PrivateContext) { + if !setting.EnableSSHLog { + ctx.Status(http.StatusOK) + return + } + + opts := web.GetForm(ctx).(*private.SSHLogOption) + + if opts.IsError { + log.Error("ssh: %v", opts.Message) + ctx.Status(http.StatusOK) + return + } + + log.Debug("ssh: %v", opts.Message) + ctx.Status(http.StatusOK) +}