Skip to content

Commit

Permalink
feat(outputs.websocket): Allow specifying secrets in headers (#14836)
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsStegman authored Feb 20, 2024
1 parent f005bfa commit da56ebd
Show file tree
Hide file tree
Showing 3 changed files with 24 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
22 changes: 14 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,13 @@ 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 fmt.Errorf("getting header secret %q failed: %w", k, err)
}

headers.Set(k, secret.String())
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 da56ebd

Please sign in to comment.