-
Notifications
You must be signed in to change notification settings - Fork 95
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
base: main
Are you sure you want to change the base?
Conversation
|
||
func TestStubStatusScraperTLS(t *testing.T) { | ||
// Create a test CA certificate and key | ||
ca := &x509.Certificate{ |
There was a problem hiding this comment.
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{ |
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
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{ |
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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
internal/config/types.go
Outdated
@@ -61,6 +61,7 @@ type ( | |||
} | |||
|
|||
NginxDataPlaneConfig struct { | |||
ApiTls TLSConfig `yaml:"api_tls" mapstructure:"api_tls"` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ApiTls TLSConfig `yaml:"api_tls" mapstructure:"api_tls"` | |
APITls TLSConfig `yaml:"api_tls" mapstructure:"api_tls"` |
api/grpc/mpi/v1/command.proto
Outdated
// the API Ca directive | ||
string Ca = 3; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// 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)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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)) |
s.settings.Logger.Error("Error starting NGINX stub scraper. "+ | ||
"Failed to read CA certificate : ", zap.Error(err)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.") |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
slog.DebugContext(ctx, "Reading from Location for Ca Cert : ", "cacertlocation", caCertLocation) | |
slog.DebugContext(ctx, "Reading CA certificate", zap.Any("file_path", caCertLocation) |
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.
CONTRIBUTING
documentmake install-tools
and have attached any dependency changes to this pull requestREADME.md
)