Skip to content

Commit

Permalink
Update to latest WebRPC (#74)
Browse files Browse the repository at this point in the history
* Update deps

* Test IPV6

* Bump version

* ridlfmt
  • Loading branch information
klaidliadon authored Nov 29, 2024
1 parent cc81153 commit f74bab2
Show file tree
Hide file tree
Showing 13 changed files with 491 additions and 295 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.23.2
// replace github.com/0xsequence/authcontrol => ../authcontrol

require (
github.com/0xsequence/authcontrol v0.3.2
github.com/0xsequence/authcontrol v0.3.3
github.com/0xsequence/go-sequence v0.43.0
github.com/alicebob/miniredis/v2 v2.33.0
github.com/go-chi/chi/v5 v5.1.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/0xsequence/authcontrol v0.3.2 h1:oiLR2fNVEpeJu3SF1cWHQORRAVs1fRj/K+wRe/OJ5Mo=
github.com/0xsequence/authcontrol v0.3.2/go.mod h1:wicKJcmJYJU6jn07JBMqwxcAzyFm2BtfAzWpnL/40g8=
github.com/0xsequence/authcontrol v0.3.3 h1:aGRukGP/PFwrvKBXsjuMhxiXnqjHa+fchL+QwKQJgTU=
github.com/0xsequence/authcontrol v0.3.3/go.mod h1:wicKJcmJYJU6jn07JBMqwxcAzyFm2BtfAzWpnL/40g8=
github.com/0xsequence/ethkit v1.28.0 h1:11p4UXXvYnixQk01+qmAcOF71N9DlSeMcEMbaCPtjaY=
github.com/0xsequence/ethkit v1.28.0/go.mod h1:rv0FAIyEyN0hhwGefbduAz4ujmyjyJXhCd6a0/yF3tk=
github.com/0xsequence/go-sequence v0.43.0 h1:PErMuTg4PeaamJutEJ6tAjrFBA8z0t6lvT9LOVC5RMs=
Expand Down
3 changes: 3 additions & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,11 @@ github.com/supranational/blst v0.3.12/go.mod h1:jZJtfjgudtNl4en1tzwPIV3KjUnQUvG3
github.com/supranational/blst v0.3.13/go.mod h1:jZJtfjgudtNl4en1tzwPIV3KjUnQUvG3/j+w+fVonLw=
github.com/tyler-smith/go-bip39 v1.1.0/go.mod h1:gUYDtqQw1JS3ZJ8UWVcGTGqqr6YIN3CWg+kkNaLt55U=
github.com/webrpc/gen-golang v0.15.0/go.mod h1:qy1qEWMlTvrRzjSuQLy+176RqNaX1ymUULDtlo7Dapo=
github.com/webrpc/gen-golang v0.17.0/go.mod h1:qy1qEWMlTvrRzjSuQLy+176RqNaX1ymUULDtlo7Dapo=
github.com/webrpc/gen-typescript v0.14.1/go.mod h1:xQzYnVaSMfcygDXA5SuW8eYyCLHBHkj15wCF7gcJF5Y=
github.com/webrpc/gen-typescript v0.16.1/go.mod h1:xQzYnVaSMfcygDXA5SuW8eYyCLHBHkj15wCF7gcJF5Y=
github.com/webrpc/webrpc v0.20.3/go.mod h1:nplt2iyIXMsMCGyQx1NgEXwQOifWqKNLXcFnQnG9dBU=
github.com/webrpc/webrpc v0.22.0/go.mod h1:eeABnLz9BC4F9GGw6UKebVPkzkFYLrZRlcOvh6o8n10=
go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo=
go.opentelemetry.io/contrib/detectors/gcp v1.29.0/go.mod h1:GW2aWZNwR2ZxDLdv8OyC2G8zkRoQBuURgV7RPQgcPoU=
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.54.0/go.mod h1:B9yO6b04uB80CzjedvewuqDhxJxi11s7/GtiGa8bAjI=
Expand Down
63 changes: 46 additions & 17 deletions middleware/middleware_ratelimit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ import (
"github.com/stretchr/testify/assert"
)

func makeIP(ipv6 bool) net.IP {
size := 4
if ipv6 {
size = 16
}
buf := make([]byte, size)
for i := 0; i < size/4; i++ {
binary.LittleEndian.PutUint32(buf[i*4:], rand.Uint32())
}
return net.IP(buf)
}

func TestRateLimiter(t *testing.T) {
const (
_CustomErrorMessage = "Custom error message"
Expand All @@ -37,25 +49,42 @@ func TestRateLimiter(t *testing.T) {
}, nil, middleware.Options{ErrHandler: eh})
handler := rl(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))

buf := make([]byte, 4)
for i := 0; i < 10; i++ {
ip := rand.Uint32()
binary.LittleEndian.PutUint32(buf, ip)
ipAddress := makeIP(false).String()
for i := 0; i < 20; i++ {
req, _ := http.NewRequest("GET", "/", nil)
req.RemoteAddr = ipAddress
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
if i < 10 {
assert.Equal(t, http.StatusOK, w.Code)
continue
}
assert.Equal(t, http.StatusTooManyRequests, w.Code)
assert.Equal(t, _TestHeaderValue, w.Header().Get(_TestHeader))
err := proto.WebRPCError{}
assert.Nil(t, json.Unmarshal(w.Body.Bytes(), &err))
assert.Contains(t, err.Message, _CustomErrorMessage)
}
}
ipAddress := net.IP(buf).String()
for i := 0; i < 20; i++ {
req, _ := http.NewRequest("GET", "/", nil)
req.RemoteAddr = ipAddress
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
if i < 10 {
assert.Equal(t, http.StatusOK, w.Code)
continue

for i := 0; i < 80; i++ {
ipAddress := makeIP(true).String()
for i := 0; i < 20; i++ {
req, _ := http.NewRequest("GET", "/", nil)
req.RemoteAddr = ipAddress
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
if i < 10 {
assert.Equal(t, http.StatusOK, w.Code)
continue
}
assert.Equal(t, http.StatusTooManyRequests, w.Code)
assert.Equal(t, _TestHeaderValue, w.Header().Get(_TestHeader))
err := proto.WebRPCError{}
assert.Nil(t, json.Unmarshal(w.Body.Bytes(), &err))
assert.Contains(t, err.Message, _CustomErrorMessage)
}
assert.Equal(t, http.StatusTooManyRequests, w.Code)
assert.Equal(t, _TestHeaderValue, w.Header().Get(_TestHeader))
err := proto.WebRPCError{}
assert.Nil(t, json.Unmarshal(w.Body.Bytes(), &err))
assert.Contains(t, err.Message, _CustomErrorMessage)
}

}
14 changes: 14 additions & 0 deletions proto/authcontrol.errors.ridl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
webrpc = v1

name = authcontrol
version = v0.9.1

error 1000 Unauthorized "Unauthorized access" HTTP 401
error 1001 PermissionDenied "Permission denied" HTTP 403
error 1002 SessionExpired "Session expired" HTTP 403
error 1003 MethodNotFound "Method not found" HTTP 404
error 1004 RequestConflict "Conflict with target resource" HTTP 409
error 1005 Aborted "Request aborted" HTTP 400
error 1006 Geoblocked "Geoblocked region" HTTP 451
error 1007 RateLimited "Rate-limited. Please slow down." HTTP 429
error 1008 ProjectNotFound "Project not found" HTTP 401
17 changes: 17 additions & 0 deletions proto/errors.ridl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
webrpc = v1

# 1100-1199: AccessKey/Project errors
error 1101 AccessKeyNotFound "Access key not found" HTTP 401
error 1102 AccessKeyMismatch "Access key mismatch" HTTP 409
error 1103 InvalidOrigin "Invalid origin for Access Key" HTTP 403
error 1104 InvalidService "Service not enabled for Access key" HTTP 403
error 1105 UnauthorizedUser "Unauthorized user" HTTP 403
# 1200-1299: Limit errors
error 1200 QuotaExceeded "Quota request exceeded" HTTP 429
error 1201 QuotaRateLimit "Quota rate limit exceeded" HTTP 429
# 1300-1399: Access Key management errors
error 1300 NoDefaultKey "No default access key found" HTTP 403
error 1301 MaxAccessKeys "Access keys limit reached" HTTP 403
error 1302 AtLeastOneKey "You need at least one Access Key" HTTP 403
# 1900-1999: Other errors
error 1900 Timeout "Request timed out" HTTP 408
4 changes: 2 additions & 2 deletions proto/proto.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:generate go run github.com/webrpc/webrpc/cmd/webrpc-gen -schema=quotacontrol.ridl -target=golang@v0.15.0 -pkg=proto -server -client -out=./quotacontrol.gen.go
//go:generate go run github.com/webrpc/webrpc/cmd/webrpc-gen -schema=quotacontrol.ridl -target=typescript@v0.12.0 -client -out=./quotacontrol.gen.ts
//go:generate go run github.com/webrpc/webrpc/cmd/webrpc-gen -schema=quotacontrol.ridl -target=golang@v0.17.0 -pkg=proto -server -client -out=./quotacontrol.gen.go
//go:generate go run github.com/webrpc/webrpc/cmd/webrpc-gen -schema=quotacontrol.ridl -target=typescript@v0.16.1 -client -out=./quotacontrol.gen.ts
package proto

import (
Expand Down
Loading

0 comments on commit f74bab2

Please sign in to comment.