|
| 1 | +// Licensed to Elasticsearch B.V. under one or more contributor |
| 2 | +// license agreements. See the NOTICE file distributed with |
| 3 | +// this work for additional information regarding copyright |
| 4 | +// ownership. Elasticsearch B.V. licenses this file to you under |
| 5 | +// the Apache License, Version 2.0 (the "License"); you may |
| 6 | +// not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +//go:build integration |
| 19 | + |
| 20 | +package integration |
| 21 | + |
| 22 | +import ( |
| 23 | + "fmt" |
| 24 | + "path/filepath" |
| 25 | + "testing" |
| 26 | + "time" |
| 27 | + |
| 28 | + "github.com/stretchr/testify/require" |
| 29 | +) |
| 30 | + |
| 31 | +func addKeystoreSecret(t *testing.T, mockbeat *BeatProc, key, value string) { |
| 32 | + t.Helper() |
| 33 | + mockbeat.Start("keystore", "add", key, "--stdin") |
| 34 | + fmt.Fprint(mockbeat.stdin, value) |
| 35 | + require.NoError(t, mockbeat.stdin.Close(), "could not close mockbeat stdin") |
| 36 | + err := mockbeat.Cmd.Wait() |
| 37 | + require.NoError(t, err) |
| 38 | + require.Equal(t, 0, mockbeat.Cmd.ProcessState.ExitCode(), "incorrect exit code") |
| 39 | +} |
| 40 | + |
| 41 | +func TestKeystoreWithPresentKey(t *testing.T) { |
| 42 | + mockbeat := NewBeat(t, "mockbeat", "../../libbeat.test") |
| 43 | + keystorePath := filepath.Join(mockbeat.TempDir(), "test.keystore") |
| 44 | + |
| 45 | + key := "mysecretpath" |
| 46 | + // Include a comma in the path, regression test for https://github.com/elastic/beats/issues/29789 |
| 47 | + secret := filepath.Join(mockbeat.TempDir(), "thisisultra,secretpath") |
| 48 | + |
| 49 | + keystoreCfg := fmt.Sprintf(` |
| 50 | +mockbeat: |
| 51 | +name: |
| 52 | +queue.mem: |
| 53 | + events: 32 |
| 54 | + flush.min_events: 8 |
| 55 | + flush.timeout: 0.1s |
| 56 | +logging: |
| 57 | + level: debug |
| 58 | +output.file: |
| 59 | + path: "${%s}" |
| 60 | + filename: "mockbeat" |
| 61 | + rotate_every_kb: 1000 |
| 62 | +keystore: |
| 63 | + path: %s |
| 64 | +`, key, keystorePath) |
| 65 | + |
| 66 | + mockbeat.WriteConfigFile(keystoreCfg) |
| 67 | + |
| 68 | + mockbeat.Start("keystore", "create") |
| 69 | + mockbeat.WaitStdOutContains("Created mockbeat keystore", 10*time.Second) |
| 70 | + mockbeat.Stop() |
| 71 | + |
| 72 | + addKeystoreSecret(t, mockbeat, key, secret) |
| 73 | + |
| 74 | + mockbeat.Start() |
| 75 | + mockbeat.WaitLogsContains("ackloop: done send ack", 60*time.Second) |
| 76 | + mockbeat.Stop() |
| 77 | + |
| 78 | + require.DirExists(t, secret) |
| 79 | +} |
| 80 | + |
| 81 | +func TestKeystoreWithKeyNotPresent(t *testing.T) { |
| 82 | + mockbeat := NewBeat(t, "mockbeat", "../../libbeat.test") |
| 83 | + keystorePath := filepath.Join(mockbeat.TempDir(), "test.keystore") |
| 84 | + |
| 85 | + keystoreCfg := fmt.Sprintf(` |
| 86 | +mockbeat: |
| 87 | +name: |
| 88 | +queue.mem: |
| 89 | + events: 32 |
| 90 | + flush.min_events: 8 |
| 91 | + flush.timeout: 0.1s |
| 92 | +logging: |
| 93 | + level: debug |
| 94 | +output.elasticsearch: |
| 95 | + hosts: ["${elasticsearch_host}:9200"] |
| 96 | +keystore: |
| 97 | + path: %s |
| 98 | +`, keystorePath) |
| 99 | + |
| 100 | + mockbeat.WriteConfigFile(keystoreCfg) |
| 101 | + |
| 102 | + mockbeat.Start() |
| 103 | + err := mockbeat.Cmd.Wait() |
| 104 | + require.Error(t, err, "mockbeat must exit with an error") |
| 105 | + require.Equal(t, 1, mockbeat.Cmd.ProcessState.ExitCode(), "incorrect exit code") |
| 106 | + mockbeat.WaitStdErrContains("missing field", 10*time.Second) |
| 107 | +} |
| 108 | + |
| 109 | +func TestKeystoreWithNestedKey(t *testing.T) { |
| 110 | + mockbeat := NewBeat(t, "mockbeat", "../../libbeat.test") |
| 111 | + keystorePath := filepath.Join(mockbeat.TempDir(), "test.keystore") |
| 112 | + |
| 113 | + key := "output.elasticsearch.hosts.0" |
| 114 | + secret := filepath.Join(mockbeat.TempDir(), "myelasticsearchsecrethost") |
| 115 | + |
| 116 | + keystoreCfg := fmt.Sprintf(` |
| 117 | +mockbeat: |
| 118 | +name: |
| 119 | +logging: |
| 120 | + level: debug |
| 121 | +queue.mem: |
| 122 | + events: 32 |
| 123 | + flush.min_events: 8 |
| 124 | + flush.timeout: 0.1s |
| 125 | +output.file: |
| 126 | + path: "${%s}" |
| 127 | + filename: "mockbeat" |
| 128 | + rotate_every_kb: 1000 |
| 129 | +keystore: |
| 130 | + path: %s |
| 131 | +`, key, keystorePath) |
| 132 | + |
| 133 | + mockbeat.WriteConfigFile(keystoreCfg) |
| 134 | + |
| 135 | + mockbeat.Start("keystore", "create") |
| 136 | + mockbeat.WaitStdOutContains("Created mockbeat keystore", 10*time.Second) |
| 137 | + mockbeat.Stop() |
| 138 | + |
| 139 | + addKeystoreSecret(t, mockbeat, key, secret) |
| 140 | + |
| 141 | + mockbeat.Start() |
| 142 | + mockbeat.WaitLogsContains("ackloop: done send ack", 60*time.Second) |
| 143 | + mockbeat.Stop() |
| 144 | + |
| 145 | + require.DirExists(t, secret) |
| 146 | +} |
| 147 | + |
| 148 | +func TestExportConfigWithKeystore(t *testing.T) { |
| 149 | + mockbeat := NewBeat(t, "mockbeat", "../../libbeat.test") |
| 150 | + keystorePath := filepath.Join(mockbeat.TempDir(), "test.keystore") |
| 151 | + |
| 152 | + key := "output.console.bulk_max_size" |
| 153 | + secret := "42" |
| 154 | + |
| 155 | + keystoreCfg := fmt.Sprintf(` |
| 156 | +mockbeat: |
| 157 | +name: |
| 158 | +queue.mem: |
| 159 | + events: 32 |
| 160 | + flush.min_events: 8 |
| 161 | + flush.timeout: 0.1s |
| 162 | +logging: |
| 163 | + level: debug |
| 164 | +output.console: |
| 165 | + codec.json: |
| 166 | + pretty: true |
| 167 | +keystore: |
| 168 | + path: %s |
| 169 | +`, keystorePath) |
| 170 | + |
| 171 | + mockbeat.WriteConfigFile(keystoreCfg) |
| 172 | + |
| 173 | + mockbeat.Start("keystore", "create") |
| 174 | + mockbeat.WaitStdOutContains("Created mockbeat keystore", 10*time.Second) |
| 175 | + mockbeat.Stop() |
| 176 | + |
| 177 | + addKeystoreSecret(t, mockbeat, key, secret) |
| 178 | + |
| 179 | + mockbeat.Start("export", "config") |
| 180 | + err := mockbeat.Cmd.Wait() |
| 181 | + require.NoError(t, err) |
| 182 | + require.Equal(t, 0, mockbeat.Cmd.ProcessState.ExitCode(), "incorrect exit code") |
| 183 | + |
| 184 | + stdout, err := mockbeat.ReadStdout() |
| 185 | + require.NoError(t, err) |
| 186 | + require.NotContains(t, stdout, "bulk_max_size: "+secret, "exported config must not contain keystore secret value") |
| 187 | +} |
0 commit comments