github.com/s-matyukevich/consul@v1.4.5/command/acl/agenttokens/agent_tokens.go (about) 1 package agenttokens 2 3 import ( 4 "flag" 5 "fmt" 6 "io" 7 8 "github.com/hashicorp/consul/command/flags" 9 "github.com/hashicorp/consul/command/helpers" 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 testStdin io.Reader 26 } 27 28 func (c *cmd) init() { 29 c.flags = flag.NewFlagSet("", flag.ContinueOnError) 30 c.http = &flags.HTTPFlags{} 31 flags.Merge(c.flags, c.http.ClientFlags()) 32 flags.Merge(c.flags, c.http.ServerFlags()) 33 c.help = flags.Usage(help, c.flags) 34 } 35 func (c *cmd) Run(args []string) int { 36 if err := c.flags.Parse(args); err != nil { 37 return 1 38 } 39 40 tokenType, token, err := c.dataFromArgs(c.flags.Args()) 41 if err != nil { 42 c.UI.Error(fmt.Sprintf("Error! %s", err)) 43 return 1 44 } 45 46 client, err := c.http.APIClient() 47 if err != nil { 48 c.UI.Error(fmt.Sprintf("Error connecting to Consul Agent: %s", err)) 49 return 1 50 } 51 52 switch tokenType { 53 case "default": 54 _, err = client.Agent().UpdateDefaultACLToken(token, nil) 55 case "agent": 56 _, err = client.Agent().UpdateAgentACLToken(token, nil) 57 case "master": 58 _, err = client.Agent().UpdateAgentMasterACLToken(token, nil) 59 case "replication": 60 _, err = client.Agent().UpdateReplicationACLToken(token, nil) 61 default: 62 c.UI.Error(fmt.Sprintf("Unknown token type")) 63 return 1 64 } 65 66 if err != nil { 67 c.UI.Error(fmt.Sprintf("Failed to set ACL token %q: %v", tokenType, err)) 68 return 1 69 } 70 71 c.UI.Info(fmt.Sprintf("ACL token %q set successfully", tokenType)) 72 return 0 73 } 74 75 func (c *cmd) dataFromArgs(args []string) (string, string, error) { 76 switch len(args) { 77 case 0: 78 return "", "", fmt.Errorf("Missing TYPE and TOKEN arguments") 79 case 1: 80 switch args[0] { 81 case "default", "agent", "master", "replication": 82 return "", "", fmt.Errorf("Missing TOKEN argument") 83 default: 84 return "", "", fmt.Errorf("MISSING TYPE argument") 85 } 86 case 2: 87 data, err := helpers.LoadDataSource(args[1], c.testStdin) 88 if err != nil { 89 return "", "", err 90 } 91 92 return args[0], data, nil 93 default: 94 return "", "", fmt.Errorf("Too many arguments: expected 2 got %d", len(args)) 95 } 96 } 97 98 func (c *cmd) Synopsis() string { 99 return synopsis 100 } 101 102 func (c *cmd) Help() string { 103 return flags.Usage(c.help, nil) 104 } 105 106 const synopsis = "Assign tokens for the Consul Agent's usage" 107 const help = ` 108 Usage: consul acl set-agent-token [options] TYPE TOKEN 109 110 This command will set the corresponding token for the agent to use. 111 Note that the tokens uploaded this way are not persisted and if 112 the agent reloads then the tokens will need to be set again. 113 114 Token Types: 115 116 default The default token is the token that the agent will use for 117 both internal agent operations and operations initiated by 118 the HTTP and DNS interfaces when no specific token is provided. 119 If not set the agent will use the anonymous token. 120 121 agent The token that the agent will use for internal agent operations. 122 If not given then the default token is used for these operations. 123 124 master This sets the token that can be used to access the Agent APIs in 125 the event that the ACL datacenter cannot be reached. 126 127 replication This is the token that the agent will use for replication 128 operations. This token will need to be configured with read access 129 to whatever data is being replicated. 130 131 Example: 132 133 $ consul acl set-agent-token default c4d0f8df-3aba-4ab6-a7a0-35b760dc29a1 134 `