Skip to content

Commit

Permalink
AUT-90: ensure request IDs are unique (#920)
Browse files Browse the repository at this point in the history
* add uuid module to prep for better request ids

* feat: use uuids for request ids to guarantee uniqueness
  • Loading branch information
bhearsum authored Jul 15, 2024
1 parent 81482ca commit dd722f2
Show file tree
Hide file tree
Showing 26 changed files with 1,495 additions and 6 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ require (
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6 // indirect
github.com/golang/protobuf v1.3.2 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/gax-go/v2 v2.0.5 // indirect
github.com/goware/prefixer v0.0.0-20160118172347-395022866408 // indirect
github.com/howeyc/gopass v0.0.0-20170109162249-bf9dde6d0d2c // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXi
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
github.com/google/shlex v0.0.0-20181106134648-c34317bd91bf/go.mod h1:RpwtwJQFrIEPstU94h88MWPXP2ektJZ8cZ0YntAmXiE=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5 h1:sjZBwGj9Jlw33ImPtvFviGYvseOtDM7hkSKB7+Tv3SM=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
Expand Down
18 changes: 12 additions & 6 deletions middleware.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package main

import (
"math/rand"
"net/http"
"time"

"github.com/google/uuid"
)

// Middleware wraps an http.Handler with additional functionality
Expand All @@ -27,13 +28,18 @@ func setResponseHeaders() Middleware {
func setRequestID() Middleware {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rid := make([]rune, 16)
letters := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
for i := range rid {
rid[i] = letters[rand.Intn(len(letters))]
// NewV7 is used instead of New because the latter will panic
// if can't generate a UUID. It's preferably for us to have
// worse request ids than panic.
uuid, err := uuid.NewV7()
var rid string
if err != nil {
rid = "-"
} else {
rid = uuid.String()
}

h.ServeHTTP(w, addToContext(r, contextKeyRequestID, string(rid)))
h.ServeHTTP(w, addToContext(r, contextKeyRequestID, rid))
})
}
}
Expand Down
30 changes: 30 additions & 0 deletions middleware_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

package main

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/google/uuid"
)

func TestRequestIDWellFormed(t *testing.T) {
// This method of testing middleware is cribbed from
// https://stackoverflow.com/questions/51201056/testing-golang-middleware-that-modifies-the-request
nextHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
val := r.Context().Value(contextKeyRequestID).(string)
if uuid.Validate(val) != nil {
t.Errorf("requestID is not a valid uuid! %v", val)
}
})

handlerToTest := setRequestID()(nextHandler)

req := httptest.NewRequest("GET", "http://foo.bar/", nil)

handlerToTest.ServeHTTP(httptest.NewRecorder(), req)
}
41 changes: 41 additions & 0 deletions vendor/github.com/google/uuid/CHANGELOG.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions vendor/github.com/google/uuid/CONTRIBUTING.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions vendor/github.com/google/uuid/CONTRIBUTORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions vendor/github.com/google/uuid/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions vendor/github.com/google/uuid/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 80 additions & 0 deletions vendor/github.com/google/uuid/dce.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions vendor/github.com/google/uuid/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 59 additions & 0 deletions vendor/github.com/google/uuid/hash.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit dd722f2

Please sign in to comment.