Skip to content

Commit 5cf4036

Browse files
committed
extend test; persistent storage
1 parent 137107c commit 5cf4036

File tree

4 files changed

+70
-7
lines changed

4 files changed

+70
-7
lines changed

gateway/api.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,16 @@ func (gw *Gateway) newPolicyPathRoot() (*osutil.Root, error) {
10891089
return osutil.NewRoot(policyPath)
10901090
}
10911091

1092+
func (gw *Gateway) removePersistentPolicyById(id string) error {
1093+
root, err := gw.newPolicyPathRoot()
1094+
1095+
if err != nil {
1096+
return err
1097+
}
1098+
1099+
return root.Remove(id + ".json")
1100+
}
1101+
10921102
func (gw *Gateway) handleAddOrUpdatePolicy(polID string, r *http.Request) (interface{}, int) {
10931103
if gw.GetConfig().Policies.PolicySource == "service" {
10941104
log.Error("Rejected new policy due to PolicySource = service")

gateway/api_test.go

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ func TestPolicyAPI(t *testing.T) {
8585
ts := StartTest(func(cnf *config.Config) {
8686
cnf.Policies.PolicyPath = "."
8787
cnf.Policies.PolicySource = "file"
88+
cnf.DisableCustomIdValidation = true
8889
})
8990

9091
defer ts.Close()
@@ -176,17 +177,27 @@ func TestPolicyAPI(t *testing.T) {
176177
})
177178
})
178179

179-
t.Run("fails if ID contains invalid characters", func(t *testing.T) {
180+
t.Run("GET does not fail if exitent policy has broken ID", func(t *testing.T) {
180181
invalidURLID := "invalid@id"
181182

183+
ts.CreatePolicy(func(p *user.Policy) {
184+
p.ID = "invalid@id"
185+
})
186+
182187
_, err := ts.Run(t, test.TestCase{
183188
Path: "/tyk/policies/" + invalidURLID,
184189
Method: http.MethodGet,
185190
AdminAuth: true,
186-
Code: http.StatusBadRequest,
187-
BodyMatch: errMsgInvalidPolicyID,
191+
Code: http.StatusOK,
188192
})
193+
189194
assert.NoError(t, err)
195+
})
196+
197+
t.Run("fails create/update if ID contains invalid characters", func(t *testing.T) {
198+
ts.setTestScopeConfig(t, func(cnf *config.Config) {
199+
cnf.DisableCustomIdValidation = false
200+
})
190201

191202
invalidBodyPol := user.Policy{
192203
ID: "invalid/id",
@@ -196,7 +207,7 @@ func TestPolicyAPI(t *testing.T) {
196207
AccessRights: make(map[string]user.AccessDefinition),
197208
}
198209

199-
_, err = ts.Run(t, test.TestCase{
210+
_, err := ts.Run(t, test.TestCase{
200211
Path: "/tyk/policies",
201212
Method: http.MethodPost,
202213
AdminAuth: true,
@@ -218,6 +229,42 @@ func TestPolicyAPI(t *testing.T) {
218229
})
219230
assert.NoError(t, err)
220231
})
232+
233+
t.Run("does not fail on create/update if ID contains invalid characters and when DisableCustomIdValidation=true", func(t *testing.T) {
234+
ts.setTestScopeConfig(t, func(cnf *config.Config) {
235+
cnf.DisableCustomIdValidation = true
236+
})
237+
238+
t.Cleanup(func() {
239+
_ = ts.Gw.removePersistentPolicyById("invalid@id") //nolint:errcheck
240+
})
241+
242+
invalidBodyPol := user.Policy{
243+
ID: "invalid@id",
244+
Rate: 100,
245+
Per: 1,
246+
OrgID: "54de205930c55e15bd000001",
247+
AccessRights: make(map[string]user.AccessDefinition),
248+
}
249+
250+
_, err := ts.Run(t, test.TestCase{
251+
Path: "/tyk/policies",
252+
Method: http.MethodPost,
253+
AdminAuth: true,
254+
Data: serializePolicy(t, invalidBodyPol),
255+
Code: http.StatusOK,
256+
})
257+
assert.NoError(t, err)
258+
259+
_, err = ts.Run(t, test.TestCase{
260+
Path: "/tyk/policies/invalid@id",
261+
Method: http.MethodPut,
262+
AdminAuth: true,
263+
Data: serializePolicy(t, invalidBodyPol),
264+
Code: http.StatusOK,
265+
})
266+
assert.NoError(t, err)
267+
})
221268
}
222269

223270
func serializePolicy(t *testing.T, pol user.Policy) string {

gateway/server.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1609,13 +1609,17 @@ func (gw *Gateway) initSystem() error {
16091609
// free resources.
16101610
go cleanIdleMemConnProviders(gw.ctx)
16111611

1612-
gw.validator = validator.New(
1613-
validator.WithDisabledPolicyIdValidation(gwConfig.DisableCustomIdValidation),
1614-
)
1612+
gw.initMembers(gwConfig)
16151613

16161614
return nil
16171615
}
16181616

1617+
func (gw *Gateway) initMembers(cfg config.Config) {
1618+
gw.validator = validator.New(
1619+
validator.WithDisabledPolicyIdValidation(cfg.DisableCustomIdValidation),
1620+
)
1621+
}
1622+
16191623
// SignatureVerifier returns a verifier to use for validating signatures.
16201624
// It is configured with the PublicKeyPath value in gateway config.
16211625
func (gw *Gateway) SignatureVerifier() (goverify.Verifier, error) {

gateway/testutil.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,8 +1127,10 @@ func (s *Test) setTestScopeConfig(t *testing.T, apply func(cnf *config.Config))
11271127
newCnf := reflect.Clone(cnf)
11281128
apply(&newCnf)
11291129
s.Gw.SetConfig(newCnf)
1130+
s.Gw.initMembers(newCnf)
11301131
t.Cleanup(func() {
11311132
s.Gw.SetConfig(cnf)
1133+
s.Gw.initMembers(cnf)
11321134
})
11331135
}
11341136

0 commit comments

Comments
 (0)