@@ -8,6 +8,8 @@ package server
8
8
9
9
import (
10
10
"context"
11
+ "crypto/tls"
12
+ "encoding/base64"
11
13
"encoding/json"
12
14
"fmt"
13
15
"io"
@@ -28,6 +30,7 @@ import (
28
30
29
31
const (
30
32
remoteESHost = "localhost:9201"
33
+ remoteESUrl = "https://localhost:9201"
31
34
)
32
35
33
36
func Checkin (t * testing.T , ctx context.Context , srv * tserver , agentID , key string , shouldHaveRemoteES bool , actionType string ) (string , string ) {
@@ -96,6 +99,13 @@ func Checkin(t *testing.T, ctx context.Context, srv *tserver, agentID, key strin
96
99
return remoteAPIKey , actionID
97
100
}
98
101
102
+ func getRemoteElasticsearchCa (t * testing.T ) string {
103
+ data , err := base64 .StdEncoding .DecodeString (strings .Replace (os .Getenv ("REMOTE_ELASTICSEARCH_CA_CRT_BASE64" ), " " , "" , - 1 ))
104
+ require .NoError (t , err )
105
+
106
+ return string (data )
107
+ }
108
+
99
109
func Ack (t * testing.T , ctx context.Context , srv * tserver , actionID , agentID , key string ) {
100
110
t .Logf ("Fake an ack for action %s for agent %s" , actionID , agentID )
101
111
body := fmt .Sprintf (`{
@@ -146,9 +156,11 @@ func Test_Agent_Remote_ES_Output(t *testing.T) {
146
156
"type" : "elasticsearch" ,
147
157
},
148
158
"remoteES" : {
149
- "type" : "remote_elasticsearch" ,
150
- "hosts" : []string {remoteESHost },
151
- "service_token" : os .Getenv ("REMOTE_ELASTICSEARCH_SERVICE_TOKEN" ),
159
+ "type" : "remote_elasticsearch" ,
160
+ "hosts" : []string {remoteESUrl },
161
+ "service_token" : os .Getenv ("REMOTE_ELASTICSEARCH_SERVICE_TOKEN" ),
162
+ "ssl.enabled" : true ,
163
+ "ssl.certificate_authorities" : []string {getRemoteElasticsearchCa (t )},
152
164
},
153
165
},
154
166
OutputPermissions : json .RawMessage (`{"default": {}, "remoteES": {}}` ),
@@ -255,13 +267,19 @@ func verifyRemoteAPIKey(t *testing.T, ctx context.Context, apiKeyID string, inva
255
267
// need to wait a bit before querying the api key
256
268
time .Sleep (time .Second )
257
269
258
- requestURL := fmt .Sprintf ("http ://elastic:changeme@%s/_security/api_key?id=%s" , remoteESHost , apiKeyID )
270
+ requestURL := fmt .Sprintf ("https ://elastic:changeme@%s/_security/api_key?id=%s" , remoteESHost , apiKeyID )
259
271
260
272
req , err := http .NewRequestWithContext (ctx , http .MethodGet , requestURL , nil )
273
+ // Skip SSL verify as ES use self-signed certificate
274
+ tr := & http.Transport {
275
+ // #nosec G402
276
+ TLSClientConfig : & tls.Config {InsecureSkipVerify : true },
277
+ }
278
+ client := & http.Client {Transport : tr }
261
279
if err != nil {
262
280
t .Fatal ("error creating request for remote api key" )
263
281
}
264
- res , err := http . DefaultClient .Do (req )
282
+ res , err := client .Do (req )
265
283
if err != nil {
266
284
t .Fatal ("error querying remote api key" )
267
285
}
@@ -292,9 +310,11 @@ func Test_Agent_Remote_ES_Output_ForceUnenroll(t *testing.T) {
292
310
"type" : "elasticsearch" ,
293
311
},
294
312
"remoteES" : {
295
- "type" : "remote_elasticsearch" ,
296
- "hosts" : []string {remoteESHost },
297
- "service_token" : os .Getenv ("REMOTE_ELASTICSEARCH_SERVICE_TOKEN" ),
313
+ "type" : "remote_elasticsearch" ,
314
+ "hosts" : []string {remoteESUrl },
315
+ "service_token" : os .Getenv ("REMOTE_ELASTICSEARCH_SERVICE_TOKEN" ),
316
+ "ssl.enabled" : true ,
317
+ "ssl.certificate_authorities" : []string {getRemoteElasticsearchCa (t )},
298
318
},
299
319
},
300
320
OutputPermissions : json .RawMessage (`{"default": {}, "remoteES": {}}` ),
@@ -411,9 +431,11 @@ func Test_Agent_Remote_ES_Output_Unenroll(t *testing.T) {
411
431
"type" : "elasticsearch" ,
412
432
},
413
433
"remoteES" : {
414
- "type" : "remote_elasticsearch" ,
415
- "hosts" : []string {remoteESHost },
416
- "service_token" : os .Getenv ("REMOTE_ELASTICSEARCH_SERVICE_TOKEN" ),
434
+ "type" : "remote_elasticsearch" ,
435
+ "hosts" : []string {remoteESUrl },
436
+ "service_token" : os .Getenv ("REMOTE_ELASTICSEARCH_SERVICE_TOKEN" ),
437
+ "ssl.enabled" : true ,
438
+ "ssl.certificate_authorities" : []string {getRemoteElasticsearchCa (t )},
417
439
},
418
440
},
419
441
OutputPermissions : json .RawMessage (`{"default": {}, "remoteES": {}}` ),
0 commit comments