Skip to content

Commit 5552d64

Browse files
filter webhook (#1243)
* add webhook filter to protobuf and ensure IsAllowed checked * generated protobuf --------- Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
1 parent b1e564f commit 5552d64

File tree

6 files changed

+155
-77
lines changed

6 files changed

+155
-77
lines changed

livekit/livekit_models.pb.go

Lines changed: 129 additions & 63 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

protobufs/livekit_models.proto

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ message ParticipantInfo {
176176
DisconnectReason disconnect_reason = 16;
177177
repeated KindDetail kind_details = 18;
178178

179-
// NEXT_ID: 19
179+
// NEXT_ID: 19
180180
}
181181

182182
enum TrackType {
@@ -310,7 +310,7 @@ message EncryptedPacket {
310310
Encryption.Type encryption_type = 1;
311311
bytes iv = 2;
312312
uint32 key_index = 3;
313-
bytes encrypted_value = 4; // This is an encrypted EncryptedPacketPayload message representation
313+
bytes encrypted_value = 4; // This is an encrypted EncryptedPacketPayload message representation
314314
}
315315

316316
message EncryptedPacketPayload {
@@ -757,13 +757,18 @@ message DataStream {
757757
}
758758
}
759759

760+
message FilterParams {
761+
repeated string include_events = 1;
762+
repeated string exclude_events = 2;
763+
}
764+
760765
message WebhookConfig {
761766
string url = 1;
762767
string signing_key = 2;
768+
FilterParams filter_params = 3;
763769
}
764770

765771
message SubscribedAudioCodec {
766772
string codec = 1;
767773
bool enabled = 2;
768774
}
769-

webhook/filter.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ func (f *filter) SetFilter(params FilterParams) {
3030
f.params = params
3131
}
3232

33+
// IncludeEvents and ExcludeEvents are mutually exclusive; only one will be checked
34+
// if neither are set, the event will be allowed
3335
func (f *filter) IsAllowed(event string) bool {
3436
// includes get higher precendence than excludes
3537
if len(f.params.IncludeEvents) != 0 {
@@ -40,6 +42,5 @@ func (f *filter) IsAllowed(event string) bool {
4042
return !slices.Contains(f.params.ExcludeEvents, event)
4143
}
4244

43-
// default allow
4445
return true
4546
}

webhook/notifier.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@ type WebHookConfig struct {
3131
APIKey string `yaml:"api_key,omitempty"`
3232
URLNotifier URLNotifierConfig `yaml:"url_notifier,omitempty"`
3333
ResourceURLNotifier ResourceURLNotifierConfig `yaml:"resource_url_notifier,omitempty"`
34+
FilterParams FilterParams `yaml:"filter_params,omitempty"`
3435
}
3536

3637
var DefaultWebHookConfig = WebHookConfig{
3738
URLNotifier: DefaultURLNotifierConfig,
3839
ResourceURLNotifier: DefaultResourceURLNotifierConfig,
40+
FilterParams: FilterParams{},
3941
}
4042

4143
type NotifyParams struct {
@@ -83,11 +85,12 @@ func NewDefaultNotifier(config WebHookConfig, kp auth.KeyProvider) (QueuedNotifi
8385
}
8486
for _, url := range config.URLs {
8587
u := NewResourceURLNotifier(ResourceURLNotifierParams{
86-
URL: url,
87-
Logger: logger.GetLogger().WithComponent("webhook"),
88-
APIKey: config.APIKey,
89-
APISecret: apiSecret,
90-
Config: config.ResourceURLNotifier,
88+
URL: url,
89+
Logger: logger.GetLogger().WithComponent("webhook"),
90+
APIKey: config.APIKey,
91+
APISecret: apiSecret,
92+
Config: config.ResourceURLNotifier,
93+
FilterParams: config.FilterParams,
9194
})
9295
n.notifiers = append(n.notifiers, u)
9396
}

webhook/resource_url_notifier.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ func (r *ResourceURLNotifier) SetFilter(params FilterParams) {
156156
r.filter.SetFilter(params)
157157
}
158158

159+
func (r *ResourceURLNotifier) IsAllowed(event string) bool {
160+
return r.filter.IsAllowed(event)
161+
}
162+
159163
func (r *ResourceURLNotifier) RegisterProcessedHook(hook func(ctx context.Context, whi *livekit.WebhookInfo)) {
160164
r.mu.Lock()
161165
defer r.mu.Unlock()

webhook/url_notifier.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@ import (
3333
"github.com/livekit/protocol/logger"
3434
)
3535

36-
const (
37-
numWorkers = 10
38-
defaultQueueSize = 100
39-
)
40-
4136
type URLNotifierConfig struct {
4237
NumWorkers int `yaml:"num_workers,omitempty"`
4338
QueueSize int `yaml:"queue_size,omitempty"`
@@ -123,6 +118,10 @@ func (n *URLNotifier) SetFilter(params FilterParams) {
123118
n.filter.SetFilter(params)
124119
}
125120

121+
func (n *URLNotifier) IsAllowed(event string) bool {
122+
return n.filter.IsAllowed(event)
123+
}
124+
126125
func (n *URLNotifier) RegisterProcessedHook(hook func(ctx context.Context, whi *livekit.WebhookInfo)) {
127126
n.mu.Lock()
128127
defer n.mu.Unlock()

0 commit comments

Comments
 (0)