Skip to content

Commit b95dcb0

Browse files
committed
code changes to support ssl
1 parent a847b66 commit b95dcb0

File tree

14 files changed

+181
-2
lines changed

14 files changed

+181
-2
lines changed

internal/collector/nginxossreceiver/internal/config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type APIDetails struct {
3333
URL string `mapstructure:"url"`
3434
Listen string `mapstructure:"listen"`
3535
Location string `mapstructure:"location"`
36+
Ca string `mapstructure:"ca"`
3637
}
3738

3839
type AccessLog struct {
@@ -56,6 +57,7 @@ func CreateDefaultConfig() component.Config {
5657
URL: "http://localhost:80/status",
5758
Listen: "localhost:80",
5859
Location: "status",
60+
Ca: "",
5961
},
6062
}
6163
}

internal/collector/nginxossreceiver/internal/scraper/stubstatus/stub_status_scraper.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ package stubstatus
77

88
import (
99
"context"
10+
"crypto/tls"
11+
"crypto/x509"
1012
"net"
1113
"net/http"
14+
"os"
1215
"strings"
1316
"sync"
1417
"time"
@@ -63,6 +66,28 @@ func (s *NginxStubStatusScraper) ID() component.ID {
6366
func (s *NginxStubStatusScraper) Start(_ context.Context, _ component.Host) error {
6467
s.logger.Info("Starting NGINX stub status scraper")
6568
httpClient := http.DefaultClient
69+
caCertLocation := s.cfg.APIDetails.Ca
70+
if caCertLocation != "" {
71+
s.settings.Logger.Debug("Reading from Location for Ca Cert : ", zap.Any(caCertLocation, caCertLocation))
72+
caCert, err := os.ReadFile(caCertLocation)
73+
if err != nil {
74+
s.settings.Logger.Error("Error starting NGINX stub scraper. "+
75+
"Failed to read CA certificate : ", zap.Error(err))
76+
77+
return nil
78+
}
79+
caCertPool := x509.NewCertPool()
80+
caCertPool.AppendCertsFromPEM(caCert)
81+
82+
httpClient = &http.Client{
83+
Transport: &http.Transport{
84+
TLSClientConfig: &tls.Config{
85+
RootCAs: caCertPool,
86+
MinVersion: tls.VersionTLS13,
87+
},
88+
},
89+
}
90+
}
6691
httpClient.Timeout = s.cfg.ClientConfig.Timeout
6792

6893
if strings.HasPrefix(s.cfg.APIDetails.Listen, "unix:") {

internal/collector/nginxplusreceiver/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type APIDetails struct {
2929
URL string `mapstructure:"url"`
3030
Listen string `mapstructure:"listen"`
3131
Location string `mapstructure:"location"`
32+
Ca string `mapstructure:"ca"`
3233
}
3334

3435
// Validate checks if the receiver configuration is valid
@@ -59,6 +60,7 @@ func createDefaultConfig() component.Config {
5960
URL: "http://localhost:80/api",
6061
Listen: "localhost:80",
6162
Location: "/api",
63+
Ca: "",
6264
},
6365
MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
6466
}

internal/collector/nginxplusreceiver/scraper.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ package nginxplusreceiver
66

77
import (
88
"context"
9+
"crypto/tls"
10+
"crypto/x509"
911
"fmt"
1012
"net"
1113
"net/http"
14+
"os"
1215
"strconv"
1316
"strings"
1417
"sync"
@@ -82,6 +85,26 @@ func (nps *NginxPlusScraper) ID() component.ID {
8285
func (nps *NginxPlusScraper) Start(_ context.Context, _ component.Host) error {
8386
endpoint := strings.TrimPrefix(nps.cfg.APIDetails.URL, "unix:")
8487
httpClient := http.DefaultClient
88+
caCertLocation := nps.cfg.APIDetails.Ca
89+
if caCertLocation != "" {
90+
nps.logger.Debug("Reading from Location for Ca Cert : ", zap.Any(caCertLocation, caCertLocation))
91+
caCert, err := os.ReadFile(caCertLocation)
92+
if err != nil {
93+
nps.logger.Error("Unable to start NGINX Plus scraper. Failed to read CA certificate: %v", zap.Error(err))
94+
return err
95+
}
96+
caCertPool := x509.NewCertPool()
97+
caCertPool.AppendCertsFromPEM(caCert)
98+
99+
httpClient = &http.Client{
100+
Transport: &http.Transport{
101+
TLSClientConfig: &tls.Config{
102+
RootCAs: caCertPool,
103+
MinVersion: tls.VersionTLS13,
104+
},
105+
},
106+
}
107+
}
85108
httpClient.Timeout = nps.cfg.ClientConfig.Timeout
86109

87110
if strings.HasPrefix(nps.cfg.APIDetails.Listen, "unix:") {

internal/collector/otel_collector_plugin.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ func (oc *Collector) checkForNewReceivers(ctx context.Context, nginxConfigContex
418418
URL: nginxConfigContext.PlusAPI.URL,
419419
Listen: nginxConfigContext.PlusAPI.Listen,
420420
Location: nginxConfigContext.PlusAPI.Location,
421+
Ca: nginxConfigContext.PlusAPI.Ca,
421422
},
422423
CollectionInterval: defaultCollectionInterval,
423424
},

internal/collector/otelcol.tmpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ receivers:
8181
url: "{{- .StubStatus.URL -}}"
8282
listen: "{{- .StubStatus.Listen -}}"
8383
location: "{{- .StubStatus.Location -}}"
84+
ca: "{{- .StubStatus.Ca -}}"
8485
{{- if .CollectionInterval }}
8586
collection_interval: {{ .CollectionInterval }}
8687
{{- end }}
@@ -98,6 +99,7 @@ receivers:
9899
url: "{{- .PlusAPI.URL -}}"
99100
listen: "{{- .PlusAPI.Listen -}}"
100101
location: "{{- .PlusAPI.Location -}}"
102+
ca: "{{- .StubStatus.Ca -}}"
101103
{{- if .CollectionInterval }}
102104
collection_interval: {{ .CollectionInterval }}
103105
{{- end }}

internal/config/config.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,12 @@ func registerFlags() {
267267
"Warning messages in the NGINX errors logs after a NGINX reload will be treated as an error.",
268268
)
269269

270+
fs.String(
271+
NginxApiTlsCa,
272+
DefNginxApiTlsCa,
273+
"The NGINX Plus CA certificate file location needed to call the NGINX Plus API if SSL is enabled.",
274+
)
275+
270276
fs.StringSlice(
271277
NginxExcludeLogsKey, []string{},
272278
"A comma-separated list of one or more NGINX log paths that you want to exclude from metrics "+
@@ -786,6 +792,7 @@ func resolveDataPlaneConfig() *DataPlaneConfig {
786792
ReloadMonitoringPeriod: viperInstance.GetDuration(NginxReloadMonitoringPeriodKey),
787793
TreatWarningsAsErrors: viperInstance.GetBool(NginxTreatWarningsAsErrorsKey),
788794
ExcludeLogs: viperInstance.GetStringSlice(NginxExcludeLogsKey),
795+
ApiTls: TLSConfig{Ca: viperInstance.GetString(NginxApiTlsCa)},
789796
},
790797
}
791798
}

internal/config/defaults.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const (
1414
DefGracefulShutdownPeriod = 5 * time.Second
1515
DefNginxReloadMonitoringPeriod = 10 * time.Second
1616
DefTreatErrorsAsWarnings = false
17+
DefNginxApiTlsCa = ""
1718

1819
DefCommandServerHostKey = ""
1920
DefCommandServerPortKey = 0

internal/config/flags.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ var (
118118
NginxReloadMonitoringPeriodKey = pre(DataPlaneConfigRootKey, "nginx") + "reload_monitoring_period"
119119
NginxTreatWarningsAsErrorsKey = pre(DataPlaneConfigRootKey, "nginx") + "treat_warnings_as_errors"
120120
NginxExcludeLogsKey = pre(DataPlaneConfigRootKey, "nginx") + "exclude_logs"
121+
NginxApiTlsCa = pre(DataPlaneConfigRootKey, "nginx") + "api_tls_ca"
121122

122123
FileWatcherMonitoringFrequencyKey = pre(FileWatcherKey) + "monitoring_frequency"
123124
NginxExcludeFilesKey = pre(FileWatcherKey) + "exclude_files"

internal/config/types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ type (
6161
}
6262

6363
NginxDataPlaneConfig struct {
64+
ApiTls TLSConfig `yaml:"api_tls" mapstructure:"api_tls"`
6465
ExcludeLogs []string `yaml:"exclude_logs" mapstructure:"exclude_logs"`
6566
ReloadMonitoringPeriod time.Duration `yaml:"reload_monitoring_period" mapstructure:"reload_monitoring_period"`
6667
TreatWarningsAsErrors bool `yaml:"treat_warnings_as_errors" mapstructure:"treat_warnings_as_errors"`
@@ -230,6 +231,7 @@ type (
230231
URL string `yaml:"url" mapstructure:"url"`
231232
Listen string `yaml:"listen" mapstructure:"listen"`
232233
Location string `yaml:"location" mapstructure:"location"`
234+
Ca string `yaml:"ca" mapstructure:"ca"`
233235
}
234236

235237
AccessLog struct {

0 commit comments

Comments
 (0)