-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathauthorizerd_mock_test.go
169 lines (142 loc) · 4.19 KB
/
authorizerd_mock_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
// Copyright 2023 LY Corporation
//
// 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 authorizerd
import (
"context"
"crypto/x509"
"time"
"github.com/AthenZ/athenz-authorizer/v5/access"
"github.com/AthenZ/athenz-authorizer/v5/jwk"
"github.com/AthenZ/athenz-authorizer/v5/policy"
"github.com/AthenZ/athenz-authorizer/v5/pubkey"
"github.com/AthenZ/athenz-authorizer/v5/role"
"github.com/pkg/errors"
)
type ConfdMock struct {
pubkey.Daemon
confdExp time.Duration
}
func (cm *ConfdMock) Start(ctx context.Context) <-chan error {
ech := make(chan error, 1)
go func() {
time.Sleep(cm.confdExp)
ech <- errors.New("pubkey error")
}()
return ech
}
type PubkeydMock struct {
StartFunc func(context.Context) <-chan error
UpdateFunc func(context.Context) error
GetProviderFunc func() pubkey.Provider
}
func (pm *PubkeydMock) Start(ctx context.Context) <-chan error {
if pm.StartFunc != nil {
return pm.StartFunc(ctx)
}
return nil
}
func (pm *PubkeydMock) Update(ctx context.Context) error {
if pm.UpdateFunc != nil {
return pm.UpdateFunc(ctx)
}
return nil
}
func (pm *PubkeydMock) GetProvider() pubkey.Provider {
if pm.GetProviderFunc != nil {
return pm.GetProviderFunc()
}
return nil
}
type PolicydMock struct {
UpdateFunc func(context.Context) error
CheckPolicyRoleFunc func(ctx context.Context, domain string, roles []string, action, resource string) ([]string, error)
GetPrincipalCacheLenFunc func() int
GetPrincipalCacheSizeFunc func() int64
policydExp time.Duration
policyCache map[string][]*policy.Assertion
principalCacheLen int
principalCacheSize int64
}
func (pdm *PolicydMock) Start(context.Context) <-chan error {
ech := make(chan error, 1)
go func() {
time.Sleep(pdm.policydExp)
ech <- errors.New("policyd error")
}()
return ech
}
func (pdm *PolicydMock) Update(ctx context.Context) error {
if pdm.UpdateFunc != nil {
return pdm.UpdateFunc(ctx)
}
return nil
}
func (pdm *PolicydMock) CheckPolicy(ctx context.Context, domain string, roles []string, action, resource string) error {
_, err := pdm.CheckPolicyRoles(ctx, domain, roles, action, resource)
return err
}
func (pdm *PolicydMock) CheckPolicyRoles(ctx context.Context, domain string, roles []string, action, resource string) ([]string, error) {
if pdm.CheckPolicyRoleFunc != nil {
return pdm.CheckPolicyRoleFunc(ctx, domain, roles, action, resource)
}
return nil, nil
}
func (pdm *PolicydMock) GetPolicyCache(ctx context.Context) map[string][]*policy.Assertion {
return pdm.policyCache
}
func (pdm *PolicydMock) GetPrincipalCacheLen() int {
return pdm.principalCacheLen
}
func (pdm *PolicydMock) GetPrincipalCacheSize() int64 {
return pdm.principalCacheSize
}
type RoleProcessorMock struct {
role.Processor
wantErr error
rt *role.Token
}
func (rpm *RoleProcessorMock) ParseAndValidateRoleToken(tok string) (*role.Token, error) {
return rpm.rt, rpm.wantErr
}
type AccessProcessorMock struct {
access.Processor
wantErr error
atc *access.OAuth2AccessTokenClaim
}
func (apm *AccessProcessorMock) ParseAndValidateOAuth2AccessToken(cred string, cert *x509.Certificate) (*access.OAuth2AccessTokenClaim, error) {
return apm.atc, apm.wantErr
}
type JwkdMock struct {
StartFunc func(context.Context) <-chan error
UpdateFunc func(context.Context) error
GetProviderFunc func() jwk.Provider
}
func (jm *JwkdMock) Start(ctx context.Context) <-chan error {
if jm.StartFunc != nil {
return jm.StartFunc(ctx)
}
return nil
}
func (jm *JwkdMock) Update(ctx context.Context) error {
if jm.UpdateFunc != nil {
return jm.UpdateFunc(ctx)
}
return nil
}
func (jm *JwkdMock) GetProvider() jwk.Provider {
if jm.GetProviderFunc != nil {
return jm.GetProviderFunc()
}
return nil
}