1
// Copyright 2013 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
12
"github.com/juju/juju/cmd/juju/block"
13
"github.com/juju/juju/cmd/modelcmd"
16
var usageAddSSHKeySummary = `
17
Adds a public SSH key to a model.`[1:]
19
var usageAddSSHKeyDetails = `
20
Juju maintains a per-model cache of public SSH keys which it copies to
21
each unit (including units already deployed). By default this includes the
22
key of the user who created the model (assuming it is stored in the
23
default location ~/.ssh/). Additional keys may be added with this command,
24
quoting the entire public key as an argument.
27
juju add-ssh-key "ssh-rsa qYfS5LieM79HIOr535ret6xy
28
AAAAB3NzaC1yc2EAAAADAQA6fgBAAABAQCygc6Rc9XgHdhQqTJ
29
Wsoj+I3xGrOtk21xYtKijnhkGqItAHmrE5+VH6PY1rVIUXhpTg
30
pSkJsHLmhE29OhIpt6yr8vQSOChqYfS5LieM79HIOJEgJEzIqC
31
52rCYXLvr/BVkd6yr4IoM1vpb/n6u9o8v1a0VUGfc/J6tQAcPR
32
ExzjZUVsfjj8HdLtcFq4JLYC41miiJtHw4b3qYu7qm3vh4eCiK
33
1LqLncXnBCJfjj0pADXaL5OQ9dmD3aCbi8KFyOEs3UumPosgmh
34
VCAfjjHObWHwNQ/ZU2KrX1/lv/+lBChx2tJliqQpyYMiA3nrtS
35
jfqQgZfjVF5vz8LESQbGc6+vLcXZ9KQpuYDt joe@ubuntu"
37
For ease of use it is possible to use shell substitution to pass the key
40
juju add-ssh-key "$(cat ~/mykey.pub)"
47
// NewAddKeysCommand is used to add a new ssh key to a model.
48
func NewAddKeysCommand() cmd.Command {
49
return modelcmd.Wrap(&addKeysCommand{})
52
// addKeysCommand is used to add a new authorized ssh key for a user.
53
type addKeysCommand struct {
59
// Info implements Command.Info.
60
func (c *addKeysCommand) Info() *cmd.Info {
63
Args: "<ssh key> ...",
64
Purpose: usageAddSSHKeySummary,
65
Doc: usageAddSSHKeyDetails,
66
Aliases: []string{"add-ssh-keys"},
70
// Init implements Command.Init.
71
func (c *addKeysCommand) Init(args []string) error {
74
return errors.New("no ssh key specified")
81
// Run implements Command.Run.
82
func (c *addKeysCommand) Run(context *cmd.Context) error {
83
client, err := c.NewKeyManagerClient()
88
// TODO(alexisb) - currently keys are global which is not ideal.
89
// keymanager needs to be updated to allow keys per user
91
results, err := client.AddKeys(c.user, c.sshKeys...)
93
return block.ProcessBlockedError(err, block.BlockChange)
95
for i, result := range results {
96
if result.Error != nil {
97
fmt.Fprintf(context.Stderr, "cannot add key %q: %v\n", c.sshKeys[i], result.Error)