Skip to content

Commit

Permalink
Add more thorough check on auth vars, and tests for the check
Browse files Browse the repository at this point in the history
  • Loading branch information
colmsnowplow committed Jul 21, 2021
1 parent 99b6055 commit f572d5b
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 4 deletions.
19 changes: 15 additions & 4 deletions pkg/target/eventhub.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,21 @@ type EventHubTarget struct {
// NewEventHubTarget creates a new client for writing messages to Azure EventHub
func NewEventHubTarget(cfg *EventHubConfig) (*EventHubTarget, error) {

keyName, keyNamePresent := os.LookupEnv("EVENTHUB_KEY_NAME")
keyValue, keyValuePresent := os.LookupEnv("EVENTHUB_KEY_VALUE")
if !(keyNamePresent && keyValuePresent) {
return nil, errors.Errorf("Error initialising EventHub client: Env vars %s and %s are required for authentication.", keyName, keyValue)
_, keyNamePresent := os.LookupEnv("EVENTHUB_KEY_NAME")
_, keyValuePresent := os.LookupEnv("EVENTHUB_KEY_VALUE")

_, connStringPresent := os.LookupEnv("EVENTHUB_CONNECTION_STRING")

_, tenantIDPresent := os.LookupEnv("AZURE_TENANT_ID")
_, clientIDPresent := os.LookupEnv("AZURE_CLIENT_ID")

_, clientSecretPresent := os.LookupEnv("AZURE_CLIENT_SECRET")

_, azCertPathPresent := os.LookupEnv("AZURE_CERTIFICATE_PATH")
_, azCertPwrdPresent := os.LookupEnv("AZURE_CERTIFICATE_PASSWORD")

if !(connStringPresent || (keyNamePresent && keyValuePresent) || (tenantIDPresent && clientIDPresent && ((azCertPathPresent && azCertPwrdPresent) || clientSecretPresent))) {
return nil, errors.Errorf("Error initialising EventHub client: No valid combination of authentication Env vars found. https://pkg.go.dev/github.com/Azure/azure-event-hubs-go#NewHubWithNamespaceNameAndEnvironment")
}

hub, err := eventhub.NewHubWithNamespaceNameAndEnvironment(cfg.EventHubNamespace, cfg.EventHubName)
Expand Down
70 changes: 70 additions & 0 deletions pkg/target/eventhub_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package target

import (
"os"
"testing"

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

var cfg = EventHubConfig{
EventHubNamespace: "test",
EventHubName: "test",
}

func unsetEverything() {
os.Unsetenv("EVENTHUB_KEY_NAME")
os.Unsetenv("EVENTHUB_KEY_VALUE")

os.Unsetenv("EVENTHUB_CONNECTION_STRING")

os.Unsetenv("AZURE_TENANT_ID")
os.Unsetenv("AZURE_CLIENT_ID")

os.Unsetenv("AZURE_CLIENT_SECRET")

os.Unsetenv("AZURE_CERTIFICATE_PATH")
os.Unsetenv("AZURE_CERTIFICATE_PASSWORD")
}

func TestNewEventHubTarget_KeyValue(t *testing.T) {
assert := assert.New(t)

unsetEverything()

// Test that we can initialise a client with Key and Value
defer os.Unsetenv("EVENTHUB_KEY_NAME")
defer os.Unsetenv("EVENTHUB_KEY_VALUE")

os.Setenv("EVENTHUB_KEY_NAME", "fake")
os.Setenv("EVENTHUB_KEY_VALUE", "fake")

tgt, err := NewEventHubTarget(&cfg)
assert.Nil(err)
assert.NotNil(tgt)
}

func TestNewEventHubTarget_ConnString(t *testing.T) {
assert := assert.New(t)

unsetEverything()

// Test that we can initialise a client with Connection String
defer os.Unsetenv("EVENTHUB_CONNECTION_STRING")

os.Setenv("EVENTHUB_CONNECTION_STRING", "Endpoint=sb://test.servicebus.windows.net/;SharedAccessKeyName=fake;SharedAccessKey=fake")

tgt, err := NewEventHubTarget(&cfg)
assert.Nil(err)
assert.NotNil(tgt)
}

func TestNewEventHubTarget_Failure(t *testing.T) {
assert := assert.New(t)

unsetEverything()

tgt, err := NewEventHubTarget(&cfg)
assert.Equal("Error initialising EventHub client: No valid combination of authentication Env vars found. https://pkg.go.dev/github.com/Azure/azure-event-hubs-go#NewHubWithNamespaceNameAndEnvironment", err.Error())
assert.Nil(tgt)
}

0 comments on commit f572d5b

Please sign in to comment.