Merge pull request #2846 from hashicorp/check-match

Bans check updates for nodes other than top-level reg. updates.
This commit is contained in:
James Phillips 2017-03-29 17:37:23 -07:00 committed by GitHub
commit f126eb56ae
3 changed files with 42 additions and 8 deletions

View file

@ -126,19 +126,21 @@ func (s *StateStore) ensureRegistrationTxn(tx *memdb.Txn, idx uint64, req *struc
}
}
// TODO (slackpad) In Consul 0.8 ban checks that don't have the same
// node as the top-level registration. This is just weird to be able to
// update unrelated nodes' checks from in here. In 0.7.2 we banned this
// up in the ACL check since that's guarded behind an opt-in flag until
// Consul 0.8.
// Add the checks, if any.
if req.Check != nil {
if req.Check.Node != req.Node {
return fmt.Errorf("check node %q does not match node %q",
req.Check.Node, req.Node)
}
if err := s.ensureCheckTxn(tx, idx, req.Check); err != nil {
return fmt.Errorf("failed inserting check: %s", err)
}
}
for _, check := range req.Checks {
if check.Node != req.Node {
return fmt.Errorf("check node %q does not match node %q",
check.Node, req.Node)
}
if err := s.ensureCheckTxn(tx, idx, check); err != nil {
return fmt.Errorf("failed inserting check: %s", err)
}

View file

@ -171,7 +171,7 @@ func TestStateStore_EnsureRegistration(t *testing.T) {
// Verify that the additional check got registered.
verifyNode()
verifyService()
{
verifyChecks := func() {
idx, out, err := s.NodeChecks(nil, "node1")
if err != nil {
t.Fatalf("err: %s", err)
@ -194,6 +194,38 @@ func TestStateStore_EnsureRegistration(t *testing.T) {
t.Fatalf("bad check returned: %#v", c2)
}
}
verifyChecks()
// Try to register a check for some other node (top-level check).
req.Check = &structs.HealthCheck{
Node: "nope",
CheckID: "check1",
Name: "check",
}
err := s.EnsureRegistration(5, req)
if err == nil || !strings.Contains(err.Error(), "does not match node") {
t.Fatalf("err: %s", err)
}
verifyNode()
verifyService()
verifyChecks()
// Try to register a check for some other node (checks array).
req.Check = nil
req.Checks = structs.HealthChecks{
&structs.HealthCheck{
Node: "nope",
CheckID: "check2",
Name: "check",
},
}
err = s.EnsureRegistration(6, req)
if err == nil || !strings.Contains(err.Error(), "does not match node") {
t.Fatalf("err: %s", err)
}
verifyNode()
verifyService()
verifyChecks()
}
func TestStateStore_EnsureRegistration_Restore(t *testing.T) {