-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
300 lines (281 loc) · 10 KB
/
config.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
package zpa
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"math"
"net/http"
"net/url"
"os"
"os/user"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
logger "github.com/SecurityGeekIO/zscaler-sdk-go/v2/logger"
rl "github.com/SecurityGeekIO/zscaler-sdk-go/v2/ratelimiter"
"github.com/hashicorp/go-retryablehttp"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging"
)
const (
defaultBaseURL = "https://config.private.zscaler.com"
betaBaseURL = "https://config.zpabeta.net"
govBaseURL = "https://config.zpagov.net"
govUsBaseURL = "https://config.zpagov.us"
previewBaseUrl = "https://config.zpapreview.net"
devBaseUrl = "https://public-api.dev.zpath.net"
devAuthUrl = "https://authn1.dev.zpath.net/authn/v1/oauth/token?grant_type=CLIENT_CREDENTIALS"
qaBaseUrl = "https://config.qa.zpath.net"
qa2BaseUrl = "https://pdx2-zpa-config.qa2.zpath.net"
defaultTimeout = 240 * time.Second
loggerPrefix = "zpa-logger: "
ZPA_CLIENT_ID = "ZPA_CLIENT_ID"
ZPA_CLIENT_SECRET = "ZPA_CLIENT_SECRET"
ZPA_CUSTOMER_ID = "ZPA_CUSTOMER_ID"
ZPA_CLOUD = "ZPA_CLOUD"
configPath string = ".zpa/credentials.json"
)
var defaultBackoffConf = &BackoffConfig{
Enabled: true,
MaxNumOfRetries: 100,
RetryWaitMaxSeconds: 10,
RetryWaitMinSeconds: 2,
}
type BackoffConfig struct {
Enabled bool // Set to true to enable backoff and retry mechanism
RetryWaitMinSeconds int // Minimum time to wait
RetryWaitMaxSeconds int // Maximum time to wait
MaxNumOfRetries int // Maximum number of retries
}
type AuthToken struct {
TokenType string `json:"token_type"`
AccessToken string `json:"access_token"`
}
type CredentialsConfig struct {
ClientID string `json:"zpa_client_id"`
ClientSecret string `json:"zpa_client_secret"`
CustomerID string `json:"zpa_customer_id"`
ZpaCloud string `json:"zpa_cloud"`
}
// Config contains all the configuration data for the API client
type Config struct {
BaseURL *url.URL
httpClient *http.Client
rateLimiter *rl.RateLimiter
// The logger writer interface to write logging messages to. Defaults to standard out.
Logger logger.Logger
// Credentials for basic authentication.
ClientID, ClientSecret, CustomerID, Cloud string
// Backoff config
BackoffConf *BackoffConfig
AuthToken *AuthToken
sync.Mutex
UserAgent string
cacheEnabled bool
freshCache bool
cacheTtl time.Duration
cacheCleanwindow time.Duration
cacheMaxSizeMB int
}
/*
NewConfig returns a default configuration for the client.
By default it will try to read the access and te secret from the environment variable.
*/
// Need to implement exponential back off to comply with the API rate limit. https://help.zscaler.com/zpa/about-rate-limiting
// 20 times in a 10 second interval for a GET call.
// 10 times in a 10 second interval for any POST/PUT/DELETE call.
// TODO Add healthCheck method to NewConfig
func NewConfig(clientID, clientSecret, customerID, cloud, userAgent string) (*Config, error) {
var logger logger.Logger = logger.GetDefaultLogger(loggerPrefix)
// if creds not provided in TF config, try loading from env vars
if clientID == "" || clientSecret == "" || customerID == "" || cloud == "" || userAgent == "" {
clientID = os.Getenv(ZPA_CLIENT_ID)
clientSecret = os.Getenv(ZPA_CLIENT_SECRET)
customerID = os.Getenv(ZPA_CUSTOMER_ID)
cloud = os.Getenv(ZPA_CLOUD)
}
// last resort to configuration file:
if clientID == "" || clientSecret == "" || customerID == "" {
creds, err := loadCredentialsFromConfig(logger)
if err != nil || creds == nil {
return nil, err
}
clientID = creds.ClientID
clientSecret = creds.ClientSecret
customerID = creds.CustomerID
cloud = creds.ZpaCloud
}
rawUrl := defaultBaseURL
if cloud == "" {
cloud = os.Getenv(ZPA_CLOUD)
} else if cloud != "" {
rawUrl = cloud
}
if strings.EqualFold(cloud, "PRODUCTION") {
rawUrl = defaultBaseURL
} else if strings.EqualFold(cloud, "BETA") {
rawUrl = betaBaseURL
} else if strings.EqualFold(cloud, "GOV") {
rawUrl = govBaseURL
} else if strings.EqualFold(cloud, "GOVUS") {
rawUrl = govUsBaseURL
} else if strings.EqualFold(cloud, "PREVIEW") {
rawUrl = previewBaseUrl
} else if strings.EqualFold(cloud, "DEV") {
rawUrl = devBaseUrl
} else if strings.EqualFold(cloud, "QA") {
rawUrl = qaBaseUrl
} else if strings.EqualFold(cloud, "QA2") {
rawUrl = qa2BaseUrl
}
baseURL, err := url.Parse(rawUrl)
if err != nil {
logger.Printf("[ERROR] error occurred while configuring the client: %v", err)
}
cacheDisabled, _ := strconv.ParseBool(os.Getenv("ZSCALER_SDK_CACHE_DISABLED"))
return &Config{
BaseURL: baseURL,
Logger: logger,
httpClient: nil,
ClientID: clientID,
ClientSecret: clientSecret,
CustomerID: customerID,
Cloud: cloud,
BackoffConf: defaultBackoffConf,
UserAgent: userAgent,
rateLimiter: rl.NewRateLimiter(20, 10, 10, 10),
cacheEnabled: !cacheDisabled,
cacheTtl: time.Minute * 10,
cacheCleanwindow: time.Minute * 8,
cacheMaxSizeMB: 0,
}, err
}
func (c *Config) WithCache(cache bool) {
c.cacheEnabled = cache
}
func (c *Config) WithCacheTtl(i time.Duration) {
c.cacheTtl = i
}
func (c *Config) WithCacheCleanWindow(i time.Duration) {
c.cacheCleanwindow = i
}
func (c *Config) SetBackoffConfig(backoffConf BackoffConfig) {
c.BackoffConf = &backoffConf
}
// loadCredentialsFromConfig Returns the credentials found in a config file
func loadCredentialsFromConfig(logger logger.Logger) (*CredentialsConfig, error) {
usr, _ := user.Current()
dir := usr.HomeDir
path := filepath.Join(dir, configPath)
logger.Printf("[INFO]Loading configuration file at:%s", path)
file, err := os.Open(path)
if err != nil {
return nil, errors.New("Could not open credentials file, needs to contain one json object with keys: zpa_client_id, zpa_client_secret, zpa_customer_id, and zpa_cloud. " + err.Error())
}
configBytes, err := io.ReadAll(file)
if err != nil {
return nil, err
}
var config CredentialsConfig
err = json.Unmarshal(configBytes, &config)
if err != nil || config.ClientID == "" || config.ClientSecret == "" || config.CustomerID == "" || config.ZpaCloud == "" {
return nil, fmt.Errorf("could not parse credentials file, needs to contain one json object with keys: zpa_client_id, zpa_client_secret, zpa_customer_id, and zpa_cloud. error: %v", err)
}
return &config, nil
}
func (c *Config) GetHTTPClient() *http.Client {
if c.httpClient == nil {
if c.BackoffConf != nil && c.BackoffConf.Enabled {
retryableClient := retryablehttp.NewClient()
retryableClient.Logger = c.Logger
retryableClient.RetryWaitMin = time.Second * time.Duration(c.BackoffConf.RetryWaitMinSeconds)
retryableClient.Backoff = func(min, max time.Duration, attemptNum int, resp *http.Response) time.Duration {
if resp != nil {
if resp.StatusCode == http.StatusTooManyRequests || resp.StatusCode == http.StatusServiceUnavailable {
// TODO: ask backend to implement such header, instead of using the logic below
if s, ok := resp.Header["Retry-After"]; ok {
if sleep, err := strconv.ParseInt(s[0], 10, 64); err == nil {
return time.Second * time.Duration(sleep)
}
}
}
if resp.Request != nil {
wait, duration := c.rateLimiter.Wait(resp.Request.Method)
if wait {
c.Logger.Printf("[INFO] rate limiter wait duration:%s\n", duration.String())
} else {
return 0
}
}
}
// default to exp backoff
mult := math.Pow(2, float64(attemptNum)) * float64(min)
sleep := time.Duration(mult)
if float64(sleep) != mult || sleep > max {
sleep = max
}
return sleep
}
retryableClient.RetryWaitMax = time.Second * time.Duration(c.BackoffConf.RetryWaitMaxSeconds)
retryableClient.RetryMax = c.BackoffConf.MaxNumOfRetries
retryableClient.HTTPClient.Transport = logging.NewSubsystemLoggingHTTPTransport("gozscaler", retryableClient.HTTPClient.Transport)
retryableClient.CheckRetry = checkRetry
retryableClient.HTTPClient.Timeout = defaultTimeout
c.httpClient = retryableClient.StandardClient()
} else {
c.httpClient = &http.Client{
Timeout: defaultTimeout,
}
}
}
return c.httpClient
}
func containsInt(codes []int, code int) bool {
for _, a := range codes {
if a == code {
return true
}
}
return false
}
// getRetryOnStatusCodes return a list of http status codes we want to apply retry on.
// return empty slice to enable retry on all connection & server errors.
// or return []int{429} to retry on only TooManyRequests error
func getRetryOnStatusCodes() []int {
return []int{http.StatusTooManyRequests}
}
// Used to make http client retry on provided list of response status codes
func checkRetry(ctx context.Context, resp *http.Response, err error) (bool, error) {
// do not retry on context.Canceled or context.DeadlineExceeded
if ctx.Err() != nil {
return false, ctx.Err()
}
if resp != nil && containsInt(getRetryOnStatusCodes(), resp.StatusCode) {
return true, nil
}
if resp != nil && resp.StatusCode == http.StatusBadRequest {
respMap := map[string]string{}
data, err := io.ReadAll(resp.Body)
resp.Body = io.NopCloser(bytes.NewBuffer(data))
if err == nil {
_ = json.Unmarshal(data, &respMap)
if errorID, ok := respMap["id"]; ok && (errorID == "non.restricted.entity.authorization.failed" || errorID == "bad.request") {
return true, nil
}
}
// Implemented to handle upstream restrictions on simultaneous requests when dealing with CRUD operations, related to ZPA Access policy rule order
// ET-53585: https://jira.corp.zscaler.com/browse/ET-53585
// ET-48860: https://confluence.corp.zscaler.com/display/ET/ET-48860+incorrect+rules+order
if err == nil {
_ = json.Unmarshal(data, &respMap)
if errorID, ok := respMap["id"]; ok && (errorID == "db.simultaneous.request" || errorID == "bad.request") {
return true, nil
}
}
}
return retryablehttp.DefaultRetryPolicy(ctx, resp, err)
}