gitea源码

runner_token.go 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package actions
  4. import (
  5. "context"
  6. "fmt"
  7. "code.gitea.io/gitea/models/db"
  8. repo_model "code.gitea.io/gitea/models/repo"
  9. user_model "code.gitea.io/gitea/models/user"
  10. "code.gitea.io/gitea/modules/timeutil"
  11. "code.gitea.io/gitea/modules/util"
  12. )
  13. // ActionRunnerToken represents runner tokens
  14. //
  15. // It can be:
  16. // 1. global token, OwnerID is 0 and RepoID is 0
  17. // 2. org/user level token, OwnerID is org/user ID and RepoID is 0
  18. // 3. repo level token, OwnerID is 0 and RepoID is repo ID
  19. //
  20. // Please note that it's not acceptable to have both OwnerID and RepoID to be non-zero,
  21. // or it will be complicated to find tokens belonging to a specific owner.
  22. // For example, conditions like `OwnerID = 1` will also return token {OwnerID: 1, RepoID: 1},
  23. // but it's a repo level token, not an org/user level token.
  24. // To avoid this, make it clear with {OwnerID: 0, RepoID: 1} for repo level tokens.
  25. type ActionRunnerToken struct {
  26. ID int64
  27. Token string `xorm:"UNIQUE"`
  28. OwnerID int64 `xorm:"index"`
  29. Owner *user_model.User `xorm:"-"`
  30. RepoID int64 `xorm:"index"`
  31. Repo *repo_model.Repository `xorm:"-"`
  32. IsActive bool // true means it can be used
  33. Created timeutil.TimeStamp `xorm:"created"`
  34. Updated timeutil.TimeStamp `xorm:"updated"`
  35. Deleted timeutil.TimeStamp `xorm:"deleted"`
  36. }
  37. func init() {
  38. db.RegisterModel(new(ActionRunnerToken))
  39. }
  40. // GetRunnerToken returns a action runner via token
  41. func GetRunnerToken(ctx context.Context, token string) (*ActionRunnerToken, error) {
  42. var runnerToken ActionRunnerToken
  43. has, err := db.GetEngine(ctx).Where("token=?", token).Get(&runnerToken)
  44. if err != nil {
  45. return nil, err
  46. } else if !has {
  47. return nil, fmt.Errorf(`runner token "%s...": %w`, util.TruncateRunes(token, 3), util.ErrNotExist)
  48. }
  49. return &runnerToken, nil
  50. }
  51. // UpdateRunnerToken updates runner token information.
  52. func UpdateRunnerToken(ctx context.Context, r *ActionRunnerToken, cols ...string) (err error) {
  53. e := db.GetEngine(ctx)
  54. if len(cols) == 0 {
  55. _, err = e.ID(r.ID).AllCols().Update(r)
  56. } else {
  57. _, err = e.ID(r.ID).Cols(cols...).Update(r)
  58. }
  59. return err
  60. }
  61. // NewRunnerTokenWithValue creates a new active runner token and invalidate all old tokens
  62. // ownerID will be ignored and treated as 0 if repoID is non-zero.
  63. func NewRunnerTokenWithValue(ctx context.Context, ownerID, repoID int64, token string) (*ActionRunnerToken, error) {
  64. if ownerID != 0 && repoID != 0 {
  65. // It's trying to create a runner token that belongs to a repository, but OwnerID has been set accidentally.
  66. // Remove OwnerID to avoid confusion; it's not worth returning an error here.
  67. ownerID = 0
  68. }
  69. runnerToken := &ActionRunnerToken{
  70. OwnerID: ownerID,
  71. RepoID: repoID,
  72. IsActive: true,
  73. Token: token,
  74. }
  75. return runnerToken, db.WithTx(ctx, func(ctx context.Context) error {
  76. if _, err := db.GetEngine(ctx).Where("owner_id =? AND repo_id = ?", ownerID, repoID).Cols("is_active").Update(&ActionRunnerToken{
  77. IsActive: false,
  78. }); err != nil {
  79. return err
  80. }
  81. _, err := db.GetEngine(ctx).Insert(runnerToken)
  82. return err
  83. })
  84. }
  85. func NewRunnerToken(ctx context.Context, ownerID, repoID int64) (*ActionRunnerToken, error) {
  86. token, err := util.CryptoRandomString(40)
  87. if err != nil {
  88. return nil, err
  89. }
  90. return NewRunnerTokenWithValue(ctx, ownerID, repoID, token)
  91. }
  92. // GetLatestRunnerToken returns the latest runner token
  93. func GetLatestRunnerToken(ctx context.Context, ownerID, repoID int64) (*ActionRunnerToken, error) {
  94. if ownerID != 0 && repoID != 0 {
  95. // It's trying to get a runner token that belongs to a repository, but OwnerID has been set accidentally.
  96. // Remove OwnerID to avoid confusion; it's not worth returning an error here.
  97. ownerID = 0
  98. }
  99. var runnerToken ActionRunnerToken
  100. has, err := db.GetEngine(ctx).Where("owner_id=? AND repo_id=?", ownerID, repoID).
  101. OrderBy("id DESC").Get(&runnerToken)
  102. if err != nil {
  103. return nil, err
  104. } else if !has {
  105. return nil, fmt.Errorf("runner token: %w", util.ErrNotExist)
  106. }
  107. return &runnerToken, nil
  108. }