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 9, 2024
1 parent ed293a3 commit a09adbe
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 @@ -14,6 +14,7 @@ package cmd
import (
"encoding/json"
"fmt"
"os"

"github.com/getsentry/sentry-go"
"github.com/pkg/errors"
Expand All @@ -38,14 +39,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")
}

// Configure Sentry
sentryEnabled := cfg.Data.Sentry.Dsn != ""
if sentryEnabled {
Expand Down Expand Up @@ -82,3 +89,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 @@ -24,17 +24,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 @@ -48,6 +78,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 @@ -59,6 +90,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 @@ -72,6 +104,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 @@ -85,6 +118,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 @@ -55,6 +55,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 @@ -90,6 +91,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 @@ -111,6 +116,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 @@ -19,7 +19,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 @@ -71,6 +71,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 @@ -37,6 +37,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 a09adbe

Please sign in to comment.