github.hscsec.cn/hashicorp/consul@v1.4.5/command/acl/token/read/token_read.go (about) 1 package tokenread 2 3 import ( 4 "flag" 5 "fmt" 6 7 "github.com/hashicorp/consul/api" 8 "github.com/hashicorp/consul/command/acl" 9 "github.com/hashicorp/consul/command/flags" 10 "github.com/mitchellh/cli" 11 ) 12 13 func New(ui cli.Ui) *cmd { 14 c := &cmd{UI: ui} 15 c.init() 16 return c 17 } 18 19 type cmd struct { 20 UI cli.Ui 21 flags *flag.FlagSet 22 http *flags.HTTPFlags 23 help string 24 25 tokenID string 26 self bool 27 showMeta bool 28 } 29 30 func (c *cmd) init() { 31 c.flags = flag.NewFlagSet("", flag.ContinueOnError) 32 c.flags.BoolVar(&c.showMeta, "meta", false, "Indicates that token metadata such "+ 33 "as the content hash and Raft indices should be shown for each entry") 34 c.flags.BoolVar(&c.self, "self", false, "Indicates that the current HTTP token "+ 35 "should be read by secret ID instead of expecting a -id option") 36 c.flags.StringVar(&c.tokenID, "id", "", "The Accessor ID of the token to read. "+ 37 "It may be specified as a unique ID prefix but will error if the prefix "+ 38 "matches multiple token Accessor IDs") 39 c.http = &flags.HTTPFlags{} 40 flags.Merge(c.flags, c.http.ClientFlags()) 41 flags.Merge(c.flags, c.http.ServerFlags()) 42 c.help = flags.Usage(help, c.flags) 43 } 44 45 func (c *cmd) Run(args []string) int { 46 if err := c.flags.Parse(args); err != nil { 47 return 1 48 } 49 50 if c.tokenID == "" && !c.self { 51 c.UI.Error(fmt.Sprintf("Must specify the -id parameter")) 52 return 1 53 } 54 55 client, err := c.http.APIClient() 56 if err != nil { 57 c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err)) 58 return 1 59 } 60 61 var token *api.ACLToken 62 if !c.self { 63 tokenID, err := acl.GetTokenIDFromPartial(client, c.tokenID) 64 if err != nil { 65 c.UI.Error(fmt.Sprintf("Error determining token ID: %v", err)) 66 return 1 67 } 68 69 token, _, err = client.ACL().TokenRead(tokenID, nil) 70 if err != nil { 71 c.UI.Error(fmt.Sprintf("Error reading token %q: %v", tokenID, err)) 72 return 1 73 } 74 } else { 75 token, _, err = client.ACL().TokenReadSelf(nil) 76 if err != nil { 77 c.UI.Error(fmt.Sprintf("Error reading token: %v", err)) 78 return 1 79 } 80 } 81 82 acl.PrintToken(token, c.UI, c.showMeta) 83 return 0 84 } 85 86 func (c *cmd) Synopsis() string { 87 return synopsis 88 } 89 90 func (c *cmd) Help() string { 91 return flags.Usage(c.help, nil) 92 } 93 94 const synopsis = "Read an ACL Token" 95 const help = ` 96 Usage: consul acl token read [options] -id TOKENID 97 98 This command will retrieve and print out the details of 99 a single token. 100 101 Using a partial ID: 102 103 $ consul acl token read -id 4be56c77-82 104 105 Using the full ID: 106 107 $ consul acl token read -id 4be56c77-8244-4c7d-b08c-667b8c71baed 108 `