/
import_sshkeys.go
116 lines (95 loc) · 3.02 KB
/
import_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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package commands
import (
"fmt"
"github.com/juju/cmd/v3"
"github.com/juju/errors"
jujucmd "github.com/juju/juju/cmd"
"github.com/juju/juju/cmd/juju/block"
"github.com/juju/juju/cmd/modelcmd"
)
var usageImportSSHKeySummary = `
Adds a public SSH key from a trusted identity source to a model.`[1:]
var usageImportSSHKeyDetails = `
Juju can add SSH keys to its cache from reliable public sources (currently
Launchpad and GitHub), allowing those users SSH access to Juju machines.
The user identity supplied is the username on the respective service given by
'lp:' or 'gh:'.
If the user has multiple keys on the service, all the keys will be added.
Once the keys are imported, they can be viewed with the `[1:] + "`juju ssh-keys`" + `
command, where comments will indicate which ones were imported in
this way.
An alternative to this command is the more manual ` + "`juju add-ssh-key`" + `.
`
const usageImportSSHKeyExamples = `
Import all public keys associated with user account 'phamilton' on the
GitHub service:
juju import-ssh-key gh:phamilton
Multiple identities may be specified in a space delimited list:
juju import-ssh-key gh:rheinlein lp:iasmiov gh:hharrison
`
// NewImportKeysCommand is used to add new authorized ssh keys to a model.
func NewImportKeysCommand() cmd.Command {
return modelcmd.Wrap(&importKeysCommand{})
}
// importKeysCommand is used to import authorized ssh keys to a model.
type importKeysCommand struct {
SSHKeysBase
user string
sshKeyIds []string
}
// Info implements Command.Info.
func (c *importKeysCommand) Info() *cmd.Info {
return jujucmd.Info(&cmd.Info{
Name: "import-ssh-key",
Args: "<lp|gh>:<user identity> ...",
Purpose: usageImportSSHKeySummary,
Doc: usageImportSSHKeyDetails,
Examples: usageImportSSHKeyExamples,
SeeAlso: []string{
"add-ssh-key",
"ssh-keys",
},
})
}
// Init implements Command.Init.
func (c *importKeysCommand) Init(args []string) error {
if len(args) == 0 {
return errors.New("no ssh key id specified")
}
c.sshKeyIds = args
for _, k := range c.sshKeyIds {
if len(k) < 3 {
return errors.NotValidf("%q key ID", k)
}
switch k[:3] {
case "lp:", "gh:":
default:
return errors.NewNotSupported(nil,
fmt.Sprintf("prefix in Key ID %q not supported, only lp: and gh: are allowed", k))
}
}
return nil
}
// Run implemetns Command.Run.
func (c *importKeysCommand) 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.ImportKeys(c.user, c.sshKeyIds...)
if err != nil {
return block.ProcessBlockedError(err, block.BlockChange)
}
for i, result := range results {
if result.Error != nil {
fmt.Fprintf(context.Stderr, "cannot import key id %q: %v\n", c.sshKeyIds[i], result.Error)
}
}
return nil
}