This repository has been archived by the owner on Feb 9, 2024. It is now read-only.
/
loc.go
319 lines (271 loc) · 9.51 KB
/
loc.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
Copyright 2018 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package loc
import (
"encoding/hex"
"fmt"
"regexp"
"strings"
"github.com/gravitational/gravity/lib/constants"
"github.com/gravitational/gravity/lib/defaults"
"github.com/coreos/go-semver/semver"
"github.com/gravitational/configure/cstrings"
"github.com/gravitational/trace"
log "github.com/sirupsen/logrus"
)
const (
// ZeroVersion is a special "zero" package version that means "no particular version"
ZeroVersion = "0.0.0"
// FirstVersion is a special package version that indicates the initial package version
FirstVersion = "0.0.1"
// LatestVersion defines a special placeholder for greatest available version
LatestVersion = "0.0.0+latest"
// StableVersion defines a special placeholder for the latest stable version
StableVersion = "0.0.0+stable"
)
// locRe expression specifies the format for package name that
// consists of the repository name and a version separated by the :
var locRe = regexp.MustCompile(`^([a-zA-Z0-9\-_\.]+):(.+)$`)
func NewDigest(t string, val []byte) (*Digest, error) {
if t != "sha512" {
return nil, trace.Errorf("unsupported digest type: '%v'", t)
}
if len(val) == 0 {
return nil, trace.Errorf("empty hash")
}
return &Digest{Type: t, Hash: val}, nil
}
func NewDigestFromHex(t string, val string) (*Digest, error) {
if t != "sha512" {
return nil, trace.Errorf("unsupported digest type: '%v'", t)
}
b, err := hex.DecodeString(val)
if len(val) == 0 {
return nil, trace.Wrap(err, "failed to decode hash string")
}
return &Digest{Type: t, Hash: b}, nil
}
type Digest struct {
Type string
Hash []byte
}
func (d Digest) Hex() string {
return fmt.Sprintf("%x", d.Hash)
}
func (d Digest) String() string {
return fmt.Sprintf("%x", d.Hash)
}
func NewLocator(repository, name, ver string) (*Locator, error) {
if !cstrings.IsValidDomainName(repository) {
return nil, trace.BadParameter(
"repository %q has invalid format, should be valid domain name, e.g. example.com", repository)
}
if name == "" {
return nil, trace.BadParameter(
"package name %q has invalid format, should be valid identifier e.g. package-name", name)
}
_, err := semver.NewVersion(ver)
if err != nil {
return nil, trace.BadParameter(
"unsupported version format %q, need semver format e.g 1.0.0: %v",
ver, err)
}
return &Locator{Repository: repository, Name: name, Version: ver}, nil
}
// Locator is a unique package locator. It consists of the name
// and version in the form of sem ver
type Locator struct {
Repository string `json:"repository"` // package software repository
Name string `json:"name"` // example: "planet-dev"
Version string `json:"version"` // example: "0.0.36"
}
// ZeroVersion returns a special 0.0.0 version of the package
func (l *Locator) ZeroVersion() Locator {
return Locator{Repository: l.Repository, Name: l.Name, Version: ZeroVersion}
}
// SemVer obtains emver from a marshalled version
func (l *Locator) SemVer() (*semver.Version, error) {
v, err := semver.NewVersion(l.Version)
if err != nil {
return nil, trace.Wrap(err,
"unsupported version format, need semver format: %v, e.g 1.0.0", l.Version)
}
return v, nil
}
// IsEqualTo returns true if this locator is equal to the other
func (l Locator) IsEqualTo(other Locator) bool {
return l.Repository == other.Repository && l.Name == other.Name && l.Version == other.Version
}
// IsEmpty returns true if locator presents empty value
func (l *Locator) IsEmpty() bool {
return l.Repository == "" && l.Name == "" && l.Version == ""
}
// IsNewerThan returns true if this locator is of greater version than the provided locator
func (l *Locator) IsNewerThan(other Locator) (bool, error) {
if l.Repository != other.Repository || l.Name != other.Name {
return false, trace.BadParameter("repository/name mismatch: %v %v", l, other)
}
ourVer, err := l.SemVer()
if err != nil {
return false, trace.Wrap(err)
}
otherVer, err := other.SemVer()
if err != nil {
return false, trace.Wrap(err)
}
return otherVer.LessThan(*ourVer), nil
}
func (l *Locator) Set(v string) error {
p, err := ParseLocator(v)
if err != nil {
return err
}
l.Repository = p.Repository
l.Name = p.Name
l.Version = p.Version
return nil
}
// String returns the locator's string representation.
func (l Locator) String() string {
str := l.Name
if l.Repository != "" {
str = fmt.Sprintf("%v/%v", l.Repository, str)
}
if l.Version != "" {
str = fmt.Sprintf("%v:%v", str, l.Version)
}
return str
}
// WithVersion returns a copy of this locator with version set to the specified one
func (l Locator) WithVersion(version *semver.Version) Locator {
return Locator{
Repository: l.Repository,
Name: l.Name,
Version: version.String(),
}
}
// WithLiteralVersion returns a copy of this locator with version set to the specified one
func (l Locator) WithLiteralVersion(version string) Locator {
return Locator{
Repository: l.Repository,
Name: l.Name,
Version: version,
}
}
func ParseLocator(v string) (*Locator, error) {
parts := strings.Split(v, "/")
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
return nil, trace.Errorf(
"package locator should be repository/name:semver, e.g. example.com/test:1.0.0, got %v", v)
}
m := locRe.FindAllStringSubmatch(parts[1], -1)
if len(m) != 1 || len(m[0]) != 3 {
return nil, trace.Errorf(
"invalid package locator, should be repository/name:semver, e.g. example.com/test:1.0.0")
}
return NewLocator(parts[0], m[0][1], m[0][2])
}
// MustCreateLocator creates new locator from parts or panics
func MustCreateLocator(repo, name, ver string) Locator {
loc, err := NewLocator(repo, name, ver)
if err != nil {
panic(err)
}
return *loc
}
func MustParseLocator(v string) Locator {
l, err := ParseLocator(v)
if err != nil {
panic(err)
}
return *l
}
// Deduplicate returns ls with duplicates removed
func Deduplicate(ls []Locator) (result []Locator) {
if len(ls) == 0 {
return ls
}
result = make([]Locator, 0, len(ls))
seen := make(map[Locator]struct{}, len(ls))
for _, loc := range ls {
if _, exists := seen[loc]; exists {
continue
}
result = append(result, loc)
seen[loc] = struct{}{}
}
return result
}
// Contains checks if a list of locators contains another one
func Contains(locator Locator, locators []Locator) bool {
for _, l := range locators {
if l.Name == locator.Name {
return true
}
}
return false
}
// Filter filters a list of locators.
// Excludes the values specified in []filters from []locators.
func Filter(locators []Locator, filters []Locator, message string) (result []Locator) {
for _, l := range locators {
if !Contains(l, filters) {
result = append(result, l)
} else {
log.Infof(message, l)
}
}
return result
}
var (
// OpsCenterCertificateAuthority is locator for the package containing certificate and private
// key for the OpsCenter
OpsCenterCertificateAuthority = MustParseLocator(
fmt.Sprintf("%v/%v:0.0.1", defaults.SystemAccountOrg, constants.OpsCenterCAPackage))
// Runtime is the default runtime application locator
Runtime = MustParseLocator(
fmt.Sprintf("%v/%v:%v", defaults.SystemAccountOrg, defaults.Runtime, LatestVersion))
// Bandwagon is the bandwagon application locator
Bandwagon = MustParseLocator(
fmt.Sprintf("%v/%v:%v", defaults.SystemAccountOrg, defaults.BandwagonPackageName, LatestVersion))
// Planet is the planet package locator
Planet = MustParseLocator(
fmt.Sprintf("%v/%v:%v", defaults.SystemAccountOrg, constants.PlanetPackage, ZeroVersion))
// Teleport is the teleport package locator
Teleport = MustParseLocator(
fmt.Sprintf("%v/%v:%v", defaults.SystemAccountOrg, constants.TeleportPackage, ZeroVersion))
// Gravity is the gravity binary package locator
Gravity = MustParseLocator(
fmt.Sprintf("%v/%v:%v", defaults.SystemAccountOrg, constants.GravityPackage, ZeroVersion))
// Fio is the fio binary package locator
Fio = MustParseLocator(
fmt.Sprintf("%v/%v:%v", defaults.SystemAccountOrg, constants.FioPackage, ZeroVersion))
// TrustedCluster is the trusted-cluster package locator
TrustedCluster = MustParseLocator(
fmt.Sprintf("%v/%v:0.0.1", defaults.SystemAccountOrg, constants.TrustedClusterPackage))
// RPCSecrets defines a package with RPC agent credentials
// that the wizard and cluster controller create.
// The credentials are then pulled by agents that participate in installation
// and join/leave operations.
RPCSecrets = MustParseLocator(
fmt.Sprintf("%v/%v:0.0.1", defaults.SystemAccountOrg, defaults.RPCAgentSecretsPackage))
// LegacyPlanetMaster is the package locator for legacy planet master packages
LegacyPlanetMaster = MustParseLocator(
fmt.Sprintf("%v/%v:%v", defaults.SystemAccountOrg, "planet-master", ZeroVersion))
// LegacyPlanetNode is the package locator for legacy planet node packages
LegacyPlanetNode = MustParseLocator(
fmt.Sprintf("%v/%v:%v", defaults.SystemAccountOrg, "planet-node", ZeroVersion))
// WebAssetsPackageLocator is the package with web assets
WebAssetsPackageLocator = MustParseLocator(
fmt.Sprintf("%v/%v:%v", defaults.SystemAccountOrg, "web-assets", ZeroVersion))
)