@@ -23,6 +23,8 @@ import (
2323
2424 "github.com/spf13/viper"
2525 "go.uber.org/zap"
26+ "google.golang.org/grpc"
27+ "google.golang.org/grpc/credentials"
2628 yaml "gopkg.in/yaml.v2"
2729
2830 "github.com/census-instrumentation/opencensus-service/exporter"
@@ -35,6 +37,7 @@ import (
3537 "github.com/census-instrumentation/opencensus-service/exporter/prometheusexporter"
3638 "github.com/census-instrumentation/opencensus-service/exporter/stackdriverexporter"
3739 "github.com/census-instrumentation/opencensus-service/exporter/zipkinexporter"
40+ "github.com/census-instrumentation/opencensus-service/receiver/opencensusreceiver"
3841 "github.com/census-instrumentation/opencensus-service/receiver/prometheusreceiver"
3942)
4043
@@ -131,6 +134,9 @@ type ReceiverConfig struct {
131134 DisableTracing bool `yaml:"disable_tracing"`
132135 // DisableMetrics disables metrics receiving and is only applicable to metrics receivers.
133136 DisableMetrics bool `yaml:"disable_metrics"`
137+
138+ // TLSCredentials is a (cert_file, key_file) configuration.
139+ TLSCredentials * TLSCredentials `yaml:"tls_credentials"`
134140}
135141
136142// ScribeReceiverConfig carries the settings for the Zipkin Scribe receiver.
@@ -191,15 +197,21 @@ func (c *Config) OpenCensusReceiverCorsAllowedOrigins() []string {
191197// CanRunOpenCensusTraceReceiver returns true if the configuration
192198// permits running the OpenCensus Trace receiver.
193199func (c * Config ) CanRunOpenCensusTraceReceiver () bool {
194- return c != nil && c .Receivers != nil &&
195- c .Receivers .OpenCensus != nil && ! c .Receivers .OpenCensus .DisableTracing
200+ return c .openCensusReceiverEnabled () && ! c .Receivers .OpenCensus .DisableTracing
196201}
197202
198203// CanRunOpenCensusMetricsReceiver returns true if the configuration
199204// permits running the OpenCensus Metrics receiver.
200205func (c * Config ) CanRunOpenCensusMetricsReceiver () bool {
206+ return c .openCensusReceiverEnabled () && ! c .Receivers .OpenCensus .DisableMetrics
207+ }
208+
209+ // openCensusReceiverEnabled returns true if both:
210+ // Config.Receivers and Config.Receivers.OpenCensus
211+ // are non-nil.
212+ func (c * Config ) openCensusReceiverEnabled () bool {
201213 return c != nil && c .Receivers != nil &&
202- c .Receivers .OpenCensus != nil && ! c . Receivers . OpenCensus . DisableMetrics
214+ c .Receivers .OpenCensus != nil
203215}
204216
205217// ZPagesDisabled returns true if zPages have not been enabled.
@@ -316,6 +328,53 @@ func (c *Config) JaegerReceiverPorts() (collectorPort, thriftPort int) {
316328 return jc .CollectorHTTPPort , jc .CollectorThriftPort
317329}
318330
331+ // HasTLSCredentials returns true if TLSCredentials is non-nil
332+ func (rCfg * ReceiverConfig ) HasTLSCredentials () bool {
333+ return rCfg != nil && rCfg .TLSCredentials != nil && rCfg .TLSCredentials .nonEmpty ()
334+ }
335+
336+ // OpenCensusReceiverTLSServerCredentials retrieves the TLS credentials
337+ // from this Config's OpenCensus receiver if any.
338+ func (c * Config ) OpenCensusReceiverTLSServerCredentials () * TLSCredentials {
339+ if ! c .openCensusReceiverEnabled () {
340+ return nil
341+ }
342+
343+ ocrConfig := c .Receivers .OpenCensus
344+ if ! ocrConfig .HasTLSCredentials () {
345+ return nil
346+ }
347+ return ocrConfig .TLSCredentials
348+ }
349+
350+ // ToOpenCensusReceiverServerOption checks if the TLS credentials
351+ // in the form of a certificate file and a key file. If they aren't,
352+ // it will return opencensusreceiver.WithNoopOption() and a nil error.
353+ // Otherwise, it will try to retrieve gRPC transport credentials from the file combinations,
354+ // and create a option, along with any errors encountered while retrieving the credentials.
355+ func (tlsCreds * TLSCredentials ) ToOpenCensusReceiverServerOption () (opt opencensusreceiver.Option , ok bool , err error ) {
356+ if tlsCreds == nil {
357+ return opencensusreceiver .WithNoopOption (), false , nil
358+ }
359+
360+ transportCreds , err := credentials .NewServerTLSFromFile (tlsCreds .CertFile , tlsCreds .KeyFile )
361+ if err != nil {
362+ return nil , false , err
363+ }
364+ gRPCCredsOpt := grpc .Creds (transportCreds )
365+ return opencensusreceiver .WithGRPCServerOptions (gRPCCredsOpt ), true , nil
366+ }
367+
368+ // OpenCensusReceiverTLSCredentialsServerOption checks if the OpenCensus receiver's Configuration
369+ // has TLS credentials in the form of a certificate file and a key file. If it doesn't
370+ // have any, it will return opencensusreceiver.WithNoopOption() and a nil error.
371+ // Otherwise, it will try to retrieve gRPC transport credentials from the file combinations,
372+ // and create a option, along with any errors encountered while retrieving the credentials.
373+ func (c * Config ) OpenCensusReceiverTLSCredentialsServerOption () (opt opencensusreceiver.Option , ok bool , err error ) {
374+ tlsCreds := c .OpenCensusReceiverTLSServerCredentials ()
375+ return tlsCreds .ToOpenCensusReceiverServerOption ()
376+ }
377+
319378// ParseOCAgentConfig unmarshals byte content in the YAML file format
320379// to retrieve the configuration that will be used to run the OpenCensus agent.
321380func ParseOCAgentConfig (yamlBlob []byte ) (* Config , error ) {
0 commit comments