2017-10-04 22:08:10 +00:00
|
|
|
package mock
|
2017-09-11 23:31:37 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-01-31 20:13:57 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2017-09-11 23:31:37 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2017-10-04 22:08:10 +00:00
|
|
|
"github.com/mitchellh/go-testing-interface"
|
2017-09-11 23:31:37 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2017-10-04 22:08:10 +00:00
|
|
|
// StateStore defines the methods required from state.StateStore but avoids a
|
|
|
|
// circular dependency.
|
|
|
|
type StateStore interface {
|
|
|
|
UpsertACLPolicies(index uint64, policies []*structs.ACLPolicy) error
|
|
|
|
UpsertACLTokens(index uint64, tokens []*structs.ACLToken) error
|
|
|
|
}
|
|
|
|
|
2017-09-11 23:31:37 +00:00
|
|
|
// NamespacePolicy is a helper for generating the policy hcl for a given
|
2017-09-26 22:26:33 +00:00
|
|
|
// namepsace. Either policy or capabilities may be nil but not both.
|
2017-09-11 23:31:37 +00:00
|
|
|
func NamespacePolicy(namespace string, policy string, capabilities []string) string {
|
|
|
|
policyHCL := fmt.Sprintf("namespace %q {", namespace)
|
|
|
|
if policy != "" {
|
|
|
|
policyHCL += fmt.Sprintf("\n\tpolicy = %q", policy)
|
|
|
|
}
|
|
|
|
if len(capabilities) != 0 {
|
2018-01-31 20:13:57 +00:00
|
|
|
for i, s := range capabilities {
|
|
|
|
if !strings.HasPrefix(s, "\"") {
|
|
|
|
capabilities[i] = strconv.Quote(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
policyHCL += fmt.Sprintf("\n\tcapabilities = [%v]", strings.Join(capabilities, ","))
|
2017-09-11 23:31:37 +00:00
|
|
|
}
|
|
|
|
policyHCL += "\n}"
|
|
|
|
return policyHCL
|
|
|
|
}
|
|
|
|
|
2017-10-06 19:02:53 +00:00
|
|
|
// AgentPolicy is a helper for generating the hcl for a given agent policy.
|
|
|
|
func AgentPolicy(policy string) string {
|
|
|
|
return fmt.Sprintf("agent {\n\tpolicy = %q\n}\n", policy)
|
|
|
|
}
|
|
|
|
|
2017-09-15 03:33:12 +00:00
|
|
|
// NodePolicy is a helper for generating the hcl for a given node policy.
|
|
|
|
func NodePolicy(policy string) string {
|
|
|
|
return fmt.Sprintf("node {\n\tpolicy = %q\n}\n", policy)
|
|
|
|
}
|
|
|
|
|
2017-10-13 21:36:02 +00:00
|
|
|
// QuotaPolicy is a helper for generating the hcl for a given quota policy.
|
|
|
|
func QuotaPolicy(policy string) string {
|
|
|
|
return fmt.Sprintf("quota {\n\tpolicy = %q\n}\n", policy)
|
|
|
|
}
|
|
|
|
|
2017-09-11 23:31:37 +00:00
|
|
|
// CreatePolicy creates a policy with the given name and rule.
|
2017-10-04 22:08:10 +00:00
|
|
|
func CreatePolicy(t testing.T, state StateStore, index uint64, name, rule string) {
|
2017-09-11 23:31:37 +00:00
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
// Create the ACLPolicy
|
|
|
|
policy := &structs.ACLPolicy{
|
|
|
|
Name: name,
|
|
|
|
Rules: rule,
|
|
|
|
}
|
|
|
|
policy.SetHash()
|
|
|
|
assert.Nil(t, state.UpsertACLPolicies(index, []*structs.ACLPolicy{policy}))
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateToken creates a local, client token for the given policies
|
2017-10-04 22:08:10 +00:00
|
|
|
func CreateToken(t testing.T, state StateStore, index uint64, policies []string) *structs.ACLToken {
|
2017-09-11 23:31:37 +00:00
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
// Create the ACLToken
|
2017-10-04 22:08:10 +00:00
|
|
|
token := ACLToken()
|
2017-09-11 23:31:37 +00:00
|
|
|
token.Policies = policies
|
|
|
|
token.SetHash()
|
|
|
|
assert.Nil(t, state.UpsertACLTokens(index, []*structs.ACLToken{token}))
|
|
|
|
return token
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreatePolicyAndToken creates a policy and then returns a token configured for
|
|
|
|
// just that policy. CreatePolicyAndToken uses the given index and index+1.
|
2017-10-04 22:08:10 +00:00
|
|
|
func CreatePolicyAndToken(t testing.T, state StateStore, index uint64, name, rule string) *structs.ACLToken {
|
2017-09-11 23:31:37 +00:00
|
|
|
CreatePolicy(t, state, index, name, rule)
|
|
|
|
return CreateToken(t, state, index+1, []string{name})
|
|
|
|
}
|