From a080aaf6b6358836e37da88ce116b62738ab5b71 Mon Sep 17 00:00:00 2001 From: Tim Gross Date: Tue, 27 Apr 2021 16:50:07 -0400 Subject: [PATCH] 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. --- command/agent/agent.go | 1 + command/agent/config.go | 4 + command/agent/testagent.go | 2 + command/agent/testagent_oss.go | 2 + command/commands.go | 5 - command/license.go | 10 +- command/license_put.go | 167 --------------------------------- command/license_put_test.go | 32 ------- e2e/e2e_test.go | 1 - e2e/license/license.go | 33 ------- nomad/server_setup_oss.go | 4 +- nomad/testing.go | 4 + nomad/testing_oss.go | 5 + 13 files changed, 23 insertions(+), 247 deletions(-) delete mode 100644 command/license_put.go delete mode 100644 command/license_put_test.go delete mode 100644 e2e/license/license.go create mode 100644 nomad/testing_oss.go diff --git a/command/agent/agent.go b/command/agent/agent.go index 4100d4bfb52..18cd812c3b2 100644 --- a/command/agent/agent.go +++ b/command/agent/agent.go @@ -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 { diff --git a/command/agent/config.go b/command/agent/config.go index 8fbb08a6736..c973c573831 100644 --- a/command/agent/config.go +++ b/command/agent/config.go @@ -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:"-"` diff --git a/command/agent/testagent.go b/command/agent/testagent.go index c40ac4fac86..c72616c72ed 100644 --- a/command/agent/testagent.go +++ b/command/agent/testagent.go @@ -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 != "" { diff --git a/command/agent/testagent_oss.go b/command/agent/testagent_oss.go index 37e18ee41f2..eae187a51ea 100644 --- a/command/agent/testagent_oss.go +++ b/command/agent/testagent_oss.go @@ -6,3 +6,5 @@ const ( // EnterpriseTestAgent is used to configure a TestAgent's Enterprise flag EnterpriseTestAgent = false ) + +func defaultEnterpriseTestServerConfig(c *ServerConfig) {} diff --git a/command/commands.go b/command/commands.go index 136d89929e2..89a42e3b565 100644 --- a/command/commands.go +++ b/command/commands.go @@ -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, diff --git a/command/license.go b/command/license.go index a6a55c49b88..ad67214c41f 100644 --- a/command/license.go +++ b/command/license.go @@ -18,18 +18,12 @@ type LicenseCommand struct { func (l *LicenseCommand) Help() string { helpText := ` Usage: nomad license [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 - -Install a new license from stdin: - $ nomad license put - - -Retrieve the current license: +Retrieve the server's license: $ nomad license get diff --git a/command/license_put.go b/command/license_put.go deleted file mode 100644 index d65740b0aff..00000000000 --- a/command/license_put.go +++ /dev/null @@ -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 - -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) -} diff --git a/command/license_put_test.go b/command/license_put_test.go deleted file mode 100644 index 4bb8e6e7571..00000000000 --- a/command/license_put_test.go +++ /dev/null @@ -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") - } - -} diff --git a/e2e/e2e_test.go b/e2e/e2e_test.go index b3326d895c4..c0758cb0fbc 100644 --- a/e2e/e2e_test.go +++ b/e2e/e2e_test.go @@ -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" diff --git a/e2e/license/license.go b/e2e/license/license.go deleted file mode 100644 index c4efb32787b..00000000000 --- a/e2e/license/license.go +++ /dev/null @@ -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) -} diff --git a/nomad/server_setup_oss.go b/nomad/server_setup_oss.go index f85158dafd3..6ada6a06e06 100644 --- a/nomad/server_setup_oss.go +++ b/nomad/server_setup_oss.go @@ -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{} diff --git a/nomad/testing.go b/nomad/testing.go index a3bc9b2d1a3..03337b4167b 100644 --- a/nomad/testing.go +++ b/nomad/testing.go @@ -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 diff --git a/nomad/testing_oss.go b/nomad/testing_oss.go new file mode 100644 index 00000000000..5f4d5e82cc2 --- /dev/null +++ b/nomad/testing_oss.go @@ -0,0 +1,5 @@ +// +build !ent + +package nomad + +func defaultEnterpriseTestConfig(c *Config) {}