-
Notifications
You must be signed in to change notification settings - Fork 492
/
remove_sshkeys.go
96 lines (82 loc) · 2.49 KB
/
remove_sshkeys.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package commands
import (
"errors"
"fmt"
"github.com/juju/cmd/v3"
jujucmd "github.com/juju/juju/cmd"
"github.com/juju/juju/cmd/juju/block"
"github.com/juju/juju/cmd/modelcmd"
)
var usageRemoveSSHKeySummary = `
Removes a public SSH key (or keys) from a model.`[1:]
var usageRemoveSSHKeyDetails = `
Juju maintains a per-model cache of public SSH keys which it copies to
each unit. This command will remove a specified key (or space separated
list of keys) from the model cache and all current units deployed in that
model. The keys to be removed may be specified by the key's fingerprint,
or by the text label associated with them. Invalid keys in the model cache
can be removed by specifying the key verbatim.
`[1:]
const usageRemoveSSHKeyExamples = `
juju remove-ssh-key ubuntu@ubuntu
juju remove-ssh-key 45:7f:33:2c:10:4e:6c:14:e3:a1:a4:c8:b2:e1:34:b4
juju remove-ssh-key bob@ubuntu carol@ubuntu
`
// NewRemoveKeysCommand is used to delete ssk keys for a user.
func NewRemoveKeysCommand() cmd.Command {
return modelcmd.Wrap(&removeKeysCommand{})
}
// removeKeysCommand is used to delete authorised ssh keys for a user.
type removeKeysCommand struct {
SSHKeysBase
user string
keyIds []string
}
// Info implements Command.Info.
func (c *removeKeysCommand) Info() *cmd.Info {
return jujucmd.Info(&cmd.Info{
Name: "remove-ssh-key",
Args: "<ssh key id> ...",
Purpose: usageRemoveSSHKeySummary,
Doc: usageRemoveSSHKeyDetails,
Examples: usageRemoveSSHKeyExamples,
SeeAlso: []string{
"ssh-keys",
"add-ssh-key",
"import-ssh-key",
},
})
}
// Init implements Command.Init.
func (c *removeKeysCommand) Init(args []string) error {
switch len(args) {
case 0:
return errors.New("no ssh key id specified")
default:
c.keyIds = args
}
return nil
}
// Run implements Command.Run.
func (c *removeKeysCommand) Run(context *cmd.Context) error {
client, err := c.NewKeyManagerClient()
if err != nil {
return err
}
defer client.Close()
// TODO(alexisb) - currently keys are global which is not ideal.
// keymanager needs to be updated to allow keys per user
c.user = "admin"
results, err := client.DeleteKeys(c.user, c.keyIds...)
if err != nil {
return block.ProcessBlockedError(err, block.BlockChange)
}
for i, result := range results {
if result.Error != nil {
fmt.Fprintf(context.Stderr, "cannot remove key id %q: %v\n", c.keyIds[i], result.Error)
}
}
return nil
}