You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

232 lines
5.1 KiB

Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
5 years ago
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
5 years ago
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package ssh
  5. import (
  6. "crypto/rand"
  7. "crypto/rsa"
  8. "crypto/x509"
  9. "encoding/pem"
  10. "fmt"
  11. "io"
  12. "os"
  13. "os/exec"
  14. "path/filepath"
  15. "strings"
  16. "sync"
  17. "syscall"
  18. "code.gitea.io/gitea/models"
  19. "code.gitea.io/gitea/modules/log"
  20. "code.gitea.io/gitea/modules/setting"
  21. "github.com/gliderlabs/ssh"
  22. "github.com/unknwon/com"
  23. gossh "golang.org/x/crypto/ssh"
  24. )
  25. type contextKey string
  26. const giteaKeyID = contextKey("gitea-key-id")
  27. func getExitStatusFromError(err error) int {
  28. if err == nil {
  29. return 0
  30. }
  31. exitErr, ok := err.(*exec.ExitError)
  32. if !ok {
  33. return 1
  34. }
  35. waitStatus, ok := exitErr.Sys().(syscall.WaitStatus)
  36. if !ok {
  37. // This is a fallback and should at least let us return something useful
  38. // when running on Windows, even if it isn't completely accurate.
  39. if exitErr.Success() {
  40. return 0
  41. }
  42. return 1
  43. }
  44. return waitStatus.ExitStatus()
  45. }
  46. func sessionHandler(session ssh.Session) {
  47. keyID := session.Context().Value(giteaKeyID).(int64)
  48. command := session.RawCommand()
  49. log.Trace("SSH: Payload: %v", command)
  50. args := []string{"serv", "key-" + com.ToStr(keyID), "--config=" + setting.CustomConf}
  51. log.Trace("SSH: Arguments: %v", args)
  52. cmd := exec.Command(setting.AppPath, args...)
  53. cmd.Env = append(
  54. os.Environ(),
  55. "SSH_ORIGINAL_COMMAND="+command,
  56. "SKIP_MINWINSVC=1",
  57. )
  58. stdout, err := cmd.StdoutPipe()
  59. if err != nil {
  60. log.Error("SSH: StdoutPipe: %v", err)
  61. return
  62. }
  63. stderr, err := cmd.StderrPipe()
  64. if err != nil {
  65. log.Error("SSH: StderrPipe: %v", err)
  66. return
  67. }
  68. stdin, err := cmd.StdinPipe()
  69. if err != nil {
  70. log.Error("SSH: StdinPipe: %v", err)
  71. return
  72. }
  73. wg := &sync.WaitGroup{}
  74. wg.Add(2)
  75. if err = cmd.Start(); err != nil {
  76. log.Error("SSH: Start: %v", err)
  77. return
  78. }
  79. go func() {
  80. defer stdin.Close()
  81. if _, err := io.Copy(stdin, session); err != nil {
  82. log.Error("Failed to write session to stdin. %s", err)
  83. }
  84. }()
  85. go func() {
  86. defer wg.Done()
  87. if _, err := io.Copy(session, stdout); err != nil {
  88. log.Error("Failed to write stdout to session. %s", err)
  89. }
  90. }()
  91. go func() {
  92. defer wg.Done()
  93. if _, err := io.Copy(session.Stderr(), stderr); err != nil {
  94. log.Error("Failed to write stderr to session. %s", err)
  95. }
  96. }()
  97. // Ensure all the output has been written before we wait on the command
  98. // to exit.
  99. wg.Wait()
  100. // Wait for the command to exit and log any errors we get
  101. err = cmd.Wait()
  102. if err != nil {
  103. log.Error("SSH: Wait: %v", err)
  104. }
  105. if err := session.Exit(getExitStatusFromError(err)); err != nil {
  106. log.Error("Session failed to exit. %s", err)
  107. }
  108. }
  109. func publicKeyHandler(ctx ssh.Context, key ssh.PublicKey) bool {
  110. if ctx.User() != setting.SSH.BuiltinServerUser {
  111. return false
  112. }
  113. pkey, err := models.SearchPublicKeyByContent(strings.TrimSpace(string(gossh.MarshalAuthorizedKey(key))))
  114. if err != nil {
  115. log.Error("SearchPublicKeyByContent: %v", err)
  116. return false
  117. }
  118. ctx.SetValue(giteaKeyID, pkey.ID)
  119. return true
  120. }
  121. // Listen starts a SSH server listens on given port.
  122. func Listen(host string, port int, ciphers []string, keyExchanges []string, macs []string) {
  123. // TODO: Handle ciphers, keyExchanges, and macs
  124. srv := ssh.Server{
  125. Addr: fmt.Sprintf("%s:%d", host, port),
  126. PublicKeyHandler: publicKeyHandler,
  127. Handler: sessionHandler,
  128. // We need to explicitly disable the PtyCallback so text displays
  129. // properly.
  130. PtyCallback: func(ctx ssh.Context, pty ssh.Pty) bool {
  131. return false
  132. },
  133. }
  134. keyPath := filepath.Join(setting.AppDataPath, "ssh/gogs.rsa")
  135. if !com.IsExist(keyPath) {
  136. filePath := filepath.Dir(keyPath)
  137. if err := os.MkdirAll(filePath, os.ModePerm); err != nil {
  138. log.Error("Failed to create dir %s: %v", filePath, err)
  139. }
  140. err := GenKeyPair(keyPath)
  141. if err != nil {
  142. log.Fatal("Failed to generate private key: %v", err)
  143. }
  144. log.Trace("New private key is generated: %s", keyPath)
  145. }
  146. err := srv.SetOption(ssh.HostKeyFile(keyPath))
  147. if err != nil {
  148. log.Error("Failed to set Host Key. %s", err)
  149. }
  150. go listen(&srv)
  151. }
  152. // GenKeyPair make a pair of public and private keys for SSH access.
  153. // Public key is encoded in the format for inclusion in an OpenSSH authorized_keys file.
  154. // Private Key generated is PEM encoded
  155. func GenKeyPair(keyPath string) error {
  156. privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
  157. if err != nil {
  158. return err
  159. }
  160. privateKeyPEM := &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)}
  161. f, err := os.OpenFile(keyPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
  162. if err != nil {
  163. return err
  164. }
  165. defer func() {
  166. if err = f.Close(); err != nil {
  167. log.Error("Close: %v", err)
  168. }
  169. }()
  170. if err := pem.Encode(f, privateKeyPEM); err != nil {
  171. return err
  172. }
  173. // generate public key
  174. pub, err := gossh.NewPublicKey(&privateKey.PublicKey)
  175. if err != nil {
  176. return err
  177. }
  178. public := gossh.MarshalAuthorizedKey(pub)
  179. p, err := os.OpenFile(keyPath+".pub", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
  180. if err != nil {
  181. return err
  182. }
  183. defer func() {
  184. if err = p.Close(); err != nil {
  185. log.Error("Close: %v", err)
  186. }
  187. }()
  188. _, err = p.Write(public)
  189. return err
  190. }