Skip to content

Commit

Permalink
Update test data
Browse files Browse the repository at this point in the history
  • Loading branch information
CVanF5 committed Sep 19, 2024
1 parent b6f83fd commit ea941fa
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 24 deletions.
36 changes: 24 additions & 12 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,34 +407,46 @@ func resolveCollector(allowedDirs []string) (*Collector, error) {
// generate self-signed certificate for OTEL receiver
// nolint: revive
func handleSelfSignedCertificates(col *Collector) error {
sanNames := []string{"127.0.0.1", "::1", "localhost"}

if col.Receivers.OtlpReceivers != nil {
for _, receiver := range col.Receivers.OtlpReceivers {
if receiver.OtlpTLSConfig != nil && receiver.OtlpTLSConfig.GenerateSelfSignedCert {
if !slices.Contains(sanNames, receiver.Server.Host) {
sanNames = append(sanNames, receiver.Server.Host)
err := processOtlpReceivers(receiver.OtlpTLSConfig)
if err != nil {
return fmt.Errorf("failed to generate self-signed certificate: %w", err)
}

// Update viper's TLS paths with defaults
receiver.OtlpTLSConfig.Ca = DefCollectorTLSCAPath
receiver.OtlpTLSConfig.Cert = DefCollectorTLSCertPath
receiver.OtlpTLSConfig.Key = DefCollectorTLSKeyPath
}
}
}

return nil
}

func processOtlpReceivers(tlsConfig *OtlpTLSConfig) error {
sanNames := []string{"127.0.0.1", "::1", "localhost"}

if !slices.Contains(sanNames, tlsConfig.ServerName) {
sanNames = append(sanNames, tlsConfig.ServerName)
}
if len(sanNames) > 0 {
err := selfsignedcerts.GenerateServerCert(
sanNames,
DefCollectorTLSCAPath,
DefCollectorTLSCertPath,
DefCollectorTLSKeyPath,
tlsConfig.Ca,
tlsConfig.Cert,
tlsConfig.Key,
)
if err != nil {
return fmt.Errorf("failed to generate self-signed certificate: %w", err)
}
}
if tlsConfig.Ca == "" {
tlsConfig.Ca = DefCollectorTLSCAPath
}
if tlsConfig.Cert == "" {
tlsConfig.Cert = DefCollectorTLSCertPath
}
if tlsConfig.Key == "" {
tlsConfig.Key = DefCollectorTLSKeyPath
}

return nil
}
Expand Down
13 changes: 7 additions & 6 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,18 +367,19 @@ func getAgentConfig() *Config {
{
Server: &ServerConfig{
Host: "localhost",
Port: 4321,
Port: 4317,
Type: 0,
},
Auth: &AuthConfig{
Token: "even-secreter-token",
},
OtlpTLSConfig: &OtlpTLSConfig{
Cert: "/path/to/server-cert.pem",
Key: "/path/to/server-cert.pem",
Ca: "/path/to/server-cert.pem",
SkipVerify: true,
ServerName: "local-dataa-plane-server",
GenerateSelfSignedCert: false,
Cert: "/path/to/server-cert.pem",
Key: "/path/to/server-cert.pem",
Ca: "/path/to/server-cert.pem",
SkipVerify: true,
ServerName: "local-data-plane-server",
},
},
},
Expand Down
7 changes: 4 additions & 3 deletions internal/config/testdata/nginx-agent.conf
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ collector:
auth:
Token: "secret-receiver-token"
tls:
generate_self_signed_cert: false
server_name: "test-local-server"
ca: /path/to/server-cert.pem
cert: /var/lib/nginx-agent/cert.pem
key: /var/lib/nginx-agent/key.pem
ca: /tmp/ca.pem
cert: /tmp/cert.pem
key: /tmp/key.pem
nginx_receivers:
- instance_id: cd7b8911-c2c5-4daf-b311-dbead151d938
stub_status: "http://localhost:4321/status"
Expand Down
4 changes: 2 additions & 2 deletions pkg/tls/self_signed_cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ func GenerateServerCert(hostnames []string, caPath, certPath, keyPath string) er
}

// Get the local time zone
location_currentzone, locErr := time.LoadLocation("Local")
locationCurrentzone, locErr := time.LoadLocation("Local")
if locErr != nil {
return fmt.Errorf("error detecting local timezone: %w", locErr)
}
now := time.Now().In(location_currentzone)
now := time.Now().In(locationCurrentzone)

// Create CA first
caCert, caKeyPair, caErr := GenerateCA(now, caPath)
Expand Down
2 changes: 1 addition & 1 deletion pkg/tls/self_signed_cert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func TestGenerateSelfSignedCert(t *testing.T) {
certPath: certPath,
keyPath: keyPath,
hostNames: hostNames,
expectedError: "error decoding certificate PEM block",
expectedError: "error reading existing certificate data",
},
{
name: "Test case 7: Error reading existing key file",
Expand Down

0 comments on commit ea941fa

Please sign in to comment.