github.com/hernad/nomad@v1.6.112/command/acl_role.go (about)

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