mirror of
https://github.com/kemko/nomad.git
synced 2026-01-06 02:15:43 +03:00
Node.UpdateDrain ACL enforcement
This commit is contained in:
@@ -386,6 +386,13 @@ func (n *Node) UpdateDrain(args *structs.NodeUpdateDrainRequest,
|
||||
}
|
||||
defer metrics.MeasureSince([]string{"nomad", "client", "update_drain"}, time.Now())
|
||||
|
||||
// Check node write permissions
|
||||
if aclObj, err := n.srv.resolveToken(args.SecretID); err != nil {
|
||||
return err
|
||||
} else if aclObj != nil && !aclObj.AllowNodeWrite() {
|
||||
return structs.ErrPermissionDenied
|
||||
}
|
||||
|
||||
// Verify the arguments
|
||||
if args.NodeID == "" {
|
||||
return fmt.Errorf("missing node ID for drain update")
|
||||
|
||||
@@ -9,10 +9,12 @@ import (
|
||||
|
||||
memdb "github.com/hashicorp/go-memdb"
|
||||
"github.com/hashicorp/net-rpc-msgpackrpc"
|
||||
"github.com/hashicorp/nomad/acl"
|
||||
"github.com/hashicorp/nomad/nomad/mock"
|
||||
"github.com/hashicorp/nomad/nomad/structs"
|
||||
"github.com/hashicorp/nomad/testutil"
|
||||
vapi "github.com/hashicorp/vault/api"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestClientEndpoint_Register(t *testing.T) {
|
||||
@@ -656,6 +658,59 @@ func TestClientEndpoint_UpdateDrain(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestClientEndpoint_UpdateDrain_ACL(t *testing.T) {
|
||||
t.Parallel()
|
||||
s1, root := testACLServer(t, nil)
|
||||
defer s1.Shutdown()
|
||||
codec := rpcClient(t, s1)
|
||||
testutil.WaitForLeader(t, s1.RPC)
|
||||
assert := assert.New(t)
|
||||
|
||||
// Create the alloc
|
||||
node := mock.Node()
|
||||
state := s1.fsm.State()
|
||||
|
||||
assert.Nil(state.UpsertNode(1, node), "UpsertNode")
|
||||
|
||||
// Create the namespace policy and tokens
|
||||
validToken := CreatePolicyAndToken(t, state, 1001, "test-valid", NodePolicy(acl.PolicyWrite))
|
||||
invalidToken := CreatePolicyAndToken(t, state, 1003, "test-invalid", NodePolicy(acl.PolicyRead))
|
||||
|
||||
// Update the status without a token and expect failure
|
||||
dereg := &structs.NodeUpdateDrainRequest{
|
||||
NodeID: node.ID,
|
||||
Drain: true,
|
||||
WriteRequest: structs.WriteRequest{Region: "global"},
|
||||
}
|
||||
{
|
||||
var resp structs.NodeDrainUpdateResponse
|
||||
assert.NotNil(msgpackrpc.CallWithCodec(codec, "Node.UpdateDrain", dereg, &resp), "RPC")
|
||||
}
|
||||
|
||||
// Try with a valid token
|
||||
dereg.SecretID = validToken.SecretID
|
||||
{
|
||||
var resp structs.NodeDrainUpdateResponse
|
||||
assert.Nil(msgpackrpc.CallWithCodec(codec, "Node.UpdateDrain", dereg, &resp), "RPC")
|
||||
}
|
||||
|
||||
// Try with a invalid token
|
||||
dereg.SecretID = invalidToken.SecretID
|
||||
{
|
||||
var resp structs.NodeDrainUpdateResponse
|
||||
err := msgpackrpc.CallWithCodec(codec, "Node.UpdateDrain", dereg, &resp)
|
||||
assert.NotNil(err, "RPC")
|
||||
assert.Equal(err.Error(), structs.ErrPermissionDenied.Error())
|
||||
}
|
||||
|
||||
// Try with a root token
|
||||
dereg.SecretID = root.SecretID
|
||||
{
|
||||
var resp structs.NodeDrainUpdateResponse
|
||||
assert.Nil(msgpackrpc.CallWithCodec(codec, "Node.UpdateDrain", dereg, &resp), "RPC")
|
||||
}
|
||||
}
|
||||
|
||||
// This test ensures that Nomad marks client state of allocations which are in
|
||||
// pending/running state to lost when a node is marked as down.
|
||||
func TestClientEndpoint_Drain_Down(t *testing.T) {
|
||||
|
||||
@@ -580,9 +580,9 @@ The table below shows this endpoint's support for
|
||||
[blocking queries](/api/index.html#blocking-queries) and
|
||||
[required ACLs](/api/index.html#acls).
|
||||
|
||||
| Blocking Queries | ACL Required |
|
||||
| ---------------- | ------------ |
|
||||
| `NO` | `none` |
|
||||
| Blocking Queries | ACL Required |
|
||||
| ---------------- | ------------------ |
|
||||
| `NO` | `node:write` |
|
||||
|
||||
### Parameters
|
||||
|
||||
|
||||
Reference in New Issue
Block a user