Skip to content

Commit a7a13f4

Browse files
authored
feat: add token auth support for event sink (#3197)
feat: add token auth and custom headers support for http event sink fixes issue #3187 Signed-off-by: Ramkumar Chinchani <rchincha.dev@gmail.com>
1 parent 8867814 commit a7a13f4

3 files changed

Lines changed: 44 additions & 3 deletions

File tree

pkg/extensions/config/events/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@ type SinkConfig struct {
4040
Channel string
4141
Timeout time.Duration
4242
Proxy *string
43+
Headers map[string]string
4344
}
4445

4546
type Credentials struct {
4647
Username string
4748
Password string
4849
File *string
50+
Token string
4951
}
5052

5153
type TLSConfig struct {

pkg/extensions/events/http_sink.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,20 @@ func NewHTTPSink(config eventsconf.SinkConfig) (*HTTPSink, error) {
4141
cehttp.WithClient(*httpClient),
4242
}
4343

44-
if config.Credentials != nil && config.Credentials.Username != "" {
45-
opts = append(opts, cehttp.WithHeader("Authorization",
46-
"Basic "+BasicAuth(config.Credentials.Username, config.Credentials.Password)))
44+
if config.Credentials != nil {
45+
if config.Credentials.Username != "" {
46+
opts = append(opts, cehttp.WithHeader("Authorization",
47+
"Basic "+BasicAuth(config.Credentials.Username, config.Credentials.Password)))
48+
} else if config.Credentials.Token != "" {
49+
opts = append(opts, cehttp.WithHeader("Authorization",
50+
"Bearer "+config.Credentials.Token))
51+
}
52+
}
53+
54+
if config.Headers != nil {
55+
for key, value := range config.Headers {
56+
opts = append(opts, cehttp.WithHeader(key, value))
57+
}
4758
}
4859

4960
// Create CloudEvents HTTP protocol

pkg/extensions/events/http_sink_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,34 @@ func TestHTTPSink(t *testing.T) {
6363
So(sink, ShouldNotBeNil)
6464
})
6565

66+
Convey("NewHTTPSink handles token auth config", t, func() {
67+
cfg := eventsconf.SinkConfig{
68+
Type: eventsconf.HTTP,
69+
Address: "http://localhost",
70+
Credentials: &eventsconf.Credentials{
71+
Token: "thisisamocktoken",
72+
},
73+
}
74+
75+
sink, err := events.NewHTTPSink(cfg)
76+
So(err, ShouldBeNil)
77+
So(sink, ShouldNotBeNil)
78+
})
79+
80+
Convey("NewHTTPSink handles custom headers config", t, func() {
81+
cfg := eventsconf.SinkConfig{
82+
Type: eventsconf.HTTP,
83+
Address: "http://localhost",
84+
Headers: map[string]string{
85+
"X-Tenant-ID": "tenant-abc123",
86+
},
87+
}
88+
89+
sink, err := events.NewHTTPSink(cfg)
90+
So(err, ShouldBeNil)
91+
So(sink, ShouldNotBeNil)
92+
})
93+
6694
Convey("GetHTTPClientForConfig returns error for invalid proxy", t, func() {
6795
badProxy := "://bad-url"
6896
cfg := eventsconf.SinkConfig{

0 commit comments

Comments
 (0)