Files
threshold/version/feature_manager.go
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

126 lines
3.7 KiB
Go
Raw Normal View History

// Copyright (c) HashiCorp, Inc.
2022-11-28 09:33:38 -05:00
// SPDX-License-Identifier: BUSL-1.1
2023-02-02 17:15:17 -08:00
2022-11-28 09:33:38 -05:00
package version
import (
gvers "github.com/hashicorp/go-version"
)
type MetadataConstraint struct {
Constraints gvers.Constraints
}
type Feature int
const (
UnknownFeature Feature = iota
MultiHopSessionFeature
2023-02-01 20:54:08 +00:00
IncludeStatusInCli
CredentialLibraryVaultSubtype
UseTargetIdForHostId
2023-03-07 16:29:34 -05:00
RequireVersionInWorkerInfo
2023-05-18 12:11:11 -04:00
SshSessionRecording
2023-06-06 16:13:44 -04:00
SupportIdInGrants
PluginDelete
2022-11-28 09:33:38 -05:00
)
var featureMap map[Feature]MetadataConstraint
// Binary is the version of the running binary.
// This can be used for feature checks.
var Binary *gvers.Version
2022-11-28 09:33:38 -05:00
func init() {
// Do this early to ensure version is valid, if this panics something is
// very broken with the version and any version checks based on the binary's
// version info will not work correctly. Also do this once since the version
// can't change while running.
var err error
Binary, err = GetReleaseVersion()
if err != nil {
panic(err)
}
2022-11-28 09:33:38 -05:00
if featureMap == nil {
featureMap = make(map[Feature]MetadataConstraint)
}
/*
Add constraints here following this format after adding a Feature to the Feature iota:
featureMap[FEATURE] = MetadataConstraint{
MetaInfo: []Metadata{OSS, HCP},
Constraints: mustNewConstraints(">= 0.1.0"), // This feature exists at 0.1.0 and above
2022-11-28 09:33:38 -05:00
}
*/
2023-02-01 20:54:08 +00:00
featureMap[IncludeStatusInCli] = MetadataConstraint{
Constraints: mustNewConstraints("< 0.14.0"),
}
featureMap[CredentialLibraryVaultSubtype] = MetadataConstraint{
Constraints: mustNewConstraints("< 0.14.0"),
}
// UseTargetIdForHostId supports old CLI clients that are unaware of host-sourceless targets,
// this feature populates the target's public id into the AuthorizeSessionResponse
// and the SessionAuthorizationData so the CLI can properly build the ssh command
// when calling "boundary connect ssh..."
featureMap[UseTargetIdForHostId] = MetadataConstraint{
Constraints: mustNewConstraints("< 0.14.0"),
}
2023-03-07 16:29:34 -05:00
// RequireVersionInWorkerInfo allows us to take action on various workers
// based on their version, e.g. to prevent incompatibilities
featureMap[RequireVersionInWorkerInfo] = MetadataConstraint{
Constraints: mustNewConstraints(">= 0.13.0"),
}
2023-05-18 12:11:11 -04:00
featureMap[SshSessionRecording] = MetadataConstraint{
Constraints: mustNewConstraints(">= 0.13.0"),
}
2023-06-06 16:13:44 -04:00
// Warn until 0.16 about using the now-deprecated id field in grants; after
2023-06-06 16:13:44 -04:00
// that disallow it
featureMap[SupportIdInGrants] = MetadataConstraint{
Constraints: mustNewConstraints("< 0.15.0"),
2023-06-06 16:13:44 -04:00
}
// PluginDelete supports calling DeleteObjects on the Storage Plugin
featureMap[PluginDelete] = MetadataConstraint{
Constraints: mustNewConstraints(">= 0.15.0"),
}
2022-11-28 09:33:38 -05:00
}
func mustNewConstraints(v string) gvers.Constraints {
c, err := gvers.NewConstraint(v)
if err != nil {
panic(err)
}
return c
}
// Check returns a bool indicating if a version meets the constraints
// for a feature. Check returns false if version is nil.
2022-11-28 09:33:38 -05:00
func (m MetadataConstraint) Check(version *gvers.Version) bool {
if version == nil {
return false
}
return m.Constraints.Check(version)
2022-11-28 09:33:38 -05:00
}
// Check returns a bool indicating if a version satisfies the feature constraints
func Check(binaryVersion *gvers.Version, featureConstraint MetadataConstraint) bool {
return featureConstraint.Check(binaryVersion)
2022-11-28 09:33:38 -05:00
}
// SupportsFeature return a bool indicating whether or not this version supports the given feature
func SupportsFeature(version *gvers.Version, feature Feature) bool {
featureVersion, found := featureMap[feature]
if !found {
return false
}
return Check(version, featureVersion)
}
// GetReleaseVersion returns a go-version of this binary's Boundary version
func GetReleaseVersion() (*gvers.Version, error) {
ver := Get()
return gvers.NewVersion(ver.VersionNumber())
2022-11-28 09:33:38 -05:00
}