licensing: remove raft storage and sync

This changeset is the OSS portion of the work to remove the raft storage and
sync for Nomad Enterprise.
This commit is contained in:
Tim Gross
2021-04-27 16:50:07 -04:00
committed by Tim Gross
parent 3e95f23250
commit 4f4b7ff579
13 changed files with 23 additions and 247 deletions

View File

@@ -423,6 +423,7 @@ func convertServerConfig(agentConfig *Config) (*nomad.Config, error) {
// Add Enterprise license configs
conf.LicenseEnv = agentConfig.Server.LicenseEnv
conf.LicensePath = agentConfig.Server.LicensePath
conf.LicenseConfig.AdditionalPubKeys = agentConfig.Server.licenseAdditionalPublicKeys
// Add the search configuration
if search := agentConfig.Server.Search; search != nil {

View File

@@ -508,6 +508,10 @@ type ServerConfig struct {
// is set, LicenseEnv will be set to the value at startup.
LicenseEnv string
// licenseAdditionalPublicKeys is an internal-only field used to
// setup test licenses.
licenseAdditionalPublicKeys []string
// ExtraKeysHCL is used by hcl to surface unexpected keys
ExtraKeysHCL []string `hcl:",unusedKeys" json:"-"`

View File

@@ -112,6 +112,8 @@ func (a *TestAgent) Start() *TestAgent {
if a.Config == nil {
a.Config = a.config()
}
defaultEnterpriseTestServerConfig(a.Config.Server)
if a.Config.DataDir == "" {
name := "agent"
if a.Name != "" {

View File

@@ -6,3 +6,5 @@ const (
// EnterpriseTestAgent is used to configure a TestAgent's Enterprise flag
EnterpriseTestAgent = false
)
func defaultEnterpriseTestServerConfig(c *ServerConfig) {}

View File

@@ -392,11 +392,6 @@ func Commands(metaPtr *Meta, agentUi cli.Ui) map[string]cli.CommandFactory {
Meta: meta,
}, nil
},
"license put": func() (cli.Command, error) {
return &LicensePutCommand{
Meta: meta,
}, nil
},
"logs": func() (cli.Command, error) {
return &AllocLogsCommand{
Meta: meta,

View File

@@ -18,18 +18,12 @@ type LicenseCommand struct {
func (l *LicenseCommand) Help() string {
helpText := `
Usage: nomad license <subcommand> [options] [args]
This command has subcommands for managing the Nomad Enterprise license.
For more detailed examples see:
https://www.nomadproject.io/docs/commands/license/
Install a new license from a file:
$ nomad license put <path>
Install a new license from stdin:
$ nomad license put -
Retrieve the current license:
Retrieve the server's license:
$ nomad license get

View File

@@ -1,167 +0,0 @@
package command
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"github.com/hashicorp/nomad/api"
"github.com/pkg/errors"
"github.com/posener/complete"
)
type LicensePutCommand struct {
Meta
testStdin io.Reader
}
func (c *LicensePutCommand) Help() string {
helpText := `
Usage: nomad license put [options]
Puts a new license in Servers and Clients
When ACLs are enabled, this command requires a token with the
'operator:write' capability.
Use the -force flag to override the currently installed license with an older
license.
General Options:
` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace) + `
License Options:
-force
Force is used to override the currently installed license. By default
Nomad will keep the newest license, as determined by the license issue
date. Use this flag to apply an older license.
Install a new license from a file:
$ nomad license put <path>
Install a new license from stdin:
$ nomad license put -
`
return strings.TrimSpace(helpText)
}
func (c *LicensePutCommand) Synopsis() string {
return "Install a new Nomad Enterprise License"
}
func (c *LicensePutCommand) AutoCompleteFlags() complete.Flags {
return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
complete.Flags{
"-force": complete.PredictNothing,
})
}
func (c *LicensePutCommand) Name() string { return "license put" }
func (c *LicensePutCommand) Run(args []string) int {
var force bool
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
flags.Usage = func() { c.Ui.Output(c.Help()) }
flags.BoolVar(&force, "force", false, "")
if err := flags.Parse(args); err != nil {
c.Ui.Error(fmt.Sprintf("Error parsing flags: %s", err))
return 1
}
args = flags.Args()
data, err := c.dataFromArgs(args)
if err != nil {
c.Ui.Error(errors.Wrap(err, "Error parsing arguments").Error())
return 1
}
client, err := c.Meta.Client()
if err != nil {
c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
return 1
}
opts := &api.ApplyLicenseOptions{
Force: force,
}
_, err = client.Operator().ApplyLicense(data, opts, nil)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error putting license: %v", err))
return 1
}
lic, _, err := client.Operator().LicenseGet(nil)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error retrieving new license: %v", err))
return 1
}
if lic.ConfigOutdated {
c.Ui.Warn(`
WARNING: The server's configured file license is now outdated. Please update or
remove the server's license configuration to prevent initialization issues with
potentially expired licenses.
`) // New line for cli output
}
c.Ui.Output("Successfully applied license")
return 0
}
func (c *LicensePutCommand) dataFromArgs(args []string) (string, error) {
switch len(args) {
case 0:
return "", fmt.Errorf("Missing LICENSE argument")
case 1:
return LoadDataSource(args[0], c.testStdin)
default:
return "", fmt.Errorf("Too many arguments, exptected 1, got %d", len(args))
}
}
func loadFromFile(path string) (string, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return "", fmt.Errorf("Failed to read file: %v", err)
}
return string(data), nil
}
func loadFromStdin(testStdin io.Reader) (string, error) {
var stdin io.Reader = os.Stdin
if testStdin != nil {
stdin = testStdin
}
var b bytes.Buffer
if _, err := io.Copy(&b, stdin); err != nil {
return "", fmt.Errorf("Failed to read stdin: %v", err)
}
return b.String(), nil
}
func LoadDataSource(file string, testStdin io.Reader) (string, error) {
// Handle empty quoted shell parameters
if len(file) == 0 {
return "", nil
}
if file == "-" {
if len(file) > 1 {
return file, nil
}
return loadFromStdin(testStdin)
}
return loadFromFile(file)
}

View File

@@ -1,32 +0,0 @@
package command
import (
"strings"
"testing"
"github.com/mitchellh/cli"
"github.com/stretchr/testify/require"
)
var _ cli.Command = &LicensePutCommand{}
func TestCommand_LicensePut_Err(t *testing.T) {
t.Parallel()
srv, _, url := testServer(t, false, nil)
defer srv.Shutdown()
ui := cli.NewMockUi()
cmd := &LicensePutCommand{Meta: Meta{Ui: ui}, testStdin: strings.NewReader("testlicenseblob")}
if code := cmd.Run([]string{"-address=" + url, "-"}); code != 1 {
require.Equal(t, code, 1)
}
if srv.Enterprise {
require.Contains(t, ui.ErrorWriter.String(), "error validating license")
} else {
require.Contains(t, ui.ErrorWriter.String(), "Nomad Enterprise only endpoint")
}
}

View File

@@ -19,7 +19,6 @@ import (
_ "github.com/hashicorp/nomad/e2e/events"
_ "github.com/hashicorp/nomad/e2e/example"
_ "github.com/hashicorp/nomad/e2e/isolation"
_ "github.com/hashicorp/nomad/e2e/license"
_ "github.com/hashicorp/nomad/e2e/lifecycle"
_ "github.com/hashicorp/nomad/e2e/metrics"
_ "github.com/hashicorp/nomad/e2e/namespaces"

View File

@@ -1,33 +0,0 @@
package license
import (
"github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/e2e/framework"
"github.com/stretchr/testify/require"
)
type LicenseE2ETest struct {
framework.TC
}
func init() {
framework.AddSuites(&framework.TestSuite{
Component: "License",
CanRunLocal: true,
Cases: []framework.TestCase{new(LicenseE2ETest)},
})
}
func (tc *LicenseE2ETest) TestLicenseGet(f *framework.F) {
t := f.T()
client := tc.Nomad()
// Get the license and do not forward to the leader
lic, _, err := client.Operator().LicenseGet(&api.QueryOptions{
AllowStale: true,
})
require.NoError(t, err)
require.NotEqual(t, "temporary-license", lic.License.LicenseID)
}

View File

@@ -8,7 +8,9 @@ import (
// LicenseConfig allows for tunable licensing config
// primarily used for enterprise testing
type LicenseConfig struct{}
type LicenseConfig struct {
AdditionalPubKeys []string
}
type EnterpriseState struct{}

View File

@@ -44,6 +44,10 @@ func TestACLServer(t testing.T, cb func(*Config)) (*Server, *structs.ACLToken, f
func TestServer(t testing.T, cb func(*Config)) (*Server, func()) {
// Setup the default settings
config := DefaultConfig()
// Setup default enterprise-specific settings, including license
defaultEnterpriseTestConfig(config)
config.Logger = testlog.HCLogger(t)
config.Build = version.Version + "+unittest"
config.DevMode = true

5
nomad/testing_oss.go Normal file
View File

@@ -0,0 +1,5 @@
// +build !ent
package nomad
func defaultEnterpriseTestConfig(c *Config) {}