-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Christoph Hartmann <[email protected]>
- Loading branch information
1 parent
ec9c0e4
commit 9e77328
Showing
4 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package scope | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/google/uuid" | ||
"go.mondoo.com/ranger-rpc" | ||
) | ||
|
||
const XRequestID = "X-Request-ID" | ||
|
||
type RequestIdOption func(s *requestIdPlugin) | ||
|
||
func WithUuid(requestId string) RequestIdOption { | ||
return func(s *requestIdPlugin) { | ||
s.requestID = requestId | ||
} | ||
} | ||
|
||
// NewRequestIDPlugin creates a RangerPlugin that adds the | ||
// X-Request-ID header to outgoing requests. All RPCs | ||
// that share the same RangerPlugin will get the same ID | ||
func NewRequestIDRangerPlugin(opts ...RequestIdOption) ranger.ClientPlugin { | ||
p := &requestIdPlugin{} | ||
|
||
for i := range opts { | ||
opts[i](p) | ||
} | ||
|
||
if p.requestID == "" { | ||
p.requestID = uuid.New().String() | ||
} | ||
|
||
return p | ||
} | ||
|
||
type requestIdPlugin struct { | ||
requestID string | ||
} | ||
|
||
func (p *requestIdPlugin) GetName() string { | ||
return "Request Scope Plugin" | ||
} | ||
|
||
func (p *requestIdPlugin) GetHeader(data []byte) http.Header { | ||
header := make(http.Header) | ||
header.Set(XRequestID, p.requestID) | ||
return header | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package scope | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/uuid" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestRequestScopePlugin(t *testing.T) { | ||
t.Run("header is valid uuid", func(t *testing.T) { | ||
p := NewRequestIDRangerPlugin() | ||
header := p.GetHeader(nil) | ||
headerValue := header.Get(XRequestID) | ||
require.NotEmpty(t, headerValue) | ||
_, err := uuid.Parse(headerValue) | ||
require.NoError(t, err) | ||
}) | ||
|
||
t.Run("multiple calls produce the same value", func(t *testing.T) { | ||
p := NewRequestIDRangerPlugin() | ||
headerValue1 := p.GetHeader(nil).Get(XRequestID) | ||
headerValue2 := p.GetHeader(nil).Get(XRequestID) | ||
require.Equal(t, headerValue1, headerValue2) | ||
}) | ||
|
||
t.Run("uniqueness", func(t *testing.T) { | ||
p1 := NewRequestIDRangerPlugin() | ||
p2 := NewRequestIDRangerPlugin() | ||
headerValue1 := p1.GetHeader(nil).Get(XRequestID) | ||
headerValue2 := p2.GetHeader(nil).Get(XRequestID) | ||
require.NotEqual(t, headerValue1, headerValue2) | ||
}) | ||
|
||
t.Run("custom uuid", func(t *testing.T) { | ||
p1 := NewRequestIDRangerPlugin(WithUuid("custom-uuid")) | ||
headerValue1 := p1.GetHeader(nil).Get(XRequestID) | ||
require.Equal(t, "custom-uuid", headerValue1) | ||
}) | ||
} |