From d4a8080bb55294ebfcdc1517da7ed1d63136f06b Mon Sep 17 00:00:00 2001 From: go-follow Date: Wed, 18 Oct 2023 19:52:00 +0700 Subject: [PATCH 01/21] [exporter/cassandra] close #27828 added authorization by username and password --- exporter/cassandraexporter/README.md | 4 ++++ exporter/cassandraexporter/config.go | 6 ++++++ exporter/cassandraexporter/exporter_logs.go | 6 ++++++ exporter/cassandraexporter/exporter_traces.go | 6 ++++++ 4 files changed, 22 insertions(+) diff --git a/exporter/cassandraexporter/README.md b/exporter/cassandraexporter/README.md index 1efd9cd72805..3daeef4f774f 100644 --- a/exporter/cassandraexporter/README.md +++ b/exporter/cassandraexporter/README.md @@ -24,6 +24,7 @@ The following settings can be optionally configured: - `replication` (default = class: SimpleStrategy, replication_factor: 1) The strategy of replication. https://cassandra.apache.org/doc/4.1/cassandra/architecture/dynamo.html#replication-strategy - `compression` (default = LZ4Compressor) https://cassandra.apache.org/doc/latest/cassandra/operating/compression.html +- `auth` (default = username: "", password: "") Authorization for the Cassandra. ## Example @@ -39,4 +40,7 @@ exporters: replication_factor: 1 compression: algorithm: "ZstdCompressor" + auth: + username: "your-username" + password: "your-password" ``` diff --git a/exporter/cassandraexporter/config.go b/exporter/cassandraexporter/config.go index b9b6bd769437..dbe0747fde5f 100644 --- a/exporter/cassandraexporter/config.go +++ b/exporter/cassandraexporter/config.go @@ -11,6 +11,7 @@ type Config struct { LogsTable string `mapstructure:"logs_table"` Replication Replication `mapstructure:"replication"` Compression Compression `mapstructure:"compression"` + Auth Auth `mapstructure:"auth"` } type Replication struct { @@ -21,3 +22,8 @@ type Replication struct { type Compression struct { Algorithm string `mapstructure:"algorithm"` } + +type Auth struct { + UserName string `mapstructure:"username"` + Password string `mapstructure:"password"` +} diff --git a/exporter/cassandraexporter/exporter_logs.go b/exporter/cassandraexporter/exporter_logs.go index 8b824829358a..4ad1cc2155af 100644 --- a/exporter/cassandraexporter/exporter_logs.go +++ b/exporter/cassandraexporter/exporter_logs.go @@ -25,6 +25,12 @@ type logsExporter struct { func newLogsExporter(logger *zap.Logger, cfg *Config) (*logsExporter, error) { cluster := gocql.NewCluster(cfg.DSN) + if cfg.Auth.UserName != "" && cfg.Auth.Password != "" { + cluster.Authenticator = gocql.PasswordAuthenticator{ + Username: cfg.Auth.UserName, + Password: cfg.Auth.Password, + } + } session, err := cluster.CreateSession() cluster.Keyspace = cfg.Keyspace cluster.Consistency = gocql.Quorum diff --git a/exporter/cassandraexporter/exporter_traces.go b/exporter/cassandraexporter/exporter_traces.go index f8f72e342d92..cb8d25aec7e5 100644 --- a/exporter/cassandraexporter/exporter_traces.go +++ b/exporter/cassandraexporter/exporter_traces.go @@ -24,6 +24,12 @@ type tracesExporter struct { func newTracesExporter(logger *zap.Logger, cfg *Config) (*tracesExporter, error) { cluster := gocql.NewCluster(cfg.DSN) + if cfg.Auth.UserName != "" && cfg.Auth.Password != "" { + cluster.Authenticator = gocql.PasswordAuthenticator{ + Username: cfg.Auth.UserName, + Password: cfg.Auth.Password, + } + } session, err := cluster.CreateSession() cluster.Keyspace = cfg.Keyspace cluster.Consistency = gocql.Quorum From ef4098044ee80dbd9b90f7b87c7ca3493b27ac1a Mon Sep 17 00:00:00 2001 From: Vyacheslav Shmeltser <59681751+go-follow@users.noreply.github.com> Date: Thu, 19 Oct 2023 09:00:37 +0700 Subject: [PATCH 02/21] Update exporter/cassandraexporter/config.go Co-authored-by: Curtis Robert <92119472+crobert-1@users.noreply.github.com> --- exporter/cassandraexporter/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exporter/cassandraexporter/config.go b/exporter/cassandraexporter/config.go index dbe0747fde5f..1e37e3a1e335 100644 --- a/exporter/cassandraexporter/config.go +++ b/exporter/cassandraexporter/config.go @@ -25,5 +25,5 @@ type Compression struct { type Auth struct { UserName string `mapstructure:"username"` - Password string `mapstructure:"password"` + Password configopaque.String `mapstructure:"password"` } From 7ee9b3ddf42c63ba9bfd5da713e1021c3009dcac Mon Sep 17 00:00:00 2001 From: go-follow Date: Thu, 19 Oct 2023 09:21:04 +0700 Subject: [PATCH 03/21] add using configopaque.String instead string for auth.password --- exporter/cassandraexporter/config.go | 3 ++- exporter/cassandraexporter/exporter_logs.go | 2 +- exporter/cassandraexporter/exporter_traces.go | 2 +- exporter/cassandraexporter/go.mod | 1 + exporter/cassandraexporter/go.sum | 2 ++ 5 files changed, 7 insertions(+), 3 deletions(-) diff --git a/exporter/cassandraexporter/config.go b/exporter/cassandraexporter/config.go index 1e37e3a1e335..89961fb7b957 100644 --- a/exporter/cassandraexporter/config.go +++ b/exporter/cassandraexporter/config.go @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 package cassandraexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/cassandraexporter" +import "go.opentelemetry.io/collector/config/configopaque" type Config struct { DSN string `mapstructure:"dsn"` @@ -24,6 +25,6 @@ type Compression struct { } type Auth struct { - UserName string `mapstructure:"username"` + UserName string `mapstructure:"username"` Password configopaque.String `mapstructure:"password"` } diff --git a/exporter/cassandraexporter/exporter_logs.go b/exporter/cassandraexporter/exporter_logs.go index 4ad1cc2155af..43b8051e0b8e 100644 --- a/exporter/cassandraexporter/exporter_logs.go +++ b/exporter/cassandraexporter/exporter_logs.go @@ -28,7 +28,7 @@ func newLogsExporter(logger *zap.Logger, cfg *Config) (*logsExporter, error) { if cfg.Auth.UserName != "" && cfg.Auth.Password != "" { cluster.Authenticator = gocql.PasswordAuthenticator{ Username: cfg.Auth.UserName, - Password: cfg.Auth.Password, + Password: string(cfg.Auth.Password), } } session, err := cluster.CreateSession() diff --git a/exporter/cassandraexporter/exporter_traces.go b/exporter/cassandraexporter/exporter_traces.go index cb8d25aec7e5..e7f34ded49b0 100644 --- a/exporter/cassandraexporter/exporter_traces.go +++ b/exporter/cassandraexporter/exporter_traces.go @@ -27,7 +27,7 @@ func newTracesExporter(logger *zap.Logger, cfg *Config) (*tracesExporter, error) if cfg.Auth.UserName != "" && cfg.Auth.Password != "" { cluster.Authenticator = gocql.PasswordAuthenticator{ Username: cfg.Auth.UserName, - Password: cfg.Auth.Password, + Password: string(cfg.Auth.Password), } } session, err := cluster.CreateSession() diff --git a/exporter/cassandraexporter/go.mod b/exporter/cassandraexporter/go.mod index 8e013299ab50..36a2592cd4f7 100644 --- a/exporter/cassandraexporter/go.mod +++ b/exporter/cassandraexporter/go.mod @@ -7,6 +7,7 @@ require ( github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.87.0 github.com/stretchr/testify v1.8.4 go.opentelemetry.io/collector/component v0.87.1-0.20231017160804-ec0725874313 + go.opentelemetry.io/collector/config/configopaque v0.87.0 go.opentelemetry.io/collector/confmap v0.87.1-0.20231017160804-ec0725874313 go.opentelemetry.io/collector/exporter v0.87.1-0.20231017160804-ec0725874313 go.opentelemetry.io/collector/pdata v1.0.0-rcv0016.0.20231017160804-ec0725874313 diff --git a/exporter/cassandraexporter/go.sum b/exporter/cassandraexporter/go.sum index db5d49672ee0..9732b4d22b04 100644 --- a/exporter/cassandraexporter/go.sum +++ b/exporter/cassandraexporter/go.sum @@ -111,6 +111,8 @@ go.opentelemetry.io/collector v0.87.1-0.20231017160804-ec0725874313 h1:20fAuaqh2 go.opentelemetry.io/collector v0.87.1-0.20231017160804-ec0725874313/go.mod h1:W4496KRc5n40etXDrwYcFs3224Fp+saQ1S+6bS0mWhQ= go.opentelemetry.io/collector/component v0.87.1-0.20231017160804-ec0725874313 h1:rQ7OKu7fxWDfNFKU8q7mMr8NVBLY0t3IFlpyglEKnm4= go.opentelemetry.io/collector/component v0.87.1-0.20231017160804-ec0725874313/go.mod h1:UHArfJc+pijiYXifkEWcq5bNYZyERvGDUQt3r2RnSuU= +go.opentelemetry.io/collector/config/configopaque v0.87.0 h1:+qqJG1oEzX4+/YNbgeaXW9YM0BPWSj5XCi5y2zZLhDY= +go.opentelemetry.io/collector/config/configopaque v0.87.0/go.mod h1:TPCHaU+QXiEV+JXbgyr6mSErTI9chwQyasDVMdJr3eY= go.opentelemetry.io/collector/config/configtelemetry v0.87.1-0.20231017160804-ec0725874313 h1:VugEaPnOkWZ8n5hYlX6lrot0qLXtv9Objvsh8gh/D84= go.opentelemetry.io/collector/config/configtelemetry v0.87.1-0.20231017160804-ec0725874313/go.mod h1:+LAXM5WFMW/UbTlAuSs6L/W72WC+q8TBJt/6z39FPOU= go.opentelemetry.io/collector/confmap v0.87.1-0.20231017160804-ec0725874313 h1:IRtWnNC6yOeBBk/2sEq71eQDkdIqPTgPrC/A/CxsG84= From 29618d1ec09b83a2c29c0bb66fc33dcb0a2eaab7 Mon Sep 17 00:00:00 2001 From: go-follow Date: Fri, 20 Oct 2023 11:04:42 +0700 Subject: [PATCH 04/21] [exporter/cassandra] add test for authorization --- exporter/cassandraexporter/config.go | 4 +- exporter/cassandraexporter/exporter_logs.go | 29 +++++---- .../cassandraexporter/exporter_logs_test.go | 59 +++++++++++++++++++ exporter/cassandraexporter/exporter_traces.go | 16 +---- 4 files changed, 81 insertions(+), 27 deletions(-) create mode 100644 exporter/cassandraexporter/exporter_logs_test.go diff --git a/exporter/cassandraexporter/config.go b/exporter/cassandraexporter/config.go index 89961fb7b957..313496b0a58c 100644 --- a/exporter/cassandraexporter/config.go +++ b/exporter/cassandraexporter/config.go @@ -2,7 +2,9 @@ // SPDX-License-Identifier: Apache-2.0 package cassandraexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/cassandraexporter" -import "go.opentelemetry.io/collector/config/configopaque" +import ( + "go.opentelemetry.io/collector/config/configopaque" +) type Config struct { DSN string `mapstructure:"dsn"` diff --git a/exporter/cassandraexporter/exporter_logs.go b/exporter/cassandraexporter/exporter_logs.go index 43b8051e0b8e..0536b5fc5819 100644 --- a/exporter/cassandraexporter/exporter_logs.go +++ b/exporter/cassandraexporter/exporter_logs.go @@ -24,18 +24,10 @@ type logsExporter struct { } func newLogsExporter(logger *zap.Logger, cfg *Config) (*logsExporter, error) { - cluster := gocql.NewCluster(cfg.DSN) - if cfg.Auth.UserName != "" && cfg.Auth.Password != "" { - cluster.Authenticator = gocql.PasswordAuthenticator{ - Username: cfg.Auth.UserName, - Password: string(cfg.Auth.Password), - } - } - session, err := cluster.CreateSession() + cluster := newCluster(cfg) cluster.Keyspace = cfg.Keyspace - cluster.Consistency = gocql.Quorum - cluster.Port = cfg.Port + session, err := cluster.CreateSession() if err != nil { return nil, err } @@ -45,9 +37,7 @@ func newLogsExporter(logger *zap.Logger, cfg *Config) (*logsExporter, error) { func initializeLogKernel(cfg *Config) error { ctx := context.Background() - cluster := gocql.NewCluster(cfg.DSN) - cluster.Consistency = gocql.Quorum - cluster.Port = cfg.Port + cluster := newCluster(cfg) session, err := cluster.CreateSession() if err != nil { @@ -68,6 +58,19 @@ func initializeLogKernel(cfg *Config) error { return nil } +func newCluster(cfg *Config) *gocql.ClusterConfig { + cluster := gocql.NewCluster(cfg.DSN) + if cfg.Auth.UserName != "" && cfg.Auth.Password != "" { + cluster.Authenticator = gocql.PasswordAuthenticator{ + Username: cfg.Auth.UserName, + Password: string(cfg.Auth.Password), + } + } + cluster.Consistency = gocql.Quorum + cluster.Port = cfg.Port + return cluster +} + func (e *logsExporter) Start(_ context.Context, _ component.Host) error { initializeErr := initializeLogKernel(e.cfg) return initializeErr diff --git a/exporter/cassandraexporter/exporter_logs_test.go b/exporter/cassandraexporter/exporter_logs_test.go new file mode 100644 index 000000000000..0b9b1e7f77fb --- /dev/null +++ b/exporter/cassandraexporter/exporter_logs_test.go @@ -0,0 +1,59 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package cassandraexporter + +import ( + "github.com/gocql/gocql" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestNewCluster(t *testing.T) { + testCases := map[string]struct { + cfg *Config + expectedAuthenticator gocql.Authenticator + }{ + "empty_auth": { + cfg: withDefaultConfig(), + expectedAuthenticator: nil, + }, + "empty_username": { + cfg: withDefaultConfig(func(config *Config) { + config.Auth.Password = "pass" + }), + expectedAuthenticator: nil, + }, + "empty_password": { + cfg: withDefaultConfig(func(config *Config) { + config.Auth.UserName = "user" + }), + expectedAuthenticator: nil, + }, + "success_auth": { + cfg: withDefaultConfig(func(config *Config) { + config.Auth.UserName = "user" + config.Auth.Password = "pass" + }), + expectedAuthenticator: gocql.PasswordAuthenticator{ + Username: "user", + Password: "pass", + }, + }, + } + for name, test := range testCases { + t.Run(name, func(t *testing.T) { + c := newCluster(test.cfg) + require.Equal(t, test.expectedAuthenticator, c.Authenticator) + }) + } +} + +func withDefaultConfig(fns ...func(*Config)) *Config { + cfg := createDefaultConfig().(*Config) + for _, fn := range fns { + fn(cfg) + } + return cfg +} diff --git a/exporter/cassandraexporter/exporter_traces.go b/exporter/cassandraexporter/exporter_traces.go index e7f34ded49b0..c9eebb91e059 100644 --- a/exporter/cassandraexporter/exporter_traces.go +++ b/exporter/cassandraexporter/exporter_traces.go @@ -23,18 +23,10 @@ type tracesExporter struct { } func newTracesExporter(logger *zap.Logger, cfg *Config) (*tracesExporter, error) { - cluster := gocql.NewCluster(cfg.DSN) - if cfg.Auth.UserName != "" && cfg.Auth.Password != "" { - cluster.Authenticator = gocql.PasswordAuthenticator{ - Username: cfg.Auth.UserName, - Password: string(cfg.Auth.Password), - } - } - session, err := cluster.CreateSession() + cluster := newCluster(cfg) cluster.Keyspace = cfg.Keyspace - cluster.Consistency = gocql.Quorum - cluster.Port = cfg.Port + session, err := cluster.CreateSession() if err != nil { return nil, err } @@ -44,9 +36,7 @@ func newTracesExporter(logger *zap.Logger, cfg *Config) (*tracesExporter, error) func initializeTraceKernel(cfg *Config) error { ctx := context.Background() - cluster := gocql.NewCluster(cfg.DSN) - cluster.Consistency = gocql.Quorum - cluster.Port = cfg.Port + cluster := newCluster(cfg) session, err := cluster.CreateSession() if err != nil { From 674fc2545dfd5c52359d550b0af6c1f637fb4617 Mon Sep 17 00:00:00 2001 From: go-follow Date: Fri, 20 Oct 2023 13:48:46 +0700 Subject: [PATCH 05/21] add .chologgen/cassandraexporter-add-authorization.yaml --- .../cassandraexporter-add-authorization.yaml | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 .chloggen/cassandraexporter-add-authorization.yaml diff --git a/.chloggen/cassandraexporter-add-authorization.yaml b/.chloggen/cassandraexporter-add-authorization.yaml new file mode 100644 index 000000000000..9f22f87938a8 --- /dev/null +++ b/.chloggen/cassandraexporter-add-authorization.yaml @@ -0,0 +1,20 @@ +# Use this changelog template to create an entry for release notes. +# If your change doesn't affect end users, such as a test fix or a tooling change, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: enhancement + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: cassandraexporter + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: added authorization by username and password + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [27827] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: From 280e6ed60d55d3e660ff8b7d464c73f07c801314 Mon Sep 17 00:00:00 2001 From: go-follow Date: Sat, 28 Oct 2023 10:12:14 +0700 Subject: [PATCH 06/21] fix check-collector-module-version --- exporter/cassandraexporter/go.mod | 2 +- exporter/cassandraexporter/go.sum | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/exporter/cassandraexporter/go.mod b/exporter/cassandraexporter/go.mod index 36a2592cd4f7..f8016a1dd66a 100644 --- a/exporter/cassandraexporter/go.mod +++ b/exporter/cassandraexporter/go.mod @@ -7,7 +7,7 @@ require ( github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.87.0 github.com/stretchr/testify v1.8.4 go.opentelemetry.io/collector/component v0.87.1-0.20231017160804-ec0725874313 - go.opentelemetry.io/collector/config/configopaque v0.87.0 + go.opentelemetry.io/collector/config/configopaque v0.87.1-0.20231017160804-ec0725874313 go.opentelemetry.io/collector/confmap v0.87.1-0.20231017160804-ec0725874313 go.opentelemetry.io/collector/exporter v0.87.1-0.20231017160804-ec0725874313 go.opentelemetry.io/collector/pdata v1.0.0-rcv0016.0.20231017160804-ec0725874313 diff --git a/exporter/cassandraexporter/go.sum b/exporter/cassandraexporter/go.sum index 9732b4d22b04..24d09eeb7c27 100644 --- a/exporter/cassandraexporter/go.sum +++ b/exporter/cassandraexporter/go.sum @@ -113,6 +113,7 @@ go.opentelemetry.io/collector/component v0.87.1-0.20231017160804-ec0725874313 h1 go.opentelemetry.io/collector/component v0.87.1-0.20231017160804-ec0725874313/go.mod h1:UHArfJc+pijiYXifkEWcq5bNYZyERvGDUQt3r2RnSuU= go.opentelemetry.io/collector/config/configopaque v0.87.0 h1:+qqJG1oEzX4+/YNbgeaXW9YM0BPWSj5XCi5y2zZLhDY= go.opentelemetry.io/collector/config/configopaque v0.87.0/go.mod h1:TPCHaU+QXiEV+JXbgyr6mSErTI9chwQyasDVMdJr3eY= +go.opentelemetry.io/collector/config/configopaque v0.87.1-0.20231017160804-ec0725874313/go.mod h1:TPCHaU+QXiEV+JXbgyr6mSErTI9chwQyasDVMdJr3eY= go.opentelemetry.io/collector/config/configtelemetry v0.87.1-0.20231017160804-ec0725874313 h1:VugEaPnOkWZ8n5hYlX6lrot0qLXtv9Objvsh8gh/D84= go.opentelemetry.io/collector/config/configtelemetry v0.87.1-0.20231017160804-ec0725874313/go.mod h1:+LAXM5WFMW/UbTlAuSs6L/W72WC+q8TBJt/6z39FPOU= go.opentelemetry.io/collector/confmap v0.87.1-0.20231017160804-ec0725874313 h1:IRtWnNC6yOeBBk/2sEq71eQDkdIqPTgPrC/A/CxsG84= From c4250e2ef1d78671f68379bb517d235435d06552 Mon Sep 17 00:00:00 2001 From: go-follow Date: Wed, 18 Oct 2023 19:52:00 +0700 Subject: [PATCH 07/21] [exporter/cassandra] close #27828 added authorization by username and password --- exporter/cassandraexporter/README.md | 4 ++++ exporter/cassandraexporter/config.go | 6 ++++++ exporter/cassandraexporter/exporter_logs.go | 6 ++++++ exporter/cassandraexporter/exporter_traces.go | 6 ++++++ 4 files changed, 22 insertions(+) diff --git a/exporter/cassandraexporter/README.md b/exporter/cassandraexporter/README.md index 1efd9cd72805..3daeef4f774f 100644 --- a/exporter/cassandraexporter/README.md +++ b/exporter/cassandraexporter/README.md @@ -24,6 +24,7 @@ The following settings can be optionally configured: - `replication` (default = class: SimpleStrategy, replication_factor: 1) The strategy of replication. https://cassandra.apache.org/doc/4.1/cassandra/architecture/dynamo.html#replication-strategy - `compression` (default = LZ4Compressor) https://cassandra.apache.org/doc/latest/cassandra/operating/compression.html +- `auth` (default = username: "", password: "") Authorization for the Cassandra. ## Example @@ -39,4 +40,7 @@ exporters: replication_factor: 1 compression: algorithm: "ZstdCompressor" + auth: + username: "your-username" + password: "your-password" ``` diff --git a/exporter/cassandraexporter/config.go b/exporter/cassandraexporter/config.go index b9b6bd769437..dbe0747fde5f 100644 --- a/exporter/cassandraexporter/config.go +++ b/exporter/cassandraexporter/config.go @@ -11,6 +11,7 @@ type Config struct { LogsTable string `mapstructure:"logs_table"` Replication Replication `mapstructure:"replication"` Compression Compression `mapstructure:"compression"` + Auth Auth `mapstructure:"auth"` } type Replication struct { @@ -21,3 +22,8 @@ type Replication struct { type Compression struct { Algorithm string `mapstructure:"algorithm"` } + +type Auth struct { + UserName string `mapstructure:"username"` + Password string `mapstructure:"password"` +} diff --git a/exporter/cassandraexporter/exporter_logs.go b/exporter/cassandraexporter/exporter_logs.go index 8b824829358a..4ad1cc2155af 100644 --- a/exporter/cassandraexporter/exporter_logs.go +++ b/exporter/cassandraexporter/exporter_logs.go @@ -25,6 +25,12 @@ type logsExporter struct { func newLogsExporter(logger *zap.Logger, cfg *Config) (*logsExporter, error) { cluster := gocql.NewCluster(cfg.DSN) + if cfg.Auth.UserName != "" && cfg.Auth.Password != "" { + cluster.Authenticator = gocql.PasswordAuthenticator{ + Username: cfg.Auth.UserName, + Password: cfg.Auth.Password, + } + } session, err := cluster.CreateSession() cluster.Keyspace = cfg.Keyspace cluster.Consistency = gocql.Quorum diff --git a/exporter/cassandraexporter/exporter_traces.go b/exporter/cassandraexporter/exporter_traces.go index f8f72e342d92..cb8d25aec7e5 100644 --- a/exporter/cassandraexporter/exporter_traces.go +++ b/exporter/cassandraexporter/exporter_traces.go @@ -24,6 +24,12 @@ type tracesExporter struct { func newTracesExporter(logger *zap.Logger, cfg *Config) (*tracesExporter, error) { cluster := gocql.NewCluster(cfg.DSN) + if cfg.Auth.UserName != "" && cfg.Auth.Password != "" { + cluster.Authenticator = gocql.PasswordAuthenticator{ + Username: cfg.Auth.UserName, + Password: cfg.Auth.Password, + } + } session, err := cluster.CreateSession() cluster.Keyspace = cfg.Keyspace cluster.Consistency = gocql.Quorum From 3e27122dd0b5c022fc6fe0ac6b7e331a7b5c1b5a Mon Sep 17 00:00:00 2001 From: Vyacheslav Shmeltser <59681751+go-follow@users.noreply.github.com> Date: Thu, 19 Oct 2023 09:00:37 +0700 Subject: [PATCH 08/21] Update exporter/cassandraexporter/config.go Co-authored-by: Curtis Robert <92119472+crobert-1@users.noreply.github.com> --- exporter/cassandraexporter/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exporter/cassandraexporter/config.go b/exporter/cassandraexporter/config.go index dbe0747fde5f..1e37e3a1e335 100644 --- a/exporter/cassandraexporter/config.go +++ b/exporter/cassandraexporter/config.go @@ -25,5 +25,5 @@ type Compression struct { type Auth struct { UserName string `mapstructure:"username"` - Password string `mapstructure:"password"` + Password configopaque.String `mapstructure:"password"` } From b9d79dbaa2f83154660a5180d7e8f45fae2362b7 Mon Sep 17 00:00:00 2001 From: go-follow Date: Thu, 19 Oct 2023 09:21:04 +0700 Subject: [PATCH 09/21] add using configopaque.String instead string for auth.password --- exporter/cassandraexporter/config.go | 3 ++- exporter/cassandraexporter/exporter_logs.go | 2 +- exporter/cassandraexporter/exporter_traces.go | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/exporter/cassandraexporter/config.go b/exporter/cassandraexporter/config.go index 1e37e3a1e335..89961fb7b957 100644 --- a/exporter/cassandraexporter/config.go +++ b/exporter/cassandraexporter/config.go @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 package cassandraexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/cassandraexporter" +import "go.opentelemetry.io/collector/config/configopaque" type Config struct { DSN string `mapstructure:"dsn"` @@ -24,6 +25,6 @@ type Compression struct { } type Auth struct { - UserName string `mapstructure:"username"` + UserName string `mapstructure:"username"` Password configopaque.String `mapstructure:"password"` } diff --git a/exporter/cassandraexporter/exporter_logs.go b/exporter/cassandraexporter/exporter_logs.go index 4ad1cc2155af..43b8051e0b8e 100644 --- a/exporter/cassandraexporter/exporter_logs.go +++ b/exporter/cassandraexporter/exporter_logs.go @@ -28,7 +28,7 @@ func newLogsExporter(logger *zap.Logger, cfg *Config) (*logsExporter, error) { if cfg.Auth.UserName != "" && cfg.Auth.Password != "" { cluster.Authenticator = gocql.PasswordAuthenticator{ Username: cfg.Auth.UserName, - Password: cfg.Auth.Password, + Password: string(cfg.Auth.Password), } } session, err := cluster.CreateSession() diff --git a/exporter/cassandraexporter/exporter_traces.go b/exporter/cassandraexporter/exporter_traces.go index cb8d25aec7e5..e7f34ded49b0 100644 --- a/exporter/cassandraexporter/exporter_traces.go +++ b/exporter/cassandraexporter/exporter_traces.go @@ -27,7 +27,7 @@ func newTracesExporter(logger *zap.Logger, cfg *Config) (*tracesExporter, error) if cfg.Auth.UserName != "" && cfg.Auth.Password != "" { cluster.Authenticator = gocql.PasswordAuthenticator{ Username: cfg.Auth.UserName, - Password: cfg.Auth.Password, + Password: string(cfg.Auth.Password), } } session, err := cluster.CreateSession() From ba8121d667f9b7b1b7a1032f61c2838c1c562eab Mon Sep 17 00:00:00 2001 From: go-follow Date: Fri, 20 Oct 2023 11:04:42 +0700 Subject: [PATCH 10/21] [exporter/cassandra] add test for authorization --- exporter/cassandraexporter/config.go | 4 +- exporter/cassandraexporter/exporter_logs.go | 29 +++++---- .../cassandraexporter/exporter_logs_test.go | 59 +++++++++++++++++++ exporter/cassandraexporter/exporter_traces.go | 16 +---- 4 files changed, 81 insertions(+), 27 deletions(-) create mode 100644 exporter/cassandraexporter/exporter_logs_test.go diff --git a/exporter/cassandraexporter/config.go b/exporter/cassandraexporter/config.go index 89961fb7b957..313496b0a58c 100644 --- a/exporter/cassandraexporter/config.go +++ b/exporter/cassandraexporter/config.go @@ -2,7 +2,9 @@ // SPDX-License-Identifier: Apache-2.0 package cassandraexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/cassandraexporter" -import "go.opentelemetry.io/collector/config/configopaque" +import ( + "go.opentelemetry.io/collector/config/configopaque" +) type Config struct { DSN string `mapstructure:"dsn"` diff --git a/exporter/cassandraexporter/exporter_logs.go b/exporter/cassandraexporter/exporter_logs.go index 43b8051e0b8e..0536b5fc5819 100644 --- a/exporter/cassandraexporter/exporter_logs.go +++ b/exporter/cassandraexporter/exporter_logs.go @@ -24,18 +24,10 @@ type logsExporter struct { } func newLogsExporter(logger *zap.Logger, cfg *Config) (*logsExporter, error) { - cluster := gocql.NewCluster(cfg.DSN) - if cfg.Auth.UserName != "" && cfg.Auth.Password != "" { - cluster.Authenticator = gocql.PasswordAuthenticator{ - Username: cfg.Auth.UserName, - Password: string(cfg.Auth.Password), - } - } - session, err := cluster.CreateSession() + cluster := newCluster(cfg) cluster.Keyspace = cfg.Keyspace - cluster.Consistency = gocql.Quorum - cluster.Port = cfg.Port + session, err := cluster.CreateSession() if err != nil { return nil, err } @@ -45,9 +37,7 @@ func newLogsExporter(logger *zap.Logger, cfg *Config) (*logsExporter, error) { func initializeLogKernel(cfg *Config) error { ctx := context.Background() - cluster := gocql.NewCluster(cfg.DSN) - cluster.Consistency = gocql.Quorum - cluster.Port = cfg.Port + cluster := newCluster(cfg) session, err := cluster.CreateSession() if err != nil { @@ -68,6 +58,19 @@ func initializeLogKernel(cfg *Config) error { return nil } +func newCluster(cfg *Config) *gocql.ClusterConfig { + cluster := gocql.NewCluster(cfg.DSN) + if cfg.Auth.UserName != "" && cfg.Auth.Password != "" { + cluster.Authenticator = gocql.PasswordAuthenticator{ + Username: cfg.Auth.UserName, + Password: string(cfg.Auth.Password), + } + } + cluster.Consistency = gocql.Quorum + cluster.Port = cfg.Port + return cluster +} + func (e *logsExporter) Start(_ context.Context, _ component.Host) error { initializeErr := initializeLogKernel(e.cfg) return initializeErr diff --git a/exporter/cassandraexporter/exporter_logs_test.go b/exporter/cassandraexporter/exporter_logs_test.go new file mode 100644 index 000000000000..0b9b1e7f77fb --- /dev/null +++ b/exporter/cassandraexporter/exporter_logs_test.go @@ -0,0 +1,59 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package cassandraexporter + +import ( + "github.com/gocql/gocql" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestNewCluster(t *testing.T) { + testCases := map[string]struct { + cfg *Config + expectedAuthenticator gocql.Authenticator + }{ + "empty_auth": { + cfg: withDefaultConfig(), + expectedAuthenticator: nil, + }, + "empty_username": { + cfg: withDefaultConfig(func(config *Config) { + config.Auth.Password = "pass" + }), + expectedAuthenticator: nil, + }, + "empty_password": { + cfg: withDefaultConfig(func(config *Config) { + config.Auth.UserName = "user" + }), + expectedAuthenticator: nil, + }, + "success_auth": { + cfg: withDefaultConfig(func(config *Config) { + config.Auth.UserName = "user" + config.Auth.Password = "pass" + }), + expectedAuthenticator: gocql.PasswordAuthenticator{ + Username: "user", + Password: "pass", + }, + }, + } + for name, test := range testCases { + t.Run(name, func(t *testing.T) { + c := newCluster(test.cfg) + require.Equal(t, test.expectedAuthenticator, c.Authenticator) + }) + } +} + +func withDefaultConfig(fns ...func(*Config)) *Config { + cfg := createDefaultConfig().(*Config) + for _, fn := range fns { + fn(cfg) + } + return cfg +} diff --git a/exporter/cassandraexporter/exporter_traces.go b/exporter/cassandraexporter/exporter_traces.go index e7f34ded49b0..c9eebb91e059 100644 --- a/exporter/cassandraexporter/exporter_traces.go +++ b/exporter/cassandraexporter/exporter_traces.go @@ -23,18 +23,10 @@ type tracesExporter struct { } func newTracesExporter(logger *zap.Logger, cfg *Config) (*tracesExporter, error) { - cluster := gocql.NewCluster(cfg.DSN) - if cfg.Auth.UserName != "" && cfg.Auth.Password != "" { - cluster.Authenticator = gocql.PasswordAuthenticator{ - Username: cfg.Auth.UserName, - Password: string(cfg.Auth.Password), - } - } - session, err := cluster.CreateSession() + cluster := newCluster(cfg) cluster.Keyspace = cfg.Keyspace - cluster.Consistency = gocql.Quorum - cluster.Port = cfg.Port + session, err := cluster.CreateSession() if err != nil { return nil, err } @@ -44,9 +36,7 @@ func newTracesExporter(logger *zap.Logger, cfg *Config) (*tracesExporter, error) func initializeTraceKernel(cfg *Config) error { ctx := context.Background() - cluster := gocql.NewCluster(cfg.DSN) - cluster.Consistency = gocql.Quorum - cluster.Port = cfg.Port + cluster := newCluster(cfg) session, err := cluster.CreateSession() if err != nil { From b973bed24134dcbd1525d69bf36e30b39e7824f9 Mon Sep 17 00:00:00 2001 From: go-follow Date: Fri, 20 Oct 2023 13:48:46 +0700 Subject: [PATCH 11/21] add .chologgen/cassandraexporter-add-authorization.yaml --- .../cassandraexporter-add-authorization.yaml | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 .chloggen/cassandraexporter-add-authorization.yaml diff --git a/.chloggen/cassandraexporter-add-authorization.yaml b/.chloggen/cassandraexporter-add-authorization.yaml new file mode 100644 index 000000000000..9f22f87938a8 --- /dev/null +++ b/.chloggen/cassandraexporter-add-authorization.yaml @@ -0,0 +1,20 @@ +# Use this changelog template to create an entry for release notes. +# If your change doesn't affect end users, such as a test fix or a tooling change, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: enhancement + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: cassandraexporter + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: added authorization by username and password + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [27827] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: From 98f0a837d1a2332a722a621fd425f4ae4dc84ab4 Mon Sep 17 00:00:00 2001 From: go-follow Date: Thu, 9 Nov 2023 12:32:45 +0700 Subject: [PATCH 12/21] add go.opentelemetry.io/collector/config/configopaque in go.mod --- exporter/cassandraexporter/go.mod | 1 + exporter/cassandraexporter/go.sum | 2 ++ 2 files changed, 3 insertions(+) diff --git a/exporter/cassandraexporter/go.mod b/exporter/cassandraexporter/go.mod index 2a8f24652069..11cc47e5d785 100644 --- a/exporter/cassandraexporter/go.mod +++ b/exporter/cassandraexporter/go.mod @@ -32,6 +32,7 @@ require ( github.com/pmezard/go-difflib v1.0.0 // indirect go.opencensus.io v0.24.0 // indirect go.opentelemetry.io/collector v0.88.1-0.20231026220224-6405e152a2d9 // indirect + go.opentelemetry.io/collector/config/configopaque v0.88.0 // indirect go.opentelemetry.io/collector/config/configtelemetry v0.88.1-0.20231026220224-6405e152a2d9 // indirect go.opentelemetry.io/collector/consumer v0.88.1-0.20231026220224-6405e152a2d9 // indirect go.opentelemetry.io/collector/extension v0.88.1-0.20231026220224-6405e152a2d9 // indirect diff --git a/exporter/cassandraexporter/go.sum b/exporter/cassandraexporter/go.sum index 0ea5296cf853..0f6ba55a1799 100644 --- a/exporter/cassandraexporter/go.sum +++ b/exporter/cassandraexporter/go.sum @@ -111,6 +111,8 @@ go.opentelemetry.io/collector v0.88.1-0.20231026220224-6405e152a2d9 h1:UIbHSFtHl go.opentelemetry.io/collector v0.88.1-0.20231026220224-6405e152a2d9/go.mod h1:5iWdJH9WM+Bp+t3Ii72ppPmeZ0B2vci07ApE+0fRGKs= go.opentelemetry.io/collector/component v0.88.1-0.20231026220224-6405e152a2d9 h1:t9GCaQDZ1MDBjEAC1Y7NvwiqvVppK6ckAfrUEAlFioA= go.opentelemetry.io/collector/component v0.88.1-0.20231026220224-6405e152a2d9/go.mod h1:4utKxz4Lilym3SPxNXJHosdaTjT1aQxI+TCmnJO54pU= +go.opentelemetry.io/collector/config/configopaque v0.88.0 h1:4Q4UnPdAQzGqPsR7mTegJ47eT/2IvCosEnJstl9re6A= +go.opentelemetry.io/collector/config/configopaque v0.88.0/go.mod h1:TPCHaU+QXiEV+JXbgyr6mSErTI9chwQyasDVMdJr3eY= go.opentelemetry.io/collector/config/configtelemetry v0.88.1-0.20231026220224-6405e152a2d9 h1:4WPy3qE1lJE1LZE7t1kAj1XSZN85w68JknZO5Uo00vw= go.opentelemetry.io/collector/config/configtelemetry v0.88.1-0.20231026220224-6405e152a2d9/go.mod h1:+LAXM5WFMW/UbTlAuSs6L/W72WC+q8TBJt/6z39FPOU= go.opentelemetry.io/collector/confmap v0.88.1-0.20231026220224-6405e152a2d9 h1:JKFChlNpigR1Q4hZUjDU2sB2VuQ+RigAh7oOQfdcaiQ= From 21cdc42f5683b8ca05df4b9eef5e00a2b3c5fd25 Mon Sep 17 00:00:00 2001 From: go-follow Date: Wed, 18 Oct 2023 19:52:00 +0700 Subject: [PATCH 13/21] [exporter/cassandra] close #27828 added authorization by username and password --- exporter/cassandraexporter/README.md | 4 ++++ exporter/cassandraexporter/config.go | 6 ++++++ exporter/cassandraexporter/exporter_logs.go | 6 ++++++ exporter/cassandraexporter/exporter_traces.go | 6 ++++++ 4 files changed, 22 insertions(+) diff --git a/exporter/cassandraexporter/README.md b/exporter/cassandraexporter/README.md index 1efd9cd72805..3daeef4f774f 100644 --- a/exporter/cassandraexporter/README.md +++ b/exporter/cassandraexporter/README.md @@ -24,6 +24,7 @@ The following settings can be optionally configured: - `replication` (default = class: SimpleStrategy, replication_factor: 1) The strategy of replication. https://cassandra.apache.org/doc/4.1/cassandra/architecture/dynamo.html#replication-strategy - `compression` (default = LZ4Compressor) https://cassandra.apache.org/doc/latest/cassandra/operating/compression.html +- `auth` (default = username: "", password: "") Authorization for the Cassandra. ## Example @@ -39,4 +40,7 @@ exporters: replication_factor: 1 compression: algorithm: "ZstdCompressor" + auth: + username: "your-username" + password: "your-password" ``` diff --git a/exporter/cassandraexporter/config.go b/exporter/cassandraexporter/config.go index b9b6bd769437..dbe0747fde5f 100644 --- a/exporter/cassandraexporter/config.go +++ b/exporter/cassandraexporter/config.go @@ -11,6 +11,7 @@ type Config struct { LogsTable string `mapstructure:"logs_table"` Replication Replication `mapstructure:"replication"` Compression Compression `mapstructure:"compression"` + Auth Auth `mapstructure:"auth"` } type Replication struct { @@ -21,3 +22,8 @@ type Replication struct { type Compression struct { Algorithm string `mapstructure:"algorithm"` } + +type Auth struct { + UserName string `mapstructure:"username"` + Password string `mapstructure:"password"` +} diff --git a/exporter/cassandraexporter/exporter_logs.go b/exporter/cassandraexporter/exporter_logs.go index 8b824829358a..4ad1cc2155af 100644 --- a/exporter/cassandraexporter/exporter_logs.go +++ b/exporter/cassandraexporter/exporter_logs.go @@ -25,6 +25,12 @@ type logsExporter struct { func newLogsExporter(logger *zap.Logger, cfg *Config) (*logsExporter, error) { cluster := gocql.NewCluster(cfg.DSN) + if cfg.Auth.UserName != "" && cfg.Auth.Password != "" { + cluster.Authenticator = gocql.PasswordAuthenticator{ + Username: cfg.Auth.UserName, + Password: cfg.Auth.Password, + } + } session, err := cluster.CreateSession() cluster.Keyspace = cfg.Keyspace cluster.Consistency = gocql.Quorum diff --git a/exporter/cassandraexporter/exporter_traces.go b/exporter/cassandraexporter/exporter_traces.go index f8f72e342d92..cb8d25aec7e5 100644 --- a/exporter/cassandraexporter/exporter_traces.go +++ b/exporter/cassandraexporter/exporter_traces.go @@ -24,6 +24,12 @@ type tracesExporter struct { func newTracesExporter(logger *zap.Logger, cfg *Config) (*tracesExporter, error) { cluster := gocql.NewCluster(cfg.DSN) + if cfg.Auth.UserName != "" && cfg.Auth.Password != "" { + cluster.Authenticator = gocql.PasswordAuthenticator{ + Username: cfg.Auth.UserName, + Password: cfg.Auth.Password, + } + } session, err := cluster.CreateSession() cluster.Keyspace = cfg.Keyspace cluster.Consistency = gocql.Quorum From 2c0453425bc2c02d86e6d19d765bf095fd65b6bf Mon Sep 17 00:00:00 2001 From: Vyacheslav Shmeltser <59681751+go-follow@users.noreply.github.com> Date: Thu, 19 Oct 2023 09:00:37 +0700 Subject: [PATCH 14/21] Update exporter/cassandraexporter/config.go Co-authored-by: Curtis Robert <92119472+crobert-1@users.noreply.github.com> --- exporter/cassandraexporter/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exporter/cassandraexporter/config.go b/exporter/cassandraexporter/config.go index dbe0747fde5f..1e37e3a1e335 100644 --- a/exporter/cassandraexporter/config.go +++ b/exporter/cassandraexporter/config.go @@ -25,5 +25,5 @@ type Compression struct { type Auth struct { UserName string `mapstructure:"username"` - Password string `mapstructure:"password"` + Password configopaque.String `mapstructure:"password"` } From c2492f0e35c784dc861f7d41881b3d1a4713dd6f Mon Sep 17 00:00:00 2001 From: go-follow Date: Thu, 19 Oct 2023 09:21:04 +0700 Subject: [PATCH 15/21] add using configopaque.String instead string for auth.password --- exporter/cassandraexporter/config.go | 3 ++- exporter/cassandraexporter/exporter_logs.go | 2 +- exporter/cassandraexporter/exporter_traces.go | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/exporter/cassandraexporter/config.go b/exporter/cassandraexporter/config.go index 1e37e3a1e335..89961fb7b957 100644 --- a/exporter/cassandraexporter/config.go +++ b/exporter/cassandraexporter/config.go @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 package cassandraexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/cassandraexporter" +import "go.opentelemetry.io/collector/config/configopaque" type Config struct { DSN string `mapstructure:"dsn"` @@ -24,6 +25,6 @@ type Compression struct { } type Auth struct { - UserName string `mapstructure:"username"` + UserName string `mapstructure:"username"` Password configopaque.String `mapstructure:"password"` } diff --git a/exporter/cassandraexporter/exporter_logs.go b/exporter/cassandraexporter/exporter_logs.go index 4ad1cc2155af..43b8051e0b8e 100644 --- a/exporter/cassandraexporter/exporter_logs.go +++ b/exporter/cassandraexporter/exporter_logs.go @@ -28,7 +28,7 @@ func newLogsExporter(logger *zap.Logger, cfg *Config) (*logsExporter, error) { if cfg.Auth.UserName != "" && cfg.Auth.Password != "" { cluster.Authenticator = gocql.PasswordAuthenticator{ Username: cfg.Auth.UserName, - Password: cfg.Auth.Password, + Password: string(cfg.Auth.Password), } } session, err := cluster.CreateSession() diff --git a/exporter/cassandraexporter/exporter_traces.go b/exporter/cassandraexporter/exporter_traces.go index cb8d25aec7e5..e7f34ded49b0 100644 --- a/exporter/cassandraexporter/exporter_traces.go +++ b/exporter/cassandraexporter/exporter_traces.go @@ -27,7 +27,7 @@ func newTracesExporter(logger *zap.Logger, cfg *Config) (*tracesExporter, error) if cfg.Auth.UserName != "" && cfg.Auth.Password != "" { cluster.Authenticator = gocql.PasswordAuthenticator{ Username: cfg.Auth.UserName, - Password: cfg.Auth.Password, + Password: string(cfg.Auth.Password), } } session, err := cluster.CreateSession() From d77c91ac95f527cf4540d16984f8ca7fec4855d1 Mon Sep 17 00:00:00 2001 From: go-follow Date: Fri, 20 Oct 2023 11:04:42 +0700 Subject: [PATCH 16/21] [exporter/cassandra] add test for authorization --- exporter/cassandraexporter/config.go | 4 +- exporter/cassandraexporter/exporter_logs.go | 29 +++++---- .../cassandraexporter/exporter_logs_test.go | 59 +++++++++++++++++++ exporter/cassandraexporter/exporter_traces.go | 16 +---- 4 files changed, 81 insertions(+), 27 deletions(-) create mode 100644 exporter/cassandraexporter/exporter_logs_test.go diff --git a/exporter/cassandraexporter/config.go b/exporter/cassandraexporter/config.go index 89961fb7b957..313496b0a58c 100644 --- a/exporter/cassandraexporter/config.go +++ b/exporter/cassandraexporter/config.go @@ -2,7 +2,9 @@ // SPDX-License-Identifier: Apache-2.0 package cassandraexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/cassandraexporter" -import "go.opentelemetry.io/collector/config/configopaque" +import ( + "go.opentelemetry.io/collector/config/configopaque" +) type Config struct { DSN string `mapstructure:"dsn"` diff --git a/exporter/cassandraexporter/exporter_logs.go b/exporter/cassandraexporter/exporter_logs.go index 43b8051e0b8e..0536b5fc5819 100644 --- a/exporter/cassandraexporter/exporter_logs.go +++ b/exporter/cassandraexporter/exporter_logs.go @@ -24,18 +24,10 @@ type logsExporter struct { } func newLogsExporter(logger *zap.Logger, cfg *Config) (*logsExporter, error) { - cluster := gocql.NewCluster(cfg.DSN) - if cfg.Auth.UserName != "" && cfg.Auth.Password != "" { - cluster.Authenticator = gocql.PasswordAuthenticator{ - Username: cfg.Auth.UserName, - Password: string(cfg.Auth.Password), - } - } - session, err := cluster.CreateSession() + cluster := newCluster(cfg) cluster.Keyspace = cfg.Keyspace - cluster.Consistency = gocql.Quorum - cluster.Port = cfg.Port + session, err := cluster.CreateSession() if err != nil { return nil, err } @@ -45,9 +37,7 @@ func newLogsExporter(logger *zap.Logger, cfg *Config) (*logsExporter, error) { func initializeLogKernel(cfg *Config) error { ctx := context.Background() - cluster := gocql.NewCluster(cfg.DSN) - cluster.Consistency = gocql.Quorum - cluster.Port = cfg.Port + cluster := newCluster(cfg) session, err := cluster.CreateSession() if err != nil { @@ -68,6 +58,19 @@ func initializeLogKernel(cfg *Config) error { return nil } +func newCluster(cfg *Config) *gocql.ClusterConfig { + cluster := gocql.NewCluster(cfg.DSN) + if cfg.Auth.UserName != "" && cfg.Auth.Password != "" { + cluster.Authenticator = gocql.PasswordAuthenticator{ + Username: cfg.Auth.UserName, + Password: string(cfg.Auth.Password), + } + } + cluster.Consistency = gocql.Quorum + cluster.Port = cfg.Port + return cluster +} + func (e *logsExporter) Start(_ context.Context, _ component.Host) error { initializeErr := initializeLogKernel(e.cfg) return initializeErr diff --git a/exporter/cassandraexporter/exporter_logs_test.go b/exporter/cassandraexporter/exporter_logs_test.go new file mode 100644 index 000000000000..0b9b1e7f77fb --- /dev/null +++ b/exporter/cassandraexporter/exporter_logs_test.go @@ -0,0 +1,59 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package cassandraexporter + +import ( + "github.com/gocql/gocql" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestNewCluster(t *testing.T) { + testCases := map[string]struct { + cfg *Config + expectedAuthenticator gocql.Authenticator + }{ + "empty_auth": { + cfg: withDefaultConfig(), + expectedAuthenticator: nil, + }, + "empty_username": { + cfg: withDefaultConfig(func(config *Config) { + config.Auth.Password = "pass" + }), + expectedAuthenticator: nil, + }, + "empty_password": { + cfg: withDefaultConfig(func(config *Config) { + config.Auth.UserName = "user" + }), + expectedAuthenticator: nil, + }, + "success_auth": { + cfg: withDefaultConfig(func(config *Config) { + config.Auth.UserName = "user" + config.Auth.Password = "pass" + }), + expectedAuthenticator: gocql.PasswordAuthenticator{ + Username: "user", + Password: "pass", + }, + }, + } + for name, test := range testCases { + t.Run(name, func(t *testing.T) { + c := newCluster(test.cfg) + require.Equal(t, test.expectedAuthenticator, c.Authenticator) + }) + } +} + +func withDefaultConfig(fns ...func(*Config)) *Config { + cfg := createDefaultConfig().(*Config) + for _, fn := range fns { + fn(cfg) + } + return cfg +} diff --git a/exporter/cassandraexporter/exporter_traces.go b/exporter/cassandraexporter/exporter_traces.go index e7f34ded49b0..c9eebb91e059 100644 --- a/exporter/cassandraexporter/exporter_traces.go +++ b/exporter/cassandraexporter/exporter_traces.go @@ -23,18 +23,10 @@ type tracesExporter struct { } func newTracesExporter(logger *zap.Logger, cfg *Config) (*tracesExporter, error) { - cluster := gocql.NewCluster(cfg.DSN) - if cfg.Auth.UserName != "" && cfg.Auth.Password != "" { - cluster.Authenticator = gocql.PasswordAuthenticator{ - Username: cfg.Auth.UserName, - Password: string(cfg.Auth.Password), - } - } - session, err := cluster.CreateSession() + cluster := newCluster(cfg) cluster.Keyspace = cfg.Keyspace - cluster.Consistency = gocql.Quorum - cluster.Port = cfg.Port + session, err := cluster.CreateSession() if err != nil { return nil, err } @@ -44,9 +36,7 @@ func newTracesExporter(logger *zap.Logger, cfg *Config) (*tracesExporter, error) func initializeTraceKernel(cfg *Config) error { ctx := context.Background() - cluster := gocql.NewCluster(cfg.DSN) - cluster.Consistency = gocql.Quorum - cluster.Port = cfg.Port + cluster := newCluster(cfg) session, err := cluster.CreateSession() if err != nil { From 7fd8aba618543cc7d76736094435424ffb9e5f45 Mon Sep 17 00:00:00 2001 From: go-follow Date: Fri, 20 Oct 2023 13:48:46 +0700 Subject: [PATCH 17/21] add .chologgen/cassandraexporter-add-authorization.yaml --- .../cassandraexporter-add-authorization.yaml | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 .chloggen/cassandraexporter-add-authorization.yaml diff --git a/.chloggen/cassandraexporter-add-authorization.yaml b/.chloggen/cassandraexporter-add-authorization.yaml new file mode 100644 index 000000000000..9f22f87938a8 --- /dev/null +++ b/.chloggen/cassandraexporter-add-authorization.yaml @@ -0,0 +1,20 @@ +# Use this changelog template to create an entry for release notes. +# If your change doesn't affect end users, such as a test fix or a tooling change, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: enhancement + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: cassandraexporter + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: added authorization by username and password + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [27827] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: From ed75f9ec144f5763238dbe1388eb56cc05823a7e Mon Sep 17 00:00:00 2001 From: go-follow Date: Thu, 19 Oct 2023 09:21:04 +0700 Subject: [PATCH 18/21] add using configopaque.String instead string for auth.password --- exporter/cassandraexporter/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exporter/cassandraexporter/config.go b/exporter/cassandraexporter/config.go index 313496b0a58c..fc9fc740ec15 100644 --- a/exporter/cassandraexporter/config.go +++ b/exporter/cassandraexporter/config.go @@ -27,6 +27,6 @@ type Compression struct { } type Auth struct { - UserName string `mapstructure:"username"` + UserName string `mapstructure:"username"` Password configopaque.String `mapstructure:"password"` } From 7b643bd10b3d8f3fc3e14905218bf899fc0abdee Mon Sep 17 00:00:00 2001 From: go-follow Date: Fri, 24 Nov 2023 13:46:06 +0700 Subject: [PATCH 19/21] [exporter/cassandra] add check fill auth.username and auth.password --- exporter/cassandraexporter/exporter_logs.go | 21 +++++++++++++++---- .../cassandraexporter/exporter_logs_test.go | 11 ++++++++-- exporter/cassandraexporter/exporter_traces.go | 10 +++++++-- exporter/cassandraexporter/go.mod | 1 + exporter/cassandraexporter/go.sum | 2 ++ 5 files changed, 37 insertions(+), 8 deletions(-) diff --git a/exporter/cassandraexporter/exporter_logs.go b/exporter/cassandraexporter/exporter_logs.go index 0536b5fc5819..aa133c709043 100644 --- a/exporter/cassandraexporter/exporter_logs.go +++ b/exporter/cassandraexporter/exporter_logs.go @@ -6,6 +6,7 @@ package cassandraexporter // import "github.com/open-telemetry/opentelemetry-col import ( "context" "encoding/json" + "errors" "fmt" "time" @@ -24,7 +25,10 @@ type logsExporter struct { } func newLogsExporter(logger *zap.Logger, cfg *Config) (*logsExporter, error) { - cluster := newCluster(cfg) + cluster, err := newCluster(cfg) + if err != nil { + return nil, err + } cluster.Keyspace = cfg.Keyspace session, err := cluster.CreateSession() @@ -37,7 +41,10 @@ func newLogsExporter(logger *zap.Logger, cfg *Config) (*logsExporter, error) { func initializeLogKernel(cfg *Config) error { ctx := context.Background() - cluster := newCluster(cfg) + cluster, err := newCluster(cfg) + if err != nil { + return err + } session, err := cluster.CreateSession() if err != nil { @@ -58,8 +65,14 @@ func initializeLogKernel(cfg *Config) error { return nil } -func newCluster(cfg *Config) *gocql.ClusterConfig { +func newCluster(cfg *Config) (*gocql.ClusterConfig, error) { cluster := gocql.NewCluster(cfg.DSN) + if cfg.Auth.UserName != "" && cfg.Auth.Password == "" { + return nil, errors.New("empty auth.password") + } + if cfg.Auth.Password != "" && cfg.Auth.UserName == "" { + return nil, errors.New("empty auth.username") + } if cfg.Auth.UserName != "" && cfg.Auth.Password != "" { cluster.Authenticator = gocql.PasswordAuthenticator{ Username: cfg.Auth.UserName, @@ -68,7 +81,7 @@ func newCluster(cfg *Config) *gocql.ClusterConfig { } cluster.Consistency = gocql.Quorum cluster.Port = cfg.Port - return cluster + return cluster, nil } func (e *logsExporter) Start(_ context.Context, _ component.Host) error { diff --git a/exporter/cassandraexporter/exporter_logs_test.go b/exporter/cassandraexporter/exporter_logs_test.go index 0b9b1e7f77fb..c3ed55a61446 100644 --- a/exporter/cassandraexporter/exporter_logs_test.go +++ b/exporter/cassandraexporter/exporter_logs_test.go @@ -4,6 +4,7 @@ package cassandraexporter import ( + "errors" "github.com/gocql/gocql" "testing" @@ -14,6 +15,7 @@ func TestNewCluster(t *testing.T) { testCases := map[string]struct { cfg *Config expectedAuthenticator gocql.Authenticator + expectedErr error }{ "empty_auth": { cfg: withDefaultConfig(), @@ -24,12 +26,14 @@ func TestNewCluster(t *testing.T) { config.Auth.Password = "pass" }), expectedAuthenticator: nil, + expectedErr: errors.New("empty auth.username"), }, "empty_password": { cfg: withDefaultConfig(func(config *Config) { config.Auth.UserName = "user" }), expectedAuthenticator: nil, + expectedErr: errors.New("empty auth.password"), }, "success_auth": { cfg: withDefaultConfig(func(config *Config) { @@ -44,8 +48,11 @@ func TestNewCluster(t *testing.T) { } for name, test := range testCases { t.Run(name, func(t *testing.T) { - c := newCluster(test.cfg) - require.Equal(t, test.expectedAuthenticator, c.Authenticator) + c, err := newCluster(test.cfg) + if err == nil { + require.Equal(t, test.expectedAuthenticator, c.Authenticator) + } + require.Equal(t, test.expectedErr, err) }) } } diff --git a/exporter/cassandraexporter/exporter_traces.go b/exporter/cassandraexporter/exporter_traces.go index c9eebb91e059..45ffe895f093 100644 --- a/exporter/cassandraexporter/exporter_traces.go +++ b/exporter/cassandraexporter/exporter_traces.go @@ -23,7 +23,10 @@ type tracesExporter struct { } func newTracesExporter(logger *zap.Logger, cfg *Config) (*tracesExporter, error) { - cluster := newCluster(cfg) + cluster, err := newCluster(cfg) + if err != nil { + return nil, err + } cluster.Keyspace = cfg.Keyspace session, err := cluster.CreateSession() @@ -36,7 +39,10 @@ func newTracesExporter(logger *zap.Logger, cfg *Config) (*tracesExporter, error) func initializeTraceKernel(cfg *Config) error { ctx := context.Background() - cluster := newCluster(cfg) + cluster, err := newCluster(cfg) + if err != nil { + return err + } session, err := cluster.CreateSession() if err != nil { diff --git a/exporter/cassandraexporter/go.mod b/exporter/cassandraexporter/go.mod index 37d8f2ce8ed5..983f305f050e 100644 --- a/exporter/cassandraexporter/go.mod +++ b/exporter/cassandraexporter/go.mod @@ -7,6 +7,7 @@ require ( github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.89.0 github.com/stretchr/testify v1.8.4 go.opentelemetry.io/collector/component v0.89.0 + go.opentelemetry.io/collector/config/configopaque v0.89.0 go.opentelemetry.io/collector/confmap v0.89.0 go.opentelemetry.io/collector/exporter v0.89.0 go.opentelemetry.io/collector/pdata v1.0.0-rcv0018 diff --git a/exporter/cassandraexporter/go.sum b/exporter/cassandraexporter/go.sum index 207d7469a6f1..362c715ee616 100644 --- a/exporter/cassandraexporter/go.sum +++ b/exporter/cassandraexporter/go.sum @@ -113,6 +113,8 @@ go.opentelemetry.io/collector v0.89.0 h1:lzpfD9NTHh+1M+qzcoYUH+i2rOgFSox3bGQFUI5 go.opentelemetry.io/collector v0.89.0/go.mod h1:UZUtmQ3kai0CLPWvPmHKpmwqqEoo50n1bwzYYhXX0eA= go.opentelemetry.io/collector/component v0.89.0 h1:PoQJX86BpaSZhzx0deQXHh3QMuW6XKVmolSdTKE506c= go.opentelemetry.io/collector/component v0.89.0/go.mod h1:ZZncnMVaNs++JIbAMiemUIWLZrZ3PMEzI3S3K8pnkws= +go.opentelemetry.io/collector/config/configopaque v0.89.0 h1:Ad6yGcGBHs+J9SNjkedY68JsLZ1vBn4kKzdqKuTCRsE= +go.opentelemetry.io/collector/config/configopaque v0.89.0/go.mod h1:TPCHaU+QXiEV+JXbgyr6mSErTI9chwQyasDVMdJr3eY= go.opentelemetry.io/collector/config/configtelemetry v0.89.0 h1:NtRknYDfMgP1r8mnByo6qQQK8IBw/lF9Qke5f7VhGZ0= go.opentelemetry.io/collector/config/configtelemetry v0.89.0/go.mod h1:+LAXM5WFMW/UbTlAuSs6L/W72WC+q8TBJt/6z39FPOU= go.opentelemetry.io/collector/confmap v0.89.0 h1:N5Vg1+FXEFBHHlGIPg4OSlM9uTHjCI7RlWWrKjtOzWQ= From 6ad50500db65f94ecdcbb2ee7c15b1ce64a3041a Mon Sep 17 00:00:00 2001 From: go-follow Date: Sun, 14 Jan 2024 17:01:04 +0700 Subject: [PATCH 20/21] fix conflicts --- exporter/cassandraexporter/config.go | 6 +++--- exporter/cassandraexporter/exporter_logs_test.go | 2 +- exporter/cassandraexporter/go.mod | 1 + exporter/cassandraexporter/go.sum | 2 ++ 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/exporter/cassandraexporter/config.go b/exporter/cassandraexporter/config.go index 8213e623924a..97885c9c19a4 100644 --- a/exporter/cassandraexporter/config.go +++ b/exporter/cassandraexporter/config.go @@ -3,8 +3,9 @@ package cassandraexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/cassandraexporter" import ( - "go.opentelemetry.io/collector/config/configopaque" "time" + + "go.opentelemetry.io/collector/config/configopaque" ) type Config struct { @@ -16,8 +17,7 @@ type Config struct { LogsTable string `mapstructure:"logs_table"` Replication Replication `mapstructure:"replication"` Compression Compression `mapstructure:"compression"` - Auth Auth `mapstructure:"auth"` - + Auth Auth `mapstructure:"auth"` } type Replication struct { diff --git a/exporter/cassandraexporter/exporter_logs_test.go b/exporter/cassandraexporter/exporter_logs_test.go index c3ed55a61446..017b14fc2346 100644 --- a/exporter/cassandraexporter/exporter_logs_test.go +++ b/exporter/cassandraexporter/exporter_logs_test.go @@ -5,9 +5,9 @@ package cassandraexporter import ( "errors" - "github.com/gocql/gocql" "testing" + "github.com/gocql/gocql" "github.com/stretchr/testify/require" ) diff --git a/exporter/cassandraexporter/go.mod b/exporter/cassandraexporter/go.mod index 4e022a162ca5..0a97e3651674 100644 --- a/exporter/cassandraexporter/go.mod +++ b/exporter/cassandraexporter/go.mod @@ -7,6 +7,7 @@ require ( github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.92.0 github.com/stretchr/testify v1.8.4 go.opentelemetry.io/collector/component v0.92.1-0.20240112172857-83d463ceba06 + go.opentelemetry.io/collector/config/configopaque v0.92.0 go.opentelemetry.io/collector/confmap v0.92.1-0.20240112172857-83d463ceba06 go.opentelemetry.io/collector/exporter v0.92.1-0.20240112172857-83d463ceba06 go.opentelemetry.io/collector/pdata v1.0.2-0.20240112172857-83d463ceba06 diff --git a/exporter/cassandraexporter/go.sum b/exporter/cassandraexporter/go.sum index 00a3712f8070..2cbe7a5ee6f6 100644 --- a/exporter/cassandraexporter/go.sum +++ b/exporter/cassandraexporter/go.sum @@ -279,6 +279,8 @@ go.opentelemetry.io/collector v0.92.1-0.20240112172857-83d463ceba06 h1:305NltdJC go.opentelemetry.io/collector v0.92.1-0.20240112172857-83d463ceba06/go.mod h1:3MM6b+3rjdLMf3grxk4l9Mu4udi4C2kObyaT+Z+KuoI= go.opentelemetry.io/collector/component v0.92.1-0.20240112172857-83d463ceba06 h1:2bkhiQn7O1UGsXlgxPQ3zf2pX+A4AGCxWs0SjZk1BXE= go.opentelemetry.io/collector/component v0.92.1-0.20240112172857-83d463ceba06/go.mod h1:J5ZAcv/ONu5Cdid/UzqF2T87k8ui8Z+c6n8s+640Vc8= +go.opentelemetry.io/collector/config/configopaque v0.92.0 h1:YvUSVa3Vxt2gSl5SwEohtn9HG0+8bO4zuAOCVICBIVU= +go.opentelemetry.io/collector/config/configopaque v0.92.0/go.mod h1:dQK8eUXjIGKaw1RB7UIg2nqx56AueNxeKFCdB0P1ypg= go.opentelemetry.io/collector/config/configretry v0.92.1-0.20240112172857-83d463ceba06 h1:o56gyZ9o4lAbcd3jP7MC3DqhIYo11qyLL0w5KB949XY= go.opentelemetry.io/collector/config/configretry v0.92.1-0.20240112172857-83d463ceba06/go.mod h1:gt1HRYyMxcMca9lbDLPbivQzsUCjVjkPAn/3S6fiD14= go.opentelemetry.io/collector/config/configtelemetry v0.92.1-0.20240112172857-83d463ceba06 h1:FqiK9txeD9sIn5edjyYXj/RB9/YVle10QR43PyhVt6o= From 87a2217c7261183d864966fcec69eca956d0265a Mon Sep 17 00:00:00 2001 From: go-follow Date: Mon, 15 Jan 2024 11:17:43 +0700 Subject: [PATCH 21/21] fix check-collector-module-version --- exporter/cassandraexporter/go.mod | 2 +- exporter/cassandraexporter/go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/exporter/cassandraexporter/go.mod b/exporter/cassandraexporter/go.mod index 0a97e3651674..c149496d2b74 100644 --- a/exporter/cassandraexporter/go.mod +++ b/exporter/cassandraexporter/go.mod @@ -7,7 +7,7 @@ require ( github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.92.0 github.com/stretchr/testify v1.8.4 go.opentelemetry.io/collector/component v0.92.1-0.20240112172857-83d463ceba06 - go.opentelemetry.io/collector/config/configopaque v0.92.0 + go.opentelemetry.io/collector/config/configopaque v0.92.1-0.20240112172857-83d463ceba06 go.opentelemetry.io/collector/confmap v0.92.1-0.20240112172857-83d463ceba06 go.opentelemetry.io/collector/exporter v0.92.1-0.20240112172857-83d463ceba06 go.opentelemetry.io/collector/pdata v1.0.2-0.20240112172857-83d463ceba06 diff --git a/exporter/cassandraexporter/go.sum b/exporter/cassandraexporter/go.sum index 2cbe7a5ee6f6..7cef378ed9d8 100644 --- a/exporter/cassandraexporter/go.sum +++ b/exporter/cassandraexporter/go.sum @@ -279,8 +279,8 @@ go.opentelemetry.io/collector v0.92.1-0.20240112172857-83d463ceba06 h1:305NltdJC go.opentelemetry.io/collector v0.92.1-0.20240112172857-83d463ceba06/go.mod h1:3MM6b+3rjdLMf3grxk4l9Mu4udi4C2kObyaT+Z+KuoI= go.opentelemetry.io/collector/component v0.92.1-0.20240112172857-83d463ceba06 h1:2bkhiQn7O1UGsXlgxPQ3zf2pX+A4AGCxWs0SjZk1BXE= go.opentelemetry.io/collector/component v0.92.1-0.20240112172857-83d463ceba06/go.mod h1:J5ZAcv/ONu5Cdid/UzqF2T87k8ui8Z+c6n8s+640Vc8= -go.opentelemetry.io/collector/config/configopaque v0.92.0 h1:YvUSVa3Vxt2gSl5SwEohtn9HG0+8bO4zuAOCVICBIVU= -go.opentelemetry.io/collector/config/configopaque v0.92.0/go.mod h1:dQK8eUXjIGKaw1RB7UIg2nqx56AueNxeKFCdB0P1ypg= +go.opentelemetry.io/collector/config/configopaque v0.92.1-0.20240112172857-83d463ceba06 h1:M1FjOD4ms4Mpzg9/r6ryjxNDz6dVK/2Y2yPu5syGKZo= +go.opentelemetry.io/collector/config/configopaque v0.92.1-0.20240112172857-83d463ceba06/go.mod h1:dQK8eUXjIGKaw1RB7UIg2nqx56AueNxeKFCdB0P1ypg= go.opentelemetry.io/collector/config/configretry v0.92.1-0.20240112172857-83d463ceba06 h1:o56gyZ9o4lAbcd3jP7MC3DqhIYo11qyLL0w5KB949XY= go.opentelemetry.io/collector/config/configretry v0.92.1-0.20240112172857-83d463ceba06/go.mod h1:gt1HRYyMxcMca9lbDLPbivQzsUCjVjkPAn/3S6fiD14= go.opentelemetry.io/collector/config/configtelemetry v0.92.1-0.20240112172857-83d463ceba06 h1:FqiK9txeD9sIn5edjyYXj/RB9/YVle10QR43PyhVt6o=