forked from fiveai/goipa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
group.go
172 lines (131 loc) · 4.01 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
package ipa
import (
"encoding/json"
"errors"
)
type GroupRecord struct {
Dn string `json:"dn"`
Description IpaString `json:"description"`
Gid IpaString `json:"cn"`
GidNumber IpaString `json:"gidnumber"`
MepManagedBy IpaString `json:"mepmanagedby"`
IpaUniqueId IpaString `json:"ipauniqueid"`
Users []string `json:"member_user"`
HbacRules []string `json:"memberof_hbacrule"`
}
// Fetch user details by calling the FreeIPA group-show method
func (c *Client) GetGroup(gid string) (*GroupRecord, error) {
options := map[string]interface{}{
"no_members": false,
"all": true}
res, err := c.rpc("group_show", []string{gid}, options)
if err != nil {
return nil, err
}
var groupRec GroupRecord
err = json.Unmarshal(res.Result.Data, &groupRec)
if err != nil {
return nil, err
}
return &groupRec, nil
}
// This doesn't work for primary groups - this appears to be a deficiency
// in FreeIPA as it also doesn't work from the ipa CLI
func (c *Client) GetGroupByGidNumber(gidNumber string) (*GroupRecord, error) {
options := map[string]interface{}{
"no_members": false,
"all": true,
"gidnumber": gidNumber,
"version": "2.228"}
res, err := c.rpc("group_find", []string{}, options)
if err != nil {
return nil, err
}
var groupRec []GroupRecord
err = json.Unmarshal(res.Result.Data, &groupRec)
if err != nil {
return nil, err
}
if len(groupRec) != 1 {
return nil, errors.New("too many records returned")
}
return &groupRec[0], nil
}
func (c *Client) GroupExists(uid string) (bool, error) {
_, err := c.GetGroup(uid)
if err == nil {
return true, nil
}
if err.(*IpaError).Code == 4001 {
return false, nil
}
return false, err
}
func (c *Client) GroupAddMember(gid string, memberId string, memberType string) (error) {
options := map[string]interface{}{
"raw": true,
"all": true,
memberType: memberId}
_, err := c.rpc("group_add_member", []string{gid}, options)
return err
}
func (c *Client) GroupRemoveMembers(gid string, members []string, memberType string) (error) {
options := map[string]interface{}{
"all": false,
"no_members": false,
"raw": false,
memberType: members,
"version": "2.164"}
_, err := c.rpc("group_remove_member", []string{gid}, options)
return err
}
func (c *Client) GroupMod(gid string, key string, value string) error {
options := map[string]interface{}{
key: value,
"version": "2.228"}
_, err := c.rpc("group_mod", []string{gid}, options)
return err
}
func (c *Client) GroupUpdateGid(oldGid string, newGid string) error {
return c.GroupMod(oldGid, "rename", newGid)
}
func (c *Client) GroupUpdateGidNumber(gid string, gidNumber string) error {
return c.GroupMod(gid, "gidnumber", gidNumber)
}
func (c *Client) GroupUpdateDescription(gid string, description string) error {
return c.GroupMod(gid, "description", description)
}
func (c *Client) GroupRemoveMember(gid string, member string, memberType string) (error) {
return c.GroupRemoveMembers(gid, []string{member}, memberType)
}
func (c *Client) GroupRemoveUser(gid string, uid string) (error) {
return c.GroupRemoveMember(gid, uid, "user")
}
func (c *Client) GroupRemoveUsers(gid string, uids []string) (error) {
return c.GroupRemoveMembers(gid, uids, "user")
}
func (c *Client) GroupAddUser(gid string, uid string) (error) {
return c.GroupAddMember(gid, uid, "user")
}
func (c *Client) CreateGroup(gid string, description string, options map[string]interface{}) (*GroupRecord, error) {
options["all"] = true
options["description"] = description
options["version"] = "2.228"
options["no_members"] = false
res, err := c.rpc("group_add", []string{gid}, options)
if err != nil {
return nil, err
}
var groupRec GroupRecord
err = json.Unmarshal(res.Result.Data, &groupRec)
if err != nil {
return nil, err
}
return &groupRec, nil
}
func (c *Client) DeleteGroup(gid string) (error) {
options := map[string]interface{}{
"version": "2.164"}
_, err := c.rpc("group_del", []string{gid}, options)
return err
}