|
| 1 | +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 2 | +// or more contributor license agreements. Licensed under the Elastic License; |
| 3 | +// you may not use this file except in compliance with the Elastic License. |
| 4 | + |
| 5 | +//go:build !integration && requirefips |
| 6 | + |
| 7 | +package config |
| 8 | + |
| 9 | +import ( |
| 10 | + "crypto/tls" |
| 11 | + "net/http" |
| 12 | + "testing" |
| 13 | + "time" |
| 14 | + |
| 15 | + "github.com/google/go-cmp/cmp" |
| 16 | + "github.com/google/go-cmp/cmp/cmpopts" |
| 17 | + "github.com/stretchr/testify/assert" |
| 18 | + "github.com/stretchr/testify/require" |
| 19 | + |
| 20 | + "github.com/elastic/elastic-agent-libs/transport/tlscommon" |
| 21 | + testlog "github.com/elastic/fleet-server/v7/internal/pkg/testing/log" |
| 22 | + "github.com/elastic/go-elasticsearch/v8" |
| 23 | +) |
| 24 | + |
| 25 | +func TestToESConfigTLS(t *testing.T) { |
| 26 | + testcases := map[string]struct { |
| 27 | + cfg Elasticsearch |
| 28 | + result elasticsearch.Config |
| 29 | + }{ |
| 30 | + "https": { |
| 31 | + cfg: Elasticsearch{ |
| 32 | + Protocol: "https", |
| 33 | + Hosts: []string{"localhost:9200", "other-host:9200"}, |
| 34 | + ServiceToken: "test-token", |
| 35 | + Headers: map[string]string{ |
| 36 | + "X-Custom-Header": "Header-Value", |
| 37 | + }, |
| 38 | + MaxRetries: 6, |
| 39 | + MaxConnPerHost: 256, |
| 40 | + Timeout: 120 * time.Second, |
| 41 | + TLS: &tlscommon.Config{ |
| 42 | + VerificationMode: tlscommon.VerifyNone, |
| 43 | + }, |
| 44 | + }, |
| 45 | + result: elasticsearch.Config{ |
| 46 | + Addresses: []string{"https://localhost:9200", "https://other-host:9200"}, |
| 47 | + ServiceToken: "test-token", |
| 48 | + Header: http.Header{"X-Custom-Header": {"Header-Value"}}, |
| 49 | + MaxRetries: 6, |
| 50 | + Transport: &http.Transport{ |
| 51 | + TLSClientConfig: &tls.Config{ |
| 52 | + InsecureSkipVerify: true, //nolint:gosec // test case |
| 53 | + MinVersion: tls.VersionTLS12, |
| 54 | + MaxVersion: tls.VersionTLS13, |
| 55 | + Certificates: []tls.Certificate{}, |
| 56 | + CurvePreferences: []tls.CurveID{}, |
| 57 | + }, |
| 58 | + TLSHandshakeTimeout: 10 * time.Second, |
| 59 | + MaxIdleConns: 100, |
| 60 | + MaxIdleConnsPerHost: 32, |
| 61 | + MaxConnsPerHost: 256, |
| 62 | + IdleConnTimeout: 60 * time.Second, |
| 63 | + ResponseHeaderTimeout: 120 * time.Second, |
| 64 | + ExpectContinueTimeout: 1 * time.Second, |
| 65 | + }, |
| 66 | + }, |
| 67 | + }, |
| 68 | + "mixed-https": { |
| 69 | + cfg: Elasticsearch{ |
| 70 | + Protocol: "http", |
| 71 | + Hosts: []string{"localhost:9200", "https://other-host:9200"}, |
| 72 | + ServiceToken: "test-token", |
| 73 | + Headers: map[string]string{ |
| 74 | + "X-Custom-Header": "Header-Value", |
| 75 | + }, |
| 76 | + MaxRetries: 6, |
| 77 | + MaxConnPerHost: 256, |
| 78 | + Timeout: 120 * time.Second, |
| 79 | + TLS: &tlscommon.Config{ |
| 80 | + VerificationMode: tlscommon.VerifyNone, |
| 81 | + }, |
| 82 | + }, |
| 83 | + result: elasticsearch.Config{ |
| 84 | + Addresses: []string{"http://localhost:9200", "https://other-host:9200"}, |
| 85 | + ServiceToken: "test-token", |
| 86 | + Header: http.Header{"X-Custom-Header": {"Header-Value"}}, |
| 87 | + MaxRetries: 6, |
| 88 | + Transport: &http.Transport{ |
| 89 | + TLSClientConfig: &tls.Config{ |
| 90 | + InsecureSkipVerify: true, //nolint:gosec // test case |
| 91 | + MinVersion: tls.VersionTLS12, |
| 92 | + MaxVersion: tls.VersionTLS13, |
| 93 | + Certificates: []tls.Certificate{}, |
| 94 | + CurvePreferences: []tls.CurveID{}, |
| 95 | + }, |
| 96 | + TLSHandshakeTimeout: 10 * time.Second, |
| 97 | + MaxIdleConns: 100, |
| 98 | + MaxIdleConnsPerHost: 32, |
| 99 | + MaxConnsPerHost: 256, |
| 100 | + IdleConnTimeout: 60 * time.Second, |
| 101 | + ResponseHeaderTimeout: 120 * time.Second, |
| 102 | + ExpectContinueTimeout: 1 * time.Second, |
| 103 | + }, |
| 104 | + }, |
| 105 | + }, |
| 106 | + } |
| 107 | + |
| 108 | + copts := cmp.Options{ |
| 109 | + cmpopts.IgnoreUnexported(http.Transport{}), |
| 110 | + cmpopts.IgnoreFields(http.Transport{}, "DialContext"), |
| 111 | + cmpopts.IgnoreUnexported(tls.Config{}), //nolint:gosec //test case |
| 112 | + } |
| 113 | + |
| 114 | + for name, test := range testcases { |
| 115 | + t.Run(name, func(t *testing.T) { |
| 116 | + _ = testlog.SetLogger(t) |
| 117 | + res, err := test.cfg.ToESConfig(false) |
| 118 | + require.NoError(t, err) |
| 119 | + |
| 120 | + // cmp.Diff can't handle function pointers. |
| 121 | + res.Transport.(*http.Transport).Proxy = nil |
| 122 | + |
| 123 | + test.result.Header.Set("X-elastic-product-origin", "fleet") |
| 124 | + assert.True(t, cmp.Equal(test.result, res, copts...), "mismatch (-want +got)\n%s", cmp.Diff(test.result, res, copts...)) |
| 125 | + }) |
| 126 | + } |
| 127 | +} |
0 commit comments