Skip to content

Commit

Permalink
Add CacheKeyPrefix to webhook client options
Browse files Browse the repository at this point in the history
  • Loading branch information
hackerwins committed Feb 3, 2025
1 parent ff9ff34 commit 538f59d
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 9 deletions.
5 changes: 3 additions & 2 deletions pkg/webhook/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ var (

// Options are the options for the webhook client.
type Options struct {
CacheKeyPrefix string
CacheTTL time.Duration

MaxRetries uint64
Expand All @@ -54,8 +55,8 @@ type Options struct {

// Client is a client for the webhook.
type Client[Req any, Res any] struct {
url string
cache *cache.LRUExpireCache[string, types.Pair[int, *Res]]
url string
options Options
}

Expand All @@ -79,7 +80,7 @@ func (c *Client[Req, Res]) Send(ctx context.Context, req Req) (*Res, int, error)
return nil, 0, fmt.Errorf("marshal webhook request: %w", err)
}

cacheKey := string(body)
cacheKey := c.options.CacheKeyPrefix + ":" + string(body)
if entry, ok := c.cache.Get(cacheKey); ok {
return entry.Second, entry.First, nil
}
Expand Down
6 changes: 3 additions & 3 deletions server/rpc/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ func AccessAttributes(pack *change.Pack) []types.AccessAttribute {
// VerifyAccess verifies the given access.
func VerifyAccess(ctx context.Context, be *backend.Backend, accessInfo *types.AccessInfo) error {
md := metadata.From(ctx)
project := projects.From(ctx)
prj := projects.From(ctx)

if !project.RequireAuth(accessInfo.Method) {
if !prj.RequireAuth(accessInfo.Method) {
return nil
}

return verifyAccess(
ctx,
be,
project.AuthWebhookURL,
prj,
md.Authorization,
accessInfo,
)
Expand Down
7 changes: 4 additions & 3 deletions server/rpc/auth/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,15 @@ var (
func verifyAccess(
ctx context.Context,
be *backend.Backend,
authWebhookURL string,
prj *types.Project,
token string,
accessInfo *types.AccessInfo,
) error {
cli := webhook.NewClient[types.AuthWebhookRequest, types.AuthWebhookResponse](
authWebhookURL,
cli := webhook.NewClient[types.AuthWebhookRequest](
prj.AuthWebhookURL,
be.WebhookCache,
webhook.Options{
CacheKeyPrefix: prj.PublicKey + ":auth",
CacheTTL: be.Config.ParseAuthWebhookCacheTTL(),
MaxRetries: be.Config.AuthWebhookMaxRetries,
MaxWaitInterval: be.Config.ParseAuthWebhookMaxWaitInterval(),
Expand Down
1 change: 0 additions & 1 deletion test/integration/auth_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,6 @@ func TestAuthWebhookErrorHandling(t *testing.T) {
})

t.Run("unexpected webhook response test", func(t *testing.T) {
t.Skip()
ctx := context.Background()
authServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := types.NewAuthWebhookRequest(r.Body)
Expand Down

0 comments on commit 538f59d

Please sign in to comment.