github.com/hhrutter/nomad@v0.6.0-rc2.0.20170723054333-80c4b03f0705/command/keygen.go (about)

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