Skip to content

Commit

Permalink
Allow env var configuration of updated transformation config (closes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
colmsnowplow committed Jul 22, 2022
1 parent 4c6eb15 commit 4baada2
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
17 changes: 17 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclparse"
"github.com/pkg/errors"
"github.com/snowplow-devops/stream-replicator/pkg/common"
"github.com/snowplow-devops/stream-replicator/pkg/failure"
"github.com/snowplow-devops/stream-replicator/pkg/failure/failureiface"
"github.com/snowplow-devops/stream-replicator/pkg/observer"
Expand Down Expand Up @@ -136,6 +137,22 @@ func newEnvConfig() (*Config, error) {
Decoder: envDecoder,
}

// If the TRANSFORM_CONFIG_B64 env var is set, parse it, and use the Transformations in our mainConfig.
b64Transformations := os.Getenv("TRANSFORM_CONFIG_B64")
if b64Transformations != "" {
err := common.DecodeB64ToFile(b64Transformations, "tmp_replicator/test.hcl")
if err != nil {
return nil, errors.Wrap(err, "Error decoding transformation config base64 from env")
}

confFromFile, err := newHclConfig("tmp_replicator/test.hcl")
if err != nil {
return nil, errors.Wrap(err, "Error parsing transformation config from env")
}

mainConfig.Data.Transformations = confFromFile.Data.Transformations
}

return &mainConfig, nil
}

Expand Down
71 changes: 71 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func TestNewConfig_FromEnv(t *testing.T) {
t.Setenv("LOG_LEVEL", "debug")
t.Setenv("TARGET_NAME", "kinesis")
t.Setenv("SOURCE_NAME", "kinesis")
t.Setenv("TRANSFORM_CONFIG_B64", `dHJhbnNmb3JtIHsKICB1c2UgImpzIiB7CiAgICAvLyBjaGFuZ2VzIGFwcF9pZCB0byAiMSIKICAgIHNvdXJjZV9iNjQgPSAiWm5WdVkzUnBiMjRnYldGcGJpaDRLU0I3Q2lBZ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VhTQTlJQ0l6SWdvZ0lDQWdmUW9nSUNBZ2NtVjBkWEp1SUhzS0lDQWdJQ0FnSUNCRVlYUmhPaUJLVTA5T0xuTjBjbWx1WjJsbWVTaHFjMjl1VDJKcUtRb2dJQ0FnZlRzS2ZRPT0iCiAgfQp9`)

c, err := NewConfig()
assert.NotNil(c)
Expand All @@ -60,6 +61,11 @@ func TestNewConfig_FromEnv(t *testing.T) {
assert.Equal("debug", c.Data.LogLevel)
assert.Equal("kinesis", c.Data.Target.Use.Name)
assert.Equal("kinesis", c.Data.Source.Use.Name)
assert.Equal(3, len(c.Data.Transformations))
for _, transf := range c.Data.Transformations {
assert.Equal("js", transf.Use.Name)

}
}

func TestNewConfig_FromEnvInvalid(t *testing.T) {
Expand Down Expand Up @@ -151,6 +157,33 @@ func TestNewConfig_InvalidStatsReceiver(t *testing.T) {
}
}

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

t.Setenv("TRANSFORM_CONFIG_B64", `fdssdnpfdspnm`)

c, err := NewConfig()
assert.Nil(c)
assert.NotNil(err)
if err != nil {
assert.Equal("Error decoding transformation config base64 from env: Failed to Base64 decode for creating file tmp_replicator/test.hcl: illegal base64 data at input byte 12", err.Error())
}

}

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

t.Setenv("TRANSFORM_CONFIG_B64", `dHJhbnNmb3JtIHsKICB1c2UgImpzIiB7CiAgICAvLyBjaGFuZ2VzIGFwcF9pZCB0byAiMSIKICAgIHNvdXJjZV9iNjQgPSAiWm5WdVkzUnBiMjRnYldGcGJpaDRLU0I3Q2lBZ0lDQjJZWElnYW5OdmJrOWlhaUE5SUVwVFQwNHVjR0Z5YzJVb2VDNUVZWFJoS1RzS0lDQWdJR3B6YjI1UFltcGJJbUZ3Y0Y5cFpDSmRJRDBnSWpFaU93b2dJQ0FnY21WMGRYSnVJSHNLSUNBZ0lDQWdJQ0JFWVhSaE9pQktVMDlPTG5OMGNtbHVaMmxtZV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Equal("Error parsing transformation config from env: tmp_replicator/test.hcl:8,11-12: Unclosed configuration block; There is no closing brace for this block before the end of the file. This may be caused by incorrect brace nesting elsewhere in this file.", err.Error())
}
}

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

Expand Down Expand Up @@ -243,3 +276,41 @@ func TestNewConfig_Hcl_sentry(t *testing.T) {
assert.Equal("{\"testKey\":\"testValue\"}", c.Data.Sentry.Tags)
assert.Equal("testDsn", c.Data.Sentry.Dsn)
}

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

filename := filepath.Join("test-fixtures", "transform-mocked-order.hcl")
t.Setenv("STREAM_REPLICATOR_CONFIG_FILE", filename)

c, err := NewConfig()
assert.NotNil(c)
if err != nil {
t.Fatalf("function NewConfig failed with error: %q", err.Error())
}

assert.Equal(5, len(c.Data.Transformations))
assert.Equal("one", c.Data.Transformations[0].Use.Name)
assert.Equal("two", c.Data.Transformations[1].Use.Name)
assert.Equal("three", c.Data.Transformations[2].Use.Name)
assert.Equal("four", c.Data.Transformations[3].Use.Name)
assert.Equal("five", c.Data.Transformations[4].Use.Name)
}

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

t.Setenv("TRANSFORM_CONFIG_B64", `dHJhbnNmb3JtIHsKICB1c2UgIm9uZSIgewogIH0KfQoKdHJhbnNmb3JtIHsKICB1c2UgInR3byIgewogIH0KfQoKdHJhbnNmb3JtIHsKICB1c2UgInRocmVlIiB7CiAgfQp9Cgp0cmFuc2Zvcm0gewogIHVzZSAiZm91ciIgewogIH0KfQoKdHJhbnNmb3JtIHsKICB1c2UgImZpdmUiIHsKICB9Cn0=`)

c, err := NewConfig()
if err != nil {
t.Fatalf("function NewConfig failed with error: %q", err.Error())
}

assert.Equal(5, len(c.Data.Transformations))
assert.Equal("one", c.Data.Transformations[0].Use.Name)
assert.Equal("two", c.Data.Transformations[1].Use.Name)
assert.Equal("three", c.Data.Transformations[2].Use.Name)
assert.Equal("four", c.Data.Transformations[3].Use.Name)
assert.Equal("five", c.Data.Transformations[4].Use.Name)
}
24 changes: 24 additions & 0 deletions config/test-fixtures/transform-mocked-order.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
transform {
use "one" {
}
}

transform {
use "two" {
}
}

transform {
use "three" {
}
}

transform {
use "four" {
}
}

transform {
use "five" {
}
}

0 comments on commit 4baada2

Please sign in to comment.