-
Notifications
You must be signed in to change notification settings - Fork 11
/
client.go
291 lines (247 loc) · 7.02 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
// Package s3creds provides support for HSDP S3 Credentials
package s3creds
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"sort"
"strings"
autoconf "github.com/philips-software/go-hsdp-api/config"
"github.com/go-playground/validator/v10"
"github.com/google/go-querystring/query"
"github.com/philips-software/go-hsdp-api/iam"
)
const (
libraryVersion = "0.1.0"
userAgent = "go-hsdp-api/s3creds/" + libraryVersion
)
// OptionFunc is the function signature function for options
type OptionFunc func(*http.Request) error
// Config contains the configuration of a client
type Config struct {
BaseURL string
Region string
Environment string
Debug bool
DebugLog string
}
// A Client manages communication with HSDP IAM API
type Client struct {
// HTTP client used to communicate with the API.
iamClient *iam.Client
config *Config
baseURL *url.URL
// User agent used when communicating with the HSDP IAM API.
UserAgent string
debugFile *os.File
Policy *PolicyService
Access *AccessService
}
// NewClient returns a new HSDP Credenials API client. A configured IAM
// client must be provided
func NewClient(iamClient *iam.Client, config *Config) (*Client, error) {
return newClient(iamClient, config)
}
func doAutoconf(config *Config) {
if config.Region != "" {
if config.Environment == "" {
return
}
ac, err := autoconf.New(
autoconf.WithRegion(config.Region),
autoconf.WithEnv(config.Environment))
if err == nil {
credsService := ac.Service("s3creds")
if credsService.URL != "" && config.BaseURL == "" {
config.BaseURL = credsService.URL
}
}
}
}
func newClient(iamClient *iam.Client, config *Config) (*Client, error) {
c := &Client{iamClient: iamClient, config: config, UserAgent: userAgent}
doAutoconf(config)
if err := c.SetBaseURL(c.config.BaseURL); err != nil {
return nil, err
}
if config.DebugLog != "" {
var err error
c.debugFile, err = os.OpenFile(config.DebugLog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
c.debugFile = nil
}
}
c.Policy = &PolicyService{client: c, validate: validator.New()}
_ = c.Policy.validate.RegisterValidation("policyActions", validateActions)
c.Access = &AccessService{client: c}
return c, nil
}
// Close releases allocated resources of clients
func (c *Client) Close() {
if c.debugFile != nil {
_ = c.debugFile.Close()
c.debugFile = nil
}
}
// SetBaseURL sets the base URL for API requests to a custom endpoint. urlStr
// should always be specified with a trailing slash.
func (c *Client) SetBaseURL(urlStr string) error {
if urlStr == "" {
return ErrBaseURLCannotBeEmpty
}
// Make sure the given URL end with a slash
if !strings.HasSuffix(urlStr, "/") {
urlStr += "/"
}
var err error
c.baseURL, err = url.Parse(urlStr)
return err
}
// newRequest creates an new TDR API request. A relative URL path can be provided in
// urlStr, in which case it is resolved relative to the base URL of the Client.
// Relative URL paths should always be specified without a preceding slash. If
// specified, the value pointed to by body is JSON encoded and included as the
// request body.
func (c *Client) newRequest(method, path string, opt interface{}, options []OptionFunc) (*http.Request, error) {
u := *c.baseURL
// Set the encoded opaque data
u.Opaque = c.baseURL.Path + path
if opt != nil {
q, err := query.Values(opt)
if err != nil {
return nil, err
}
u.RawQuery = q.Encode()
}
req := &http.Request{
Method: method,
URL: &u,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Header: make(http.Header),
Host: u.Host,
}
for _, fn := range options {
if fn == nil {
continue
}
if err := fn(req); err != nil {
return nil, err
}
}
if method == "POST" || method == "PUT" {
bodyBytes, err := json.Marshal(opt)
if err != nil {
return nil, err
}
bodyReader := bytes.NewReader(bodyBytes)
u.RawQuery = ""
req.Body = ioutil.NopCloser(bodyReader)
req.ContentLength = int64(bodyReader.Len())
req.Header.Set("Content-Type", "application/json")
}
token, err := c.iamClient.Token()
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Authorization", "Bearer "+token)
if c.UserAgent != "" {
req.Header.Set("User-Agent", c.UserAgent)
}
return req, nil
}
// Response is a HSDP IAM API response. This wraps the standard http.Response
// returned from HSDP IAM and provides convenient access to things like errors
type Response struct {
*http.Response
}
// newResponse creates a new Response for the provided http.Response.
func newResponse(r *http.Response) *Response {
response := &Response{Response: r}
return response
}
// Do executes a http request. If v implements the io.Writer
// interface, the raw response body will be written to v, without attempting to
// first decode it.
func (c *Client) Do(req *http.Request, v interface{}) (*Response, error) {
resp, err := c.iamClient.HttpClient().Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
response := newResponse(resp)
err = checkResponse(resp)
if err != nil {
// even though there was an error, we still return the response
// in case the caller wants to inspect it further
return response, err
}
if v != nil {
if w, ok := v.(io.Writer); ok {
_, err = io.Copy(w, resp.Body)
} else {
err = json.NewDecoder(resp.Body).Decode(v)
}
}
return response, err
}
// ErrorResponse represents an IAM errors response
// containing a code and a human readable message
type ErrorResponse struct {
Response *http.Response `json:"-"`
Code string `json:"responseCode"`
Message string `json:"responseMessage"`
}
func (e *ErrorResponse) Error() string {
path, _ := url.QueryUnescape(e.Response.Request.URL.Opaque)
u := fmt.Sprintf("%s://%s%s", e.Response.Request.URL.Scheme, e.Response.Request.URL.Host, path)
return fmt.Sprintf("%s %s: %d %s", e.Response.Request.Method, u, e.Response.StatusCode, e.Message)
}
func checkResponse(r *http.Response) error {
switch r.StatusCode {
case 200, 201, 202, 204, 304:
return nil
}
errorResponse := &ErrorResponse{Response: r}
data, err := ioutil.ReadAll(r.Body)
if err == nil && data != nil {
var raw interface{}
if err := json.Unmarshal(data, &raw); err != nil {
errorResponse.Message = "failed to parse unknown error format"
}
errorResponse.Message = parseError(raw)
}
return errorResponse
}
func parseError(raw interface{}) string {
switch raw := raw.(type) {
case string:
return raw
case []interface{}:
var errs []string
for _, v := range raw {
errs = append(errs, parseError(v))
}
return fmt.Sprintf("[%s]", strings.Join(errs, ", "))
case map[string]interface{}:
var errs []string
for k, v := range raw {
errs = append(errs, fmt.Sprintf("{%s: %s}", k, parseError(v)))
}
sort.Strings(errs)
return strings.Join(errs, ", ")
case float64:
return fmt.Sprintf("%d", int64(raw))
case int64:
return fmt.Sprintf("%d", raw)
default:
return fmt.Sprintf("failed to parse unexpected error type: %T", raw)
}
}