/
policyversion.go
181 lines (138 loc) · 5.17 KB
/
policyversion.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
// Copyright (c) 2021 AccelByte Inc. All Rights Reserved.
// This is licensed software from AccelByte Inc, for limitations
// and restrictions contact your company contract manager.
package legal
import (
"encoding/json"
"github.com/patrickmn/go-cache"
"io/ioutil"
"net/http"
"time"
"github.com/cenkalti/backoff"
"github.com/pkg/errors"
)
// nolint: funlen
func (client *DefaultLegalClient) remoteValidatePolicyVersion(listPolicyVersion []string, clientID, country, namespace string) (bool, error) {
req, err := http.NewRequest("GET", client.legalConfig.LegalBaseURL + crucialPolicyVersionPath, nil)
if err != nil {
return false, errors.Wrap(err, "getCrucialPolicyVersion: unable to create new Crucial policy request")
}
b := backoff.NewExponentialBackOff()
b.MaxElapsedTime = maxBackOffTime
var responseStatusCode int
var responseBodyBytes []byte
//nolint: dupl
err = backoff.Retry(
func() error {
var e error
resp, e := client.httpClient.Do(req)
if e != nil {
return backoff.Permanent(e)
}
defer resp.Body.Close()
responseStatusCode = resp.StatusCode
if resp.StatusCode >= http.StatusInternalServerError {
return errors.Errorf("getCrucialPolicyVersion: endpoint returned status code : %v", responseStatusCode)
}
responseBodyBytes, e = ioutil.ReadAll(resp.Body)
if e != nil {
return errors.Wrap(e, "getCrucialPolicyVersion: unable to read response body")
}
return nil
},
b,
)
if err != nil {
return false, errors.Wrap(err, "getCrucialPolicyVersion: unable to do HTTP request to get crucial policy version")
}
if responseStatusCode != http.StatusOK {
return false, errors.Errorf("getCrucialPolicyVersion: unable to get crucial policy version: error code : %d, error message : %s",
responseStatusCode, string(responseBodyBytes))
}
var getCrucialPolicyVersionResponse CrucialPolicyVersionResponse
err = json.Unmarshal(responseBodyBytes, &getCrucialPolicyVersionResponse)
if err != nil {
return false, errors.Wrap(err, "getCrucialPolicyVersion: unable to unmarshal response body")
}
if getCrucialPolicyVersionResponse.AffectedClient == nil {
return true, nil
}
// cache the client id result from remote call
for clientID, affectedPolicyVersion := range getCrucialPolicyVersionResponse.AffectedClient {
client.policyVersionCache.Set(clientID, affectedPolicyVersion, cache.DefaultExpiration)
}
// Check for affected clientID
if !validate(listPolicyVersion, getCrucialPolicyVersionResponse.AffectedClient[clientID], country, namespace, client.legalConfig.PublisherNamespace) {
return false, nil
}
if !validate(listPolicyVersion, getCrucialPolicyVersionResponse.AffectedClient[allAffectedClientID], country, namespace, client.legalConfig.PublisherNamespace) {
return false, nil
}
// all policy versions is accepted, user eligible
log("all crucial policy version accepted")
return true, nil
}
func (client *DefaultLegalClient) getCrucialPolicyVersion() error {
req, err := http.NewRequest("GET", client.legalConfig.LegalBaseURL + crucialPolicyVersionPath, nil)
if err != nil {
return errors.Wrap(err, "getCrucialPolicyVersion: unable to create new Crucial policy request")
}
b := backoff.NewExponentialBackOff()
b.MaxElapsedTime = maxBackOffTime
var responseStatusCode int
var responseBodyBytes []byte
//nolint: dupl
err = backoff.Retry(
func() error {
var e error
resp, e := client.httpClient.Do(req)
if e != nil {
return backoff.Permanent(e)
}
defer resp.Body.Close()
responseStatusCode = resp.StatusCode
if resp.StatusCode >= http.StatusInternalServerError {
return errors.Errorf("getCrucialPolicyVersion: endpoint returned status code : %v", responseStatusCode)
}
responseBodyBytes, e = ioutil.ReadAll(resp.Body)
if e != nil {
return errors.Wrap(e, "getCrucialPolicyVersion: unable to read response body")
}
return nil
},
b,
)
if err != nil {
return errors.Wrap(err, "getCrucialPolicyVersion: unable to do HTTP request to get crucial policy version")
}
if responseStatusCode != http.StatusOK {
return errors.Errorf("getCrucialPolicyVersion: unable to get crucial policy version: error code : %d, error message : %s",
responseStatusCode, string(responseBodyBytes))
}
var getCrucialPolicyVersionResponse CrucialPolicyVersionResponse
err = json.Unmarshal(responseBodyBytes, &getCrucialPolicyVersionResponse)
if err != nil {
return errors.Wrap(err, "getCrucialPolicyVersion: unable to unmarshal response body")
}
client.policyVersion = getCrucialPolicyVersionResponse.AffectedClient
for clientID, affectedPolicyVersion := range getCrucialPolicyVersionResponse.AffectedClient {
client.policyVersionCache.Set(clientID, affectedPolicyVersion, cache.DefaultExpiration)
}
return nil
}
func (client *DefaultLegalClient) refreshCrucialPolicyVersion() {
backOffTime := time.Second
time.Sleep(client.legalConfig.PolicyVersionRefreshInterval)
for {
client.policyVersionRefreshError = client.getCrucialPolicyVersion()
if client.policyVersionRefreshError != nil {
time.Sleep(backOffTime)
if backOffTime < maxBackOffTime {
backOffTime *= 2
}
continue
}
backOffTime = time.Second
time.Sleep(client.legalConfig.PolicyVersionRefreshInterval)
}
}