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.

120 lines
3.3 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
  1. // Copyright 2018 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 models
  5. import (
  6. "code.gitea.io/gitea/modules/log"
  7. "code.gitea.io/gitea/modules/util"
  8. "github.com/tstranex/u2f"
  9. )
  10. // U2FRegistration represents the registration data and counter of a security key
  11. type U2FRegistration struct {
  12. ID int64 `xorm:"pk autoincr"`
  13. Name string
  14. UserID int64 `xorm:"INDEX"`
  15. Raw []byte
  16. Counter uint32 `xorm:"BIGINT"`
  17. CreatedUnix util.TimeStamp `xorm:"INDEX created"`
  18. UpdatedUnix util.TimeStamp `xorm:"INDEX updated"`
  19. }
  20. // TableName returns a better table name for U2FRegistration
  21. func (reg U2FRegistration) TableName() string {
  22. return "u2f_registration"
  23. }
  24. // Parse will convert the db entry U2FRegistration to an u2f.Registration struct
  25. func (reg *U2FRegistration) Parse() (*u2f.Registration, error) {
  26. r := new(u2f.Registration)
  27. return r, r.UnmarshalBinary(reg.Raw)
  28. }
  29. func (reg *U2FRegistration) updateCounter(e Engine) error {
  30. _, err := e.ID(reg.ID).Cols("counter").Update(reg)
  31. return err
  32. }
  33. // UpdateCounter will update the database value of counter
  34. func (reg *U2FRegistration) UpdateCounter() error {
  35. return reg.updateCounter(x)
  36. }
  37. // U2FRegistrationList is a list of *U2FRegistration
  38. type U2FRegistrationList []*U2FRegistration
  39. // ToRegistrations will convert all U2FRegistrations to u2f.Registrations
  40. func (list U2FRegistrationList) ToRegistrations() []u2f.Registration {
  41. regs := make([]u2f.Registration, 0, len(list))
  42. for _, reg := range list {
  43. r, err := reg.Parse()
  44. if err != nil {
  45. log.Fatal("parsing u2f registration: %v", err)
  46. continue
  47. }
  48. regs = append(regs, *r)
  49. }
  50. return regs
  51. }
  52. func getU2FRegistrationsByUID(e Engine, uid int64) (U2FRegistrationList, error) {
  53. regs := make(U2FRegistrationList, 0)
  54. return regs, e.Where("user_id = ?", uid).Find(&regs)
  55. }
  56. // GetU2FRegistrationByID returns U2F registration by id
  57. func GetU2FRegistrationByID(id int64) (*U2FRegistration, error) {
  58. return getU2FRegistrationByID(x, id)
  59. }
  60. func getU2FRegistrationByID(e Engine, id int64) (*U2FRegistration, error) {
  61. reg := new(U2FRegistration)
  62. if found, err := e.ID(id).Get(reg); err != nil {
  63. return nil, err
  64. } else if !found {
  65. return nil, ErrU2FRegistrationNotExist{ID: id}
  66. }
  67. return reg, nil
  68. }
  69. // GetU2FRegistrationsByUID returns all U2F registrations of the given user
  70. func GetU2FRegistrationsByUID(uid int64) (U2FRegistrationList, error) {
  71. return getU2FRegistrationsByUID(x, uid)
  72. }
  73. func createRegistration(e Engine, user *User, name string, reg *u2f.Registration) (*U2FRegistration, error) {
  74. raw, err := reg.MarshalBinary()
  75. if err != nil {
  76. return nil, err
  77. }
  78. r := &U2FRegistration{
  79. UserID: user.ID,
  80. Name: name,
  81. Counter: 0,
  82. Raw: raw,
  83. }
  84. _, err = e.InsertOne(r)
  85. if err != nil {
  86. return nil, err
  87. }
  88. return r, nil
  89. }
  90. // CreateRegistration will create a new U2FRegistration from the given Registration
  91. func CreateRegistration(user *User, name string, reg *u2f.Registration) (*U2FRegistration, error) {
  92. return createRegistration(x, user, name, reg)
  93. }
  94. // DeleteRegistration will delete U2FRegistration
  95. func DeleteRegistration(reg *U2FRegistration) error {
  96. return deleteRegistration(x, reg)
  97. }
  98. func deleteRegistration(e Engine, reg *U2FRegistration) error {
  99. _, err := e.Delete(reg)
  100. return err
  101. }