-
Notifications
You must be signed in to change notification settings - Fork 123
/
admin_test.go
146 lines (118 loc) · 3.84 KB
/
admin_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
// Copyright 2021-2023 Zenauth Ltd.
// SPDX-License-Identifier: Apache-2.0
package client
import (
"context"
"errors"
"fmt"
"io"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
auditv1 "github.com/cerbos/cerbos/api/genpb/cerbos/audit/v1"
responsev1 "github.com/cerbos/cerbos/api/genpb/cerbos/response/v1"
svcv1 "github.com/cerbos/cerbos/api/genpb/cerbos/svc/v1"
"github.com/cerbos/cerbos/client/testutil"
"github.com/cerbos/cerbos/internal/namer"
"github.com/cerbos/cerbos/internal/test"
)
func TestCollectLogs(t *testing.T) {
t.Run("access logs", func(t *testing.T) {
receiver := func() (*responsev1.ListAuditLogEntriesResponse, error) {
return &responsev1.ListAuditLogEntriesResponse{Entry: &responsev1.ListAuditLogEntriesResponse_AccessLogEntry{
AccessLogEntry: &auditv1.AccessLogEntry{CallId: "test"},
}}, nil
}
logs, err := collectLogs(receiver)
require.NoError(t, err)
log := <-logs
require.Equal(t, "test", log.accessLog.CallId)
require.Empty(t, logs)
})
t.Run("return io.EOF directly", func(t *testing.T) {
receiver := func() (*responsev1.ListAuditLogEntriesResponse, error) {
return nil, io.EOF
}
logs, err := collectLogs(receiver)
require.NoError(t, err)
require.Empty(t, logs)
})
t.Run("error from receiver", func(t *testing.T) {
receiver := func() (*responsev1.ListAuditLogEntriesResponse, error) { return nil, errors.New("test-error") }
logs, err := collectLogs(receiver)
require.NoError(t, err)
log := <-logs
al, err := log.AccessLog()
require.Nil(t, al)
require.Error(t, err)
})
}
func TestAuditLogs(t *testing.T) {
t.Run("should fail on invalid log options", func(t *testing.T) {
c := GrpcAdminClient{client: svcv1.NewCerbosAdminServiceClient(&grpc.ClientConn{})}
_, err := c.AuditLogs(context.Background(), AuditLogOptions{
Type: AccessLogs,
Tail: 10000,
})
require.Error(t, err)
})
t.Run("should fail if log type is different", func(t *testing.T) {
c := GrpcAdminClient{client: svcv1.NewCerbosAdminServiceClient(&grpc.ClientConn{})}
_, err := c.AuditLogs(context.Background(), AuditLogOptions{
Type: AuditLogType(100),
Tail: 10000,
})
require.Error(t, err)
})
}
func TestListPolicies(t *testing.T) {
const (
adminUsername = "cerbos"
adminPassword = "cerbosAdmin"
timeout = 15 * time.Second
)
serverOpts := []testutil.ServerOpt{
testutil.WithPolicyRepositorySQLite3(fmt.Sprintf("%s?_fk=true", filepath.Join(t.TempDir(), "cerbos.db"))),
testutil.WithAdminAPI(adminUsername, adminPassword),
}
s, err := testutil.StartCerbosServer(serverOpts...)
require.NoError(t, err)
t.Cleanup(func() {
_ = s.Stop()
})
ac, err := NewAdminClientWithCredentials(s.GRPCAddr(), adminUsername, adminPassword, WithPlaintext(), WithConnectTimeout(timeout))
require.NoError(t, err)
ps := 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))
t.Run("should get the list of policies", func(t *testing.T) {
have, err := ac.ListPolicies(context.Background(), WithIncludeDisabled())
require.NoError(t, err)
require.NotEmpty(t, have)
policyList := ps.GetPolicies()
want := make([]string, len(policyList))
for i, p := range policyList {
want[i] = namer.PolicyKey(p)
}
require.ElementsMatch(t, want, have)
})
t.Run("policy metadata should include store identifier", func(t *testing.T) {
policyList := ps.GetPolicies()
for _, p := range policyList {
want := namer.PolicyKey(p)
t.Run(want, func(t *testing.T) {
have, err := ac.GetPolicy(context.Background(), want)
require.NoError(t, err)
require.Len(t, have, 1)
require.NotNil(t, have[0].Metadata)
require.Equal(t, want, have[0].Metadata.StoreIdentifier)
})
}
})
}