forked from dell/gounity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunityclient.go
214 lines (181 loc) · 5.95 KB
/
unityclient.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
package gounity
import (
"bytes"
"context"
"encoding/base64"
"errors"
"fmt"
"net/http"
"os"
"strconv"
"github.com/dell/gounity/util"
"github.com/dell/gounity/api"
"github.com/dell/gounity/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
const (
emcCsrfToken = "EMC-CSRF-TOKEN"
)
var (
accHeader string
conHeader string
errNoLink = errors.New("error: problem finding link")
debug, _ = strconv.ParseBool(os.Getenv("GOUNITY_DEBUG"))
showHTTP, _ = strconv.ParseBool(os.Getenv("GOUNITY_SHOWHTTP"))
)
//Client Struct holds the configuration & REST Client.
type Client struct {
configConnect *ConfigConnect
api api.Client
}
//ConfigConnect Struct holds the endpoint & credential info.
type ConfigConnect struct {
Endpoint string
Username string
Password string
}
// Authenticate make a REST API call [/loginSessionInfo] to Unity to get authenticate the given credentials.
// The response contains the EMC-CSRF-TOKEN and the client caches it for further communication.
func (c *Client) Authenticate(ctx context.Context, configConnect *ConfigConnect) error {
log := util.GetRunIDLogger(ctx)
log.Debug("Executing Authenticate REST client")
c.configConnect = configConnect
c.api.SetToken("")
headers := make(map[string]string, 3)
headers[api.AuthorizationHeader] = "Basic " + basicAuth(configConnect.Username, configConnect.Password)
headers[api.XEmcRestClient] = "true"
headers[api.HeaderKeyContentType] = api.HeaderValContentTypeJSON
resp, err := c.api.DoAndGetResponseBody(ctx, http.MethodGet, api.UnityAPILoginSessionInfoURI, headers, nil)
if err != nil {
return fmt.Errorf("authentication error: %v", err)
}
if resp != nil {
log.Debugf("Authentication response code: %d", resp.StatusCode)
if err != nil {
log.Errorf("Reading Authentication response body error:%v", err)
}
defer resp.Body.Close()
switch {
case resp.StatusCode >= 200 && resp.StatusCode <= 299:
{
log.Debug("Authentication successful")
}
case resp.StatusCode == 401:
{
return status.Errorf(codes.Unauthenticated, "Authentication failed. Unable to login to Unity. Verify username and password.")
}
default:
return fmt.Errorf("authenticate error. Response: %v", c.api.ParseJSONError(ctx, resp))
}
c.api.SetToken(resp.Header.Get(emcCsrfToken))
} else {
log.Errorf("Authenticate error: Nil response received")
}
return nil
}
// basicAuth converts the given username & password to Base64 encoded string.
func basicAuth(username, password string) string {
auth := username + ":" + password
return base64.StdEncoding.EncodeToString([]byte(auth))
}
// GetJSONWithRetry method responsible to make the given API call to Unity REST API Server.
// In case if the given EMC-CSRF-TOKEN becomes invalid, retries the same operation after performing authentication.
func (c *Client) executeWithRetryAuthenticate(ctx context.Context, method, uri string, body, resp interface{}) error {
log := util.GetRunIDLogger(ctx)
headers := make(map[string]string, 2)
headers[api.HeaderKeyAccept] = accHeader
headers[api.HeaderKeyContentType] = conHeader
headers[api.XEmcRestClient] = "true"
log.Debug("Invoking REST API server info Method: ", method, ", URI: ", uri)
err := c.api.DoWithHeaders(ctx, method, uri, headers, body, resp)
if err == nil {
log.Debug("Execution successful on Method: ", method, ", URI: ", uri)
return nil
}
// check if we need to authenticate
if e, ok := err.(*types.Error); ok {
log.Debugf("Error in response. Method:%s URI:%s Error: %v JSON Error: %+v", method, uri, err, e)
if e.ErrorContent.HTTPStatusCode == 401 {
log.Debug("need to re-authenticate")
// Authenticate then try again
if err := c.Authenticate(ctx, c.configConnect); err != nil {
return fmt.Errorf("authentication failure due to: %v", err)
}
log.Debug("Authentication success")
return c.api.DoWithHeaders(ctx, method, uri, headers, body, resp)
}
} else {
log.Error("Error is not a type of \"*types.Error\". Error:", err)
}
log.WithError(err).Error("failed to invoke Unity REST API server")
return err
}
//SetToken function sets token
func (c *Client) SetToken(token string) {
c.api.SetToken(token)
}
//GetToken function gets token
func (c *Client) GetToken() string {
return c.api.GetToken()
}
// NewClient initialize the new REST Client with default options.
func NewClient(ctx context.Context) (client *Client, err error) {
insecure, _ := strconv.ParseBool(os.Getenv("GOUNITY_INSECURE"))
return NewClientWithArgs(ctx, os.Getenv("GOUNITY_ENDPOINT"), insecure)
}
// NewClientWithArgs initialize the new REST Client with the given arguments.
func NewClientWithArgs(ctx context.Context, endpoint string, insecure bool) (client *Client, err error) {
log := util.GetRunIDLogger(ctx)
if showHTTP {
debug = true
}
fields := map[string]interface{}{
"endpoint": endpoint,
"insecure": insecure,
"debug": debug,
"showHTTP": showHTTP,
}
log.WithFields(fields).Debug("unity client init")
if endpoint == "" {
log.WithFields(fields).Error("endpoint is required")
return nil, withFields(fields, "endpoint is required")
}
opts := api.ClientOptions{
Insecure: insecure,
ShowHTTP: showHTTP,
}
ac, err := api.New(ctx, endpoint, opts, debug)
if err != nil {
return nil, fmt.Errorf("unable to create HTTP client %v", err)
}
client = &Client{
api: ac,
configConnect: &ConfigConnect{},
}
conHeader = api.HeaderValContentTypeJSON
return client, nil
}
func withFields(fields map[string]interface{}, message string) error {
return withFieldsE(fields, message, nil)
}
func withFieldsE(fields map[string]interface{}, message string, inner error) error {
if fields == nil {
fields = make(map[string]interface{})
}
if inner != nil {
fields["inner"] = inner
}
x := 0
l := len(fields)
var b bytes.Buffer
for k, v := range fields {
if x < l-1 {
b.WriteString(fmt.Sprintf("%s=%v,", k, v))
} else {
b.WriteString(fmt.Sprintf("%s=%v", k, v))
}
x = x + 1
}
return fmt.Errorf("%s %s", message, b.String())
}