-
Notifications
You must be signed in to change notification settings - Fork 13
/
configuration.go
500 lines (430 loc) · 16.2 KB
/
configuration.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
package platformclientv2
import (
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"regexp"
"strings"
"sync"
"time"
)
// Configuration has settings to configure the SDK
type Configuration struct {
UserName string `json:"userName,omitempty"`
Password string `json:"password,omitempty"`
APIKeyPrefix map[string]string `json:"APIKeyPrefix,omitempty"`
APIKey map[string]string `json:"APIKey,omitempty"`
OAuthToken string `json:"oAuthToken,omitempty"`
Timeout int `json:"timeout,omitempty"`
BasePath string `json:"basePath,omitempty"`
Host string `json:"host,omitempty"`
Scheme string `json:"scheme,omitempty"`
AccessToken string `json:"accessToken,omitempty"`
RefreshToken string `json:"refreshToken,omitempty"`
ClientID string `json:"clientId,omitempty"`
ClientSecret string `json:"clientSecret,omitempty"`
ShouldRefreshAccessToken bool `json:"shouldRefreshAccessToken,omitempty"`
RefreshInProgress int64 `json:"refreshInProgress,omitempty"`
RefreshTokenWaitTime int `json:"refreshTokenWaitTime,omitempty"`
DefaultHeader map[string]string `json:"defaultHeader,omitempty"`
UserAgent string `json:"userAgent,omitempty"`
APIClient APIClient `json:"APIClient,omitempty"`
RetryConfiguration *RetryConfiguration `json:"retryConfiguration,omitempty"`
LoggingConfiguration *LoggingConfiguration `json:"loggingConfiguration,omitempty"`
ConfigFilePath string `json:"configFilePath,omitempty"`
AutoReloadConfig bool `json:"autoReloadConfig,omitempty"`
}
const (
USEast1 = "https://api.mypurecloud.com"
EUWest1 = "https://api.mypurecloud.ie"
APSoutheast2 = "https://api.mypurecloud.com.au"
APNortheast1 = "https://api.mypurecloud.jp"
EUCentral1 = "https://api.mypurecloud.de"
USWest2 = "https://api.usw2.pure.cloud"
CACentral1 = "https://api.cac1.pure.cloud"
APNortheast2 = "https://api.apne2.pure.cloud"
EUWest2 = "https://api.euw2.pure.cloud"
APSouth1 = "https://api.aps1.pure.cloud"
USEast2 = "https://api.use2.us-gov-pure.cloud"
)
// RetryConfiguration has settings to configure the SDK retry logic
type RetryConfiguration struct {
RetryWaitMin time.Duration `json:"retry_wait_min,omitempty"`
RetryWaitMax time.Duration `json:"retry_wait_max,omitempty"`
RetryMax int `json:"retry_max,omitempty"`
RequestLogHook RequestLogHook `json:"request_log_hook,omitempty"`
}
type RequestLogHook func(*http.Request, int)
// LoggingConfiguration has settings to configure the SDK logging
type LoggingConfiguration struct {
LogLevel LoggingLevel `json:"logLevel,omitempty"`
LogRequestBody bool `json:"logRequestBody,omitempty"`
LogResponseBody bool `json:"logResponseBody,omitempty"`
logFormat LoggingFormat
logFilePath string
logToConsole bool
}
// AuthResponse contains the access token to use in future requests
type AuthResponse struct {
AccessToken string `json:"access_token,omitempty"`
RefreshToken string `json:"refresh_token,omitempty"`
TokenType string `json:"token_type,omitempty"`
ExpiresIn int `json:"expires_in,omitempty"`
}
// AuthErrorResponse gives you some intel when authorization goes boom
type AuthErrorResponse struct {
Error string `json:"error,omitempty"`
Description string `json:"description,omitempty"`
ErrorDescription string `json:"error_description,omitempty"`
}
var (
once sync.Once
instance *Configuration
)
// GetDefaultConfiguration returns the shared default Configuration instance
func GetDefaultConfiguration() *Configuration {
once.Do(func() {
instance = NewConfiguration()
})
return instance
}
// NewConfiguration returns a new Configuration instance
func NewConfiguration() *Configuration {
homeDir, _ := os.UserHomeDir()
// Make initial Configuration instance with default values
c := generateDefaultConfig(filepath.Join(homeDir, ".genesyscloudgo", "config"), true)
_ = c.updateConfigFromFile()
if c.AutoReloadConfig {
go c.periodicConfigUpdater()
}
c.LoggingConfiguration.configureLogging()
c.APIClient = NewAPIClient(c)
return c
}
// GetDefaultConfigurationWithConfigFile returns the shared default Configuration instance with overrides provided by a config file
func GetDefaultConfigurationWithConfigFile(filePath string, autoReload bool) *Configuration {
once.Do(func() {
instance = NewConfigurationWithConfigFile(filePath, autoReload)
})
return instance
}
// NewConfigurationWithConfigFile returns a new Configuration instance with overrides provided by a config file
func NewConfigurationWithConfigFile(filePath string, autoReload bool) *Configuration {
// Make initial Configuration instance with default values
c := generateDefaultConfig(filePath, autoReload)
_ = c.updateConfigFromFile()
if c.AutoReloadConfig {
go c.periodicConfigUpdater()
}
c.LoggingConfiguration.configureLogging()
c.APIClient = NewAPIClient(c)
return c
}
func generateDefaultConfig(filePath string, autoReload bool) *Configuration {
return &Configuration{
BasePath: "https://api.mypurecloud.com",
UserName: "",
LoggingConfiguration: &LoggingConfiguration{
LogLevel: LNone,
logFormat: Text,
logToConsole: true,
},
RetryConfiguration: &RetryConfiguration{
RetryMax: 0,
RetryWaitMax: time.Duration(0),
},
ShouldRefreshAccessToken: true,
RefreshTokenWaitTime: 10,
DefaultHeader: make(map[string]string),
APIKey: make(map[string]string),
APIKeyPrefix: make(map[string]string),
UserAgent: "PureCloud SDK",
ConfigFilePath: filePath,
AutoReloadConfig: autoReload,
}
}
func isJsonFile(filePath string) (bool, error) {
s, err := ioutil.ReadFile(filePath)
if err != nil {
return false, err
}
var js map[string]interface{}
return json.Unmarshal(s, &js) == nil, nil
}
func (c *Configuration) updateConfigFromFile() error {
isJson, err := isJsonFile(c.ConfigFilePath)
if err != nil {
return err
}
if isJson {
viper.SetConfigType("json")
} else {
viper.SetConfigType("ini")
}
viper.SetConfigFile(c.ConfigFilePath)
if err := viper.ReadInConfig(); err != nil {
return err
}
// logging
logLevel := getConfigString("logging", "log_level")
if logLevel != "" {
c.LoggingConfiguration.LogLevel = *(loggingLevelFromString(logLevel))
}
logFormat := getConfigString("logging", "log_format")
if logFormat != "" {
c.LoggingConfiguration.logFormat = *(loggingFormatFromString(logFormat))
}
// Only update if the value is present
if getConfigString("logging", "log_to_console") != "" {
c.LoggingConfiguration.logToConsole = getConfigBool("logging", "log_to_console")
}
logFilePath := getConfigString("logging", "log_file_path")
if logFilePath != "" {
c.LoggingConfiguration.logFilePath = logFilePath
}
if getConfigString("logging", "log_request_body") != "" {
c.LoggingConfiguration.LogRequestBody = getConfigBool("logging", "log_request_body")
}
if getConfigString("logging", "log_response_body") != "" {
c.LoggingConfiguration.LogResponseBody = getConfigBool("logging", "log_response_body")
}
c.LoggingConfiguration.configureLogging()
// general
host := getConfigString("general", "host")
if host != "" {
c.BasePath = host
}
if getConfigString("general", "live_reload_config") != "" {
c.AutoReloadConfig = getConfigBool("general", "live_reload_config")
}
// reauthentication
if getConfigString("reauthentication", "refresh_access_token") != "" {
c.ShouldRefreshAccessToken = getConfigBool("reauthentication", "refresh_access_token")
}
if getConfigString("reauthentication", "refresh_token_wait_max") != "" {
c.RefreshTokenWaitTime = getConfigInt("reauthentication", "refresh_token_wait_max")
}
// retry
if getConfigString("retry", "retry_wait_min") != "" {
c.RetryConfiguration.RetryWaitMin = time.Duration(getConfigInt("retry", "retry_wait_min")) * time.Second
}
if getConfigString("retry", "retry_wait_max") != "" {
c.RetryConfiguration.RetryWaitMax = time.Duration(getConfigInt("retry", "retry_wait_max")) * time.Second
}
if getConfigString("retry", "retry_max") != "" {
c.RetryConfiguration.RetryMax = getConfigInt("retry", "retry_max")
}
return nil
}
func getConfigString(section, key string) string {
value := viper.GetString(fmt.Sprintf("%s.%s", section, key))
return strings.Trim(fmt.Sprintf("%s", value), " ")
}
func getConfigBool(section, key string) bool {
return viper.GetBool(fmt.Sprintf("%s.%s", section, key))
}
func getConfigInt(section, key string) int {
return viper.GetInt(fmt.Sprintf("%s.%s", section, key))
}
func (c *Configuration) periodicConfigUpdater() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return
}
defer watcher.Close()
done := make(chan bool)
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Op&fsnotify.Write == fsnotify.Write ||
event.Op&fsnotify.Create == fsnotify.Create {
if !c.AutoReloadConfig {
return
}
// Only act on updates to the config file.
if event.Name == c.ConfigFilePath {
_ = c.updateConfigFromFile()
}
}
case _, ok := <-watcher.Errors:
if !ok {
return
}
}
}
}()
watchedDirectory := filepath.Dir(c.ConfigFilePath)
err = watcher.Add(watchedDirectory)
if err != nil {
return
}
<-done
}
func getFileHash(filePath string) (string, error) {
hasher := sha256.New()
s, err := ioutil.ReadFile(filePath)
if err != nil {
return "", err
}
hasher.Write(s)
return hex.EncodeToString(hasher.Sum(nil)), nil
}
// AuthorizeClientCredentials authorizes this Configuration instance using client credentials.
// The access token will be set automatically and API instances using this configuration object can now make authorized requests.
func (c *Configuration) AuthorizeClientCredentials(clientID string, clientSecret string) error {
authHostRegex := regexp.MustCompile(`(?i)\/\/api\.`)
authHost := authHostRegex.ReplaceAllString(c.BasePath, "//login.")
headerParams := make(map[string]string)
headerParams["Authorization"] = "Basic " + base64.StdEncoding.EncodeToString([]byte(clientID+":"+clientSecret))
formParams := url.Values{}
formParams["grant_type"] = []string{"client_credentials"}
response, err := c.APIClient.CallAPI(authHost+"/oauth/token", "POST", nil, headerParams, nil, formParams, "", nil)
if err != nil {
fmt.Println(err)
return err
}
if response.StatusCode != http.StatusOK {
var authErrorResponse *AuthErrorResponse
err = json.Unmarshal([]byte(response.RawBody), &authErrorResponse)
if err != nil {
return err
}
return fmt.Errorf("Auth Error: %v (%v - %v)", authErrorResponse.Description, authErrorResponse.Error, authErrorResponse.ErrorDescription)
}
var authResponse *AuthResponse
err = json.Unmarshal([]byte(response.RawBody), &authResponse)
if err != nil {
return err
}
c.AccessToken = authResponse.AccessToken
if c.AccessToken == "" {
return fmt.Errorf("Auth Error: No access token found")
}
return nil
}
// AuthorizeCodeGrant authorizes this Configuration instance using an authorization code grant.
// The access and refresh tokens will be set automatically and API instances using this configuration object can now make authorized requests.
func (c *Configuration) AuthorizeCodeGrant(clientID string, clientSecret string, authCode string, redirectUri string) (*AuthResponse, error) {
c.ClientID = clientID
c.ClientSecret = clientSecret
authHostRegex := regexp.MustCompile(`(?i)\/\/api\.`)
authHost := authHostRegex.ReplaceAllString(c.BasePath, "//login.")
headerParams := make(map[string]string)
headerParams["Authorization"] = "Basic " + base64.StdEncoding.EncodeToString([]byte(clientID+":"+clientSecret))
headerParams["Content-Type"] = "application/x-www-form-urlencoded"
formParams := url.Values{}
formParams["grant_type"] = []string{"authorization_code"}
formParams["code"] = []string{authCode}
formParams["redirect_uri"] = []string{redirectUri}
response, err := c.APIClient.CallAPI(authHost+"/oauth/token", "POST", nil, headerParams, nil, formParams, "", nil)
if err != nil {
fmt.Println(err)
return nil, err
}
if response.StatusCode != http.StatusOK {
var authErrorResponse *AuthErrorResponse
err = json.Unmarshal([]byte(response.RawBody), &authErrorResponse)
if err != nil {
return nil, err
}
return nil, fmt.Errorf("Auth Error: %v (%v - %v)", authErrorResponse.Description, authErrorResponse.Error, authErrorResponse.ErrorDescription)
}
var authResponse *AuthResponse
err = json.Unmarshal([]byte(response.RawBody), &authResponse)
if err != nil {
return nil, err
}
c.AccessToken = authResponse.AccessToken
if c.AccessToken == "" {
return nil, fmt.Errorf("Auth Error: No access token found")
}
c.RefreshToken = authResponse.RefreshToken
if c.RefreshToken == "" {
return nil, fmt.Errorf("Auth Error: No refresh token found")
}
return authResponse, nil
}
// RefreshAuthorizationCodeGrant requests a new access token for the authorization code grant.
// The access and refresh tokens will be set automatically and API instances using this configuration object can continue to make authorized requests.
func (c *Configuration) RefreshAuthorizationCodeGrant(clientID string, clientSecret string, refreshToken string) (*AuthResponse, error) {
authHostRegex := regexp.MustCompile(`(?i)\/\/api\.`)
authHost := authHostRegex.ReplaceAllString(c.BasePath, "//login.")
headerParams := make(map[string]string)
headerParams["Authorization"] = "Basic " + base64.StdEncoding.EncodeToString([]byte(clientID+":"+clientSecret))
headerParams["Content-Type"] = "application/x-www-form-urlencoded"
formParams := url.Values{}
formParams["grant_type"] = []string{"refresh_token"}
formParams["refresh_token"] = []string{refreshToken}
response, err := c.APIClient.CallAPI(authHost+"/oauth/token", "POST", nil, headerParams, nil, formParams, "", nil)
if err != nil {
fmt.Println(err)
return nil, err
}
if response.StatusCode != http.StatusOK {
var authErrorResponse *AuthErrorResponse
err = json.Unmarshal([]byte(response.RawBody), &authErrorResponse)
if err != nil {
return nil, err
}
return nil, fmt.Errorf("Auth Error: %v (%v - %v)", authErrorResponse.Description, authErrorResponse.Error, authErrorResponse.ErrorDescription)
}
var authResponse *AuthResponse
err = json.Unmarshal([]byte(response.RawBody), &authResponse)
if err != nil {
return nil, err
}
c.AccessToken = authResponse.AccessToken
if c.AccessToken == "" {
return nil, fmt.Errorf("Auth Error: No access token found")
}
c.RefreshToken = authResponse.RefreshToken
if c.RefreshToken == "" {
return nil, fmt.Errorf("Auth Error: No refresh token found")
}
return authResponse, nil
}
// AddDefaultHeader sets a header that will be set on every request
func (c *Configuration) AddDefaultHeader(key string, value string) {
c.DefaultHeader[key] = value
}
// GetAPIKeyWithPrefix appends a prefix to the API key
func (c *Configuration) GetAPIKeyWithPrefix(APIKeyIdentifier string) string {
if c.APIKeyPrefix[APIKeyIdentifier] != "" {
return c.APIKeyPrefix[APIKeyIdentifier] + " " + c.APIKey[APIKeyIdentifier]
}
return c.APIKey[APIKeyIdentifier]
}
func (c *LoggingConfiguration) SetLogFormat(logFormat LoggingFormat) {
c.logFormat = logFormat
c.configureLogging()
}
func (c *LoggingConfiguration) GetLogFormat() LoggingFormat {
return c.logFormat
}
func (c *LoggingConfiguration) SetLogFilePath(logFilePath string) {
c.logFilePath = logFilePath
c.configureLogging()
}
func (c *LoggingConfiguration) GetLogFilePath() string {
return c.logFilePath
}
func (c *LoggingConfiguration) SetLogToConsole(logToConsole bool) {
c.logToConsole = logToConsole
c.configureLogging()
}
func (c *LoggingConfiguration) GetLogToConsole() bool {
return c.logToConsole
}