/
client.go
232 lines (210 loc) · 6.9 KB
/
client.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
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package usermanager
import (
"fmt"
"strings"
"github.com/juju/errors"
"github.com/juju/loggo"
"github.com/juju/names/v5"
"github.com/juju/juju/api/base"
"github.com/juju/juju/rpc/params"
)
var logger = loggo.GetLogger("juju.api.usermanager")
// Client provides methods that the Juju client command uses to interact
// with users stored in the Juju Server.
type Client struct {
base.ClientFacade
facade base.FacadeCaller
}
// NewClient creates a new `Client` based on an existing authenticated API
// connection.
func NewClient(st base.APICallCloser) *Client {
frontend, backend := base.NewClientFacade(st, "UserManager")
return &Client{ClientFacade: frontend, facade: backend}
}
// AddUser creates a new local user in the controller, sharing with that user any specified models.
func (c *Client) AddUser(
username, displayName, password string,
) (_ names.UserTag, secretKey []byte, _ error) {
if !names.IsValidUser(username) {
return names.UserTag{}, nil, fmt.Errorf("invalid user name %q", username)
}
userArgs := params.AddUsers{
Users: []params.AddUser{{
Username: username,
DisplayName: displayName,
Password: password,
}},
}
var results params.AddUserResults
err := c.facade.FacadeCall("AddUser", userArgs, &results)
if err != nil {
return names.UserTag{}, nil, errors.Trace(err)
}
if count := len(results.Results); count != 1 {
logger.Errorf("expected 1 result, got %#v", results)
return names.UserTag{}, nil, errors.Errorf("expected 1 result, got %d", count)
}
result := results.Results[0]
if result.Error != nil {
return names.UserTag{}, nil, errors.Trace(result.Error)
}
tag, err := names.ParseUserTag(result.Tag)
if err != nil {
return names.UserTag{}, nil, errors.Trace(err)
}
return tag, result.SecretKey, nil
}
func (c *Client) userCall(username string, methodCall string) error {
if !names.IsValidUser(username) {
return errors.Errorf("%q is not a valid username", username)
}
tag := names.NewUserTag(username)
var results params.ErrorResults
args := params.Entities{
[]params.Entity{{tag.String()}},
}
err := c.facade.FacadeCall(methodCall, args, &results)
if err != nil {
return errors.Trace(err)
}
return results.OneError()
}
// DisableUser disables a user. If the user is already disabled, the action
// is considered a success.
func (c *Client) DisableUser(username string) error {
return c.userCall(username, "DisableUser")
}
// EnableUser enables a users. If the user is already enabled, the action is
// considered a success.
func (c *Client) EnableUser(username string) error {
return c.userCall(username, "EnableUser")
}
// RemoveUser deletes a user. That is it permanently removes the user, while
// retaining the record of the user to maintain provenance.
func (c *Client) RemoveUser(username string) error {
return c.userCall(username, "RemoveUser")
}
// IncludeDisabled is a type alias to avoid bare true/false values
// in calls to the client method.
type IncludeDisabled bool
var (
// ActiveUsers indicates to only return active users.
ActiveUsers IncludeDisabled = false
// AllUsers indicates that both enabled and disabled users should be
// returned.
AllUsers IncludeDisabled = true
)
// UserInfo returns information about the specified users. If no users are
// specified, the call should return all users. If includeDisabled is set to
// ActiveUsers, only enabled users are returned.
func (c *Client) UserInfo(usernames []string, all IncludeDisabled) ([]params.UserInfo, error) {
var results params.UserInfoResults
var entities []params.Entity
for _, username := range usernames {
if !names.IsValidUser(username) {
return nil, errors.Errorf("%q is not a valid username", username)
}
tag := names.NewUserTag(username)
entities = append(entities, params.Entity{Tag: tag.String()})
}
args := params.UserInfoRequest{
Entities: entities,
IncludeDisabled: bool(all),
}
err := c.facade.FacadeCall("UserInfo", args, &results)
if err != nil {
return nil, errors.Trace(err)
}
// Only need to look for errors if users were explicitly specified, because
// if we didn't ask for any, we should get all, and we shouldn't get any
// errors for listing all. We care here because we index into the users
// slice.
if len(results.Results) == len(usernames) {
var errorStrings []string
for i, result := range results.Results {
if result.Error != nil {
annotated := errors.Annotate(result.Error, usernames[i])
errorStrings = append(errorStrings, annotated.Error())
}
}
// TODO(wallyworld) - we should return these errors to the caller so that any
// users which are successfully found can be handled.
if len(errorStrings) > 0 {
return nil, errors.New(strings.Join(errorStrings, ", "))
}
}
info := []params.UserInfo{}
for i, result := range results.Results {
if result.Result == nil {
return nil, errors.Errorf("unexpected nil result at position %d", i)
}
info = append(info, *result.Result)
}
return info, nil
}
// ModelUserInfo returns information on all users in the model.
func (c *Client) ModelUserInfo(modelUUID string) ([]params.ModelUserInfo, error) {
var results params.ModelUserInfoResults
args := params.Entities{
Entities: []params.Entity{{
Tag: names.NewModelTag(modelUUID).String(),
}},
}
err := c.facade.FacadeCall("ModelUserInfo", args, &results)
if err != nil {
return nil, errors.Trace(err)
}
info := []params.ModelUserInfo{}
for i, result := range results.Results {
if result.Result == nil {
return nil, errors.Errorf("unexpected nil result at position %d", i)
}
info = append(info, *result.Result)
}
return info, nil
}
// SetPassword changes the password for the specified user.
func (c *Client) SetPassword(username, password string) error {
if !names.IsValidUser(username) {
return errors.Errorf("%q is not a valid username", username)
}
tag := names.NewUserTag(username)
args := params.EntityPasswords{
Changes: []params.EntityPassword{{
Tag: tag.String(),
Password: password}},
}
var results params.ErrorResults
err := c.facade.FacadeCall("SetPassword", args, &results)
if err != nil {
return err
}
return results.OneError()
}
// ResetPassword resets password for the specified user.
func (c *Client) ResetPassword(username string) ([]byte, error) {
if !names.IsValidUser(username) {
return nil, fmt.Errorf("invalid user name %q", username)
}
in := params.Entities{
Entities: []params.Entity{{
Tag: names.NewUserTag(username).String(),
}},
}
var out params.AddUserResults
err := c.facade.FacadeCall("ResetPassword", in, &out)
if err != nil {
return nil, errors.Trace(err)
}
if count := len(out.Results); count != 1 {
logger.Errorf("expected 1 result, got %#v", out)
return nil, errors.Errorf("expected 1 result, got %d", count)
}
result := out.Results[0]
if result.Error != nil {
return nil, errors.Trace(result.Error)
}
return result.SecretKey, nil
}