github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/nomad/mock/acl.go (about) 1 package mock 2 3 import ( 4 "fmt" 5 "strconv" 6 "strings" 7 8 testing "github.com/mitchellh/go-testing-interface" 9 10 "github.com/hashicorp/nomad/nomad/structs" 11 "github.com/stretchr/testify/assert" 12 ) 13 14 // StateStore defines the methods required from state.StateStore but avoids a 15 // circular dependency. 16 type StateStore interface { 17 UpsertACLPolicies(index uint64, policies []*structs.ACLPolicy) error 18 UpsertACLTokens(index uint64, tokens []*structs.ACLToken) error 19 } 20 21 // NamespacePolicy is a helper for generating the policy hcl for a given 22 // namespace. Either policy or capabilities may be nil but not both. 23 func NamespacePolicy(namespace string, policy string, capabilities []string) string { 24 policyHCL := fmt.Sprintf("namespace %q {", namespace) 25 if policy != "" { 26 policyHCL += fmt.Sprintf("\n\tpolicy = %q", policy) 27 } 28 if len(capabilities) != 0 { 29 for i, s := range capabilities { 30 if !strings.HasPrefix(s, "\"") { 31 capabilities[i] = strconv.Quote(s) 32 } 33 } 34 35 policyHCL += fmt.Sprintf("\n\tcapabilities = [%v]", strings.Join(capabilities, ",")) 36 } 37 policyHCL += "\n}" 38 return policyHCL 39 } 40 41 // HostVolumePolicy is a helper for generating the policy hcl for a given 42 // host-volume. Either policy or capabilities may be nil but not both. 43 func HostVolumePolicy(vol string, policy string, capabilities []string) string { 44 policyHCL := fmt.Sprintf("host_volume %q {", vol) 45 if policy != "" { 46 policyHCL += fmt.Sprintf("\n\tpolicy = %q", policy) 47 } 48 if len(capabilities) != 0 { 49 for i, s := range capabilities { 50 if !strings.HasPrefix(s, "\"") { 51 capabilities[i] = strconv.Quote(s) 52 } 53 } 54 55 policyHCL += fmt.Sprintf("\n\tcapabilities = [%v]", strings.Join(capabilities, ",")) 56 } 57 policyHCL += "\n}" 58 return policyHCL 59 } 60 61 // AgentPolicy is a helper for generating the hcl for a given agent policy. 62 func AgentPolicy(policy string) string { 63 return fmt.Sprintf("agent {\n\tpolicy = %q\n}\n", policy) 64 } 65 66 // NodePolicy is a helper for generating the hcl for a given node policy. 67 func NodePolicy(policy string) string { 68 return fmt.Sprintf("node {\n\tpolicy = %q\n}\n", policy) 69 } 70 71 // QuotaPolicy is a helper for generating the hcl for a given quota policy. 72 func QuotaPolicy(policy string) string { 73 return fmt.Sprintf("quota {\n\tpolicy = %q\n}\n", policy) 74 } 75 76 // PluginPolicy is a helper for generating the hcl for a given plugin policy. 77 func PluginPolicy(policy string) string { 78 return fmt.Sprintf("plugin {\n\tpolicy = %q\n}\n", policy) 79 } 80 81 // CreatePolicy creates a policy with the given name and rule. 82 func CreatePolicy(t testing.T, state StateStore, index uint64, name, rule string) { 83 t.Helper() 84 85 // Create the ACLPolicy 86 policy := &structs.ACLPolicy{ 87 Name: name, 88 Rules: rule, 89 } 90 policy.SetHash() 91 assert.Nil(t, state.UpsertACLPolicies(index, []*structs.ACLPolicy{policy})) 92 } 93 94 // CreateToken creates a local, client token for the given policies 95 func CreateToken(t testing.T, state StateStore, index uint64, policies []string) *structs.ACLToken { 96 t.Helper() 97 98 // Create the ACLToken 99 token := ACLToken() 100 token.Policies = policies 101 token.SetHash() 102 assert.Nil(t, state.UpsertACLTokens(index, []*structs.ACLToken{token})) 103 return token 104 } 105 106 // CreatePolicyAndToken creates a policy and then returns a token configured for 107 // just that policy. CreatePolicyAndToken uses the given index and index+1. 108 func CreatePolicyAndToken(t testing.T, state StateStore, index uint64, name, rule string) *structs.ACLToken { 109 CreatePolicy(t, state, index, name, rule) 110 return CreateToken(t, state, index+1, []string{name}) 111 }