Skip to content

Commit

Permalink
chore: rm exporter receiver from getTLSConfig function
Browse files Browse the repository at this point in the history
  • Loading branch information
lvlcn-t committed Apr 14, 2024
1 parent 77079d1 commit 1535e27
Showing 1 changed file with 50 additions and 51 deletions.
101 changes: 50 additions & 51 deletions pkg/sparrow/metrics/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,49 +69,6 @@ func (e Exporter) IsExporting() bool {
return e == HTTP || e == GRPC
}

// FileOpener is the function used to open a file
type FileOpener func(string) (fs.File, error)

// openFile is the function used to open a file
var openFile FileOpener = func() FileOpener {
return func(name string) (fs.File, error) {
return os.Open(name) // #nosec G304 // TODO: Can we hide this behind an interface instead?
}
}()

// TLSConfig returns the TLS configuration based on the certificate file
func (e Exporter) TLSConfig(certFile string) (conf *tls.Config, err error) {
if certFile == "" || certFile == "insecure" {
return nil, nil
}

file, err := openFile(certFile)
if err != nil {
return nil, fmt.Errorf("failed to open certificate file: %w", err)
}
defer func() {
if cErr := file.Close(); cErr != nil {
err = errors.Join(err, cErr)
}
}()

b, err := io.ReadAll(file)
if err != nil {
return nil, fmt.Errorf("failed to read certificate file: %w", err)
}

certPool := x509.NewCertPool()
if !certPool.AppendCertsFromPEM(b) {
return nil, fmt.Errorf("failed to append certificate from file: %s", certFile)
}

return &tls.Config{
RootCAs: certPool,
InsecureSkipVerify: false,
MinVersion: tls.VersionTLS12,
}, nil
}

// exporterFactory is a function that creates a new exporter
type exporterFactory func(ctx context.Context, config *Config) (sdktrace.SpanExporter, error)

Expand Down Expand Up @@ -172,27 +129,69 @@ func newGRPCExporter(ctx context.Context, config *Config) (sdktrace.SpanExporter
return otlptracegrpc.New(ctx, opts...)
}

// newStdoutExporter creates a new stdout exporter
func newStdoutExporter(_ context.Context, _ *Config) (sdktrace.SpanExporter, error) {
return stdouttrace.New(stdouttrace.WithPrettyPrint())
}

// newNoopExporter creates a new noop exporter
func newNoopExporter(_ context.Context, _ *Config) (sdktrace.SpanExporter, error) {
return nil, nil
}

// getCommonConfig returns the common configuration for the exporters
func getCommonConfig(config *Config) (map[string]string, *tls.Config, error) {
headers := make(map[string]string)
if config.Token != "" {
headers["Authorization"] = fmt.Sprintf("Bearer %s", config.Token)
}

tlsCfg, err := config.Exporter.TLSConfig(config.CertPath)
tlsCfg, err := getTLSConfig(config.CertPath)
if err != nil {
return nil, nil, fmt.Errorf("failed to create TLS configuration: %w", err)
}

return headers, tlsCfg, nil
}

// newStdoutExporter creates a new stdout exporter
func newStdoutExporter(_ context.Context, _ *Config) (sdktrace.SpanExporter, error) {
return stdouttrace.New(stdouttrace.WithPrettyPrint())
}
// FileOpener is the function used to open a file
type FileOpener func(string) (fs.File, error)

// newNoopExporter creates a new noop exporter
func newNoopExporter(_ context.Context, _ *Config) (sdktrace.SpanExporter, error) {
return nil, nil
// openFile is the function used to open a file
var openFile FileOpener = func() FileOpener {
return func(name string) (fs.File, error) {
return os.Open(name) // #nosec G304 // How else to open the file?
}
}()

func getTLSConfig(certFile string) (conf *tls.Config, err error) {
if certFile == "" || certFile == "insecure" {
return nil, nil
}

file, err := openFile(certFile)
if err != nil {
return nil, fmt.Errorf("failed to open certificate file: %w", err)
}
defer func() {
if cErr := file.Close(); cErr != nil {
err = errors.Join(err, cErr)
}
}()

b, err := io.ReadAll(file)
if err != nil {
return nil, fmt.Errorf("failed to read certificate file: %w", err)
}

pool := x509.NewCertPool()
if !pool.AppendCertsFromPEM(b) {
return nil, fmt.Errorf("failed to append certificate(s) from file: %s", certFile)
}

return &tls.Config{
RootCAs: pool,
InsecureSkipVerify: false,
MinVersion: tls.VersionTLS12,
}, nil
}

0 comments on commit 1535e27

Please sign in to comment.