Skip to content

Commit

Permalink
Merge pull request #150 from shogo82148/fix-request-id-generator
Browse files Browse the repository at this point in the history
fix short id generator
  • Loading branch information
shogo82148 authored Dec 5, 2021
2 parents dbe5856 + 0596cea commit 92cb7e5
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 7 deletions.
4 changes: 3 additions & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,9 @@ func drainBody(b io.ReadCloser) (r1, r2 io.ReadCloser, err error) {
// Do not use as a reliable way to get unique IDs, instead use for things like logging.
func shortID() string {
b := make([]byte, 6)
io.ReadFull(rand.Reader, b)
if _, err := io.ReadFull(rand.Reader, b); err != nil {
panic(err)
}
return base64.StdEncoding.EncodeToString(b)
}

Expand Down
4 changes: 3 additions & 1 deletion error.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,8 @@ func asErrorResponse(err error) *ErrorResponse {
// are not catastrophic.
func newErrorID() string {
b := make([]byte, 6)
io.ReadFull(rand.Reader, b)
if _, err := io.ReadFull(rand.Reader, b); err != nil {
panic(err)
}
return base64.StdEncoding.EncodeToString(b)
}
4 changes: 3 additions & 1 deletion middleware/log_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ func LogRequest(verbose bool, sensitiveHeaders ...string) goa.Middleware {
// Do not use as a reliable way to get unique IDs, instead use for things like logging.
func shortID() string {
b := make([]byte, 6)
io.ReadFull(rand.Reader, b)
if _, err := io.ReadFull(rand.Reader, b); err != nil {
panic(err)
}
return base64.StdEncoding.EncodeToString(b)
}

Expand Down
8 changes: 6 additions & 2 deletions middleware/request_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/rand"
"encoding/base64"
"fmt"
"io"
"net/http"
"strings"
"sync/atomic"
Expand All @@ -31,10 +32,13 @@ func init() {
// algorithm taken from https://github.com/zenazn/goji/blob/master/web/middleware/request_id.go#L44-L50
var buf [12]byte
var b64 string
replacer := strings.NewReplacer("+", "", "/", "")
for len(b64) < 10 {
rand.Read(buf[:])
if _, err := io.ReadFull(rand.Reader, buf[:]); err != nil {
panic(err)
}
b64 = base64.StdEncoding.EncodeToString(buf[:])
b64 = strings.NewReplacer("+", "", "/", "").Replace(b64)
b64 = replacer.Replace(b64)
}
reqPrefix = string(b64[0:10])
}
Expand Down
5 changes: 3 additions & 2 deletions middleware/xray/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"crypto/rand"
"fmt"
"io"
"net"
"net/http"
"sync"
Expand Down Expand Up @@ -107,7 +108,7 @@ func New(service, daemon string) (goa.Middleware, error) {
// compatible with AWS X-Ray.
func NewID() string {
b := make([]byte, 8)
if _, err := rand.Read(b); err != nil {
if _, err := io.ReadFull(rand.Reader, b); err != nil {
panic(err)
}
return fmt.Sprintf("%x", b)
Expand All @@ -117,7 +118,7 @@ func NewID() string {
// compatible with AWS X-Ray.
func NewTraceID() string {
b := make([]byte, 12)
if _, err := rand.Read(b); err != nil {
if _, err := io.ReadFull(rand.Reader, b); err != nil {
panic(err)
}
return fmt.Sprintf("%d-%x-%s", 1, time.Now().Unix(), fmt.Sprintf("%x", b))
Expand Down

0 comments on commit 92cb7e5

Please sign in to comment.