github.com/hernad/nomad@v1.6.112/command/operator_gossip_keyring_generate.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package command 5 6 import ( 7 "crypto/rand" 8 "encoding/base64" 9 "fmt" 10 "strings" 11 ) 12 13 // OperatorGossipKeyringGenerateCommand is a Command implementation that 14 // generates an encryption key for use in `nomad agent`. 15 type OperatorGossipKeyringGenerateCommand struct { 16 Meta 17 } 18 19 func (c *OperatorGossipKeyringGenerateCommand) Synopsis() string { 20 return "Generates a new encryption key" 21 } 22 23 func (c *OperatorGossipKeyringGenerateCommand) Help() string { 24 helpText := ` 25 Usage: nomad operator gossip keying generate 26 27 Generates a new 32-byte encryption key that can be used to configure the 28 agent to encrypt traffic. The output of this command is already 29 in the proper format that the agent expects. 30 ` 31 return strings.TrimSpace(helpText) 32 } 33 34 func (c *OperatorGossipKeyringGenerateCommand) Name() string { 35 return "operator gossip keyring generate" 36 } 37 38 func (c *OperatorGossipKeyringGenerateCommand) Run(_ []string) int { 39 key := make([]byte, 32) 40 n, err := rand.Reader.Read(key) 41 if err != nil { 42 c.Ui.Error(fmt.Sprintf("Error reading random data: %s", err)) 43 return 1 44 } 45 if n != 32 { 46 c.Ui.Error("Couldn't read enough entropy. Generate more entropy!") 47 return 1 48 } 49 50 c.Ui.Output(base64.StdEncoding.EncodeToString(key)) 51 return 0 52 }