github.com/anuvu/nomad@v0.8.7-atom1/nomad/mock/acl.go (about)

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