-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathfetch_secret_test.go
246 lines (206 loc) · 7.3 KB
/
fetch_secret_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
//go:build secrets
// +build secrets
package secrets
import (
"bytes"
"fmt"
"os"
"os/exec"
"runtime"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/DataDog/datadog-agent/pkg/util/common"
)
var (
binExtension = ""
)
func build(m *testing.M, outBin, pkg string) {
_, err := exec.Command("go", "build", "-o", outBin+binExtension, pkg).Output()
if err != nil {
fmt.Printf("Could not compile test secretBackendCommand: %s", err)
os.Exit(1)
}
}
func TestMain(m *testing.M) {
if runtime.GOOS == "windows" {
binExtension = ".exe"
}
// We rely on Go for the test executable since it's the only common
// tool we're sure to have on Windows, OSX and Linux.
build(m, "./test/argument/argument", "./test/argument")
build(m, "./test/error/error", "./test/error")
build(m, "./test/input/input", "./test/input")
build(m, "./test/response_too_long/response_too_long", "./test/response_too_long")
build(m, "./test/simple/simple", "./test/simple")
build(m, "./test/timeout/timeout", "./test/timeout")
res := m.Run()
os.Remove("test/argument/argument" + binExtension)
os.Remove("test/error/error" + binExtension)
os.Remove("test/input/input" + binExtension)
os.Remove("test/response_too_long/response_too_long" + binExtension)
os.Remove("test/simple/simple" + binExtension)
os.Remove("test/timeout/timeout" + binExtension)
os.Exit(res)
}
func TestLimitBuffer(t *testing.T) {
lb := limitBuffer{
buf: &bytes.Buffer{},
max: 5,
}
n, err := lb.Write([]byte("012"))
assert.Equal(t, 3, n)
require.Nil(t, err)
assert.Equal(t, []byte("012"), lb.buf.Bytes())
n, err = lb.Write([]byte("abc"))
assert.Equal(t, 0, n)
require.NotNil(t, err)
assert.Equal(t, []byte("012"), lb.buf.Bytes())
n, err = lb.Write([]byte("ab"))
assert.Equal(t, 2, n)
require.Nil(t, err)
assert.Equal(t, []byte("012ab"), lb.buf.Bytes())
}
func TestExecCommandError(t *testing.T) {
defer func() {
secretBackendCommand = ""
secretBackendArguments = []string{}
secretBackendTimeout = 0
}()
inputPayload := "{\"version\": \"" + PayloadVersion + "\" , \"secrets\": [\"sec1\", \"sec2\"]}"
// empty secretBackendCommand
secretBackendCommand = ""
_, err := execCommand(inputPayload)
require.NotNil(t, err)
// test timeout
secretBackendCommand = "./test/timeout/timeout" + binExtension
setCorrectRight(secretBackendCommand)
secretBackendTimeout = 2
_, err = execCommand(inputPayload)
require.NotNil(t, err)
require.Equal(t, "error while running './test/timeout/timeout"+binExtension+"': command timeout", err.Error())
// test simple (no error)
secretBackendCommand = "./test/simple/simple" + binExtension
setCorrectRight(secretBackendCommand)
resp, err := execCommand(inputPayload)
require.Nil(t, err)
require.Equal(t, []byte("{\"handle1\":{\"value\":\"simple_password\"}}"), resp)
// test error
secretBackendCommand = "./test/error/error" + binExtension
setCorrectRight(secretBackendCommand)
_, err = execCommand(inputPayload)
require.NotNil(t, err)
// test arguments
secretBackendCommand = "./test/argument/argument" + binExtension
setCorrectRight(secretBackendCommand)
secretBackendArguments = []string{"arg1"}
_, err = execCommand(inputPayload)
require.NotNil(t, err)
secretBackendArguments = []string{"arg1", "arg2"}
resp, err = execCommand(inputPayload)
require.Nil(t, err)
require.Equal(t, []byte("{\"handle1\":{\"value\":\"arg_password\"}}"), resp)
// test input
secretBackendCommand = "./test/input/input" + binExtension
setCorrectRight(secretBackendCommand)
resp, err = execCommand(inputPayload)
require.Nil(t, err)
require.Equal(t, []byte("{\"handle1\":{\"value\":\"input_password\"}}"), resp)
// test buffer limit
secretBackendCommand = "./test/response_too_long/response_too_long" + binExtension
setCorrectRight(secretBackendCommand)
SecretBackendOutputMaxSize = 20
_, err = execCommand(inputPayload)
require.NotNil(t, err)
assert.Equal(t, "error while running './test/response_too_long/response_too_long"+binExtension+"': command output was too long: exceeded 20 bytes", err.Error())
}
func TestFetchSecretExecError(t *testing.T) {
defer func() {
secretCache = map[string]string{}
secretOrigin = map[string]common.StringSet{}
}()
runCommand = func(string) ([]byte, error) { return nil, fmt.Errorf("some error") }
_, err := fetchSecret([]string{"handle1", "handle2"}, "test")
assert.NotNil(t, err)
}
func TestFetchSecretUnmarshalError(t *testing.T) {
defer func() {
secretCache = map[string]string{}
secretOrigin = map[string]common.StringSet{}
}()
runCommand = func(string) ([]byte, error) { return []byte("{"), nil }
_, err := fetchSecret([]string{"handle1", "handle2"}, "test")
assert.NotNil(t, err)
}
func TestFetchSecretMissingSecret(t *testing.T) {
defer func() {
secretCache = map[string]string{}
secretOrigin = map[string]common.StringSet{}
}()
secrets := []string{"handle1", "handle2"}
runCommand = func(string) ([]byte, error) { return []byte("{}"), nil }
_, err := fetchSecret(secrets, "test")
assert.NotNil(t, err)
assert.Equal(t, "secret handle 'handle1' was not decrypted by the secret_backend_command", err.Error())
}
func TestFetchSecretErrorForHandle(t *testing.T) {
defer func() {
secretCache = map[string]string{}
secretOrigin = map[string]common.StringSet{}
}()
runCommand = func(string) ([]byte, error) {
return []byte("{\"handle1\":{\"value\": null, \"error\": \"some error\"}}"), nil
}
_, err := fetchSecret([]string{"handle1"}, "test")
assert.NotNil(t, err)
assert.Equal(t, "an error occurred while decrypting 'handle1': some error", err.Error())
}
func TestFetchSecretEmptyValue(t *testing.T) {
defer func() {
secretCache = map[string]string{}
secretOrigin = map[string]common.StringSet{}
}()
runCommand = func(string) ([]byte, error) {
return []byte("{\"handle1\":{\"value\": null}}"), nil
}
_, err := fetchSecret([]string{"handle1"}, "test")
assert.NotNil(t, err)
assert.Equal(t, "decrypted secret for 'handle1' is empty", err.Error())
runCommand = func(string) ([]byte, error) {
return []byte("{\"handle1\":{\"value\": \"\"}}"), nil
}
_, err = fetchSecret([]string{"handle1"}, "test")
assert.NotNil(t, err)
assert.Equal(t, "decrypted secret for 'handle1' is empty", err.Error())
}
func TestFetchSecret(t *testing.T) {
defer func() {
secretCache = map[string]string{}
secretOrigin = map[string]common.StringSet{}
}()
secrets := []string{"handle1", "handle2"}
// some dummy value to check the cache is not purge
secretCache["test"] = "yes"
runCommand = func(string) ([]byte, error) {
res := []byte("{\"handle1\":{\"value\":\"p1\"},")
res = append(res, []byte("\"handle2\":{\"value\":\"p2\"},")...)
res = append(res, []byte("\"handle3\":{\"value\":\"p3\"}}")...)
return res, nil
}
resp, err := fetchSecret(secrets, "test")
require.Nil(t, err)
assert.Equal(t, map[string]string{
"handle1": "p1",
"handle2": "p2",
}, resp)
assert.Equal(t, map[string]string{
"test": "yes",
"handle1": "p1",
"handle2": "p2",
}, secretCache)
assert.Equal(t, map[string]common.StringSet{"handle1": common.NewStringSet("test"), "handle2": common.NewStringSet("test")}, secretOrigin)
}