-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
job endpoint hooks to enforce access to vault/consul clusters (CE) (#…
…18521) In Nomad Enterprise, namespace rules can control access to Vault and Consul clusters. Add job endpoint mutating and validating hooks for both Vault and Consul so that ENT can enforce these namespace rules. This changeset includes the stub behaviors for CE. Ref: hashicorp/nomad-enterprise#1234
- Loading branch information
Showing
9 changed files
with
243 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
package nomad | ||
|
||
// jobConsulHook is a job registration admission controller for Consul | ||
// configuration in Consul, Service, and Template blocks | ||
type jobConsulHook struct { | ||
srv *Server | ||
} | ||
|
||
func (jobConsulHook) Name() string { | ||
return "consul" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
//go:build !ent | ||
// +build !ent | ||
|
||
package nomad | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/hashicorp/nomad/nomad/structs" | ||
) | ||
|
||
func (h jobConsulHook) Validate(job *structs.Job) ([]error, error) { | ||
|
||
for _, group := range job.TaskGroups { | ||
if group.Consul != nil { | ||
if err := h.validateCluster(group.Consul.Cluster); err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
for _, service := range group.Services { | ||
if service.Provider == structs.ServiceProviderConsul { | ||
if err := h.validateCluster(service.Cluster); err != nil { | ||
return nil, err | ||
} | ||
} | ||
} | ||
|
||
for _, task := range group.Tasks { | ||
for _, service := range task.Services { | ||
if service.Provider == structs.ServiceProviderConsul { | ||
if err := h.validateCluster(service.Cluster); err != nil { | ||
return nil, err | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return nil, nil | ||
} | ||
|
||
func (h jobConsulHook) validateCluster(name string) error { | ||
if name != "default" { | ||
return errors.New("non-default Consul cluster requires Nomad Enterprise") | ||
} | ||
return nil | ||
} | ||
|
||
// Mutate ensures that the job's Consul cluster has been configured to be the | ||
// default Consul cluster if unset | ||
func (j jobConsulHook) Mutate(job *structs.Job) (*structs.Job, []error, error) { | ||
for _, group := range job.TaskGroups { | ||
if group.Consul != nil && group.Consul.Cluster == "" { | ||
group.Consul.Cluster = "default" | ||
} | ||
|
||
for _, service := range group.Services { | ||
if service.Provider == structs.ServiceProviderConsul && service.Cluster == "" { | ||
service.Cluster = "default" | ||
} | ||
} | ||
|
||
for _, task := range group.Tasks { | ||
for _, service := range task.Services { | ||
if service.Provider == structs.ServiceProviderConsul && service.Cluster == "" { | ||
service.Cluster = "default" | ||
} | ||
} | ||
} | ||
} | ||
|
||
return job, nil, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
//go:build !ent | ||
// +build !ent | ||
|
||
package nomad | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/nomad/ci" | ||
"github.com/hashicorp/nomad/nomad/mock" | ||
"github.com/hashicorp/nomad/nomad/structs" | ||
"github.com/hashicorp/nomad/testutil" | ||
"github.com/shoenig/test" | ||
"github.com/shoenig/test/must" | ||
) | ||
|
||
func TestJobEndpointHook_ConsulCE(t *testing.T) { | ||
ci.Parallel(t) | ||
|
||
srv, cleanup := TestServer(t, func(c *Config) { | ||
c.NumSchedulers = 0 | ||
}) | ||
t.Cleanup(cleanup) | ||
testutil.WaitForLeader(t, srv.RPC) | ||
|
||
job := mock.Job() | ||
|
||
// create two group-level services and assign to clusters | ||
taskSvc := job.TaskGroups[0].Tasks[0].Services[0] | ||
taskSvc.Provider = structs.ServiceProviderConsul | ||
taskSvc.Cluster = "nondefault" | ||
job.TaskGroups[0].Tasks[0].Services = []*structs.Service{taskSvc} | ||
|
||
job.TaskGroups[0].Services = append(job.TaskGroups[0].Services, taskSvc.Copy()) | ||
job.TaskGroups[0].Services = append(job.TaskGroups[0].Services, taskSvc.Copy()) | ||
job.TaskGroups[0].Services[0].Cluster = "" | ||
job.TaskGroups[0].Services[1].Cluster = "infra" | ||
|
||
hook := jobConsulHook{srv} | ||
|
||
_, _, err := hook.Mutate(job) | ||
|
||
must.NoError(t, err) | ||
test.Eq(t, "default", job.TaskGroups[0].Services[0].Cluster) | ||
test.Eq(t, "infra", job.TaskGroups[0].Services[1].Cluster) | ||
test.Eq(t, "nondefault", job.TaskGroups[0].Tasks[0].Services[0].Cluster) | ||
|
||
_, err = hook.Validate(job) | ||
must.EqError(t, err, "non-default Consul cluster requires Nomad Enterprise") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
//go:build !ent | ||
// +build !ent | ||
|
||
package nomad | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/nomad/ci" | ||
"github.com/hashicorp/nomad/nomad/mock" | ||
"github.com/hashicorp/nomad/nomad/structs" | ||
"github.com/hashicorp/nomad/testutil" | ||
"github.com/shoenig/test/must" | ||
) | ||
|
||
func TestJobEndpointHook_VaultCE(t *testing.T) { | ||
ci.Parallel(t) | ||
|
||
srv, cleanup := TestServer(t, func(c *Config) { | ||
c.NumSchedulers = 0 | ||
}) | ||
t.Cleanup(cleanup) | ||
testutil.WaitForLeader(t, srv.RPC) | ||
|
||
job := mock.Job() | ||
|
||
// create two different Vault blocks and assign to clusters | ||
job.TaskGroups[0].Tasks = append(job.TaskGroups[0].Tasks, job.TaskGroups[0].Tasks[0].Copy()) | ||
job.TaskGroups[0].Tasks[0].Vault = &structs.Vault{Cluster: "default"} | ||
job.TaskGroups[0].Tasks[1].Name = "web2" | ||
job.TaskGroups[0].Tasks[1].Vault = &structs.Vault{Cluster: "infra"} | ||
|
||
hook := jobVaultHook{srv} | ||
_, _, err := hook.Mutate(job) | ||
must.NoError(t, err) | ||
must.Eq(t, "default", job.TaskGroups[0].Tasks[0].Vault.Cluster) | ||
must.Eq(t, "infra", job.TaskGroups[0].Tasks[1].Vault.Cluster) | ||
|
||
// skipping over the rest of Validate b/c it requires an actual | ||
// Vault cluster | ||
err = hook.validateClustersForNamespace(job, job.Vault()) | ||
must.EqError(t, err, "non-default Vault cluster requires Nomad Enterprise") | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters