Skip to content

Commit

Permalink
Add ability to configure TLS for Kafka and HTTP
Browse files Browse the repository at this point in the history
  • Loading branch information
colmsnowplow committed Jul 1, 2022
1 parent 3ae1054 commit 5a82f8b
Show file tree
Hide file tree
Showing 19 changed files with 310 additions and 117 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ vendor/
build/
dist/
.localstack/

#temporary directory created by tests
tmp_replicator/
3 changes: 0 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ go_dirs = `go list ./... | grep -v /build/ | grep -v /vendor/`
build_dir = build
vendor_dir = vendor
integration_dir = integration
cert_dir = $(integration_dir)/http
abs_cert_dir = $$(pwd)/$(cert_dir)
ngrok_path = ${NGROK_DIR}ngrok # Set NGROK_DIR to `/path/to/directory/` for local setup

coverage_dir = $(build_dir)/coverage
Expand Down Expand Up @@ -144,7 +142,6 @@ test: test-setup
GO111MODULE=on go tool cover -func=$(coverage_out)

integration-test: test-setup
export CERT_DIR=$(abs_cert_dir); \
GO111MODULE=on go test $(go_dirs) -v -covermode=count -coverprofile=$(coverage_out)
GO111MODULE=on go tool cover -html=$(coverage_out) -o $(coverage_html)
GO111MODULE=on go tool cover -func=$(coverage_out)
Expand Down
6 changes: 6 additions & 0 deletions cmd/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
_ "net/http/pprof"

"github.com/snowplow-devops/stream-replicator/cmd"
"github.com/snowplow-devops/stream-replicator/pkg/common"
"github.com/snowplow-devops/stream-replicator/pkg/failure/failureiface"
"github.com/snowplow-devops/stream-replicator/pkg/models"
"github.com/snowplow-devops/stream-replicator/pkg/observer"
Expand Down Expand Up @@ -118,6 +119,11 @@ func RunCli(supportedSourceConfigPairs []sourceconfig.ConfigPair) {
stop <- struct{}{}
}()

err := common.DeleteTemporaryDir()
if err != nil {
log.Debugf(`error deleting tmp directory: %v`, err)
}

select {
case <-stop:
log.Debug("source.Stop() finished successfully!")
Expand Down
43 changes: 24 additions & 19 deletions config/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package config

import (
"errors"
"os"
"path/filepath"
"reflect"
"testing"
Expand All @@ -19,6 +20,10 @@ import (
"github.com/snowplow-devops/stream-replicator/pkg/target"
)

func init() {
os.Clearenv()
}

func TestCreateTargetComponentHCL(t *testing.T) {
testCases := []struct {
File string
Expand Down Expand Up @@ -73,9 +78,9 @@ func TestCreateTargetComponentHCL(t *testing.T) {
Headers: "",
BasicAuthUsername: "",
BasicAuthPassword: "",
CertFile: "",
KeyFile: "",
CaFile: "",
TLSCert: "",
TLSKey: "",
TLSCa: "",
SkipVerifyTLS: false,
},
},
Expand All @@ -90,9 +95,9 @@ func TestCreateTargetComponentHCL(t *testing.T) {
Headers: "{\"Accept-Language\":\"en-US\"}",
BasicAuthUsername: "testUsername",
BasicAuthPassword: "testPass",
CertFile: "test.cert",
KeyFile: "test.key",
CaFile: "test.ca",
TLSCert: "dGVzdC5jZXJ0",
TLSKey: "dGVzdC5rZXkK",
TLSCa: "dGVzdC5jYQ==",
SkipVerifyTLS: true,
},
},
Expand All @@ -112,9 +117,9 @@ func TestCreateTargetComponentHCL(t *testing.T) {
SASLUsername: "",
SASLPassword: "",
SASLAlgorithm: "sha512",
CertFile: "",
KeyFile: "",
CaFile: "",
TLSCert: "",
TLSKey: "",
TLSCa: "",
SkipVerifyTLS: false,
ForceSync: false,
FlushFrequency: 0,
Expand All @@ -138,9 +143,9 @@ func TestCreateTargetComponentHCL(t *testing.T) {
SASLUsername: "testUsername",
SASLPassword: "testPass",
SASLAlgorithm: "sha256",
CertFile: "test.cert",
KeyFile: "test.key",
CaFile: "test.ca",
TLSCert: "dGVzdC5jZXJ0",
TLSKey: "dGVzdC5rZXkK",
TLSCa: "dGVzdC5jYQ==",
SkipVerifyTLS: true,
ForceSync: true,
FlushFrequency: 2,
Expand Down Expand Up @@ -217,9 +222,9 @@ func TestCreateFailureTargetComponentENV(t *testing.T) {
SASLUsername: "testUsername",
SASLPassword: "testPass",
SASLAlgorithm: "sha256",
CertFile: "test.cert",
KeyFile: "test.key",
CaFile: "test.ca",
TLSCert: "dGVzdA==",
TLSKey: "dGVzdA==",
TLSCa: "dGVzdA==",
SkipVerifyTLS: true,
ForceSync: true,
FlushFrequency: 2,
Expand All @@ -230,7 +235,7 @@ func TestCreateFailureTargetComponentENV(t *testing.T) {

t.Run(testCase.Name, func(t *testing.T) {
assert := assert.New(t)

t.Setenv("STREAM_REPLICATOR_CONFIG_FILE", "")
t.Setenv("FAILURE_TARGET_NAME", "kafka")
t.Setenv("FAILURE_TARGET_KAFKA_BROKERS", "testBrokers")
t.Setenv("FAILURE_TARGET_KAFKA_TOPIC_NAME", "testTopic")
Expand All @@ -244,9 +249,9 @@ func TestCreateFailureTargetComponentENV(t *testing.T) {
t.Setenv("FAILURE_TARGET_KAFKA_SASL_USERNAME", "testUsername")
t.Setenv("FAILURE_TARGET_KAFKA_SASL_PASSWORD", "testPass")
t.Setenv("FAILURE_TARGET_KAFKA_SASL_ALGORITHM", "sha256")
t.Setenv("FAILURE_TARGET_KAFKA_TLS_CERT_FILE", "test.cert")
t.Setenv("FAILURE_TARGET_KAFKA_TLS_KEY_FILE", "test.key")
t.Setenv("FAILURE_TARGET_KAFKA_TLS_CA_FILE", "test.ca")
t.Setenv("FAILURE_TARGET_KAFKA_TLS_CERT_B64", "dGVzdA==")
t.Setenv("FAILURE_TARGET_KAFKA_TLS_KEY_B64", "dGVzdA==")
t.Setenv("FAILURE_TARGET_KAFKA_TLS_CA_B64", "dGVzdA==")
t.Setenv("FAILURE_TARGET_KAFKA_TLS_SKIP_VERIFY_TLS", "true")
t.Setenv("FAILURE_TARGET_KAFKA_FORCE_SYNC_PRODUCER", "true")
t.Setenv("FAILURE_TARGET_KAFKA_FLUSH_FREQUENCY", "2")
Expand Down
1 change: 1 addition & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func TestNewConfig(t *testing.T) {
observer, err := c.GetObserver(map[string]string{})
assert.NotNil(observer)
assert.Nil(err)
os.RemoveAll(`tmp_replicator`)
}

func TestNewConfig_FromEnv(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions config/examples/failure-targets/http-extended.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ failure_target {
basic_auth_password = env.MY_AUTH_PASSWORD

# The optional certificate file for client authentication
cert_file = "myLocalhost.crt"
tls_cert = "dGVzdCBzdHJpbmc="

# The optional key file for client authentication
key_file = "MyLocalhost.key"
tls_key = "c29tZSBzdHJpbmc="

# The optional certificate authority file for TLS client authentication
ca_file = "myRootCA.crt"
tls_ca = "b3RoZXIgc3RyaW5ncw=="

# Whether to skip verifying ssl certificates chain (default: false)
# If cert_file and key_file are not provided, this setting is not applied.
# If tls_cert and tls_key are not provided, this setting is not applied.
skip_verify_tls = true
}
}
6 changes: 3 additions & 3 deletions config/examples/failure-targets/kafka-extended.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ failure_target {
sasl_algorithm = "sha256"

# The optional certificate file for client authentication
cert_file = "myLocalhost.crt"
tls_cert = "dGVzdCBzdHJpbmc="

# The optional key file for client authentication
key_file = "MyLocalhost.key"
tls_key = "c29tZSBzdHJpbmc="

# The optional certificate authority file for TLS client authentication
ca_file = "myRootCA.crt"
tls_ca = "b3RoZXIgc3RyaW5ncw=="

# Whether to skip verifying ssl certificates chain (default: false)
skip_verify_tls = true
Expand Down
8 changes: 4 additions & 4 deletions config/examples/targets/http-extended.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ target {
basic_auth_password = env.MY_AUTH_PASSWORD

# The optional certificate file for client authentication
cert_file = "myLocalhost.crt"
tls_cert = "dGVzdCBzdHJpbmc="

# The optional key file for client authentication
key_file = "MyLocalhost.key"
tls_key = "c29tZSBzdHJpbmc="

# The optional certificate authority file for TLS client authentication
ca_file = "myRootCA.crt"
tls_ca = "b3RoZXIgc3RyaW5ncw=="

# Whether to skip verifying ssl certificates chain (default: false)
# If cert_file and key_file are not provided, this setting is not applied.
# If tls_cert and tls_key are not provided, this setting is not applied.
skip_verify_tls = true
}
}
6 changes: 3 additions & 3 deletions config/examples/targets/kafka-extended.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ target {
sasl_algorithm = "sha256"

# The optional certificate file for client authentication
cert_file = "myLocalhost.crt"
tls_cert = "dGVzdCBzdHJpbmc="

# The optional key file for client authentication
key_file = "MyLocalhost.key"
tls_key = "c29tZSBzdHJpbmc="

# The optional certificate authority file for TLS client authentication
ca_file = "myRootCA.crt"
tls_ca = "b3RoZXIgc3RyaW5ncw=="

# Whether to skip verifying ssl certificates chain (default: false)
skip_verify_tls = true
Expand Down
6 changes: 3 additions & 3 deletions config/test-fixtures/target-http-extended.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ target {
headers = "{\"Accept-Language\":\"en-US\"}"
basic_auth_username = "testUsername"
basic_auth_password = "testPass"
cert_file = "test.cert"
key_file = "test.key"
ca_file = "test.ca"
tls_cert = "dGVzdC5jZXJ0"
tls_key = "dGVzdC5rZXkK"
tls_ca = "dGVzdC5jYQ=="
skip_verify_tls = true
}
}
6 changes: 3 additions & 3 deletions config/test-fixtures/target-kafka-extended.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ target {
sasl_username = "testUsername"
sasl_password = "testPass"
sasl_algorithm = "sha256"
cert_file = "test.cert"
key_file = "test.key"
ca_file = "test.ca"
tls_cert = "dGVzdC5jZXJ0"
tls_key = "dGVzdC5rZXkK"
tls_ca = "dGVzdC5jYQ=="
skip_verify_tls = true
force_sync_producer = true
flush_frequency = 2
Expand Down
2 changes: 1 addition & 1 deletion integration/http/localhost.key
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ itxdXvoomlhwDKZv0Y+vPm4V9SBx/36ubf6bM6vKoZTSuv2+ktA/uInFW+y/1mLH
KD1JlQKBgQDNZry00fN3iJ9stUNEYVaAtXQ1a0/LY/r2NuC04IwemwOyFUvzY7G9
sNXeIxTYjQ9OCp9+EE1n6Q3yg63MmTrNuD51f0h2tftokYBaoYBny34HuQf0N7qF
laOI6yiORZ4eGdYrpCq+q+J0fAkRca0M4Nq/lDEw4bric38WpPxV3Q==
-----END RSA PRIVATE KEY-----
-----END RSA PRIVATE KEY-----
2 changes: 1 addition & 1 deletion integration/http/rootCA.crt
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ Ggg1Qo5z0+XT2l+2KhOC02ydgHV1/tT6cVVX3ZkBvvb/WPHmVp9bT8zqeJzrMQkM
9DaKEyZKw+LYy7sZp4p4giE/JAzBLidsfIdznhYguPjKgboPMfiJvapzyZPEJsDu
ShYb5uIlytHwAVlGiUgjx+z/YXBQN1vWsCm5pVL4RGdXdcq5HZzZRaJxAUBrfmiU
uCJPEnUJ1emIqakgSy3yA+9WtQ==
-----END CERTIFICATE-----
-----END CERTIFICATE-----
Loading

0 comments on commit 5a82f8b

Please sign in to comment.