Skip to content

Commit

Permalink
Replace validation for app token to UUID validation
Browse files Browse the repository at this point in the history
  • Loading branch information
AkhigbeEromo committed Jan 10, 2025
1 parent f633532 commit cdf7fa2
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 14 deletions.
13 changes: 9 additions & 4 deletions exporter/sematextexporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package sematextexporter // import "github.com/open-telemetry/opentelemetry-coll
import (
"fmt"
"strings"
"regexp"

"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/config/configretry"
Expand Down Expand Up @@ -62,11 +63,11 @@ func (cfg *Config) Validate() error {
if strings.ToLower(cfg.Region) != euRegion && strings.ToLower(cfg.Region) != usRegion {
return fmt.Errorf("invalid region: %s. please use either 'EU' or 'US'", cfg.Region)
}
if len(cfg.MetricsConfig.AppToken) != appTokenLength {
return fmt.Errorf("invalid metrics app_token: %s. app_token should be 36 characters", cfg.MetricsConfig.AppToken)
if !isValidUUID(cfg.MetricsConfig.AppToken) {
return fmt.Errorf("invalid metrics app_token: %s. app_token is not a valid UUID", cfg.MetricsConfig.AppToken)
}
if len(cfg.LogsConfig.AppToken) != appTokenLength {
return fmt.Errorf("invalid logs app_token: %s. app_token should be 36 characters", cfg.LogsConfig.AppToken)
if !isValidUUID(cfg.LogsConfig.AppToken) {
return fmt.Errorf("invalid metrics app_token: %s. app_token is not a valid UUID", cfg.MetricsConfig.AppToken)
}
if strings.ToLower(cfg.Region) == euRegion {
cfg.MetricsEndpoint = euMetricsEndpoint
Expand All @@ -79,3 +80,7 @@ func (cfg *Config) Validate() error {

return nil
}
func isValidUUID(uuid string) bool {
const uuidPattern = `^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`
return regexp.MustCompile(uuidPattern).MatchString(strings.ToLower(uuid))
}
21 changes: 18 additions & 3 deletions exporter/sematextexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"path/filepath"
"testing"
"time"
"os"
"strings"

"github.com/cenkalti/backoff/v4"
"github.com/stretchr/testify/assert"
Expand All @@ -24,9 +26,22 @@ import (
func TestLoadConfig(t *testing.T) {
t.Parallel()

cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
content, err := os.ReadFile(filepath.Join("testdata", "config.yaml"))
require.NoError(t, err)

// Step 3: Replace placeholders with dynamically generated UUIDs
contentStr := strings.ReplaceAll(string(content), "<METRICS_APP_TOKEN>", metricsAppToken)
contentStr = strings.ReplaceAll(contentStr, "<LOGS_APP_TOKEN>", logsAppToken)

// Step 4: Write the updated content to a temporary file for testing
tmpConfigPath := filepath.Join("testdata", "config_tmp.yaml")
err = os.WriteFile(tmpConfigPath, []byte(contentStr), 0644) // Replace ioutil.WriteFile with os.WriteFile
require.NoError(t, err)
defer os.Remove(tmpConfigPath) // Clean up after the test

// Step 5: Load the modified configuration file
cm, err := confmaptest.LoadConf(tmpConfigPath)
require.NoError(t, err)
tests := []struct {
id component.ID
expected component.Config
Expand Down Expand Up @@ -132,7 +147,7 @@ func TestConfigValidation(t *testing.T) {
expectError: true,
},
{
name: "Invalid metrics AppToken length",
name: "Invalid metrics AppToken",
config: &Config{
Region: usRegion,
MetricsConfig: MetricsConfig{
Expand All @@ -145,7 +160,7 @@ func TestConfigValidation(t *testing.T) {
expectError: true,
},
{
name: "Invalid logs AppToken length",
name: "Invalid logs AppToken",
config: &Config{
Region: euRegion,
MetricsConfig: MetricsConfig{
Expand Down
9 changes: 5 additions & 4 deletions exporter/sematextexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package sematextexporter // import "github.com/open-telemetry/opentelemetry-coll
import (
"context"
"time"
"github.com/google/uuid"

"github.com/influxdata/influxdb-observability/common"
"go.opentelemetry.io/collector/component"
Expand All @@ -22,10 +23,10 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/sematextexporter/internal/metadata"
)

const (
metricsAppToken = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
logsAppToken = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
)

var metricsAppToken = uuid.NewString()
var logsAppToken = uuid.NewString()


// NewFactory creates a factory for the Sematext metrics exporter.
func NewFactory() exporter.Factory {
Expand Down
2 changes: 1 addition & 1 deletion exporter/sematextexporter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.22.0

require (
github.com/cenkalti/backoff/v4 v4.3.0
github.com/google/uuid v1.6.0
github.com/influxdata/influxdb-observability/common v0.5.12
github.com/stretchr/testify v1.9.0
go.opentelemetry.io/collector/component v0.112.0
Expand All @@ -26,7 +27,6 @@ require (
github.com/go-viper/mapstructure/v2 v2.2.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.11 // indirect
github.com/knadh/koanf/maps v0.1.1 // indirect
Expand Down
4 changes: 2 additions & 2 deletions exporter/sematextexporter/testdata/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ sematext/override-config:
max_interval: 3s
max_elapsed_time: 10s
metrics:
app_token: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
app_token: "<METRICS_APP_TOKEN>"
sending_queue:
enabled: true
num_consumers: 3
queue_size: 10
payload_max_lines: 72
payload_max_bytes: 27
logs:
app_token: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
app_token: "<LOGS_APP_TOKEN>"

0 comments on commit cdf7fa2

Please sign in to comment.