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.

interceptor.go 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package runner
  4. import (
  5. "context"
  6. "crypto/subtle"
  7. "errors"
  8. "strings"
  9. actions_model "code.gitea.io/gitea/models/actions"
  10. auth_model "code.gitea.io/gitea/models/auth"
  11. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/timeutil"
  13. "code.gitea.io/gitea/modules/util"
  14. "github.com/bufbuild/connect-go"
  15. "google.golang.org/grpc/codes"
  16. "google.golang.org/grpc/status"
  17. )
  18. const (
  19. uuidHeaderKey = "x-runner-uuid"
  20. tokenHeaderKey = "x-runner-token"
  21. )
  22. var withRunner = connect.WithInterceptors(connect.UnaryInterceptorFunc(func(unaryFunc connect.UnaryFunc) connect.UnaryFunc {
  23. return func(ctx context.Context, request connect.AnyRequest) (connect.AnyResponse, error) {
  24. methodName := getMethodName(request)
  25. if methodName == "Register" {
  26. return unaryFunc(ctx, request)
  27. }
  28. uuid := request.Header().Get(uuidHeaderKey)
  29. token := request.Header().Get(tokenHeaderKey)
  30. runner, err := actions_model.GetRunnerByUUID(ctx, uuid)
  31. if err != nil {
  32. if errors.Is(err, util.ErrNotExist) {
  33. return nil, status.Error(codes.Unauthenticated, "unregistered runner")
  34. }
  35. return nil, status.Error(codes.Internal, err.Error())
  36. }
  37. if subtle.ConstantTimeCompare([]byte(runner.TokenHash), []byte(auth_model.HashToken(token, runner.TokenSalt))) != 1 {
  38. return nil, status.Error(codes.Unauthenticated, "unregistered runner")
  39. }
  40. cols := []string{"last_online"}
  41. runner.LastOnline = timeutil.TimeStampNow()
  42. if methodName == "UpdateTask" || methodName == "UpdateLog" {
  43. runner.LastActive = timeutil.TimeStampNow()
  44. cols = append(cols, "last_active")
  45. }
  46. if err := actions_model.UpdateRunner(ctx, runner, cols...); err != nil {
  47. log.Error("can't update runner status: %v", err)
  48. }
  49. ctx = context.WithValue(ctx, runnerCtxKey{}, runner)
  50. return unaryFunc(ctx, request)
  51. }
  52. }))
  53. func getMethodName(req connect.AnyRequest) string {
  54. splits := strings.Split(req.Spec().Procedure, "/")
  55. if len(splits) > 0 {
  56. return splits[len(splits)-1]
  57. }
  58. return ""
  59. }
  60. type runnerCtxKey struct{}
  61. func GetRunner(ctx context.Context) *actions_model.ActionRunner {
  62. if v := ctx.Value(runnerCtxKey{}); v != nil {
  63. if r, ok := v.(*actions_model.ActionRunner); ok {
  64. return r
  65. }
  66. }
  67. return nil
  68. }