-
Notifications
You must be signed in to change notification settings - Fork 1
/
interceptor.go
236 lines (213 loc) · 7.46 KB
/
interceptor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package auth
import (
"context"
"fmt"
"github.com/ScoreTrak/ScoreTrak/pkg/policy/policyclient"
"github.com/ScoreTrak/ScoreTrak/pkg/user"
authv1 "go.buf.build/library/go-grpc/scoretrak/scoretrakapis/scoretrak/auth/v1"
checkv1 "go.buf.build/library/go-grpc/scoretrak/scoretrakapis/scoretrak/check/v1"
hostv1 "go.buf.build/library/go-grpc/scoretrak/scoretrakapis/scoretrak/host/v1"
policyv1 "go.buf.build/library/go-grpc/scoretrak/scoretrakapis/scoretrak/policy/v1"
propertyv1 "go.buf.build/library/go-grpc/scoretrak/scoretrakapis/scoretrak/property/v1"
reportv1 "go.buf.build/library/go-grpc/scoretrak/scoretrakapis/scoretrak/report/v1"
servicev1 "go.buf.build/library/go-grpc/scoretrak/scoretrakapis/scoretrak/service/v1"
userv1 "go.buf.build/library/go-grpc/scoretrak/scoretrakapis/scoretrak/user/v1"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)
type Interceptor struct {
jwtManager *Manager
accessibleRoles map[string][]authorizationMap
}
type isAllowedFunc func() bool
func AlwaysAllowFunc() bool {
return true
}
type authorizationMap struct {
role string
isAllowed isAllowedFunc
}
// NewAuthInterceptor returns an instance of Interceptor. It takes in Manager struct, and policyClient as input. Policy Client allows to dynamically change authorization policies.
func NewAuthInterceptor(jwtManager *Manager, policyClient *policyclient.Client) *Interceptor {
authMap := map[string][]authorizationMap{}
authServicePath := fmt.Sprintf("/%s/Login", authv1.AuthService_ServiceDesc.ServiceName)
authMap[authServicePath] = []authorizationMap{{
role: user.Anonymous,
isAllowed: AlwaysAllowFunc,
}}
const grpcReflection = "/grpc.reflection.v1alpha.ServerReflection/ServerReflectionInfo"
authMap[grpcReflection] = []authorizationMap{{
role: user.Anonymous,
isAllowed: AlwaysAllowFunc,
}}
propertyServicePath := fmt.Sprintf("/%s/", propertyv1.PropertyService_ServiceDesc.ServiceName)
authMap[propertyServicePath+"GetByServiceIDKey"] = []authorizationMap{{
role: user.Blue,
isAllowed: AlwaysAllowFunc,
}, {
role: user.Red,
isAllowed: AlwaysAllowFunc,
}}
authMap[propertyServicePath+"GetAllByServiceID"] = []authorizationMap{{
role: user.Blue,
isAllowed: AlwaysAllowFunc,
}, {
role: user.Red,
isAllowed: AlwaysAllowFunc,
}}
authMap[propertyServicePath+"Update"] = []authorizationMap{{
role: user.Blue,
isAllowed: AlwaysAllowFunc,
}, {
role: user.Red,
isAllowed: AlwaysAllowFunc,
}}
serviceServicePath := fmt.Sprintf("/%s/", servicev1.ServiceService_ServiceDesc.ServiceName)
authMap[serviceServicePath+"GetAll"] = []authorizationMap{{
role: user.Red,
isAllowed: policyClient.GetAllowRedTeamLaunchingServiceTestsManually,
}}
authMap[serviceServicePath+"GetByID"] = []authorizationMap{{
role: user.Blue,
isAllowed: AlwaysAllowFunc,
}, {
role: user.Red,
isAllowed: AlwaysAllowFunc,
}}
authMap[serviceServicePath+"TestService"] = []authorizationMap{{
role: user.Red,
isAllowed: policyClient.GetAllowRedTeamLaunchingServiceTestsManually,
}}
hostServicePath := fmt.Sprintf("/%s/", hostv1.HostService_ServiceDesc.ServiceName)
authMap[hostServicePath+"Update"] = []authorizationMap{{
role: user.Blue,
isAllowed: AlwaysAllowFunc,
}, {
role: user.Red,
isAllowed: AlwaysAllowFunc,
}}
authMap[hostServicePath+"GetByID"] = []authorizationMap{{
role: user.Blue,
isAllowed: AlwaysAllowFunc,
}, {
role: user.Red,
isAllowed: AlwaysAllowFunc,
}}
checkServicePath := fmt.Sprintf("/%s/", checkv1.CheckService_ServiceDesc.ServiceName)
authMap[checkServicePath+"GetByRoundServiceID"] = []authorizationMap{{
role: user.Blue,
isAllowed: AlwaysAllowFunc,
}, {
role: user.Red,
isAllowed: AlwaysAllowFunc,
}}
authMap[checkServicePath+"GetAllByServiceID"] = []authorizationMap{{
role: user.Blue,
isAllowed: AlwaysAllowFunc,
}, {
role: user.Red,
isAllowed: AlwaysAllowFunc,
}}
reportServicePath := fmt.Sprintf("/%s/", reportv1.ReportService_ServiceDesc.ServiceName)
authMap[reportServicePath+"Get"] = []authorizationMap{{
role: user.Blue,
isAllowed: AlwaysAllowFunc,
}, {
role: user.Red,
isAllowed: AlwaysAllowFunc,
}, {
role: user.Anonymous,
isAllowed: policyClient.GetAllowUnauthenticatedUsers,
}}
policyServicePath := fmt.Sprintf("/%s/", policyv1.PolicyService_ServiceDesc.ServiceName)
authMap[policyServicePath+"Get"] = []authorizationMap{{
role: user.Blue,
isAllowed: AlwaysAllowFunc,
}, {
role: user.Red,
isAllowed: AlwaysAllowFunc,
}, {
role: user.Anonymous,
isAllowed: policyClient.GetAllowUnauthenticatedUsers,
}}
userServicePath := fmt.Sprintf("/%s/", userv1.UserService_ServiceDesc.ServiceName)
authMap[userServicePath+"Get"] = []authorizationMap{{
role: user.Blue,
isAllowed: policyClient.GetAllowChangingUsernamesAndPasswords,
}, {
role: user.Red,
isAllowed: policyClient.GetAllowChangingUsernamesAndPasswords,
}}
return &Interceptor{jwtManager, authMap}
}
// Custom Unary( interceptor that adds claim extraction and authorization
func (interceptor *Interceptor) Unary() grpc.UnaryServerInterceptor {
return func(
ctx context.Context,
req interface{},
info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler,
) (interface{}, error) {
claims, err := interceptor.authorize(ctx, info.FullMethod)
if err != nil {
return nil, err
}
if claims != nil {
ctx = context.WithValue(ctx, KeyClaim, claims)
}
return handler(ctx, req)
}
}
// Custom Stream that allows embedding of user claims for stream grpc (Similar to what describe in: https://stackoverflow.com/questions/60982406/how-to-safely-add-values-to-grpc-serverstream-in-interceptor)
type StreamClaimInjector struct {
grpc.ServerStream
Claims *UserClaims
}
func (s StreamClaimInjector) Context() context.Context {
if s.Claims != nil {
return context.WithValue(s.ServerStream.Context(), KeyClaim, s.Claims)
}
return s.ServerStream.Context()
}
// Custom Stream interceptor that adds claim extraction and authorization
func (interceptor *Interceptor) Stream() grpc.StreamServerInterceptor {
return func(
srv interface{},
stream grpc.ServerStream,
info *grpc.StreamServerInfo,
handler grpc.StreamHandler,
) error {
claims, err := interceptor.authorize(stream.Context(), info.FullMethod)
if err != nil {
return err
}
return handler(srv, StreamClaimInjector{stream, claims})
}
}
// authorize takes in context, extracts roles from the context if there are any, and ensures that a given roles has rights to access a given method. If a given role has no access, it returns permission denied error.
func (interceptor *Interceptor) authorize(ctx context.Context, method string) (claims *UserClaims, err error) {
r := user.Anonymous
md, ok := metadata.FromIncomingContext(ctx)
if ok {
values := md["authorization"]
if len(values) != 0 {
accessToken := values[0]
claims, err = interceptor.jwtManager.Verify(accessToken)
if err != nil {
return nil, status.Errorf(codes.Unauthenticated, "access token is invalid: %v", err)
}
r = claims.Role
}
}
if r == user.Black {
return
}
for i := range interceptor.accessibleRoles[method] {
if (r == interceptor.accessibleRoles[method][i].role || user.Anonymous == interceptor.accessibleRoles[method][i].role) && interceptor.accessibleRoles[method][i].isAllowed() {
return
}
}
return nil, status.Error(codes.PermissionDenied, "no permission to access this RPC")
}