-
Notifications
You must be signed in to change notification settings - Fork 0
/
secret_agent_test.go
209 lines (183 loc) · 6.08 KB
/
secret_agent_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
// Copyright 2024 Aerospike, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package backup
import (
"encoding/binary"
"encoding/json"
"fmt"
"net"
"testing"
"time"
saClient "github.com/aerospike/backup-go/pkg/secret-agent"
"github.com/aerospike/backup-go/pkg/secret-agent/connection"
"github.com/aerospike/backup-go/pkg/secret-agent/models"
"github.com/stretchr/testify/require"
)
const (
testSAPort = 2222
testSASecretKey = "secrets:resource:key"
testSASecretErrPrefix = "sacred:resource:key"
testSASecretKeyErr = "resource:key"
testSASecretKeyErrLong = "secrets:resource:key:val"
testCaFile = "tests/integration/cert_test.pem"
magic = 0x51dec1cc
testPKey = `MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwgg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xWG8CgYBqOw1sJEjKrSBD2w8IY8zgd6dXHv/hyCeu/TT7FJFxNnAczrhg
FFQv7n0wb2xqkCWJRbFd9iAeYtWI7RA4hmYVatdYlBHYV0DHJtwuFB+UHG7SJHZ/
tJK26Dh5hpTzzYMWvAFMuGR0OPRCgCHO4QbNk7zRTUgV2ch9yYKOqlhkmQKBgGYE
21KtpAvd3H8IDK66DQLLyGk6EY5XUHTQLnkDl6jYnCg1/IJKb2kar7f2mt4yLu3y
UhElUW+bSMR2u9yrOkRm8pI22+1+pA88nbLCE4ePNjvm+P8tX5vMsP5dMw3Nfivs
FP/P34Ge5nNmSyP5atj9rdMBPR6c4T/TdDPndykvAoGBAKkxR2CDdmpwzR9pdNHc
N3TlUGL8bf3ri9vC1Uwx4HFrqo99pEz4JwXT8VZ/J3KYH9H6hsPfoS9swlBxDjwU
rqDyLDkROLjIJE1InlzrtH9rHGInExFVWbzL1jpCV7GtQsYVdiSxHzNaRB47UGZI
ah87+EsQLgoao6VWDlepN54P`
)
func mockTCPServer(address string, handler func(net.Conn)) (net.Listener, error) {
listener, err := net.Listen(saClient.ConnectionTypeTCP, address)
if err != nil {
return nil, err
}
go func() {
for {
conn, err := listener.Accept()
if err != nil {
continue
}
go handler(conn)
}
}()
return listener, nil
}
func mockHandler(conn net.Conn) {
defer conn.Close()
_, _ = connection.ReadBytes(conn, 10)
resp := models.Response{
SecretValue: testPKey,
Error: "",
}
respJSON, _ := json.Marshal(resp)
length := len(respJSON)
header := make([]byte, 8)
binary.BigEndian.PutUint32(header[:4], magic)
binary.BigEndian.PutUint32(header[4:], uint32(length))
_, err := conn.Write(append(header, respJSON...))
if err != nil {
fmt.Println(err)
}
}
func TestSecretAgent_isSecret(t *testing.T) {
t.Parallel()
testCases := []struct {
secret string
result bool
}{
{testSASecretKey, true},
{testSASecretKeyErr, false},
{"", false},
}
for _, tt := range testCases {
result := isSecret(tt.secret)
require.Equal(t, tt.result, result)
}
}
func TestSecretAgent_getResourceKey(t *testing.T) {
t.Parallel()
testCases := []struct {
key string
resource string
secretKey string
errContent string
}{
{testSASecretKey, "resource", "key", ""},
{testSASecretErrPrefix, "", "", "invalid secret key format"},
{testSASecretKeyErrLong, "", "", "invalid secret key format"},
}
for i, tt := range testCases {
res, sk, err := getResourceKey(tt.key)
require.Equal(t, tt.resource, res)
require.Equal(t, tt.secretKey, sk)
if tt.errContent != "" {
require.ErrorContains(t, err, tt.errContent, fmt.Sprintf("case %d", i))
} else {
require.NoError(t, err, fmt.Sprintf("case %d", i))
}
}
}
func TestSecretAgent_getTlSConfig(t *testing.T) {
t.Parallel()
filePem := testCaFile
filePemNotExist := "tests/integration/smth.pem"
filePemWrong := "tests/integration/pkey_test"
testCases := []struct {
file *string
errContent string
}{
{nil, ""},
{&filePem, ""},
{&filePemNotExist, "unable to read ca file"},
{&filePemWrong, "nothing to append to ca cert pool"},
}
for i, tt := range testCases {
_, err := getTlSConfig(tt.file)
if tt.errContent != "" {
require.ErrorContains(t, err, tt.errContent, fmt.Sprintf("case %d", i))
} else {
require.NoError(t, err, fmt.Sprintf("case %d", i))
}
}
}
func TestSecretAgent_getSecret(t *testing.T) {
t.Parallel()
listener, err := mockTCPServer(fmt.Sprintf(":%d", testSAPort), mockHandler)
require.NoError(t, err)
defer listener.Close()
// Wait for server start.
time.Sleep(1 * time.Second)
cfg := testSecretAgentConfig()
testCases := []struct {
config *SecretAgentConfig
key string
errContent string
}{
{cfg, testSASecretKey, ""},
{nil, testSASecretKey, "secret config not initialized"},
{cfg, testSASecretKeyErr, "invalid secret key format"},
{cfg, testSASecretErrPrefix, "invalid secret key format"},
{cfg, testSASecretKeyErrLong, "invalid secret key format"},
}
for i, tt := range testCases {
_, err = getSecret(tt.config, tt.key)
if tt.errContent != "" {
require.ErrorContains(t, err, tt.errContent, fmt.Sprintf("case %d", i))
} else {
require.NoError(t, err, fmt.Sprintf("case %d", i))
}
}
}