Skip to content

Commit

Permalink
Add SLULA config and env var (close #321)
Browse files Browse the repository at this point in the history
  • Loading branch information
colmsnowplow committed May 3, 2024
1 parent cae6f75 commit f2c6b33
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 3 deletions.
23 changes: 21 additions & 2 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package cmd
import (
"encoding/json"
"fmt"
"os"

"github.com/getsentry/sentry-go"
"github.com/pkg/errors"
Expand All @@ -34,14 +35,20 @@ var (
// and handles:
//
// 1. Loading the Config from the environment
// 2. Configuring Sentry
// 3. Configuring Logrus (+Logrus -> Sentry)
// 2. Checking for licence acceptance
// 3. Configuring Sentry
// 4. Configuring Logrus (+Logrus -> Sentry)
func Init() (*config.Config, bool, error) {
cfg, err := config.NewConfig()
if err != nil {
return nil, false, errors.Wrap(err, "Failed to build config")
}

// If licence not accepted, fail on startup
if !cfg.Data.Licence.Accept && !handleSLULAEnvVar() {
return nil, false, errors.New("Please accept the terms of the Snowplow Limited Use License Agreement to proceed. See https://docs.snowplow.io/docs/destinations/forwarding-events/snowbridge/configuration/#license for more information on the license and how to configure this.")

Check failure on line 49 in cmd/init.go

View workflow job for this annotation

GitHub Actions / Compile & Test (1.22, ubuntu-latest)

error strings should not be capitalized or end with punctuation or a newline
}

// Configure Sentry
sentryEnabled := cfg.Data.Sentry.Dsn != ""
if sentryEnabled {
Expand Down Expand Up @@ -78,3 +85,15 @@ func Init() (*config.Config, bool, error) {
log.Debugf("Config: %+v", cfg)
return cfg, sentryEnabled, nil
}

func handleSLULAEnvVar() bool {
foundVal := os.Getenv("ACCEPT_LIMITED_USE_LICENSE")
truthyVals := []string{"true", "yes", "on", "1"}

for _, truthyVal := range truthyVals {
if foundVal == truthyVal {
return true
}
}
return false
}
34 changes: 34 additions & 0 deletions cmd/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,47 @@ func TestMain(m *testing.M) {
os.Exit(exitVal)
}

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

t.Setenv("ACCEPT_LIMITED_USE_LICENSE", "true")
assert.True(handleSLULAEnvVar())

t.Setenv("ACCEPT_LIMITED_USE_LICENSE", "yes")
assert.True(handleSLULAEnvVar())

t.Setenv("ACCEPT_LIMITED_USE_LICENSE", "1")
assert.True(handleSLULAEnvVar())

t.Setenv("ACCEPT_LIMITED_USE_LICENSE", "on")
assert.True(handleSLULAEnvVar())
}

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

t.Setenv("ACCEPT_LIMITED_USE_LICENSE", "yes")

cfg, _, err := Init()
assert.NotNil(cfg)
assert.Nil(err)
}

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

cfg, _, err := Init()
assert.Nil(cfg)
assert.NotNil(err)
if err != nil {
assert.Equal("Please accept the terms of the Snowplow Limited Use License Agreement to proceed. See https://docs.snowplow.io/docs/destinations/forwarding-events/snowbridge/configuration/#license for more information on the license and how to configure this.", err.Error())
}
}

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

t.Setenv("ACCEPT_LIMITED_USE_LICENSE", "on")
t.Setenv("STATS_RECEIVER_TIMEOUT_SEC", "debug")

cfg, _, err := Init()
Expand All @@ -44,6 +74,7 @@ func TestInit_Failure(t *testing.T) {
func TestInit_Success_Sentry(t *testing.T) {
assert := assert.New(t)

t.Setenv("ACCEPT_LIMITED_USE_LICENSE", "1")
t.Setenv("SENTRY_DSN", "https://[email protected]/28")
t.Setenv("SENTRY_TAGS", "{\"client_name\":\"com.acme\"}")

Expand All @@ -55,6 +86,7 @@ func TestInit_Success_Sentry(t *testing.T) {
func TestInit_Failure_LogLevel(t *testing.T) {
assert := assert.New(t)

t.Setenv("ACCEPT_LIMITED_USE_LICENSE", "true")
t.Setenv("LOG_LEVEL", "DEBUG")

cfg, _, err := Init()
Expand All @@ -68,6 +100,7 @@ func TestInit_Failure_LogLevel(t *testing.T) {
func TestInit_Failure_SentryDSN(t *testing.T) {
assert := assert.New(t)

t.Setenv("ACCEPT_LIMITED_USE_LICENSE", "yes")
t.Setenv("SENTRY_DSN", "blahblah")

cfg, _, err := Init()
Expand All @@ -81,6 +114,7 @@ func TestInit_Failure_SentryDSN(t *testing.T) {
func TestInit_Failure_SentryTags(t *testing.T) {
assert := assert.New(t)

t.Setenv("ACCEPT_LIMITED_USE_LICENSE", "yes")
t.Setenv("SENTRY_DSN", "https://[email protected]/28")
t.Setenv("SENTRY_TAGS", "asdasdasd")

Expand Down
8 changes: 8 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type configurationData struct {
LogLevel string `hcl:"log_level,optional" env:"LOG_LEVEL"`
UserProvidedID string `hcl:"user_provided_id,optional" env:"USER_PROVIDED_ID"`
DisableTelemetry bool `hcl:"disable_telemetry,optional" env:"DISABLE_TELEMETRY"`
Licence *licenceConfig `hcl:"licence,block"`
}

// component is a type to abstract over configuration blocks.
Expand Down Expand Up @@ -86,6 +87,10 @@ type statsConfig struct {
BufferSec int `hcl:"buffer_sec,optional" env:"STATS_RECEIVER_BUFFER_SEC"`
}

type licenceConfig struct {
Accept bool `hcl:"accept,optional"`
}

// defaultConfigData returns the initial main configuration target.
func defaultConfigData() *configurationData {
return &configurationData{
Expand All @@ -107,6 +112,9 @@ func defaultConfigData() *configurationData {
Transformations: nil,
LogLevel: "info",
DisableTelemetry: false,
Licence: &licenceConfig{
Accept: false,
},
}
}

Expand Down
1 change: 0 additions & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/stretchr/testify/assert"
)

// The GetSource part needs to move anyway - causes circular dep.
func TestNewConfig(t *testing.T) {
assert := assert.New(t)

Expand Down
1 change: 1 addition & 0 deletions docs/configuration_monitoring_docs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func testStatsDConfig(t *testing.T, configpath string, fullExample bool) {
func testSentryConfig(t *testing.T, configpath string, fullExample bool) {
assert := assert.New(t)
t.Setenv("SNOWBRIDGE_CONFIG_FILE", configpath)
t.Setenv("ACCEPT_LIMITED_USE_LICENSE", "true")

// Since sentry lives in cmd, we call Init to test it.
cfgSentry, sentryEnabled, initErr := cmd.Init()
Expand Down
1 change: 1 addition & 0 deletions release_test/e2e_common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var cmdTemplate = `cat %s | docker run -i \
--add-host host.docker.internal:host-gateway \
--mount type=bind,source=%s,target=/config.hcl \
--env SNOWBRIDGE_CONFIG_FILE=/config.hcl %s \
--env ACCEPT_LIMITED_USE_LICENSE=true \
snowplow/snowbridge:%s%s`

// explanation of arguments:
Expand Down

0 comments on commit f2c6b33

Please sign in to comment.