This repository has been archived by the owner on Jan 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
version.go
248 lines (211 loc) · 7.2 KB
/
version.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
237
238
239
240
241
242
243
244
245
246
247
248
package version
import (
"context"
"strconv"
"strings"
"github.com/kinecosystem/agora-common/headers"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type KinVersion uint16
const (
KinVersionUnknown KinVersion = iota
KinVersionReserved
KinVersion2
KinVersion3
KinVersion4
)
const (
KinVersionHeader = "kin-version"
DesiredKinVersionHeader = "desired-kin-version"
minVersion = KinVersion2
maxVersion = KinVersion4
defaultVersion = KinVersion3
)
var (
preconditionFailedCounter = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "agora",
Name: "precondition_failed",
Help: "Number of precondition failed responses from agora",
})
)
func init() {
if err := registerMetrics(); err != nil {
logrus.WithError(err).Error("failed to register precondition failed counter")
}
}
// GetCtxKinVersion determines which version of Kin to use based on the headers in the provided context.
func GetCtxKinVersion(ctx context.Context) (version KinVersion, err error) {
val, err := headers.GetASCIIHeaderByName(ctx, KinVersionHeader)
if err != nil {
return 0, errors.Wrap(err, "failed to get kin version header")
}
if len(val) == 0 {
return defaultVersion, nil
}
i, err := strconv.Atoi(val)
if err != nil {
return 0, errors.Wrap(err, "could not parse integer version from string")
}
if i < int(minVersion) || i > int(maxVersion) {
return 0, errors.Wrap(err, "invalid kin version")
}
return KinVersion(i), nil
}
// GetCtxDesiredVersion determines which version of Kin the requestor whiches to have enforced.
func GetCtxDesiredVersion(ctx context.Context) (version KinVersion, err error) {
val, err := headers.GetASCIIHeaderByName(ctx, DesiredKinVersionHeader)
if err != nil {
return 0, errors.Wrap(err, "failed to get desired kin version header")
}
if len(val) == 0 {
return 0, errors.New("no desired kin version set")
}
i, err := strconv.Atoi(val)
if err != nil {
return 0, errors.Wrap(err, "could not parse integer version from string")
}
if i < int(minVersion) || i > int(maxVersion) {
return 0, errors.Wrap(err, "invalid desired kin version")
}
return KinVersion(i), nil
}
func DisabledVersionUnaryServerInterceptor(defaultVersion KinVersion, disabledVersions []int) grpc.UnaryServerInterceptor {
log := logrus.StandardLogger().WithField("type", "version/interceptor")
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
if strings.Contains(info.FullMethod, "GetMinimumKinVersion") {
return handler(ctx, req)
}
if strings.Contains(info.FullMethod, "Check") {
return handler(ctx, req)
}
if strings.Contains(info.FullMethod, "v4") {
for _, v := range disabledVersions {
if v == 4 {
preconditionFailedCounter.Inc()
return nil, status.Error(codes.FailedPrecondition, "unsupported kin version")
}
}
return handler(ctx, req)
}
version, err := GetCtxKinVersion(ctx)
if err != nil {
log.WithError(err).Warn("failed to get kin version; reverting to default")
version = defaultVersion
}
for i := range disabledVersions {
if int(version) == disabledVersions[i] {
preconditionFailedCounter.Inc()
return nil, status.Error(codes.FailedPrecondition, "unsupported kin version")
}
}
return handler(ctx, req)
}
}
func DisabledVersionStreamServerInterceptor(defaultVersion KinVersion, disabledVersions []int) grpc.StreamServerInterceptor {
log := logrus.StandardLogger().WithField("type", "version/interceptor")
return func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
if strings.Contains(info.FullMethod, "v4") {
for _, v := range disabledVersions {
if v == 4 {
preconditionFailedCounter.Inc()
return status.Error(codes.FailedPrecondition, "unsupported kin version")
}
}
return handler(srv, ss)
}
version, err := GetCtxKinVersion(ss.Context())
if err != nil {
log.WithError(err).Warn("failed to get kin version; reverting to default")
version = defaultVersion
}
for i := range disabledVersions {
if int(version) == disabledVersions[i] {
preconditionFailedCounter.Inc()
return status.Error(codes.FailedPrecondition, "unsupported kin version")
}
}
return handler(srv, ss)
}
}
// MinVersionUnaryServerInterceptor prevents versions below the minimum
// version from accessing lower version APIs.
func MinVersionUnaryServerInterceptor() grpc.UnaryServerInterceptor {
log := logrus.StandardLogger().WithField("type", "version/interceptor")
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
if strings.Contains(info.FullMethod, "GetMinimumKinVersion") {
return handler(ctx, req)
}
desired, err := GetCtxDesiredVersion(ctx)
if err != nil {
log.WithError(err).Warn("failed to get desired kin version; ignoring")
return handler(ctx, req)
}
switch desired {
case KinVersion2, KinVersion3:
actual, err := GetCtxKinVersion(ctx)
if err != nil {
log.WithError(err).Warn("failed to get kin version; ignoring")
return handler(ctx, req)
}
if actual < desired {
preconditionFailedCounter.Inc()
return nil, status.Error(codes.FailedPrecondition, "unsupported kin version")
}
case KinVersion4:
if !strings.Contains(info.FullMethod, "v4") {
preconditionFailedCounter.Inc()
return nil, status.Error(codes.FailedPrecondition, "version not supported")
}
default:
log.WithField("version", desired).Warn("unhandled kin version; ignoring")
}
return handler(ctx, req)
}
}
// MinVersionStreamServerInterceptor prevents versions below the minimum
// version from accessing lower version APIs.
func MinVersionStreamServerInterceptor() grpc.StreamServerInterceptor {
return func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
log := logrus.StandardLogger().WithField("type", "version/interceptor")
desired, err := GetCtxDesiredVersion(ss.Context())
if err != nil {
log.WithError(err).Warn("failed to get desired kin version; ignoring")
return handler(srv, ss)
}
switch desired {
case KinVersion2, KinVersion3:
actual, err := GetCtxKinVersion(ss.Context())
if err != nil {
log.WithError(err).Warn("failed to get kin version; ignoring")
return handler(srv, ss)
}
if actual < desired {
preconditionFailedCounter.Inc()
return status.Error(codes.FailedPrecondition, "unsupported kin version")
}
case KinVersion4:
if !strings.Contains(info.FullMethod, "v4") {
preconditionFailedCounter.Inc()
return status.Error(codes.FailedPrecondition, "version not supported")
}
default:
log.WithField("version", desired).Warn("unhandled kin version; ignoring")
}
return handler(srv, ss)
}
}
func registerMetrics() error {
if err := prometheus.Register(preconditionFailedCounter); err != nil {
if e, ok := err.(prometheus.AlreadyRegisteredError); ok {
preconditionFailedCounter = e.ExistingCollector.(prometheus.Counter)
} else {
return err
}
}
return nil
}