@@ -27,7 +27,12 @@ import (
2727 "github.com/go-kratos/kratos/v2/middleware"
2828)
2929
30- func CheckOrgRequirements (uc biz.CASBackendReader ) middleware.Middleware {
30+ const validationTimeOffset = 5 * time .Minute
31+
32+ // ValidateCASBackend checks that the current organization has a valid CAS Backend configured
33+ // If the last validation happened more than validationTimeOffset ago it will re-run the validation
34+ // This middleware does not block the request if the CAS Backend is not valid
35+ func ValidateCASBackend (uc biz.CASBackendReader ) middleware.Middleware {
3136 return func (handler middleware.Handler ) middleware.Handler {
3237 return func (ctx context.Context , req interface {}) (interface {}, error ) {
3338 org := entities .CurrentOrg (ctx )
@@ -52,11 +57,6 @@ func CheckOrgRequirements(uc biz.CASBackendReader) middleware.Middleware {
5257 }
5358 }
5459
55- // 2 - compare the status
56- if repo .ValidationStatus != biz .CASBackendValidationOK {
57- return nil , v1 .ErrorCasBackendErrorReasonInvalid ("your CAS backend can't be reached" )
58- }
59-
6060 return handler (ctx , req )
6161 }
6262 }
@@ -78,10 +78,9 @@ func validateCASBackend(ctx context.Context, uc biz.CASBackendReader, repo *biz.
7878 return repo , nil
7979}
8080
81- const validationTimeOffset = 5 * time .Minute
82-
83- // Since this check happens synchronously on every request it has a big performance impact
84- // that's why we run it only in refresh windows
81+ // shouldRevalidate returns true if the repo needs to be re-validated
82+ // Currently, we only re-validate if the last status was "failed"
83+ // or if the last validation was more than validationTimeOffset ago
8584func shouldRevalidate (repo * biz.CASBackend ) bool {
8685 // If the validation is currently failed we want to make sure we re-validate
8786 if repo .ValidationStatus == biz .CASBackendValidationFailed {
@@ -91,3 +90,31 @@ func shouldRevalidate(repo *biz.CASBackend) bool {
9190 // if it has been more than validationTimeOffset since the last validation
9291 return repo .ValidatedAt .Before (time .Now ().Add (- validationTimeOffset ))
9392}
93+
94+ // BlockIfCASBackendNotValid checks that the current organization has a valid CAS Backend configured
95+ // If the CAS Backend is not valid it will block the request
96+ func BlockIfCASBackendNotValid (uc biz.CASBackendReader ) middleware.Middleware {
97+ return func (handler middleware.Handler ) middleware.Handler {
98+ return func (ctx context.Context , req interface {}) (interface {}, error ) {
99+ org := entities .CurrentOrg (ctx )
100+ if org == nil {
101+ // Make sure that this middleware is ran after WithCurrentUser
102+ return nil , errors .New ("organization not found" )
103+ }
104+
105+ // 1 - Figure out main repository for this organization
106+ repo , err := uc .FindDefaultBackend (ctx , org .ID )
107+ if err != nil && ! biz .IsNotFound (err ) {
108+ return nil , fmt .Errorf ("checking for CAS backends in the org: %w" , err )
109+ } else if repo == nil {
110+ return nil , v1 .ErrorCasBackendErrorReasonRequired ("your organization does not have a CAS Backend configured yet" )
111+ }
112+
113+ // 2 - compare the status
114+ if repo .ValidationStatus != biz .CASBackendValidationOK {
115+ return nil , v1 .ErrorCasBackendErrorReasonInvalid ("your CAS backend can't be reached" )
116+ }
117+ return handler (ctx , req )
118+ }
119+ }
120+ }
0 commit comments