Skip to content

Commit

Permalink
Refactor SecureReceiverSettings to use TLSSetting (open-telemetry#1015)
Browse files Browse the repository at this point in the history
* Refactor SecureReceiverSettings to use TLSSetting

* Address test code coverage failure

* Update file to use new license format

* Address few small comments
  • Loading branch information
ccaraman authored and wyTrivail committed Jul 13, 2020
1 parent f6400f6 commit 6cfe25c
Show file tree
Hide file tree
Showing 19 changed files with 250 additions and 363 deletions.
2 changes: 1 addition & 1 deletion config/configgrpc/configgrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func GrpcSettingsToDialOptions(settings GRPCClientSettings) ([]grpc.DialOption,
}
}

tlsDialOption, err := settings.TLSSetting.LoadGRPCTLSCredentials()
tlsDialOption, err := settings.TLSSetting.LoadgRPCTLSClientCredentials()
if err != nil {
return nil, err
}
Expand Down
11 changes: 10 additions & 1 deletion config/configtls/configtls.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (c TLSSetting) loadCert(caPath string) (*x509.CertPool, error) {
return certPool, nil
}

func (c TLSClientSetting) LoadGRPCTLSCredentials() (grpc.DialOption, error) {
func (c TLSClientSetting) LoadgRPCTLSClientCredentials() (grpc.DialOption, error) {
if c.Insecure && c.CAFile == "" {
return grpc.WithInsecure(), nil
}
Expand All @@ -122,3 +122,12 @@ func (c TLSClientSetting) LoadGRPCTLSCredentials() (grpc.DialOption, error) {
creds := credentials.NewTLS(tlsConf)
return grpc.WithTransportCredentials(creds), nil
}

func (c TLSSetting) LoadgRPCTLSServerCredentials() (grpc.ServerOption, error) {
tlsConf, err := c.LoadTLSConfig()
if err != nil {
return nil, fmt.Errorf("failed to load TLS config: %w", err)
}
creds := credentials.NewTLS(tlsConf)
return grpc.Creds(creds), nil
}
10 changes: 9 additions & 1 deletion config/configtls/configtls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ func TestOptionsToConfig(t *testing.T) {
tests := []struct {
name string
options TLSSetting
fakeSysPool bool
expectError string
}{
{
Expand Down Expand Up @@ -123,3 +122,12 @@ func TestOptionsToConfig(t *testing.T) {
})
}
}

func TestTLSSetting_LoadgRPCTLSServerCredentialsError(t *testing.T) {
tlsSetting := TLSSetting{
CertFile: "doesnt/exist",
KeyFile: "doesnt/exist",
}
_, err := tlsSetting.LoadgRPCTLSServerCredentials()
assert.Error(t, err)
}
22 changes: 22 additions & 0 deletions receiver/empty.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package receiver contains implementations of Receiver components.
//
// To implement a custom receiver you will need to implement component.ReceiverFactory
// interface and component.Receiver interface.
//
// To make the custom receiver part of the Collector build the factory must be added
// to defaultcomponents.Components() function.
package receiver
17 changes: 12 additions & 5 deletions receiver/jaegerreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package jaegerreceiver
import (
"go.opentelemetry.io/collector/config/configgrpc"
"go.opentelemetry.io/collector/config/configmodels"
"go.opentelemetry.io/collector/receiver"
"go.opentelemetry.io/collector/config/configtls"
)

// The config field name to load the protocol map from
Expand All @@ -30,12 +30,19 @@ type RemoteSamplingConfig struct {
configgrpc.GRPCClientSettings `mapstructure:",squash"`
}

type SecureSetting struct {
configmodels.ReceiverSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct
// Configures the receiver to use TLS.
// The default value is nil, which will cause the receiver to not use TLS.
TLSCredentials *configtls.TLSSetting `mapstructure:"tls_credentials, omitempty"`
}

// Config defines configuration for Jaeger receiver.
type Config struct {
TypeVal configmodels.Type `mapstructure:"-"`
NameVal string `mapstructure:"-"`
Protocols map[string]*receiver.SecureReceiverSettings `mapstructure:"protocols"`
RemoteSampling *RemoteSamplingConfig `mapstructure:"remote_sampling"`
TypeVal configmodels.Type `mapstructure:"-"`
NameVal string `mapstructure:"-"`
Protocols map[string]*SecureSetting `mapstructure:"protocols"`
RemoteSampling *RemoteSamplingConfig `mapstructure:"remote_sampling"`
}

// Name gets the receiver name.
Expand Down
12 changes: 6 additions & 6 deletions receiver/jaegerreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/configgrpc"
"go.opentelemetry.io/collector/config/configmodels"
"go.opentelemetry.io/collector/receiver"
"go.opentelemetry.io/collector/config/configtls"
)

func TestLoadConfig(t *testing.T) {
Expand All @@ -45,7 +45,7 @@ func TestLoadConfig(t *testing.T) {
&Config{
TypeVal: typeStr,
NameVal: "jaeger/customname",
Protocols: map[string]*receiver.SecureReceiverSettings{
Protocols: map[string]*SecureSetting{
"grpc": {
ReceiverSettings: configmodels.ReceiverSettings{
Endpoint: "localhost:9876",
Expand Down Expand Up @@ -81,7 +81,7 @@ func TestLoadConfig(t *testing.T) {
&Config{
TypeVal: typeStr,
NameVal: "jaeger/defaults",
Protocols: map[string]*receiver.SecureReceiverSettings{
Protocols: map[string]*SecureSetting{
"grpc": {
ReceiverSettings: configmodels.ReceiverSettings{
Endpoint: defaultGRPCBindEndpoint,
Expand Down Expand Up @@ -110,7 +110,7 @@ func TestLoadConfig(t *testing.T) {
&Config{
TypeVal: typeStr,
NameVal: "jaeger/mixed",
Protocols: map[string]*receiver.SecureReceiverSettings{
Protocols: map[string]*SecureSetting{
"grpc": {
ReceiverSettings: configmodels.ReceiverSettings{
Endpoint: "localhost:9876",
Expand All @@ -130,12 +130,12 @@ func TestLoadConfig(t *testing.T) {
&Config{
TypeVal: typeStr,
NameVal: "jaeger/tls",
Protocols: map[string]*receiver.SecureReceiverSettings{
Protocols: map[string]*SecureSetting{
"grpc": {
ReceiverSettings: configmodels.ReceiverSettings{
Endpoint: "localhost:9876",
},
TLSCredentials: &receiver.TLSCredentials{
TLSCredentials: &configtls.TLSSetting{
CertFile: "/test.crt",
KeyFile: "/test.key",
},
Expand Down
9 changes: 4 additions & 5 deletions receiver/jaegerreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"go.opentelemetry.io/collector/config/configerror"
"go.opentelemetry.io/collector/config/configmodels"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/receiver"
)

const (
Expand Down Expand Up @@ -104,7 +103,7 @@ func (f *Factory) CreateDefaultConfig() configmodels.Receiver {
return &Config{
TypeVal: typeStr,
NameVal: typeStr,
Protocols: map[string]*receiver.SecureReceiverSettings{},
Protocols: map[string]*SecureSetting{},
}
}

Expand Down Expand Up @@ -141,7 +140,7 @@ func (f *Factory) CreateTraceReceiver(
}

if protoGRPC.TLSCredentials != nil {
option, err := protoGRPC.TLSCredentials.ToGrpcServerOption()
option, err := protoGRPC.TLSCredentials.LoadgRPCTLSServerCredentials()
if err != nil {
return nil, fmt.Errorf("failed to configure TLS: %v", err)
}
Expand Down Expand Up @@ -248,7 +247,7 @@ func extractPortFromEndpoint(endpoint string) (int, error) {
}

// returns a default value for a protocol name. this really just boils down to the endpoint
func defaultsForProtocol(proto string) (*receiver.SecureReceiverSettings, error) {
func defaultsForProtocol(proto string) (*SecureSetting, error) {
var defaultEndpoint string

switch proto {
Expand All @@ -264,7 +263,7 @@ func defaultsForProtocol(proto string) (*receiver.SecureReceiverSettings, error)
return nil, fmt.Errorf("unknown Jaeger protocol %s", proto)
}

return &receiver.SecureReceiverSettings{
return &SecureSetting{
ReceiverSettings: configmodels.ReceiverSettings{
Endpoint: defaultEndpoint,
},
Expand Down
25 changes: 11 additions & 14 deletions receiver/jaegerreceiver/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"go.opentelemetry.io/collector/config/configerror"
"go.opentelemetry.io/collector/config/configgrpc"
"go.opentelemetry.io/collector/config/configmodels"
"go.opentelemetry.io/collector/receiver"
"go.opentelemetry.io/collector/config/configtls"
)

func TestTypeStr(t *testing.T) {
Expand Down Expand Up @@ -80,16 +80,13 @@ func TestCreateTLSGPRCEndpoint(t *testing.T) {
rCfg := cfg.(*Config)

rCfg.Protocols[protoGRPC], _ = defaultsForProtocol(protoGRPC)
rCfg.Protocols[protoGRPC].TLSCredentials = &receiver.TLSCredentials{}
params := component.ReceiverCreateParams{Logger: zap.NewNop()}
_, err := factory.CreateTraceReceiver(context.Background(), params, cfg, nil)
assert.Error(t, err, "tls-enabled receiver creation with no credentials must fail")

rCfg.Protocols[protoGRPC].TLSCredentials = &receiver.TLSCredentials{
rCfg.Protocols[protoGRPC].TLSCredentials = &configtls.TLSSetting{
CertFile: "./testdata/certificate.pem",
KeyFile: "./testdata/key.pem",
}
_, err = factory.CreateTraceReceiver(context.Background(), params, cfg, nil)
params := component.ReceiverCreateParams{Logger: zap.NewNop()}

_, err := factory.CreateTraceReceiver(context.Background(), params, cfg, nil)
assert.NoError(t, err, "tls-enabled receiver creation failed")
}

Expand Down Expand Up @@ -172,7 +169,7 @@ func TestCreateNoPort(t *testing.T) {
cfg := factory.CreateDefaultConfig()
rCfg := cfg.(*Config)

rCfg.Protocols[protoThriftHTTP] = &receiver.SecureReceiverSettings{
rCfg.Protocols[protoThriftHTTP] = &SecureSetting{
ReceiverSettings: configmodels.ReceiverSettings{
Endpoint: "localhost:",
},
Expand All @@ -187,7 +184,7 @@ func TestCreateLargePort(t *testing.T) {
cfg := factory.CreateDefaultConfig()
rCfg := cfg.(*Config)

rCfg.Protocols[protoThriftHTTP] = &receiver.SecureReceiverSettings{
rCfg.Protocols[protoThriftHTTP] = &SecureSetting{
ReceiverSettings: configmodels.ReceiverSettings{
Endpoint: "localhost:65536",
},
Expand All @@ -202,7 +199,7 @@ func TestCreateInvalidHost(t *testing.T) {
cfg := factory.CreateDefaultConfig()
rCfg := cfg.(*Config)

rCfg.Protocols[protoGRPC] = &receiver.SecureReceiverSettings{
rCfg.Protocols[protoGRPC] = &SecureSetting{
ReceiverSettings: configmodels.ReceiverSettings{
Endpoint: "1234",
},
Expand All @@ -217,7 +214,7 @@ func TestCreateNoProtocols(t *testing.T) {
cfg := factory.CreateDefaultConfig()
rCfg := cfg.(*Config)

rCfg.Protocols = make(map[string]*receiver.SecureReceiverSettings)
rCfg.Protocols = make(map[string]*SecureSetting)

params := component.ReceiverCreateParams{Logger: zap.NewNop()}
_, err := factory.CreateTraceReceiver(context.Background(), params, cfg, nil)
Expand All @@ -229,7 +226,7 @@ func TestThriftBinaryBadPort(t *testing.T) {
cfg := factory.CreateDefaultConfig()
rCfg := cfg.(*Config)

rCfg.Protocols[protoThriftBinary] = &receiver.SecureReceiverSettings{
rCfg.Protocols[protoThriftBinary] = &SecureSetting{
ReceiverSettings: configmodels.ReceiverSettings{
Endpoint: "localhost:65536",
},
Expand All @@ -245,7 +242,7 @@ func TestThriftCompactBadPort(t *testing.T) {
cfg := factory.CreateDefaultConfig()
rCfg := cfg.(*Config)

rCfg.Protocols[protoThriftCompact] = &receiver.SecureReceiverSettings{
rCfg.Protocols[protoThriftCompact] = &SecureSetting{
ReceiverSettings: configmodels.ReceiverSettings{
Endpoint: "localhost:65536",
},
Expand Down
7 changes: 3 additions & 4 deletions receiver/jaegerreceiver/trace_receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ import (
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/exporter/exportertest"
otlptrace "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/trace/v1"
"go.opentelemetry.io/collector/receiver"
"go.opentelemetry.io/collector/testutils"
"go.opentelemetry.io/collector/translator/conventions"
tracetranslator "go.opentelemetry.io/collector/translator/trace"
Expand Down Expand Up @@ -254,12 +253,12 @@ func TestGRPCReception(t *testing.T) {
func TestGRPCReceptionWithTLS(t *testing.T) {
// prepare
grpcServerOptions := []grpc.ServerOption{}
tlsCreds := receiver.TLSCredentials{
tlsCreds := configtls.TLSSetting{
CertFile: path.Join(".", "testdata", "certificate.pem"),
KeyFile: path.Join(".", "testdata", "key.pem"),
}

tlsOption, _ := tlsCreds.ToGrpcServerOption()
tlsOption, _ := tlsCreds.LoadgRPCTLSServerCredentials()

grpcServerOptions = append(grpcServerOptions, tlsOption)

Expand Down Expand Up @@ -591,7 +590,7 @@ func TestSamplingStrategiesMutualTLS(t *testing.T) {
// at least one protocol has to be enabled
thriftHTTPPort, err := randomAvailablePort()
require.NoError(t, err)
cfg.Protocols = map[string]*receiver.SecureReceiverSettings{
cfg.Protocols = map[string]*SecureSetting{
"thrift_http": {ReceiverSettings: configmodels.ReceiverSettings{
Endpoint: fmt.Sprintf("localhost:%d", thriftHTTPPort),
}},
Expand Down
40 changes: 17 additions & 23 deletions receiver/opencensusreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,17 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/keepalive"

"go.opentelemetry.io/collector/receiver"
"go.opentelemetry.io/collector/config/configmodels"
"go.opentelemetry.io/collector/config/configtls"
)

// Config defines configuration for OpenCensus receiver.
type Config struct {
receiver.SecureReceiverSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct
configmodels.ReceiverSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct

// Configures the receiver to use TLS.
// The default value is nil, which will cause the receiver to not use TLS.
TLSCredentials *configtls.TLSSetting `mapstructure:"tls_credentials, omitempty"`

// Transport to use: one of tcp or unix, defaults to tcp
Transport string `mapstructure:"transport"`
Expand Down Expand Up @@ -71,14 +76,16 @@ type keepaliveEnforcementPolicy struct {
PermitWithoutStream bool `mapstructure:"permit_without_stream,omitempty"`
}

func (rOpts *Config) buildOptions() (opts []Option, err error) {
tlsCredsOption, hasTLSCreds, err := ToOpenCensusReceiverServerOption(rOpts.TLSCredentials)
if err != nil {
return opts, fmt.Errorf("error initializing OpenCensus receiver %q TLS Credentials: %v", rOpts.NameVal, err)
}
if hasTLSCreds {
opts = append(opts, tlsCredsOption)
func (rOpts *Config) buildOptions() ([]Option, error) {
var opts []Option
if rOpts.TLSCredentials != nil {
tlsCredsOptions, err := rOpts.TLSCredentials.LoadgRPCTLSServerCredentials()
if err != nil {
return nil, fmt.Errorf("error initializing OpenCensus receiver %q TLS Credentials: %v", rOpts.NameVal, err)
}
opts = append(opts, WithGRPCServerOptions(tlsCredsOptions))
}

if len(rOpts.CorsOrigins) > 0 {
opts = append(opts, WithCorsOrigins(rOpts.CorsOrigins))
}
Expand All @@ -88,7 +95,7 @@ func (rOpts *Config) buildOptions() (opts []Option, err error) {
opts = append(opts, WithGRPCServerOptions(grpcServerOptions...))
}

return opts, err
return opts, nil
}

func (rOpts *Config) grpcServerOptions() []grpc.ServerOption {
Expand Down Expand Up @@ -129,16 +136,3 @@ func (rOpts *Config) grpcServerOptions() []grpc.ServerOption {

return grpcServerOptions
}

// ToOpenCensusReceiverServerOption checks if the TLS credentials
// in the form of a certificate file and a key file. If they aren't,
// it will return opencensusreceiver.WithNoopOption() and a nil error.
// Otherwise, it will try to retrieve gRPC transport credentials from the file combinations,
// and create a option, along with any errors encountered while retrieving the credentials.
func ToOpenCensusReceiverServerOption(tlsCreds *receiver.TLSCredentials) (opt Option, ok bool, err error) {
gRPCCredsOpt, err := tlsCreds.ToGrpcServerOption()
if err != nil {
return nil, false, err
}
return WithGRPCServerOptions(gRPCCredsOpt), true, nil
}
Loading

0 comments on commit 6cfe25c

Please sign in to comment.