-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
293 lines (260 loc) · 7.93 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
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
// Author(s): Michael Koeppl
package jenkinsrole
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
)
// Client implements functionality for manipulating roles in Jenkins.
type Client struct {
// The Jenkins hostname
HostName string
// The user used to manipulate roles
User string
// The token for the given user
Token string
}
func hostnameHasCorrectPrefix(hostname string) bool {
return strings.HasPrefix(hostname, "http://") || strings.HasPrefix(hostname, "https://")
}
// NewClient creates a new instance of Client and does additional checks to
// ensure that all attributes are valid.
func NewClient(hostname, user, token string) (*Client, error) {
if !hostnameHasCorrectPrefix(hostname) {
return nil, errors.New("Hostname has incorrect prefix ('https://' or 'http://' required)")
}
hostname = strings.TrimSuffix(hostname, "/")
return &Client{
HostName: hostname,
User: user,
Token: token,
}, nil
}
// performRequest creates a request, attaches the basic authentication header
// to it and sends the request off.
func (c *Client) performRequest(method, url string, body *bytes.Reader) (*http.Response, error) {
var req *http.Request
var err error
if body == nil {
req, err = http.NewRequest(method, url, nil)
} else {
req, err = http.NewRequest(method, url, body)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
}
if err != nil {
return nil, err
}
attachAuthHeader(req, c.User, c.Token)
hc := &http.Client{}
resp, err := hc.Do(req)
if err != nil {
return nil, err
}
return resp, nil
}
// AddRole adds a role to the role map.
// See https://github.com/runzexia/role-strategy-plugin/blob/5fdea531bc5aff5865a64cead6abcf9461720b1b/src/main/java/com/michelin/cio/hudson/plugins/rolestrategy/RoleBasedAuthorizationStrategy.java#L233
func (c *Client) AddRole(roleType, roleName string, permissions []Permission, overwrite bool, pattern ...string) error {
targetURL := fmt.Sprintf("%s/role-strategy/strategy/addRole", c.HostName)
var permStrings []string
// If the package user added the 'All' permission to the list of
// permissions for the new role, we just add all available permissions
// (just use the pre-defined list).
if (len(permissions) == 1 && permissions[0] == All) || permListContainsAllPermission(permissions) {
permStrings = permissionStrings
} else {
permStrings = make([]string, len(permissions))
for i, p := range permissions {
permStrings[i] = p.getPermissionString()
}
}
bodyStr := fmt.Sprintf(
"type=%s&roleName=%s&permissionIds=%s&overwrite=%t",
roleType,
roleName,
strings.Join(permStrings, ","),
overwrite,
)
if len(pattern) >= 1 && pattern[0] != "" {
bodyStr += fmt.Sprintf("&pattern=%s", pattern[0])
}
body := bytes.NewReader([]byte(bodyStr))
resp, err := c.performRequest(http.MethodPost, targetURL, body)
if err != nil {
return err
}
switch resp.StatusCode {
case http.StatusOK:
return nil
default:
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
return errors.New(string(respBody))
}
}
// RemoveRoles removes one or multiple roles with the given names.
// https://github.com/runzexia/role-strategy-plugin/blob/5fdea531bc5aff5865a64cead6abcf9461720b1b/src/main/java/com/michelin/cio/hudson/plugins/rolestrategy/RoleBasedAuthorizationStrategy.java#L363
func (c *Client) RemoveRoles(roleType string, roleNames []string) error {
targetURL := fmt.Sprintf("%s/role-strategy/strategy/removeRoles", c.HostName)
body := bytes.NewReader([]byte(
fmt.Sprintf("type=%s&roleNames=%s",
roleType,
strings.Join(roleNames, ","),
),
))
resp, err := c.performRequest(http.MethodPost, targetURL, body)
if err != nil {
return err
}
switch resp.StatusCode {
case http.StatusOK:
return nil
default:
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
return errors.New(string(respBody))
}
}
// AssignRole assigns the role with the given name to the user with the given
// SID.
// https://github.com/runzexia/role-strategy-plugin/blob/5fdea531bc5aff5865a64cead6abcf9461720b1b/src/main/java/com/michelin/cio/hudson/plugins/rolestrategy/RoleBasedAuthorizationStrategy.java#L393
func (c *Client) AssignRole(roleType, roleName, sid string) error {
targetURL := fmt.Sprintf("%s/role-strategy/strategy/assignRole", c.HostName)
body := bytes.NewReader([]byte(
fmt.Sprintf("type=%s&roleName=%s&sid=%s",
roleType,
roleName,
sid,
),
))
resp, err := c.performRequest(http.MethodPost, targetURL, body)
if err != nil {
return err
}
switch resp.StatusCode {
case http.StatusOK:
return nil
default:
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
return errors.New(string(respBody))
}
}
// UnassignRole unassigns the role with the given name from the user with the
// given SID.
// https://github.com/runzexia/role-strategy-plugin/blob/5fdea531bc5aff5865a64cead6abcf9461720b1b/src/main/java/com/michelin/cio/hudson/plugins/rolestrategy/RoleBasedAuthorizationStrategy.java#L453
func (c *Client) UnassignRole(roleType, roleName, sid string) error {
targetURL := fmt.Sprintf("%s/role-strategy/strategy/unassignRole", c.HostName)
body := bytes.NewReader([]byte(
fmt.Sprintf("type=%s&roleName=%s&sid=%s",
roleType,
roleName,
sid,
),
))
resp, err := c.performRequest(http.MethodPost, targetURL, body)
if err != nil {
return err
}
switch resp.StatusCode {
case http.StatusOK:
return nil
default:
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
return errors.New(string(respBody))
}
}
// DeleteSID deletes an SID from all granted roles.
// https://github.com/runzexia/role-strategy-plugin/blob/5fdea531bc5aff5865a64cead6abcf9461720b1b/src/main/java/com/michelin/cio/hudson/plugins/rolestrategy/RoleBasedAuthorizationStrategy.java#L431
func (c *Client) DeleteSID(roleType, sid string) error {
targetURL := fmt.Sprintf("%s/role-strategy/strategy/deleteSid", c.HostName)
body := bytes.NewReader([]byte(
fmt.Sprintf("type=%s&sid=%s",
roleType,
sid,
),
))
resp, err := c.performRequest(http.MethodPost, targetURL, body)
if err != nil {
return err
}
switch resp.StatusCode {
case http.StatusOK:
return nil
default:
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
return errors.New(string(respBody))
}
}
// GetRole gets the role with the given role name.
// https://github.com/runzexia/role-strategy-plugin/blob/5fdea531bc5aff5865a64cead6abcf9461720b1b/src/main/java/com/michelin/cio/hudson/plugins/rolestrategy/RoleBasedAuthorizationStrategy.java#L324
func (c *Client) GetRole(roleType, roleName string) (*Role, error) {
targetURL := fmt.Sprintf("%s/role-strategy/strategy/getRole?type=%s&roleName=%s",
c.HostName,
roleType,
roleName,
)
resp, err := c.performRequest(http.MethodGet, targetURL, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
switch resp.StatusCode {
case http.StatusOK:
r := &Role{}
if err := json.Unmarshal(respBody, r); err != nil {
return nil, err
}
return r, nil
default:
return nil, errors.New(string(respBody))
}
}
// GetAllRoles gets a list of all roles with all users they're assigned to.
func (c *Client) GetAllRoles(roleType string) (RolesWithUsers, error) {
targetURL := fmt.Sprintf("%s/role-strategy/strategy/getAllRoles?type=%s", c.HostName, roleType)
resp, err := c.performRequest(http.MethodGet, targetURL, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
switch resp.StatusCode {
case http.StatusOK:
rwu := make(map[string][]string)
if err := json.Unmarshal(respBody, &rwu); err != nil {
return nil, err
}
return rwu, nil
default:
return nil, errors.New(string(respBody))
}
}