Skip to content

Commit

Permalink
Add secret support to websocket as well
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsStegman committed Feb 16, 2024
1 parent 95c7a13 commit f34843e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
8 changes: 8 additions & 0 deletions plugins/outputs/websocket/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.

[CONFIGURATION.md]: ../../../docs/CONFIGURATION.md#plugins

## Secret-store support

This plugin supports secrets from secret-stores for the `headers` option.
See the [secret-store documentation][SECRETSTORE] for more details on how
to use them.

[SECRETSTORE]: ../../../docs/CONFIGURATION.md#secret-store-secrets

## Configuration

```toml @sample.conf
Expand Down
23 changes: 15 additions & 8 deletions plugins/outputs/websocket/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ const (

// WebSocket can output to WebSocket endpoint.
type WebSocket struct {
URL string `toml:"url"`
ConnectTimeout config.Duration `toml:"connect_timeout"`
WriteTimeout config.Duration `toml:"write_timeout"`
ReadTimeout config.Duration `toml:"read_timeout"`
Headers map[string]string `toml:"headers"`
UseTextFrames bool `toml:"use_text_frames"`
Log telegraf.Logger `toml:"-"`
URL string `toml:"url"`
ConnectTimeout config.Duration `toml:"connect_timeout"`
WriteTimeout config.Duration `toml:"write_timeout"`
ReadTimeout config.Duration `toml:"read_timeout"`
Headers map[string]*config.Secret `toml:"headers"`
UseTextFrames bool `toml:"use_text_frames"`
Log telegraf.Logger `toml:"-"`
proxy.HTTPProxy
proxy.Socks5ProxyConfig
tls.ClientConfig
Expand Down Expand Up @@ -92,7 +92,14 @@ func (w *WebSocket) Connect() error {

headers := http.Header{}
for k, v := range w.Headers {
headers.Set(k, v)
secret, err := v.Get()
if err != nil {
return err
}

headerVal := secret.String()
headers.Set(k, headerVal)
secret.Destroy()
}

conn, resp, err := dialer.Dial(w.URL, headers)
Expand Down
3 changes: 2 additions & 1 deletion plugins/outputs/websocket/websocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ func initWebSocket(s *testServer) *WebSocket {
w := newWebSocket()
w.Log = testutil.Logger{}
w.URL = s.URL
w.Headers = map[string]string{testHeaderName: testHeaderValue}
headerSecret := config.NewSecret([]byte(testHeaderValue))
w.Headers = map[string]*config.Secret{testHeaderName: &headerSecret}
w.SetSerializer(newTestSerializer())
return w
}
Expand Down

0 comments on commit f34843e

Please sign in to comment.