github.com/hernad/nomad@v1.6.112/e2e/e2eutil/acl.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package e2eutil
     5  
     6  import (
     7  	"fmt"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/hernad/nomad/api"
    12  	"github.com/hernad/nomad/helper/uuid"
    13  	"github.com/shoenig/test"
    14  	"github.com/shoenig/test/must"
    15  )
    16  
    17  // ApplyJobPolicy applies an ACL job policy or noops if ACLs are disabled.
    18  // Registers a cleanup function to delete the policy.
    19  func ApplyJobPolicy(t *testing.T, nomad *api.Client, ns, j, g, task, rules string) *api.ACLPolicy {
    20  
    21  	policy := &api.ACLPolicy{
    22  		Name: j + uuid.Short(),
    23  		Description: fmt.Sprintf("Policy for test=%s ns=%s job=%s group=%s task=%s rules=%s",
    24  			t.Name(), ns, j, g, task, rules),
    25  		Rules: rules,
    26  		JobACL: &api.JobACL{
    27  			Namespace: ns,
    28  			JobID:     j,
    29  			Group:     g,
    30  			Task:      task,
    31  		},
    32  	}
    33  
    34  	wm, err := nomad.ACLPolicies().Upsert(policy, nil)
    35  	if err != nil {
    36  		if strings.Contains(err.Error(), "ACL support disabled") {
    37  			t.Logf("ACL support disabled. Skipping ApplyJobPolicy(t, c, %q, %q, %q, %q, %q)",
    38  				ns, j, g, task, rules)
    39  			return nil
    40  		}
    41  		must.NoError(t, err)
    42  	}
    43  
    44  	t.Cleanup(func() {
    45  		_, err := nomad.ACLPolicies().Delete(policy.Name, nil)
    46  		test.NoError(t, err)
    47  	})
    48  
    49  	policy.CreateIndex = wm.LastIndex
    50  	policy.ModifyIndex = wm.LastIndex
    51  	return policy
    52  }