Skip to content

Commit

Permalink
[azureeventhub] SA container: replace underscores(_) with hyphens(-) (#…
Browse files Browse the repository at this point in the history
…31384)

* SA container: replace underscores(_) with hyphens(-)

When a user specifies an event hub name in the input settings,
the configuration uses it to compose the storage account (SA)
container name.

For example, with an event hub name <EVENT-HUB-NAME>,
the SA container name becomes:

`filebeat-<DATA-STREAM>-<EVENT-HUB-NAME>`
(e.g., `filebeat-auditlogs-eventhub00`).

The Event Hub allows names with underscores (_) characters,
but unfortunately, the SA container does not permit them.
  • Loading branch information
zmoog authored and chrisberkhout committed Jun 1, 2023
1 parent 546763e commit 46441c7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...main[Check the HEAD dif
- Add URL Encode template function for httpjson input. {pull}30962[30962]
- Add `storage_account_container` configuration option to Azure logs. {pull}31279[31279]
- Add `application/zip` decoder to the `httpsjon` input. {issue}31282[31282] {pull}31304[31304]
- Sanitize the Azure storage account container names with underscores (_). {pull}31384[31384]

*Auditbeat*

Expand Down
18 changes: 18 additions & 0 deletions x-pack/filebeat/input/azureeventhub/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ package azureeventhub
import (
"errors"
"fmt"
"strings"
"unicode"

"github.com/elastic/beats/v7/libbeat/logp"
)

type azureInputConfig struct {
Expand All @@ -29,6 +32,7 @@ const ephContainerName = "filebeat"

// Validate validates the config.
func (conf *azureInputConfig) Validate() error {
logger := logp.NewLogger("azureeventhub.config")
if conf.ConnectionString == "" {
return errors.New("no connection string configured")
}
Expand All @@ -41,6 +45,20 @@ func (conf *azureInputConfig) Validate() error {
if conf.SAContainer == "" {
conf.SAContainer = fmt.Sprintf("%s-%s", ephContainerName, conf.EventHubName)
}
if strings.Contains(conf.SAContainer, "_") {
originalValue := conf.SAContainer
// When a user specifies an event hub name in the input settings,
// the configuration uses it to compose the storage account (SA) container
// name (for example, `filebeat-<DATA-STREAM>-<EVENTHUB>`).
//
// The event hub allows names with underscores (_) characters, but unfortunately,
// the SA container does not permit them.
//
// So instead of throwing an error to the user, we decided to replace
// underscores (_) characters with hyphens (-).
conf.SAContainer = strings.ReplaceAll(conf.SAContainer, "_", "-")
logger.Warnf("replaced underscores (_) with hyphens (-) in the storage account container name (before: %s, now: %s", originalValue, conf.SAContainer)
}
err := storageContainerValidate(conf.SAContainer)
if err != nil {
return err
Expand Down
27 changes: 27 additions & 0 deletions x-pack/filebeat/input/azureeventhub/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ package azureeventhub

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestStorageContainerValidate(t *testing.T) {
Expand All @@ -20,6 +22,8 @@ func TestStorageContainerValidate(t *testing.T) {
{"a", false},
{"a-name-that-is-really-too-long-to-be-valid-and-should-never-be-used-no-matter-what", false},
{"-not-valid", false},
{"not-valid-", false},
{"not--valid", false},
{"capital-A-not-valid", false},
{"no_underscores_either", false},
}
Expand All @@ -30,3 +34,26 @@ func TestStorageContainerValidate(t *testing.T) {
}
}
}

func TestValidate(t *testing.T) {
t.Run("Sanitize storage account containers with underscores", func(t *testing.T) {
config := azureInputConfig{
ConnectionString: "sb://test-ns.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=SECRET",
EventHubName: "event_hub_00",
SAName: "teststorageaccount",
SAKey: "secret",
SAContainer: "filebeat-activitylogs-event_hub_00",
}

if err := config.Validate(); err != nil {
t.Fatalf("unexpected validation error: %v", err)
}

assert.Equal(
t,
"filebeat-activitylogs-event-hub-00",
config.SAContainer,
"underscores (_) not replaced with hyphens (-)",
)
})
}

0 comments on commit 46441c7

Please sign in to comment.