@@ -18,10 +18,12 @@ package e2e
18
18
19
19
import (
20
20
"context"
21
+ "errors"
21
22
"log"
22
23
"path/filepath"
23
24
"time"
24
25
26
+ "github.com/mdelapenya/tlscert"
25
27
"github.com/testcontainers/testcontainers-go"
26
28
"github.com/testcontainers/testcontainers-go/wait"
27
29
)
@@ -37,50 +39,91 @@ const (
37
39
38
40
func runReceivingSolarWindsOTELCollector (
39
41
ctx context.Context ,
42
+ certDir string ,
40
43
networkName string ,
41
44
) (testcontainers.Container , error ) {
42
45
configPath , err := filepath .Abs (filepath .Join ("." , "testdata" , "receiving_collector.yaml" ))
43
46
if err != nil {
44
47
return nil , err
45
48
}
46
49
47
- container , err := runSolarWindsOTELCollector (ctx , networkName , receivingContainer , configPath )
48
- return container , err
50
+ // Used by the OTLP/gRPC Receiver for TLS (see its config).
51
+ additionalFiles := []testcontainers.ContainerFile {
52
+ {
53
+ HostFilePath : filepath .Join (certDir , "cert-server.pem" ),
54
+ ContainerFilePath : "/opt/cert-server.pem" ,
55
+ FileMode : 0o644 ,
56
+ },
57
+ {
58
+ HostFilePath : filepath .Join (certDir , "key-server.pem" ),
59
+ ContainerFilePath : "/opt/key-server.pem" ,
60
+ FileMode : 0o644 ,
61
+ },
62
+ }
63
+
64
+ return runSolarWindsOTELCollector (
65
+ ctx ,
66
+ networkName ,
67
+ receivingContainer ,
68
+ configPath ,
69
+ additionalFiles ,
70
+ )
49
71
}
50
72
51
73
func runTestedSolarWindsOTELCollector (
52
74
ctx context.Context ,
75
+ certDir string ,
53
76
networkName string ,
54
77
) (testcontainers.Container , error ) {
55
78
configPath , err := filepath .Abs (filepath .Join ("." , "testdata" , "emitting_collector.yaml" ))
56
79
if err != nil {
57
80
return nil , err
58
81
}
59
82
60
- container , err := runSolarWindsOTELCollector (ctx , networkName , testedContainer , configPath )
61
- return container , err
83
+ // Add the root certificate for the self-signed certs as trusted.
84
+ // Warning: This actually replaces all root certificates in the container.
85
+ additionalFiles := []testcontainers.ContainerFile {
86
+ {
87
+ HostFilePath : filepath .Join (certDir , "cert-ca.pem" ),
88
+ ContainerFilePath : "/etc/ssl/certs/ca-certificates.crt" ,
89
+ FileMode : 0o644 ,
90
+ },
91
+ }
92
+
93
+ return runSolarWindsOTELCollector (
94
+ ctx ,
95
+ networkName ,
96
+ testedContainer ,
97
+ configPath ,
98
+ additionalFiles ,
99
+ )
62
100
}
63
101
64
102
func runSolarWindsOTELCollector (
65
103
ctx context.Context ,
66
104
networkName string ,
67
105
containerName string ,
68
106
configPath string ,
107
+ additionalFiles []testcontainers.ContainerFile ,
69
108
) (testcontainers.Container , error ) {
70
109
lc := new (logConsumer )
71
110
lc .Prefix = containerName
111
+
112
+ files := []testcontainers.ContainerFile {
113
+ {
114
+ HostFilePath : configPath ,
115
+ ContainerFilePath : "/opt/default-config.yaml" ,
116
+ FileMode : 0o440 ,
117
+ },
118
+ }
119
+ files = append (files , additionalFiles ... )
120
+
72
121
req := testcontainers.ContainerRequest {
73
122
Image : "solarwinds-otel-collector:latest" ,
74
123
LogConsumerCfg : & testcontainers.LogConsumerConfig {
75
124
Consumers : []testcontainers.LogConsumer {lc },
76
125
},
77
- Files : []testcontainers.ContainerFile {
78
- {
79
- HostFilePath : configPath ,
80
- ContainerFilePath : "/opt/default-config.yaml" ,
81
- FileMode : 0o440 ,
82
- },
83
- },
126
+ Files : files ,
84
127
WaitingFor : wait .ForLog ("Everything is ready. Begin running and processing data." ),
85
128
Networks : []string {networkName },
86
129
Name : containerName ,
@@ -94,6 +137,45 @@ func runSolarWindsOTELCollector(
94
137
return container , err
95
138
}
96
139
140
+ type CertPaths struct {
141
+ CaCertFile string
142
+ CertFile string
143
+ KeyFile string
144
+ }
145
+
146
+ // generateCertificates generates a new CA certificate and a server
147
+ // key and certificate derived from it for a given `host`.
148
+ // All files are stored in a `path`. All paths of files written are
149
+ // returned in a CertPaths struct.
150
+ func generateCertificates (host , path string ) (* CertPaths , error ) {
151
+ caCert := tlscert .SelfSignedFromRequest (tlscert.Request {
152
+ Name : "ca" ,
153
+ Host : host ,
154
+ IsCA : true ,
155
+ ParentDir : path ,
156
+ })
157
+ if caCert == nil {
158
+ return nil , errors .New ("failed to generate ca certificate" )
159
+ }
160
+
161
+ cert := tlscert .SelfSignedFromRequest (tlscert.Request {
162
+ Name : "server" ,
163
+ Host : host ,
164
+ IsCA : true ,
165
+ Parent : caCert ,
166
+ ParentDir : path ,
167
+ })
168
+ if cert == nil {
169
+ return nil , errors .New ("failed to generate server certificate" )
170
+ }
171
+
172
+ return & CertPaths {
173
+ CaCertFile : caCert .CertPath ,
174
+ CertFile : cert .CertPath ,
175
+ KeyFile : cert .KeyPath ,
176
+ }, nil
177
+ }
178
+
97
179
func runGeneratorContainer (
98
180
ctx context.Context ,
99
181
networkName string ,
0 commit comments