consul/command/acl/acl_test.go

114 lines
2.2 KiB
Go
Raw Permalink Normal View History

// Copyright (c) HashiCorp, Inc.
[COMPLIANCE] License changes (#18443) * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at <Blog URL>, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * add missing license headers * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
2023-08-11 13:12:13 +00:00
// SPDX-License-Identifier: BUSL-1.1
package acl
import (
"fmt"
"io"
"testing"
"github.com/hashicorp/consul/agent"
"github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/consul/testrpc"
"github.com/stretchr/testify/require"
)
func Test_GetPolicyIDByName_Builtins(t *testing.T) {
t.Parallel()
a := agent.StartTestAgent(t,
agent.TestAgent{
LogOutput: io.Discard,
HCL: `
primary_datacenter = "dc1"
acl {
enabled = true
tokens {
initial_management = "root"
}
}
`,
},
)
defer a.Shutdown()
testrpc.WaitForTestAgent(t, a.RPC, "dc1", testrpc.WithToken("root"))
client := a.Client()
client.AddHeader("X-Consul-Token", "root")
for _, policy := range structs.ACLBuiltinPolicies {
name := fmt.Sprintf("%s policy", policy.Name)
t.Run(name, func(t *testing.T) {
id, err := GetPolicyIDByName(client, policy.Name)
require.NoError(t, err)
require.Equal(t, policy.ID, id)
})
}
}
func Test_GetPolicyIDByName_NotFound(t *testing.T) {
t.Parallel()
a := agent.StartTestAgent(t,
agent.TestAgent{
LogOutput: io.Discard,
HCL: `
primary_datacenter = "dc1"
acl {
enabled = true
tokens {
initial_management = "root"
}
}
`,
},
)
defer a.Shutdown()
testrpc.WaitForTestAgent(t, a.RPC, "dc1", testrpc.WithToken("root"))
client := a.Client()
client.AddHeader("X-Consul-Token", "root")
id, err := GetPolicyIDByName(client, "not_found")
require.Error(t, err)
require.Equal(t, "", id)
}
func Test_GetPolicyIDFromPartial_Builtins(t *testing.T) {
t.Parallel()
a := agent.StartTestAgent(t,
agent.TestAgent{
LogOutput: io.Discard,
HCL: `
primary_datacenter = "dc1"
acl {
enabled = true
tokens {
initial_management = "root"
}
}
`,
},
)
defer a.Shutdown()
testrpc.WaitForTestAgent(t, a.RPC, "dc1", testrpc.WithToken("root"))
client := a.Client()
client.AddHeader("X-Consul-Token", "root")
for _, policy := range structs.ACLBuiltinPolicies {
name := fmt.Sprintf("%s policy", policy.Name)
t.Run(name, func(t *testing.T) {
id, err := GetPolicyIDFromPartial(client, policy.Name)
require.NoError(t, err)
require.Equal(t, policy.ID, id)
})
}
}