/
versionChecker.go
186 lines (161 loc) · 6.94 KB
/
versionChecker.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
// The MIT License
//
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
//
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package headers
import (
"context"
"fmt"
"strings"
"github.com/blang/semver/v4"
"golang.org/x/exp/slices"
"google.golang.org/grpc/metadata"
"go.temporal.io/api/serviceerror"
)
const (
ClientNameServer = "temporal-server"
ClientNameGoSDK = "temporal-go"
ClientNameJavaSDK = "temporal-java"
ClientNamePHPSDK = "temporal-php"
ClientNameTypeScriptSDK = "temporal-typescript"
ClientNamePythonSDK = "temporal-python"
ClientNameCLI = "temporal-cli"
ClientNameUI = "temporal-ui"
ServerVersion = "1.20.4"
// SupportedServerVersions is used by CLI and inter role communication.
SupportedServerVersions = ">=1.0.0 <2.0.0"
// FeatureFollowsNextRunID means that the client supports following next execution run id for
// completed/failed/timedout completion events when getting the final result of a workflow.
FeatureFollowsNextRunID = "follows-next-run-id"
)
var (
// AllFeatures contains all known features. This list is used as the value of the supported
// features header for internal server requests. There is an assumption that if a feature is
// defined, then the server itself supports it.
AllFeatures = strings.Join([]string{
FeatureFollowsNextRunID,
}, SupportedFeaturesHeaderDelim)
SupportedClients = map[string]string{
ClientNameGoSDK: "<2.0.0",
ClientNameJavaSDK: "<2.0.0",
ClientNamePHPSDK: "<2.0.0",
ClientNameTypeScriptSDK: "<2.0.0",
ClientNameCLI: "<2.0.0",
ClientNameServer: "<2.0.0",
ClientNameUI: "<3.0.0",
}
internalVersionHeaderPairs = []string{
ClientNameHeaderName, ClientNameServer,
ClientVersionHeaderName, ServerVersion,
SupportedServerVersionsHeaderName, SupportedServerVersions,
SupportedFeaturesHeaderName, AllFeatures,
}
)
type (
// VersionChecker is used to check client/server compatibility and client's capabilities
VersionChecker interface {
ClientSupported(ctx context.Context) error
ClientSupportsFeature(ctx context.Context, feature string) bool
}
versionChecker struct {
supportedClients map[string]string
supportedClientsRange map[string]semver.Range
serverVersion semver.Version
}
)
// NewDefaultVersionChecker constructs a new VersionChecker using default versions from const.
func NewDefaultVersionChecker() *versionChecker {
return NewVersionChecker(SupportedClients, ServerVersion)
}
// NewVersionChecker constructs a new VersionChecker
func NewVersionChecker(supportedClients map[string]string, serverVersion string) *versionChecker {
return &versionChecker{
serverVersion: semver.MustParse(serverVersion),
supportedClients: supportedClients,
supportedClientsRange: mustParseRanges(supportedClients),
}
}
// GetClientNameAndVersion extracts SDK name and version from context headers
func GetClientNameAndVersion(ctx context.Context) (string, string) {
headers := GetValues(ctx, ClientNameHeaderName, ClientVersionHeaderName)
clientName := headers[0]
clientVersion := headers[1]
return clientName, clientVersion
}
// SetVersions sets headers for internal communications.
func SetVersions(ctx context.Context) context.Context {
return metadata.AppendToOutgoingContext(ctx, internalVersionHeaderPairs...)
}
// SetVersionsForTests sets headers as they would be received from the client.
// Must be used in tests only.
func SetVersionsForTests(ctx context.Context, clientVersion, clientName, supportedServerVersions, supportedFeatures string) context.Context {
return metadata.NewIncomingContext(ctx, metadata.New(map[string]string{
ClientNameHeaderName: clientName,
ClientVersionHeaderName: clientVersion,
SupportedServerVersionsHeaderName: supportedServerVersions,
SupportedFeaturesHeaderName: supportedFeatures,
}))
}
// ClientSupported returns an error if client is unsupported, nil otherwise.
func (vc *versionChecker) ClientSupported(ctx context.Context) error {
headers := GetValues(ctx, ClientNameHeaderName, ClientVersionHeaderName, SupportedServerVersionsHeaderName)
clientName := headers[0]
clientVersion := headers[1]
supportedServerVersions := headers[2]
// Validate client version only if it is provided and server knows about this client.
if clientName != "" && clientVersion != "" {
if supportedClientRange, ok := vc.supportedClientsRange[clientName]; ok {
clientVersionParsed, parseErr := semver.Parse(clientVersion)
if parseErr != nil {
return serviceerror.NewInvalidArgument(fmt.Sprintf("Unable to parse client version: %v", parseErr))
}
if !supportedClientRange(clientVersionParsed) {
return serviceerror.NewClientVersionNotSupported(clientVersion, clientName, vc.supportedClients[clientName])
}
}
}
// Validate supported server version if it is provided.
if supportedServerVersions != "" {
supportedServerVersionsParsed, parseErr := semver.ParseRange(supportedServerVersions)
if parseErr != nil {
return serviceerror.NewInvalidArgument(fmt.Sprintf("Unable to parse supported server versions: %v", parseErr))
}
if !supportedServerVersionsParsed(vc.serverVersion) {
return serviceerror.NewServerVersionNotSupported(vc.serverVersion.String(), supportedServerVersions)
}
}
return nil
}
// ClientSupportsFeature returns true if the client reports support for the
// given feature (which should be one of the Feature... constants above).
func (vc *versionChecker) ClientSupportsFeature(ctx context.Context, feature string) bool {
headers := GetValues(ctx, SupportedFeaturesHeaderName)
clientFeatures := strings.Split(headers[0], SupportedFeaturesHeaderDelim)
return slices.Contains(clientFeatures, feature)
}
func mustParseRanges(ranges map[string]string) map[string]semver.Range {
out := make(map[string]semver.Range, len(ranges))
for c, r := range ranges {
out[c] = semver.MustParseRange(r)
}
return out
}