@@ -4,50 +4,57 @@ package main
4
4
5
5
import (
6
6
"fmt"
7
+ "os/user"
7
8
)
8
9
9
10
// one instance per task
10
11
type OSGroups struct {
11
12
Task * TaskRun
12
13
// keep track of which groups we successfully update
13
- AddedGroups []string
14
+ AddedGroups []* user. Group
14
15
}
15
16
16
17
func (osGroups * OSGroups ) Start () * CommandExecutionError {
17
- groups := osGroups .Task .Payload .OSGroups
18
- if len (groups ) == 0 {
18
+ groupNames := osGroups .Task .Payload .OSGroups
19
+ if len (groupNames ) == 0 {
19
20
return nil
20
21
}
21
22
if config .RunTasksAsCurrentUser {
22
- osGroups .Task .Infof ("Not adding task user to group(s) %v since we are running as current user." , groups )
23
+ osGroups .Task .Infof ("Not adding task user to group(s) %v since we are running as current user." , groupNames )
23
24
return nil
24
25
}
25
- notAddedGroups := []string {}
26
- for _ , group := range groups {
27
- err := addUserToGroup (taskContext .User .Name , group )
28
- if err == nil {
29
- osGroups .AddedGroups = append (osGroups .AddedGroups , group )
30
- } else {
31
- notAddedGroups = append (notAddedGroups , group )
32
- osGroups .Task .Errorf ("[osGroups] Could not add task user to OS group %v: %v" , group , err )
26
+ notAddedGroupNames := []string {}
27
+ for _ , groupName := range groupNames {
28
+ err := addUserToGroup (taskContext .User .Name , groupName )
29
+ if err != nil {
30
+ notAddedGroupNames = append (notAddedGroupNames , groupName )
31
+ osGroups .Task .Errorf ("[osGroups] Could not add task user to OS group %v: %v" , groupName , err )
32
+ continue
33
33
}
34
+ group , err := user .LookupGroup (groupName )
35
+ if err != nil {
36
+ notAddedGroupNames = append (notAddedGroupNames , groupName )
37
+ osGroups .Task .Errorf ("[osGroups] Could not look up group ID for OS group %v: %v" , groupName , err )
38
+ continue
39
+ }
40
+ osGroups .AddedGroups = append (osGroups .AddedGroups , group )
34
41
}
35
- if len (notAddedGroups ) > 0 {
36
- return MalformedPayloadError (fmt .Errorf ("Could not add task user to OS group(s) %v" , notAddedGroups ))
42
+ if len (notAddedGroupNames ) > 0 {
43
+ return MalformedPayloadError (fmt .Errorf ("Could not add task user to OS group(s) %v" , notAddedGroupNames ))
37
44
}
38
45
return osGroups .refreshTaskCommands ()
39
46
}
40
47
41
48
func (osGroups * OSGroups ) Stop (err * ExecutionErrors ) {
42
- notRemovedGroups := []string {}
49
+ notRemovedGroupNames := []string {}
43
50
for _ , group := range osGroups .AddedGroups {
44
- e := removeUserFromGroup (taskContext .User .Name , group )
51
+ e := removeUserFromGroup (taskContext .User .Name , group . Name )
45
52
if e != nil {
46
- notRemovedGroups = append (notRemovedGroups , group )
53
+ notRemovedGroupNames = append (notRemovedGroupNames , group . Name )
47
54
osGroups .Task .Errorf ("[osGroups] Could not remove task user from OS group %v: %v" , group , e )
48
55
}
49
56
}
50
- if len (notRemovedGroups ) > 0 {
51
- err .add (executionError (internalError , errored , fmt .Errorf ("Could not remove task user from OS group(s) %v" , notRemovedGroups )))
57
+ if len (notRemovedGroupNames ) > 0 {
58
+ err .add (executionError (internalError , errored , fmt .Errorf ("Could not remove task user from OS group(s) %v" , notRemovedGroupNames )))
52
59
}
53
60
}
0 commit comments