-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
283 lines (233 loc) · 8.11 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
// Copyright 2022 Benjamin Böhmke <[email protected]>.
//
// 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 jazz
import (
"context"
"encoding/base64"
"errors"
"fmt"
"io"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
"github.com/beevik/etree"
"go.uber.org/zap"
"golang.org/x/net/publicsuffix"
)
// Client to communicate with various application of a jazz server
type Client struct {
// HttpClient used for requests to the jazz server
HttpClient *http.Client
// maximum amount of actions that should run in parallel
Worker int
// GC provides all functionalities to access the "Global Configuration Management" application
GC *GCApplication
// CCM provides all functionalities to access the "Change and Configuration Management" application
CCM *CCMApplication
// QM provides all functionalities to access the "Quality Management" application
QM *QMApplication
// GlobalConfiguration used for API requests
configContext *GlobalConfiguration
// Logger instance used for debug logging
Logger *zap.Logger
baseUrl string
user string
password string
basicAuth bool
}
// NewClient creates a new client for the given server
func NewClient(baseUrl, user, password string) (*Client, error) {
// cookie store
jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
if err != nil {
return nil, fmt.Errorf("failed to create cookiejar: %w", err)
}
// ensure base url ends with /
baseUrl = strings.TrimSpace(baseUrl)
client := &Client{
HttpClient: &http.Client{
Jar: jar,
},
baseUrl: baseUrl,
user: user,
// hide password in debugger
password: base64.StdEncoding.EncodeToString([]byte(password)),
Worker: 20,
Logger: zap.NewNop(),
}
// register applications
client.GC = &GCApplication{client: client}
client.CCM = &CCMApplication{client: client}
client.QM = &QMApplication{client: client}
return client, nil
}
// WithConfig creates a new client (copy of existing) with the given global configuration
func (c *Client) WithConfig(config *GlobalConfiguration) *Client {
client := &Client{
HttpClient: c.HttpClient,
baseUrl: c.baseUrl,
user: c.user,
password: c.password,
Worker: c.Worker,
basicAuth: c.basicAuth,
Logger: c.Logger,
configContext: config,
}
// register applications
client.GC = &GCApplication{client: client}
client.CCM = &CCMApplication{client: client}
client.QM = &QMApplication{client: client}
return client
}
// buildUrl for the given path.
// If path is already a complete URL return the value.
func (c *Client) buildUrl(path string) string {
if !strings.HasPrefix(path, "http:") && !strings.HasPrefix(path, "https:") {
return c.baseUrl + path
}
return path
}
// getEtree send a GET HTTP request (same as get) and return content as XML etree
func (c *Client) getEtree(ctx context.Context, url, contentType, errorMessage string, statusCode int) (*http.Response, *etree.Element, error) {
response, err := c.get(ctx, url, contentType, false)
if err != nil {
return nil, nil, fmt.Errorf("%s: %w", errorMessage, err)
}
defer response.Body.Close()
if statusCode != 0 && response.StatusCode != statusCode {
return nil, nil, errors.New(errorMessage)
}
// for non XML responses
if !strings.Contains(response.Header.Get("Content-Type"), "xml") {
// read raw content
all, err := io.ReadAll(response.Body)
if err != nil {
return nil, nil, fmt.Errorf("failed to read response body: %w", err)
}
// and store in dummy XML element
e := etree.NewElement("error")
e.SetText(string(all))
return response, e, nil
}
// load XML response
doc := etree.NewDocument()
_, err = doc.ReadFrom(response.Body)
if err != nil {
return nil, nil, fmt.Errorf("failed to parse XML response: %w", err)
}
return response, doc.Root(), nil
}
// get sends GET request to server
func (c *Client) get(ctx context.Context, url, contentType string, noGc bool) (*http.Response, error) {
request, err := http.NewRequestWithContext(ctx, "GET", c.buildUrl(url), nil)
if err != nil {
return nil, fmt.Errorf("failed to create get request: %w", err)
}
request.Header.Set("Content-type", contentType)
return c.sendRequest(request, noGc)
}
// put sends GET request to server
func (c *Client) put(ctx context.Context, url, contentType string, reader io.Reader) (*http.Response, error) {
request, err := http.NewRequestWithContext(ctx, "PUT", c.buildUrl(url), reader)
if err != nil {
return nil, fmt.Errorf("failed to create put request: %w", err)
}
request.Header.Set("Content-type", contentType)
return c.sendRequest(request, false)
}
// sendRequest to server and handle auth if required
func (c *Client) sendRequest(request *http.Request, noGc bool) (*http.Response, error) {
// send request
response, err := c.sendRawRequest(request, true, noGc)
if err != nil {
return nil, err
}
// check if auth is required
// https://jazz.net/wiki/bin/view/Main/NativeClientAuthentication
// > form challenge
authMsg := response.Header.Get("x-com-ibm-team-repository-web-auth-msg")
if authMsg != "" {
// close response as it is not used
_ = response.Body.Close()
// if content of header if not "authrequired" there is an error
if authMsg != "authrequired" {
return nil, fmt.Errorf("server authentication error: %s", authMsg)
}
c.Logger.Sugar().Debugf("Login to %s as %s (Form challenge)", c.baseUrl, c.user)
// send the login request
values := make(url.Values)
values.Set("j_username", c.user)
pass, _ := base64.StdEncoding.DecodeString(c.password)
values.Set("j_password", string(pass))
jtsRequest, err := http.NewRequestWithContext(request.Context(), "GET", c.buildUrl("jts/j_security_check?"+values.Encode()), nil)
if err != nil {
return nil, fmt.Errorf("failed to create JTS request: %w", err)
}
response, err = c.sendRawRequest(jtsRequest, false, noGc)
// close response as it is not used
_ = response.Body.Close()
// if header is still set the login failed
authMsg = response.Header.Get("x-com-ibm-team-repository-web-auth-msg")
if authMsg != "" {
return nil, fmt.Errorf("server authentication failed: %s", authMsg)
}
// resend original request
return c.sendRawRequest(request, true, noGc)
}
// > basic auth
if response.StatusCode == 401 && response.Header.Get("www-authenticate") != "" {
// close response as it is not used
_ = response.Body.Close()
c.Logger.Sugar().Debugf("Login to %s as %s (basic auth)", c.baseUrl, c.user)
c.basicAuth = true
// resend original request
response, err = c.sendRawRequest(request, true, noGc)
if err != nil {
return nil, err
}
if response.StatusCode == 401 {
return nil, errors.New("server authentication failed")
}
return response, nil
}
// unknown auth method
if response.StatusCode == 401 {
// close response as it is not used
_ = response.Body.Close()
return nil, fmt.Errorf("unknown auth method")
}
return response, nil
}
// sendRawRequest to server
func (c *Client) sendRawRequest(request *http.Request, log, noGc bool) (*http.Response, error) {
if log {
c.Logger.Sugar().Debugf("Send %s request to %s", request.Method, request.URL)
}
if request.Header.Get("Accept") == "" {
request.Header.Set("Accept", request.Header.Get("Content-Type"))
}
// normally only required for OSLC request but has no effect for others
request.Header.Set("OSLC-Core-Version", "2.0")
// set configuration context if enabled and set
if !noGc && c.configContext != nil {
request.Header.Set("Configuration-Context", c.configContext.URL)
}
// only add basic auth data if it was enabled
if c.basicAuth {
pass, _ := base64.StdEncoding.DecodeString(c.password)
request.SetBasicAuth(c.user, string(pass))
}
return c.HttpClient.Do(request)
}