/
showcredential.go
281 lines (242 loc) · 7.64 KB
/
showcredential.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Copyright 2018 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package cloud
import (
"fmt"
"github.com/juju/cmd/v3"
"github.com/juju/errors"
"github.com/juju/gnuflag"
apicloud "github.com/juju/juju/api/client/cloud"
"github.com/juju/juju/cloud"
jujucmd "github.com/juju/juju/cmd"
"github.com/juju/juju/cmd/juju/common"
"github.com/juju/juju/cmd/modelcmd"
"github.com/juju/juju/jujuclient"
"github.com/juju/juju/rpc/params"
)
type showCredentialCommand struct {
modelcmd.OptionalControllerCommand
out cmd.Output
newAPIFunc func() (CredentialContentAPI, error)
CloudName string
CredentialName string
ShowSecrets bool
}
// NewShowCredentialCommand returns a command to show information about
// credentials stored on the controller.
func NewShowCredentialCommand() cmd.Command {
store := jujuclient.NewFileClientStore()
command := &showCredentialCommand{
OptionalControllerCommand: modelcmd.OptionalControllerCommand{
Store: store,
ReadOnly: true,
},
}
command.newAPIFunc = func() (CredentialContentAPI, error) {
return command.NewCredentialAPI()
}
return modelcmd.WrapBase(command)
}
func (c *showCredentialCommand) SetFlags(f *gnuflag.FlagSet) {
c.OptionalControllerCommand.SetFlags(f)
// We only support yaml for display purposes.
c.out.AddFlags(f, "yaml", map[string]cmd.Formatter{
"yaml": cmd.FormatYaml,
})
f.BoolVar(&c.ShowSecrets, "show-secrets", false, "Display credential secret attributes")
}
func (c *showCredentialCommand) Init(args []string) error {
if err := c.OptionalControllerCommand.Init(args); err != nil {
return err
}
switch len(args) {
case 0:
// will get all credentials stored on the controller for this user.
break
case 1:
return errors.New("both cloud and credential name are needed")
case 2:
c.CloudName = args[0]
c.CredentialName = args[1]
default:
return errors.New("only cloud and credential names are supported")
}
return nil
}
func (c *showCredentialCommand) Info() *cmd.Info {
return jujucmd.Info(&cmd.Info{
Name: "show-credential",
Args: "[<cloud name> <credential name>]",
Purpose: "Shows credential information stored either on this client or on a controller.",
Doc: showCredentialDoc,
Aliases: []string{"show-credentials"},
Examples: showCredentialExamples,
SeeAlso: []string{
"credentials",
"add-credential",
"update-credential",
"remove-credential",
"autoload-credentials",
},
})
}
func (c *showCredentialCommand) Run(ctxt *cmd.Context) error {
if err := c.MaybePrompt(ctxt, fmt.Sprintf("show credential %q for cloud %q from", c.CredentialName, c.CloudName)); err != nil {
return errors.Trace(err)
}
all := ControllerCredentials{}
var returnErr error
if c.Client {
result, err := c.localCredentials(ctxt)
if err != nil {
ctxt.Infof("ERROR client credential content lookup failed: %v", err)
returnErr = cmd.ErrSilent
} else {
all.Client = c.parseContents(ctxt, result)
}
}
if c.ControllerName != "" {
remoteContents, err := c.remoteCredentials(ctxt)
if err != nil {
ctxt.Infof("ERROR credential content lookup on the controller failed: %v", err)
returnErr = cmd.ErrSilent
} else {
all.Controller = c.parseContents(ctxt, remoteContents)
}
}
if len(all.Client) == 0 && len(all.Controller) == 0 {
ctxt.Infof("No credentials from this client or from a controller to display.")
return nil
}
if err := c.out.Write(ctxt, all); err != nil {
return err
}
return returnErr
}
func (c *showCredentialCommand) remoteCredentials(ctxt *cmd.Context) ([]params.CredentialContentResult, error) {
client, err := c.newAPIFunc()
if err != nil {
return nil, err
}
defer client.Close()
remoteContents, err := client.CredentialContents(c.CloudName, c.CredentialName, c.ShowSecrets)
if err != nil {
return nil, err
}
return remoteContents, nil
}
func (c *showCredentialCommand) localCredentials(ctxt *cmd.Context) ([]params.CredentialContentResult, error) {
locals, err := credentialsFromLocalCache(c.Store, c.CloudName, c.CredentialName)
if err != nil {
return nil, err
}
if c.CloudName != "" {
_, ok := locals[c.CloudName]
if !ok {
return nil, errors.NotFoundf("client credentials for cloud %q", c.CloudName)
}
}
result := []params.CredentialContentResult{}
for cloudName, cloudLocal := range locals {
if !c.ShowSecrets {
if err := removeSecrets(cloudName, &cloudLocal, cloud.CloudByName); err != nil {
ctxt.Warningf("removing secrets from credentials for cloud %v: %v", c.CloudName, err)
continue
}
}
for name, details := range cloudLocal.AuthCredentials {
result = append(result, params.CredentialContentResult{
Result: ¶ms.ControllerCredentialInfo{
Content: params.CredentialContent{
Name: name,
Cloud: cloudName,
AuthType: string(details.AuthType()),
Attributes: details.Attributes(),
},
},
})
}
}
return result, nil
}
type CredentialContentAPI interface {
CredentialContents(cloud, credential string, withSecrets bool) ([]params.CredentialContentResult, error)
Close() error
}
func (c *showCredentialCommand) NewCredentialAPI() (CredentialContentAPI, error) {
api, err := c.NewAPIRoot(c.Store, c.ControllerName, "")
if err != nil {
return nil, errors.Annotate(err, "opening API connection")
}
return apicloud.NewClient(api), nil
}
type CredentialContent struct {
AuthType string `yaml:"auth-type"`
Validity string `yaml:"validity-check,omitempty"`
Attributes map[string]string `yaml:",inline"`
}
type CredentialDetails struct {
Content CredentialContent `yaml:"content"`
Models map[string]string `yaml:"models,omitempty"`
}
type NamedCredentials map[string]CredentialDetails
type CloudCredentials map[string]NamedCredentials
type ControllerCredentials struct {
Controller CloudCredentials `yaml:"controller-credentials,omitempty"`
Client CloudCredentials `yaml:"client-credentials,omitempty"`
}
func (c *showCredentialCommand) parseContents(ctxt *cmd.Context, in []params.CredentialContentResult) CloudCredentials {
if len(in) == 0 {
return nil
}
out := CloudCredentials{}
for _, result := range in {
if result.Error != nil {
ctxt.Infof("%v", result.Error)
continue
}
info := result.Result
_, ok := out[info.Content.Cloud]
if !ok {
out[info.Content.Cloud] = NamedCredentials{}
}
models := make(map[string]string, len(info.Models))
for _, m := range info.Models {
ownerAccess := m.Access
if ownerAccess == "" {
ownerAccess = "no access"
}
models[m.Model] = ownerAccess
}
valid := ""
if info.Content.Valid != nil {
valid = common.HumanReadableBoolPointer(info.Content.Valid, "valid", "invalid")
}
out[info.Content.Cloud][info.Content.Name] = CredentialDetails{
Content: CredentialContent{
AuthType: info.Content.AuthType,
Attributes: info.Content.Attributes,
Validity: valid,
},
Models: models,
}
}
return out
}
var showCredentialDoc = `
This command displays information about cloud credential(s) stored
either on this client or on a controller for this user.
To see the contents of a specific credential, supply its cloud and name.
To see all credentials stored for you, supply no arguments.
To see secrets, content attributes marked as hidden, use --show-secrets option.
To see credentials from this client, use "--client" option.
To see credentials from a controller, use "--controller" option.
`
const showCredentialExamples = `
juju show-credential google my-admin-credential
juju show-credentials
juju show-credentials --controller mycontroller --client
juju show-credentials --controller mycontroller
juju show-credentials --client
juju show-credentials --show-secrets
`