Files
nomad/command/agent/consul/self.go
Luiz Aoqui 4068b68b29 client: fix Consul version finterprint (#17349)
Consul v1.13.8 was released with a breaking change in the /v1/agent/self
endpoint version where a line break was being returned.

This caused the Nomad finterprint to fail because `NewVersion` errors on
parse.

This commit removes any extra space from the Consul version returned by
the API.
2023-05-30 11:07:57 -04:00

60 lines
1.5 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package consul
import (
"strings"
"github.com/hashicorp/go-version"
)
// Self represents the response body from Consul /v1/agent/self API endpoint.
// Care must always be taken to do type checks when casting, as structure could
// potentially change over time.
type Self = map[string]map[string]interface{}
func SKU(info Self) (string, bool) {
v, ok := info["Config"]["Version"].(string)
if !ok {
return "", ok
}
ver, vErr := version.NewVersion(strings.TrimSpace(v))
if vErr != nil {
return "", false
}
if strings.Contains(ver.Metadata(), "ent") {
return "ent", true
}
return "oss", true
}
// Namespaces returns true if the "Namespaces" feature is enabled in Consul, and
// false otherwise. Consul OSS will always return false, and Consul ENT will return
// false if the license file does not contain the necessary feature.
func Namespaces(info Self) bool {
return feature("Namespaces", info)
}
// feature returns whether the indicated feature is enabled by Consul and the
// associated License.
// possible values as of v1.9.5+ent:
//
// Automated Backups, Automated Upgrades, Enhanced Read Scalability,
// Network Segments, Redundancy Zone, Advanced Network Federation,
// Namespaces, SSO, Audit Logging
func feature(name string, info Self) bool {
lic, licOK := info["Stats"]["license"].(map[string]interface{})
if !licOK {
return false
}
features, exists := lic["features"].(string)
if !exists {
return false
}
return strings.Contains(features, name)
}