2017-09-11 17:46:17 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-06-03 11:37:24 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2017-09-11 17:46:17 +00:00
|
|
|
"strings"
|
2022-07-20 08:06:23 +00:00
|
|
|
"time"
|
2017-09-11 17:46:17 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/api"
|
|
|
|
"github.com/posener/complete"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ACLBootstrapCommand struct {
|
|
|
|
Meta
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ACLBootstrapCommand) Help() string {
|
|
|
|
helpText := `
|
|
|
|
Usage: nomad acl bootstrap [options]
|
|
|
|
|
2017-10-13 21:36:02 +00:00
|
|
|
Bootstrap is used to bootstrap the ACL system and get an initial token.
|
2017-09-11 17:46:17 +00:00
|
|
|
|
|
|
|
General Options:
|
|
|
|
|
2020-11-19 16:15:23 +00:00
|
|
|
` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace) + `
|
2017-09-11 17:46:17 +00:00
|
|
|
|
2021-10-29 07:00:50 +00:00
|
|
|
Bootstrap Options:
|
|
|
|
|
|
|
|
-json
|
|
|
|
Output the bootstrap response in JSON format.
|
|
|
|
|
|
|
|
-t
|
|
|
|
Format and display the bootstrap response using a Go template.
|
|
|
|
|
2017-09-11 17:46:17 +00:00
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ACLBootstrapCommand) AutocompleteFlags() complete.Flags {
|
|
|
|
return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
|
2021-10-29 07:00:50 +00:00
|
|
|
complete.Flags{
|
|
|
|
"-json": complete.PredictNothing,
|
|
|
|
"-t": complete.PredictAnything,
|
|
|
|
})
|
2017-09-11 17:46:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ACLBootstrapCommand) AutocompleteArgs() complete.Predictor {
|
|
|
|
return complete.PredictNothing
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ACLBootstrapCommand) Synopsis() string {
|
|
|
|
return "Bootstrap the ACL system for initial token"
|
|
|
|
}
|
|
|
|
|
2018-04-18 16:02:11 +00:00
|
|
|
func (c *ACLBootstrapCommand) Name() string { return "acl bootstrap" }
|
|
|
|
|
2017-09-11 17:46:17 +00:00
|
|
|
func (c *ACLBootstrapCommand) Run(args []string) int {
|
2021-10-29 07:00:50 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
json bool
|
|
|
|
tmpl string
|
2022-06-03 11:37:24 +00:00
|
|
|
file string
|
2021-10-29 07:00:50 +00:00
|
|
|
)
|
|
|
|
|
2018-04-18 16:02:11 +00:00
|
|
|
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
|
2017-09-11 17:46:17 +00:00
|
|
|
flags.Usage = func() { c.Ui.Output(c.Help()) }
|
2021-10-29 07:00:50 +00:00
|
|
|
flags.BoolVar(&json, "json", false, "")
|
|
|
|
flags.StringVar(&tmpl, "t", "", "")
|
2017-09-11 17:46:17 +00:00
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that we got no arguments
|
|
|
|
args = flags.Args()
|
2022-06-03 11:37:24 +00:00
|
|
|
if l := len(args); l < 0 || l > 1 {
|
|
|
|
c.Ui.Error("This command takes up to one argument")
|
2018-04-18 16:02:11 +00:00
|
|
|
c.Ui.Error(commandErrorText(c))
|
2017-09-11 17:46:17 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2022-06-03 11:37:24 +00:00
|
|
|
var terminalToken []byte
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if len(args) == 1 {
|
|
|
|
switch args[0] {
|
|
|
|
case "":
|
|
|
|
terminalToken = []byte{}
|
|
|
|
case "-":
|
|
|
|
terminalToken, err = ioutil.ReadAll(os.Stdin)
|
|
|
|
default:
|
|
|
|
file = args[0]
|
|
|
|
terminalToken, err = ioutil.ReadFile(file)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error reading provided token: %v", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove newline from the token if it was passed by stdin
|
|
|
|
boottoken := strings.TrimSuffix(string(terminalToken), "\n")
|
|
|
|
|
2017-09-11 17:46:17 +00:00
|
|
|
// Get the HTTP client
|
|
|
|
client, err := c.Meta.Client()
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the bootstrap token
|
2022-06-03 11:37:24 +00:00
|
|
|
token, _, err := client.ACLTokens().BootstrapOpts(boottoken, nil)
|
2017-09-11 17:46:17 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error bootstrapping: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2021-10-29 07:00:50 +00:00
|
|
|
if json || len(tmpl) > 0 {
|
|
|
|
out, err := Format(json, tmpl, token)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Ui.Output(out)
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2017-09-11 17:46:17 +00:00
|
|
|
// Format the output
|
|
|
|
c.Ui.Output(formatKVACLToken(token))
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2017-09-17 05:11:32 +00:00
|
|
|
// formatKVPolicy returns a K/V formatted policy
|
|
|
|
func formatKVPolicy(policy *api.ACLPolicy) string {
|
|
|
|
output := []string{
|
|
|
|
fmt.Sprintf("Name|%s", policy.Name),
|
|
|
|
fmt.Sprintf("Description|%s", policy.Description),
|
|
|
|
fmt.Sprintf("Rules|%s", policy.Rules),
|
2017-09-17 13:47:18 +00:00
|
|
|
fmt.Sprintf("CreateIndex|%v", policy.CreateIndex),
|
|
|
|
fmt.Sprintf("ModifyIndex|%v", policy.ModifyIndex),
|
2017-09-17 05:11:32 +00:00
|
|
|
}
|
|
|
|
return formatKV(output)
|
|
|
|
}
|
|
|
|
|
2017-09-11 17:46:17 +00:00
|
|
|
// formatKVACLToken returns a K/V formatted ACL token
|
|
|
|
func formatKVACLToken(token *api.ACLToken) string {
|
|
|
|
// Add the fixed preamble
|
|
|
|
output := []string{
|
|
|
|
fmt.Sprintf("Accessor ID|%s", token.AccessorID),
|
|
|
|
fmt.Sprintf("Secret ID|%s", token.SecretID),
|
|
|
|
fmt.Sprintf("Name|%s", token.Name),
|
|
|
|
fmt.Sprintf("Type|%s", token.Type),
|
|
|
|
fmt.Sprintf("Global|%v", token.Global),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Special case the policy output
|
|
|
|
if token.Type == "management" {
|
|
|
|
output = append(output, "Policies|n/a")
|
|
|
|
} else {
|
|
|
|
output = append(output, fmt.Sprintf("Policies|%v", token.Policies))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the generic output
|
|
|
|
output = append(output,
|
|
|
|
fmt.Sprintf("Create Time|%v", token.CreateTime),
|
2022-07-20 08:06:23 +00:00
|
|
|
fmt.Sprintf("Expiry Time |%s", expiryTimeString(token.ExpirationTime)),
|
2017-09-11 17:46:17 +00:00
|
|
|
fmt.Sprintf("Create Index|%d", token.CreateIndex),
|
|
|
|
fmt.Sprintf("Modify Index|%d", token.ModifyIndex),
|
|
|
|
)
|
|
|
|
return formatKV(output)
|
|
|
|
}
|
2022-07-20 08:06:23 +00:00
|
|
|
|
|
|
|
func expiryTimeString(t *time.Time) string {
|
|
|
|
if t == nil || t.IsZero() {
|
|
|
|
return "<never>"
|
|
|
|
}
|
|
|
|
return t.String()
|
|
|
|
}
|