github.com/cloudwego/kitex@v0.9.0/pkg/acl/acl.go (about) 1 /* 2 * Copyright 2021 CloudWeGo Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 // Package acl implements ACL functionality. 18 package acl 19 20 import ( 21 "context" 22 "errors" 23 24 "github.com/cloudwego/kitex/pkg/endpoint" 25 "github.com/cloudwego/kitex/pkg/kerrors" 26 ) 27 28 // RejectFunc judges if to reject a request by the given context and request. 29 // Returns a reason if rejected, otherwise returns nil. 30 type RejectFunc func(ctx context.Context, request interface{}) (reason error) 31 32 // NewACLMiddleware creates a new ACL middleware using the provided reject funcs. 33 func NewACLMiddleware(rules []RejectFunc) endpoint.Middleware { 34 if len(rules) == 0 { 35 return endpoint.DummyMiddleware 36 } 37 return func(next endpoint.Endpoint) endpoint.Endpoint { 38 return func(ctx context.Context, request, response interface{}) error { 39 for _, r := range rules { 40 if e := r(ctx, request); e != nil { 41 if !errors.Is(e, kerrors.ErrACL) { 42 e = kerrors.ErrACL.WithCause(e) 43 } 44 return e 45 } 46 } 47 return next(ctx, request, response) 48 } 49 } 50 }