-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathversion.go
169 lines (140 loc) · 3.89 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
package client
import (
"context"
"fmt"
"regexp"
"strconv"
"strings"
"github.com/pkg/errors"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
const (
versionPattern = "\\d+.\\d+(.\\d+)?"
)
var (
versionRegex = regexp.MustCompile(fmt.Sprintf("^%s$", versionPattern))
// todo: configurable
minVersionByDevice = map[DeviceType]*Version{
DeviceTypeIOS: {
Major: 0,
Minor: 0,
Patch: 0,
},
DeviceTypeAndroid: {
Major: 0,
Minor: 0,
Patch: 0,
},
}
)
type Version struct {
Major int
Minor int
Patch int
}
func ParseVersion(value string) (*Version, error) {
value = strings.TrimSpace(value)
if !versionRegex.MatchString(value) {
return nil, errors.New("version doesn't match regex")
}
versionComponents := strings.Split(value, ".")
major, err := strconv.Atoi(versionComponents[0])
if err != nil {
return nil, errors.Wrap(err, "unable to parse major part as an int")
}
var minor int
if len(versionComponents) > 1 {
minor, err = strconv.Atoi(versionComponents[1])
if err != nil {
return nil, errors.Wrap(err, "unable to parse minor part as an int")
}
}
var patch int
if len(versionComponents) > 2 {
patch, err = strconv.Atoi(versionComponents[2])
if err != nil {
return nil, errors.Wrap(err, "unable to parse patch part as an int")
}
}
return &Version{
Major: major,
Minor: minor,
Patch: patch,
}, nil
}
func (v *Version) GreaterThanOrEqualTo(other *Version) bool {
if other == nil {
return true
}
// Compare major versions first
if v.Major > other.Major {
return true
} else if v.Major < other.Major {
return false
}
// Major versions match, so compare minor version next
if v.Minor > other.Minor {
return true
} else if v.Minor < other.Minor {
return false
}
// Minor versions match, so compare patch version next
return v.Patch >= other.Patch
}
func (v *Version) Before(other *Version) bool {
return !v.GreaterThanOrEqualTo(other)
}
func (v *Version) String() string {
return fmt.Sprintf("%d.%d.%d", v.Major, v.Minor, v.Patch)
}
// MinVersionUnaryServerInterceptor prevents versions below the minimum
// version from accessing outdated APIs.
func MinVersionUnaryServerInterceptor() grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
// Health checks are internal and don't have an external client user agent
//
// todo: Generic utility for whether the method is internal. Not needed right
// now because all RPC endpoints except health are external.
if strings.Contains(info.FullMethod, "grpc.health.v1.Health/Check") {
return handler(ctx, req)
}
userAgent, err := GetUserAgent(ctx)
if err != nil {
// Just continue on errors because we have a breaking change wrt the
// user agent header value atm
return handler(ctx, req)
}
if err := checkMinVersion(userAgent); err != nil {
return nil, err
}
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 {
userAgent, err := GetUserAgent(ss.Context())
if err != nil {
// Just continue on errors because we have a breaking change wrt the
// user agent header value atm
return handler(srv, ss)
}
if err := checkMinVersion(userAgent); err != nil {
return err
}
return handler(srv, ss)
}
}
func checkMinVersion(userAgent *UserAgent) error {
minVersion, ok := minVersionByDevice[userAgent.DeviceType]
if !ok {
return status.Error(codes.FailedPrecondition, "unsupported client type")
}
if userAgent.Version.Before(minVersion) {
return status.Error(codes.FailedPrecondition, "version too low")
}
return nil
}