github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/command/acl_role.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  	"strings"
     7  
     8  	"github.com/hashicorp/nomad/api"
     9  	"github.com/mitchellh/cli"
    10  )
    11  
    12  // Ensure ACLRoleCommand satisfies the cli.Command interface.
    13  var _ cli.Command = &ACLRoleCommand{}
    14  
    15  // ACLRoleCommand implements cli.Command.
    16  type ACLRoleCommand struct {
    17  	Meta
    18  }
    19  
    20  // Help satisfies the cli.Command Help function.
    21  func (a *ACLRoleCommand) Help() string {
    22  	helpText := `
    23  Usage: nomad acl role <subcommand> [options] [args]
    24  
    25    This command groups subcommands for interacting with ACL roles. Nomad's ACL
    26    system can be used to control access to data and APIs. ACL roles are
    27    associated with one or more ACL policies which grant specific capabilities.
    28    For a full guide see: https://www.nomadproject.io/guides/acl.html
    29  
    30    Create an ACL role:
    31  
    32        $ nomad acl role create -name="name" -policy-name="policy-name"
    33  
    34    List all ACL roles:
    35  
    36        $ nomad acl role list
    37  
    38    Lookup a specific ACL role:
    39  
    40        $ nomad acl role info <acl_role_id>
    41  
    42    Update an ACL role:
    43  
    44        $ nomad acl role update -name="updated-name" <acl_role_id>
    45  
    46    Delete an ACL role:
    47  
    48        $ nomad acl role delete <acl_role_id>
    49  
    50    Please see the individual subcommand help for detailed usage information.
    51  `
    52  	return strings.TrimSpace(helpText)
    53  }
    54  
    55  // Synopsis satisfies the cli.Command Synopsis function.
    56  func (a *ACLRoleCommand) Synopsis() string { return "Interact with ACL roles" }
    57  
    58  // Name returns the name of this command.
    59  func (a *ACLRoleCommand) Name() string { return "acl role" }
    60  
    61  // Run satisfies the cli.Command Run function.
    62  func (a *ACLRoleCommand) Run(_ []string) int { return cli.RunResultHelp }
    63  
    64  // formatACLRole formats and converts the ACL role API object into a string KV
    65  // representation suitable for console output.
    66  func formatACLRole(aclRole *api.ACLRole) string {
    67  	return formatKV([]string{
    68  		fmt.Sprintf("ID|%s", aclRole.ID),
    69  		fmt.Sprintf("Name|%s", aclRole.Name),
    70  		fmt.Sprintf("Description|%s", aclRole.Description),
    71  		fmt.Sprintf("Policies|%s", strings.Join(aclRolePolicyLinkToStringList(aclRole.Policies), ",")),
    72  		fmt.Sprintf("Create Index|%d", aclRole.CreateIndex),
    73  		fmt.Sprintf("Modify Index|%d", aclRole.ModifyIndex),
    74  	})
    75  }
    76  
    77  // aclRolePolicyLinkToStringList converts an array of ACL role policy links to
    78  // an array of string policy names. The returned array will be sorted.
    79  func aclRolePolicyLinkToStringList(policyLinks []*api.ACLRolePolicyLink) []string {
    80  	policies := make([]string, len(policyLinks))
    81  	for i, policy := range policyLinks {
    82  		policies[i] = policy.Name
    83  	}
    84  	sort.Strings(policies)
    85  	return policies
    86  }
    87  
    88  // aclRolePolicyNamesToPolicyLinks takes a list of policy names as a string
    89  // array and converts this to an array of ACL role policy links. Any duplicate
    90  // names are removed.
    91  func aclRolePolicyNamesToPolicyLinks(policyNames []string) []*api.ACLRolePolicyLink {
    92  	var policyLinks []*api.ACLRolePolicyLink
    93  	keys := make(map[string]struct{})
    94  
    95  	for _, policyName := range policyNames {
    96  		if _, ok := keys[policyName]; !ok {
    97  			policyLinks = append(policyLinks, &api.ACLRolePolicyLink{Name: policyName})
    98  			keys[policyName] = struct{}{}
    99  		}
   100  	}
   101  	return policyLinks
   102  }