Skip to content

Add support for ssl in v3 #1175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

Add support for ssl in v3 #1175

wants to merge 6 commits into from

Conversation

Akshay2191
Copy link

Proposed changes

Describe the use case and detail of the change. If this PR addresses an issue on GitHub, make sure to include a link to that issue using one of the supported keywords here in this description (not in the title of the PR).

Checklist

Before creating a PR, run through this checklist and mark each as complete.

  • I have read the CONTRIBUTING document
  • I have run make install-tools and have attached any dependency changes to this pull request
  • If applicable, I have added tests that prove my fix is effective or that my feature works
  • If applicable, I have checked that any relevant tests pass after adding my changes
  • If applicable, I have updated any relevant documentation (README.md)
  • If applicable, I have tested my cross-platform changes on Ubuntu 22, Redhat 8, SUSE 15 and FreeBSD 13

@Akshay2191 Akshay2191 requested a review from a team as a code owner July 17, 2025 16:00
@github-actions github-actions bot added chore Pull requests for routine tasks documentation Improvements or additions to documentation labels Jul 17, 2025

func TestStubStatusScraperTLS(t *testing.T) {
// Create a test CA certificate and key
ca := &x509.Certificate{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could the GenerateSelfSignedCert helper in test/helpers/certs_utils.go be used here

require.NoError(t, caBytesErr)

// Create a test server certificate signed by the CA
cert := &x509.Certificate{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

tempDir := t.TempDir()

// Save CA certificate to a file
caFile := filepath.Join(tempDir, "ca.crt")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can WriteCertFiles from test/helpers/certs_utils.go be used here

defer listener.Close()

// Start a simple HTTP server on the Unix socket
server := &http.Server{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can httptest be used here instead


func TestStubStatusScraperUnixSocket(t *testing.T) {
// Use a shorter path for the socket to avoid path length issues
socketPath := filepath.Join(os.TempDir(), "test-nginx.sock")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can t.TempDir be used here instead of os.TempDir()

}

// Create a test server with TLS
server := httptest.NewUnstartedServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can httptest.NewTLSServer be used here

@@ -61,6 +61,7 @@ type (
}

NginxDataPlaneConfig struct {
ApiTls TLSConfig `yaml:"api_tls" mapstructure:"api_tls"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ApiTls TLSConfig `yaml:"api_tls" mapstructure:"api_tls"`
APITls TLSConfig `yaml:"api_tls" mapstructure:"api_tls"`

Comment on lines 355 to 356
// the API Ca directive
string Ca = 3;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// the API Ca directive
string Ca = 3;
// the API CA file path
string ca = 3;

@@ -63,6 +66,28 @@ func (s *NginxStubStatusScraper) ID() component.ID {
func (s *NginxStubStatusScraper) Start(_ context.Context, _ component.Host) error {
s.logger.Info("Starting NGINX stub status scraper")
httpClient := http.DefaultClient
caCertLocation := s.cfg.APIDetails.Ca
if caCertLocation != "" {
s.settings.Logger.Debug("Reading from Location for Ca Cert : ", zap.Any(caCertLocation, caCertLocation))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
s.settings.Logger.Debug("Reading from Location for Ca Cert : ", zap.Any(caCertLocation, caCertLocation))
s.settings.Logger.Debug("Reading CA certificate", zap.Any("file_path", caCertLocation))

Comment on lines 74 to 75
s.settings.Logger.Error("Error starting NGINX stub scraper. "+
"Failed to read CA certificate : ", zap.Error(err))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
s.settings.Logger.Error("Error starting NGINX stub scraper. "+
"Failed to read CA certificate : ", zap.Error(err))
s.settings.Logger.Error("Error starting NGINX stub status scraper. "+
"Failed to read CA certificate", zap.Error(err))

@@ -82,6 +85,26 @@ func (nps *NginxPlusScraper) ID() component.ID {
func (nps *NginxPlusScraper) Start(_ context.Context, _ component.Host) error {
endpoint := strings.TrimPrefix(nps.cfg.APIDetails.URL, "unix:")
httpClient := http.DefaultClient
caCertLocation := nps.cfg.APIDetails.Ca
if caCertLocation != "" {
nps.logger.Debug("Reading from Location for Ca Cert : ", zap.Any(caCertLocation, caCertLocation))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
nps.logger.Debug("Reading from Location for Ca Cert : ", zap.Any(caCertLocation, caCertLocation))
nps.logger.Debug("Reading CA certificate", zap.Any("file_path", caCertLocation))

nps.logger.Debug("Reading from Location for Ca Cert : ", zap.Any(caCertLocation, caCertLocation))
caCert, err := os.ReadFile(caCertLocation)
if err != nil {
nps.logger.Error("Unable to start NGINX Plus scraper. Failed to read CA certificate: %v", zap.Error(err))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
nps.logger.Error("Unable to start NGINX Plus scraper. Failed to read CA certificate: %v", zap.Error(err))
nps.logger.Error("Error starting NGINX stub status scraper. "+
"Failed to read CA certificate", zap.Error(err))


if caCertLocation != "" && !ncp.agentConfig.IsDirectoryAllowed(caCertLocation) {
// If SSL is enabled but CA cert is provided and not allowed, treat it as if no CA cert
slog.Warn("CA certificate location is not allowed, treating as if no CA cert provided.")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use WarnContext function instead?

slog.Debug("Reading from Location for Ca Cert : ", "cacertlocation", caCertLocation)
caCert, err := os.ReadFile(caCertLocation)
if err != nil {
slog.Error("Unable to Create NGINX Plus client. Failed to read CA certificate : ", "err", err)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to log error since you are returning the error anyways

slog.DebugContext(ctx, "Reading from Location for Ca Cert : ", "cacertlocation", caCertLocation)
caCert, err := os.ReadFile(caCertLocation)
if err != nil {
slog.ErrorContext(ctx, "Failed to read CA certificate", "error", err)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to log error since you are returning the error anyways

@@ -348,6 +351,26 @@ func (r *ResourceService) createPlusClient(instance *mpi.Instance) (*client.Ngin
}

httpClient := http.DefaultClient
caCertLocation := plusAPI.GetCa()
if caCertLocation != "" {
slog.Debug("Reading from Location for Ca Cert : ", "cacertlocation", caCertLocation)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
slog.Debug("Reading from Location for Ca Cert : ", "cacertlocation", caCertLocation)
slog.Debug("Reading CA certificate", zap.Any("file_path", caCertLocation)

caCertLocation := ncp.agentConfig.DataPlaneConfig.Nginx.ApiTls.Ca

if caCertLocation != "" && ncp.agentConfig.IsDirectoryAllowed(caCertLocation) {
slog.DebugContext(ctx, "Reading from Location for Ca Cert : ", "cacertlocation", caCertLocation)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
slog.DebugContext(ctx, "Reading from Location for Ca Cert : ", "cacertlocation", caCertLocation)
slog.DebugContext(ctx, "Reading CA certificate", zap.Any("file_path", caCertLocation)

@Akshay2191 Akshay2191 requested review from dhurley and aphralG July 21, 2025 14:09
@oCHRISo oCHRISo added the v3.x Issues and Pull Requests related to the major version v3 label Jul 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
chore Pull requests for routine tasks documentation Improvements or additions to documentation v3.x Issues and Pull Requests related to the major version v3
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants