/
group.go
401 lines (357 loc) · 14.4 KB
/
group.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
// Copyright © 2016 Asteris, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package group
import (
"fmt"
"os/user"
"github.com/asteris-llc/converge/resource"
"github.com/pkg/errors"
"golang.org/x/net/context"
)
// State type for Group
type State string
const (
// StatePresent indicates the group should be present
StatePresent State = "present"
// StateAbsent indicates the group should be absent
StateAbsent State = "absent"
)
// Group manages user groups
type Group struct {
// the configured group ID
GID string `export:"gid"`
// the configured group name
Name string `export:"name"`
// the desired group name
NewName string `export:"newname"`
// the group state
State State `export:"state"`
system SystemUtils
}
// ModGroupOptions are the options specified in the configuration to be used
// when modifying a group
type ModGroupOptions struct {
GID string
NewName string
}
// SystemUtils provides system utilities for group
type SystemUtils interface {
AddGroup(groupName, groupID string) error
DelGroup(groupName string) error
ModGroup(groupName string, options *ModGroupOptions) error
LookupGroup(groupName string) (*user.Group, error)
LookupGroupID(groupID string) (*user.Group, error)
}
// ErrUnsupported is used when a system is not supported
var ErrUnsupported = fmt.Errorf("group: not supported on this system")
// NewGroup constructs and returns a new Group
func NewGroup(system SystemUtils) *Group {
return &Group{
system: system,
}
}
// Check if a user group exists
func (g *Group) Check(context.Context, resource.Renderer) (resource.TaskStatus, error) {
var (
groupByGid *user.Group
gidErr error
groupByNewName *user.Group
newNameErr error
)
// lookup the group by name and lookup the group by gid
// the lookups return ErrUnsupported if the system is not supported
// LookupGroup returns user.UnknownGroupError if the group is not found
// LookupGroupID returns user.UnknownGroupIdError if the gid is not found
groupByName, nameErr := g.system.LookupGroup(g.Name)
if g.GID != "" {
groupByGid, gidErr = g.system.LookupGroupID(g.GID)
}
if g.NewName != "" {
groupByNewName, newNameErr = g.system.LookupGroup(g.NewName)
}
status := &resource.Status{}
if nameErr == ErrUnsupported {
status.RaiseLevel(resource.StatusFatal)
return status, ErrUnsupported
}
switch g.State {
case StatePresent:
switch {
case g.GID == "":
_, nameNotFound := nameErr.(user.UnknownGroupError)
switch {
case g.NewName == "":
switch {
case nameNotFound:
status.Output = append(status.Output, "add group")
status.AddDifference("group", string(StateAbsent), fmt.Sprintf("group %s", g.Name), "")
case groupByName != nil:
status.Output = append(status.Output, fmt.Sprintf("group add: group %s already exists", g.Name))
}
case g.NewName != "":
_, newNameNotFound := newNameErr.(user.UnknownGroupError)
switch {
case nameNotFound:
status.RaiseLevel(resource.StatusCantChange)
status.Output = append(status.Output, fmt.Sprintf("group modify: group %s does not exist", g.Name))
return status, errors.New("cannot modify group")
case newNameNotFound:
status.Output = append(status.Output, "modify group name")
status.AddDifference("group", fmt.Sprintf("group %s", g.Name), fmt.Sprintf("group %s", g.NewName), "")
case groupByNewName != nil:
status.RaiseLevel(resource.StatusCantChange)
status.Output = append(status.Output, fmt.Sprintf("group modify: group %s already exists", g.NewName))
return status, errors.New("cannot modify group")
}
}
case g.GID != "":
_, nameNotFound := nameErr.(user.UnknownGroupError)
_, gidNotFound := gidErr.(user.UnknownGroupIdError)
switch {
case g.NewName == "":
switch {
case nameNotFound && gidNotFound:
status.Output = append(status.Output, "add group with gid")
status.AddDifference("group", string(StateAbsent), fmt.Sprintf("group %s with gid %s", g.Name, g.GID), "")
case nameNotFound:
status.RaiseLevel(resource.StatusCantChange)
status.Output = append(status.Output, fmt.Sprintf("group add: gid %s already exists", g.GID))
return status, errors.New("cannot add group")
case gidNotFound:
status.Output = append(status.Output, "modify group gid")
status.AddDifference("group", fmt.Sprintf("group %s with gid %s", g.Name, groupByName.Gid), fmt.Sprintf("group %s with gid %s", g.Name, g.GID), "")
case groupByName != nil && groupByGid != nil && groupByName.Name != groupByGid.Name || groupByName.Gid != groupByGid.Gid:
status.RaiseLevel(resource.StatusCantChange)
status.Output = append(status.Output, fmt.Sprintf("group add/modify: group %s and gid %s belong to different groups", g.Name, g.GID))
return status, errors.New("cannot add or modify group")
case groupByName != nil && groupByGid != nil && *groupByName == *groupByGid:
status.RaiseLevel(resource.StatusCantChange)
status.Output = append(status.Output, fmt.Sprintf("group add/modify: group %s with gid %s already exists", g.Name, g.GID))
return status, errors.New("cannot add or modify group")
}
case g.NewName != "":
_, newNameNotFound := newNameErr.(user.UnknownGroupError)
switch {
case newNameNotFound && gidNotFound:
status.Output = append(status.Output, "modify group name and gid")
status.AddDifference("group", fmt.Sprintf("group %s with gid %s", g.Name, groupByName.Gid), fmt.Sprintf("group %s with gid %s", g.NewName, g.GID), "")
case gidNotFound:
status.RaiseLevel(resource.StatusCantChange)
status.Output = append(status.Output, fmt.Sprintf("group modify: group %s already exists", g.NewName))
return status, errors.New("cannot modify group")
case newNameNotFound:
status.RaiseLevel(resource.StatusCantChange)
status.Output = append(status.Output, fmt.Sprintf("group modify: gid %s already exists", g.GID))
return status, errors.New("cannot modify group")
}
}
}
case StateAbsent:
switch {
case g.GID == "":
_, nameNotFound := nameErr.(user.UnknownGroupError)
switch {
case nameNotFound:
status.Output = append(status.Output, fmt.Sprintf("group delete: group %s does not exist", g.Name))
case groupByName != nil:
status.Output = append(status.Output, "delete group")
status.AddDifference("group", fmt.Sprintf("group %s", g.Name), string(StateAbsent), "")
}
case g.GID != "":
_, nameNotFound := nameErr.(user.UnknownGroupError)
_, gidNotFound := gidErr.(user.UnknownGroupIdError)
switch {
case nameNotFound && gidNotFound:
status.Output = append(status.Output, fmt.Sprintf("group delete: group %s and gid %s do not exist", g.Name, g.GID))
case nameNotFound:
status.RaiseLevel(resource.StatusCantChange)
status.Output = append(status.Output, fmt.Sprintf("group delete: group %s does not exist", g.Name))
return status, errors.New("cannot delete group")
case gidNotFound:
status.RaiseLevel(resource.StatusCantChange)
status.Output = append(status.Output, fmt.Sprintf("group delete: gid %s does not exist", g.GID))
return status, errors.New("cannot delete group")
case groupByName != nil && groupByGid != nil && groupByName.Name != groupByGid.Name || groupByName.Gid != groupByGid.Gid:
status.RaiseLevel(resource.StatusCantChange)
status.Output = append(status.Output, fmt.Sprintf("group delete: group %s and gid %s belong to different groups", g.Name, g.GID))
return status, errors.New("cannot delete group")
case groupByName != nil && groupByGid != nil && *groupByName == *groupByGid:
status.Output = append(status.Output, "delete group with gid")
status.AddDifference("group", fmt.Sprintf("group %s with gid %s", g.Name, g.GID), string(StateAbsent), "")
}
}
default:
status.RaiseLevel(resource.StatusFatal)
return status, fmt.Errorf("group: unrecognized state %s", g.State)
}
status.RaiseLevelForDiffs()
return status, nil
}
// Apply changes for group
func (g *Group) Apply(context.Context) (resource.TaskStatus, error) {
var (
groupByGid *user.Group
gidErr error
newNameErr error
)
// lookup the group by name and lookup the group by gid
// the lookups return ErrUnsupported if the system is not supported
// LookupGroup returns user.UnknownGroupError if the group is not found
// LookupGroupID returns user.UnknownGroupIdError if the gid is not found
groupByName, nameErr := g.system.LookupGroup(g.Name)
if g.GID != "" {
groupByGid, gidErr = g.system.LookupGroupID(g.GID)
}
if g.NewName != "" {
_, newNameErr = g.system.LookupGroup(g.NewName)
}
status := &resource.Status{}
if nameErr == ErrUnsupported {
status.RaiseLevel(resource.StatusFatal)
return status, ErrUnsupported
}
switch g.State {
case StatePresent:
switch {
case g.GID == "":
_, nameNotFound := nameErr.(user.UnknownGroupError)
switch {
case g.NewName == "":
switch {
case nameNotFound:
err := g.system.AddGroup(g.Name, g.GID)
if err != nil {
status.RaiseLevel(resource.StatusFatal)
status.Output = append(status.Output, fmt.Sprintf("error adding group %s", g.Name))
return status, errors.Wrap(err, "group add")
}
status.Output = append(status.Output, fmt.Sprintf("added group %s", g.Name))
default:
status.RaiseLevel(resource.StatusCantChange)
return status, fmt.Errorf("will not attempt add: group %s", g.Name)
}
case g.NewName != "":
_, newNameNotFound := newNameErr.(user.UnknownGroupError)
switch {
case groupByName != nil && newNameNotFound:
options := SetModGroupOptions(g)
err := g.system.ModGroup(g.Name, options)
if err != nil {
status.RaiseLevel(resource.StatusFatal)
status.Output = append(status.Output, fmt.Sprintf("error modifying group %s", g.Name))
return status, errors.Wrap(err, "group modify")
}
status.Output = append(status.Output, fmt.Sprintf("modified group %s with new name %s", g.Name, g.NewName))
default:
status.RaiseLevel(resource.StatusCantChange)
return status, fmt.Errorf("will not attempt modify: group %s", g.Name)
}
}
case g.GID != "":
_, nameNotFound := nameErr.(user.UnknownGroupError)
_, gidNotFound := gidErr.(user.UnknownGroupIdError)
switch {
case g.NewName == "":
switch {
case nameNotFound && gidNotFound:
err := g.system.AddGroup(g.Name, g.GID)
if err != nil {
status.RaiseLevel(resource.StatusFatal)
status.Output = append(status.Output, fmt.Sprintf("error adding group %s with gid %s", g.Name, g.GID))
return status, errors.Wrap(err, "group add")
}
status.Output = append(status.Output, fmt.Sprintf("added group %s with gid %s", g.Name, g.GID))
case gidNotFound:
options := SetModGroupOptions(g)
err := g.system.ModGroup(g.Name, options)
if err != nil {
status.RaiseLevel(resource.StatusFatal)
status.Output = append(status.Output, fmt.Sprintf("error modifying group %s with new gid %s", g.Name, g.GID))
return status, errors.Wrap(err, "group modify")
}
status.Output = append(status.Output, fmt.Sprintf("modified group %s with new gid %s", g.Name, g.GID))
default:
status.RaiseLevel(resource.StatusCantChange)
return status, fmt.Errorf("will not attempt add/modify: group %s with gid %s", g.Name, g.GID)
}
case g.NewName != "":
_, newNameNotFound := newNameErr.(user.UnknownGroupError)
switch {
case groupByName != nil && newNameNotFound && gidNotFound:
options := SetModGroupOptions(g)
err := g.system.ModGroup(g.Name, options)
if err != nil {
status.RaiseLevel(resource.StatusFatal)
status.Output = append(status.Output, fmt.Sprintf("error modifying group %s with new name %s and new gid %s", g.Name, g.NewName, g.GID))
return status, errors.Wrap(err, "group modify")
}
status.Output = append(status.Output, fmt.Sprintf("modified group %s with new name %s and new gid %s", g.Name, g.NewName, g.GID))
default:
status.RaiseLevel(resource.StatusCantChange)
return status, fmt.Errorf("will not attempt modify: group %s with new name %s and new gid %s", g.Name, g.NewName, g.GID)
}
}
}
case StateAbsent:
switch {
case g.GID == "":
_, nameNotFound := nameErr.(user.UnknownGroupError)
switch {
case !nameNotFound && groupByName != nil:
err := g.system.DelGroup(g.Name)
if err != nil {
status.RaiseLevel(resource.StatusFatal)
status.Output = append(status.Output, fmt.Sprintf("error deleting group %s", g.Name))
return status, errors.Wrap(err, "group delete")
}
status.Output = append(status.Output, fmt.Sprintf("deleted group %s", g.Name))
default:
status.RaiseLevel(resource.StatusCantChange)
return status, fmt.Errorf("will not attempt delete: group %s", g.Name)
}
case g.GID != "":
_, nameNotFound := nameErr.(user.UnknownGroupError)
_, gidNotFound := gidErr.(user.UnknownGroupIdError)
switch {
case !nameNotFound && !gidNotFound && groupByName != nil && groupByGid != nil && *groupByName == *groupByGid:
err := g.system.DelGroup(g.Name)
if err != nil {
status.RaiseLevel(resource.StatusFatal)
status.Output = append(status.Output, fmt.Sprintf("error deleting group %s with gid %s", g.Name, g.GID))
return status, errors.Wrap(err, "group delete")
}
status.Output = append(status.Output, fmt.Sprintf("deleted group %s with gid %s", g.Name, g.GID))
default:
status.RaiseLevel(resource.StatusCantChange)
return status, fmt.Errorf("will not attempt delete: group %s with gid %s", g.Name, g.GID)
}
}
default:
status.RaiseLevel(resource.StatusFatal)
return status, fmt.Errorf("group: unrecognized state %s", g.State)
}
return status, nil
}
// SetModGroupOptions returns a ModGroupOptions struct with the options
// specified in the configuration for modifying a group
func SetModGroupOptions(g *Group) *ModGroupOptions {
options := new(ModGroupOptions)
if g.GID != "" {
options.GID = g.GID
}
if g.NewName != "" {
options.NewName = g.NewName
}
return options
}