-
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
Open
Akshay2191
wants to merge
6
commits into
main
Choose a base branch
from
add-ssl-support-v3
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+390
−12
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a847b66
adding changes to proto
Akshay2191 b95dcb0
code changes to support ssl
Akshay2191 06e701d
fixing otel collector config
Akshay2191 961954a
added unit test to increase test coverage
Akshay2191 bcdcd99
fixing lint errors
Akshay2191 6c2f5ca
Changes as per PR review comments
Akshay2191 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
...al/collector/nginxossreceiver/internal/scraper/stubstatus/stub_status_scraper_tls_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
// Copyright (c) F5, Inc. | ||
// | ||
// This source code is licensed under the Apache License, Version 2.0 license found in the | ||
// LICENSE file in the root directory of this source tree. | ||
|
||
package stubstatus | ||
|
||
import ( | ||
"context" | ||
"crypto/tls" | ||
"crypto/x509" | ||
"net" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.opentelemetry.io/collector/component" | ||
"go.opentelemetry.io/collector/component/componenttest" | ||
"go.opentelemetry.io/collector/receiver/receivertest" | ||
|
||
"github.com/nginx/agent/v3/internal/collector/nginxossreceiver/internal/config" | ||
"github.com/nginx/agent/v3/test/helpers" | ||
) | ||
|
||
func TestStubStatusScraperTLS(t *testing.T) { | ||
// Generate self-signed certificate using helper | ||
keyBytes, certBytes := helpers.GenerateSelfSignedCert(t) | ||
|
||
// Create a temporary directory for test files | ||
tempDir := t.TempDir() | ||
|
||
// Save certificate to a file | ||
certFile := helpers.WriteCertFiles(t, tempDir, helpers.Cert{ | ||
Name: "server.crt", | ||
Type: "CERTIFICATE", | ||
Contents: certBytes, | ||
}) | ||
|
||
// Parse the private key | ||
key, err := x509.ParsePKCS1PrivateKey(keyBytes) | ||
require.NoError(t, err) | ||
|
||
// Create a TLS config with our self-signed certificate | ||
tlsCert := tls.Certificate{ | ||
Certificate: [][]byte{certBytes}, | ||
PrivateKey: key, | ||
} | ||
|
||
serverTLSConfig := &tls.Config{ | ||
MinVersion: tls.VersionTLS13, | ||
Certificates: []tls.Certificate{tlsCert}, | ||
} | ||
|
||
// Create a test server with our custom TLS config | ||
server := httptest.NewUnstartedServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { | ||
if req.URL.Path == "/status" { | ||
rw.WriteHeader(http.StatusOK) | ||
_, _ = rw.Write([]byte(`Active connections: 291 | ||
server accepts handled requests | ||
16630948 16630946 31070465 | ||
Reading: 6 Writing: 179 Waiting: 106 | ||
`)) | ||
|
||
return | ||
} | ||
rw.WriteHeader(http.StatusNotFound) | ||
})) | ||
|
||
server.TLS = serverTLSConfig | ||
server.StartTLS() | ||
defer server.Close() | ||
|
||
// Test with TLS configuration using our self-signed certificate | ||
t.Run("with self-signed TLS", func(t *testing.T) { | ||
cfg, ok := config.CreateDefaultConfig().(*config.Config) | ||
require.True(t, ok) | ||
|
||
cfg.APIDetails.URL = server.URL + "/status" | ||
// Use the self-signed certificate for verification | ||
cfg.APIDetails.Ca = certFile | ||
|
||
scraper := NewScraper(receivertest.NewNopSettings(component.Type{}), cfg) | ||
|
||
startErr := scraper.Start(context.Background(), componenttest.NewNopHost()) | ||
require.NoError(t, startErr) | ||
|
||
_, err = scraper.Scrape(context.Background()) | ||
assert.NoError(t, err, "Scraping with self-signed certificate should succeed") | ||
}) | ||
} | ||
|
||
func TestStubStatusScraperUnixSocket(t *testing.T) { | ||
// Create a test server with a Unix domain socket | ||
handler := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { | ||
if req.URL.Path == "/status" { | ||
rw.WriteHeader(http.StatusOK) | ||
_, _ = rw.Write([]byte(`Active connections: 291 | ||
server accepts handled requests | ||
16630948 16630946 31070465 | ||
Reading: 6 Writing: 179 Waiting: 106 | ||
`)) | ||
|
||
return | ||
} | ||
rw.WriteHeader(http.StatusNotFound) | ||
}) | ||
|
||
// Create a socket file in a temporary directory with a shorter path | ||
socketPath := "/tmp/nginx-test.sock" | ||
|
||
// Clean up any existing socket file | ||
os.Remove(socketPath) | ||
|
||
// Create a listener for the Unix socket | ||
listener, err := net.Listen("unix", socketPath) | ||
require.NoError(t, err, "Failed to create Unix socket listener") | ||
|
||
// Create a test server with our custom listener | ||
server := &httptest.Server{ | ||
Listener: listener, | ||
Config: &http.Server{Handler: handler}, | ||
} | ||
|
||
// Start the server | ||
server.Start() | ||
|
||
// Ensure cleanup of the socket file | ||
t.Cleanup(func() { | ||
server.Close() | ||
os.Remove(socketPath) | ||
}) | ||
|
||
// Test with Unix socket | ||
t.Run("with Unix socket", func(t *testing.T) { | ||
cfg, ok := config.CreateDefaultConfig().(*config.Config) | ||
require.True(t, ok) | ||
|
||
cfg.APIDetails.URL = "http://unix/status" | ||
cfg.APIDetails.Listen = "unix:" + socketPath | ||
|
||
scraper := NewScraper(receivertest.NewNopSettings(component.Type{}), cfg) | ||
|
||
startErr := scraper.Start(context.Background(), componenttest.NewNopHost()) | ||
require.NoError(t, startErr) | ||
|
||
_, err = scraper.Scrape(context.Background()) | ||
assert.NoError(t, err) | ||
}) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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