-
Notifications
You must be signed in to change notification settings - Fork 123
/
client_test.go
161 lines (130 loc) · 3.81 KB
/
client_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
// Copyright 2021-2023 Zenauth Ltd.
// SPDX-License-Identifier: Apache-2.0
//go:build tests
package client_test
import (
"context"
"fmt"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/cerbos/cerbos/client"
"github.com/cerbos/cerbos/client/testutil"
"github.com/cerbos/cerbos/internal/test"
)
const (
adminUsername = "cerbos"
adminPassword = "cerbosAdmin"
readyTimeout = 90 * time.Second
readyPollInterval = 100 * time.Millisecond
healthCheckTimeout = 80 * time.Millisecond
)
func TestClient(t *testing.T) {
jwt := client.GenerateToken(t, time.Now().Add(5*time.Minute))
testCases := []struct {
name string
tls bool
opts []client.Opt
}{
{
name: "with_tls",
tls: true,
opts: []client.Opt{client.WithTLSInsecure()},
},
{
name: "without_tls",
tls: false,
opts: []client.Opt{client.WithPlaintext()},
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Run("tcp", func(t *testing.T) {
s, err := testutil.StartCerbosServer(mkServerOpts(t, tc.tls)...)
require.NoError(t, err)
defer s.Stop() //nolint:errcheck
require.Eventually(t, serverIsReady(t, s), readyTimeout, readyPollInterval)
ac, err := client.NewAdminClientWithCredentials(s.GRPCAddr(), adminUsername, adminPassword, tc.opts...)
require.NoError(t, err)
loadPolicies(t, ac)
ports := []struct {
name string
addr string
}{
{
name: "grpc",
addr: s.GRPCAddr(),
},
{
name: "http",
addr: s.HTTPAddr(),
},
}
for _, port := range ports {
c, err := client.New(port.addr, tc.opts...)
require.NoError(t, err)
t.Run(port.name, client.TestGRPCClient(c))
}
})
t.Run("uds", func(t *testing.T) {
serverOpts := mkServerOpts(t, tc.tls)
tempDir := t.TempDir()
serverOpts = append(serverOpts,
testutil.WithHTTPListenAddr(fmt.Sprintf("unix:%s", filepath.Join(tempDir, "http.sock"))),
testutil.WithGRPCListenAddr(fmt.Sprintf("unix:%s", filepath.Join(tempDir, "grpc.sock"))),
)
s, err := testutil.StartCerbosServer(serverOpts...)
require.NoError(t, err)
defer s.Stop() //nolint:errcheck
require.Eventually(t, serverIsReady(t, s), readyTimeout, readyPollInterval)
ac, err := client.NewAdminClientWithCredentials(s.GRPCAddr(), adminUsername, adminPassword, tc.opts...)
require.NoError(t, err)
loadPolicies(t, ac)
c, err := client.New(s.GRPCAddr(), tc.opts...)
require.NoError(t, err)
t.Run("grpc", client.TestGRPCClient(c.With(client.AuxDataJWT(jwt, ""))))
})
})
}
}
func mkServerOpts(t *testing.T, withTLS bool) []testutil.ServerOpt {
t.Helper()
dbName := test.RandomStr(5)
serverOpts := []testutil.ServerOpt{
testutil.WithPolicyRepositorySQLite3(fmt.Sprintf("%s?_fk=true", filepath.Join(t.TempDir(), dbName))),
testutil.WithAdminAPI(adminUsername, adminPassword),
}
if withTLS {
certDir := test.PathToDir(t, "server")
tlsCert := filepath.Join(certDir, "tls.crt")
tlsKey := filepath.Join(certDir, "tls.key")
serverOpts = append(serverOpts, testutil.WithTLSCertAndKey(tlsCert, tlsKey))
}
return serverOpts
}
func loadPolicies(t *testing.T, ac client.AdminClient) {
t.Helper()
ps := client.NewPolicySet()
err := test.FindPolicyFiles(t, "store", func(path string) error {
ps.AddPolicyFromFile(path)
return ps.Err()
})
require.NoError(t, err)
require.NoError(t, ac.AddOrUpdatePolicy(context.Background(), ps))
}
func serverIsReady(t *testing.T, s *testutil.ServerInfo) func() bool {
t.Helper()
return func() bool {
ctx, cancelFunc := context.WithTimeout(context.Background(), healthCheckTimeout)
defer cancelFunc()
ready, err := s.IsReady(ctx)
if err != nil {
t.Logf("Server is not ready: %v", err)
return false
}
t.Logf("Server ready = %T", ready)
return ready
}
}