diff --git a/command/agent/agent.go b/command/agent/agent.go index 13c896c7f..a31d008c2 100644 --- a/command/agent/agent.go +++ b/command/agent/agent.go @@ -170,7 +170,6 @@ func convertServerConfig(agentConfig *Config) (*nomad.Config, error) { conf.EnableDebug = agentConfig.EnableDebug conf.Build = agentConfig.Version.VersionNumber() - conf.BuildDate = agentConfig.Version.BuildDate conf.Revision = agentConfig.Version.Revision if agentConfig.Region != "" { conf.Region = agentConfig.Region @@ -550,9 +549,12 @@ 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 + conf.LicenseConfig = &nomad.LicenseConfig{ + BuildDate: agentConfig.Version.BuildDate, + AdditionalPubKeys: agentConfig.Server.licenseAdditionalPublicKeys, + LicenseEnvBytes: agentConfig.Server.LicenseEnv, + LicensePath: agentConfig.Server.LicensePath, + } // Add the search configuration if search := agentConfig.Server.Search; search != nil { diff --git a/nomad/config.go b/nomad/config.go index 6c789aef4..191dd72af 100644 --- a/nomad/config.go +++ b/nomad/config.go @@ -127,9 +127,6 @@ type Config struct { // operators track which versions are actively deployed Build string - // BuildDate is the time of the git commit used to build the program. - BuildDate time.Time - // Revision is a string that carries the version.GitCommit of Nomad that // was compiled. Revision string @@ -389,10 +386,8 @@ type Config struct { // connections from a single IP address. nil/0 means no limit. RPCMaxConnsPerClient int - // LicenseConfig is a tunable knob for enterprise license testing. + // LicenseConfig stores information about the Enterprise license loaded for the server. LicenseConfig *LicenseConfig - LicenseEnv string - LicensePath string // SearchConfig provides knobs for Search API. SearchConfig *structs.SearchConfig diff --git a/nomad/server_setup.go b/nomad/license_config.go similarity index 91% rename from nomad/server_setup.go rename to nomad/license_config.go index a37d8b517..59f0e1330 100644 --- a/nomad/server_setup.go +++ b/nomad/license_config.go @@ -3,7 +3,6 @@ package nomad import ( "time" - "github.com/hashicorp/go-hclog" "golang.org/x/exp/slices" ) @@ -21,8 +20,6 @@ type LicenseConfig struct { // AdditionalPubKeys is a set of public keys to AdditionalPubKeys []string - - Logger hclog.InterceptLogger } func (c *LicenseConfig) Copy() *LicenseConfig { diff --git a/nomad/license_config_oss.go b/nomad/license_config_oss.go new file mode 100644 index 000000000..32d00d39a --- /dev/null +++ b/nomad/license_config_oss.go @@ -0,0 +1,7 @@ +//go:build !ent + +package nomad + +func (c *LicenseConfig) Validate() error { + return nil +} diff --git a/nomad/server.go b/nomad/server.go index 7a2e268ae..4d4cb910d 100644 --- a/nomad/server.go +++ b/nomad/server.go @@ -345,6 +345,11 @@ func NewServer(config *Config, consulCatalog consul.CatalogAPI, consulConfigEntr // Create the logger logger := config.Logger.ResetNamedIntercept("nomad") + // Validate enterprise license before anything stateful happens + if err = config.LicenseConfig.Validate(); err != nil { + return nil, err + } + // Create the server s := &Server{ config: config, @@ -868,8 +873,11 @@ func (s *Server) Reload(newConfig *Config) error { } } - if newConfig.LicenseEnv != "" || newConfig.LicensePath != "" { - s.EnterpriseState.ReloadLicense(newConfig) + if newConfig.LicenseConfig.LicenseEnvBytes != "" || newConfig.LicenseConfig.LicensePath != "" { + if err = s.EnterpriseState.ReloadLicense(newConfig); err != nil { + s.logger.Error("error reloading license", "error", err) + _ = multierror.Append(&mErr, err) + } } // Because this is a new configuration, we extract the worker pool arguments without acquiring a lock diff --git a/nomad/server_setup_oss.go b/nomad/server_setup_oss.go index 5e0684729..b37657269 100644 --- a/nomad/server_setup_oss.go +++ b/nomad/server_setup_oss.go @@ -1,5 +1,4 @@ //go:build !ent -// +build !ent package nomad diff --git a/nomad/testing.go b/nomad/testing.go index d2ec5d616..de933f271 100644 --- a/nomad/testing.go +++ b/nomad/testing.go @@ -1,7 +1,6 @@ package nomad import ( - "errors" "fmt" "math/rand" "net" @@ -43,7 +42,11 @@ func TestServer(t testing.T, cb func(*Config)) (*Server, func()) { return s, c } -func TestServerErr(t testing.T, cb func(*Config)) (*Server, func(), error) { +// TestConfigForServer provides a fully functional Config to pass to NewServer() +// It can be changed beforehand to induce different behavior such as specific errors. +func TestConfigForServer(t testing.T) *Config { + t.Helper() + // Setup the default settings config := DefaultConfig() @@ -95,6 +98,19 @@ func TestServerErr(t testing.T, cb func(*Config)) (*Server, func(), error) { MinTermLength: 2, } + // Get random ports for RPC and Serf + ports := ci.PortAllocator.Grab(2) + config.RPCAddr = &net.TCPAddr{ + IP: []byte{127, 0, 0, 1}, + Port: ports[0], + } + config.SerfConfig.MemberlistConfig.BindPort = ports[1] + + return config +} + +func TestServerErr(t testing.T, cb func(*Config)) (*Server, func(), error) { + config := TestConfigForServer(t) // Invoke the callback if any if cb != nil { cb(config) @@ -104,18 +120,12 @@ func TestServerErr(t testing.T, cb func(*Config)) (*Server, func(), error) { cConfigs := consul.NewMockConfigsAPI(config.Logger) cACLs := consul.NewMockACLsAPI(config.Logger) + var server *Server + var err error + for i := 10; i >= 0; i-- { - // Get random ports, need to cleanup later - ports := ci.PortAllocator.Grab(2) - - config.RPCAddr = &net.TCPAddr{ - IP: []byte{127, 0, 0, 1}, - Port: ports[0], - } - config.SerfConfig.MemberlistConfig.BindPort = ports[1] - // Create server - server, err := NewServer(config, cCatalog, cConfigs, cACLs) + server, err = NewServer(config, cCatalog, cConfigs, cACLs) if err == nil { return server, func() { ch := make(chan error) @@ -145,9 +155,17 @@ func TestServerErr(t testing.T, cb func(*Config)) (*Server, func(), error) { wait := time.Duration(rand.Int31n(2000)) * time.Millisecond time.Sleep(wait) } + + // if it failed for port reasons, try new ones + ports := ci.PortAllocator.Grab(2) + config.RPCAddr = &net.TCPAddr{ + IP: []byte{127, 0, 0, 1}, + Port: ports[0], + } + config.SerfConfig.MemberlistConfig.BindPort = ports[1] } - return nil, nil, errors.New("unable to acquire ports for test server") + return nil, nil, fmt.Errorf("error starting test server: %w", err) } func TestJoin(t testing.T, servers ...*Server) {