@@ -14,6 +14,8 @@ import (
14
14
"github.com/pkg/errors"
15
15
16
16
aclpb "github.com/authorizer-tech/access-controller/genprotos/authorizer/accesscontroller/v1alpha1"
17
+ "github.com/authorizer-tech/access-controller/internal/hashring"
18
+ namespacemgr "github.com/authorizer-tech/access-controller/internal/namespace-manager"
17
19
log "github.com/sirupsen/logrus"
18
20
"google.golang.org/grpc"
19
21
"google.golang.org/grpc/codes"
@@ -36,11 +38,11 @@ type AccessController struct {
36
38
37
39
Memberlist * memberlist.Memberlist
38
40
RPCRouter ClientRouter
39
- Hashring Hashring
41
+ Hashring hashring. Hashring
40
42
41
43
RelationTupleStore
42
- PeerNamespaceConfigStore
43
- NamespaceManager
44
+ namespacemgr. PeerNamespaceConfigStore
45
+ namespacemgr. NamespaceManager
44
46
NodeConfigs
45
47
46
48
shutdown chan struct {}
@@ -56,7 +58,7 @@ func WithStore(store RelationTupleStore) AccessControllerOption {
56
58
}
57
59
58
60
// WithNamespaceManager sets the AccessController's NamespaceManager.
59
- func WithNamespaceManager (m NamespaceManager ) AccessControllerOption {
61
+ func WithNamespaceManager (m namespacemgr. NamespaceManager ) AccessControllerOption {
60
62
return func (ac * AccessController ) {
61
63
ac .NamespaceManager = m
62
64
}
@@ -80,7 +82,7 @@ func (a *AccessController) watchNamespaceConfigs(ctx context.Context) {
80
82
go func () {
81
83
for {
82
84
83
- var iter ChangelogIterator
85
+ var iter namespacemgr. ChangelogIterator
84
86
var err error
85
87
86
88
err = backoff .Retry (func () error {
@@ -115,7 +117,7 @@ func (a *AccessController) watchNamespaceConfigs(ctx context.Context) {
115
117
timestamp := change .Timestamp
116
118
117
119
switch change .Operation {
118
- case AddNamespace , UpdateNamespace :
120
+ case namespacemgr . AddNamespace , namespacemgr . UpdateNamespace :
119
121
err := a .PeerNamespaceConfigStore .SetNamespaceConfigSnapshot (a .ServerID , namespace , config , timestamp )
120
122
if err != nil {
121
123
log .Errorf ("Failed to set the namespace config snapshot for this node: %v" , err )
@@ -137,7 +139,7 @@ func (a *AccessController) watchNamespaceConfigs(ctx context.Context) {
137
139
138
140
// chooseNamespaceConfigSnapshot selects the most recent namespace config snapshot that is
139
141
// common to all peers/nodes within the cluster that this node is a part of.
140
- func (a * AccessController ) chooseNamespaceConfigSnapshot (namespace string ) (* NamespaceConfigSnapshot , error ) {
142
+ func (a * AccessController ) chooseNamespaceConfigSnapshot (namespace string ) (* namespacemgr. NamespaceConfigSnapshot , error ) {
141
143
142
144
peerSnapshots , err := a .PeerNamespaceConfigStore .ListNamespaceConfigSnapshots (namespace )
143
145
if err != nil {
@@ -180,10 +182,10 @@ func (a *AccessController) chooseNamespaceConfigSnapshot(namespace string) (*Nam
180
182
}
181
183
182
184
if len (commonTimestamps ) < 1 {
183
- return nil , ErrNoLocalNamespacesDefined
185
+ return nil , namespacemgr . ErrNoLocalNamespacesDefined
184
186
}
185
187
} else {
186
- return nil , ErrNoLocalNamespacesDefined
188
+ return nil , namespacemgr . ErrNoLocalNamespacesDefined
187
189
}
188
190
189
191
var selectedTS time.Time
@@ -195,7 +197,7 @@ func (a *AccessController) chooseNamespaceConfigSnapshot(namespace string) (*Nam
195
197
196
198
config := peerSnapshots [peerWithMin ][selectedTS ]
197
199
198
- snapshot := & NamespaceConfigSnapshot {
200
+ snapshot := & namespacemgr. NamespaceConfigSnapshot {
199
201
Config : config ,
200
202
Timestamp : selectedTS ,
201
203
}
@@ -206,14 +208,10 @@ func (a *AccessController) chooseNamespaceConfigSnapshot(namespace string) (*Nam
206
208
// NewAccessController constructs a new AccessController with the options provided.
207
209
func NewAccessController (opts ... AccessControllerOption ) (* AccessController , error ) {
208
210
209
- peerConfigs := & inmemPeerNamespaceConfigStore {
210
- configs : make (map [string ]map [string ]map [time.Time ]* aclpb.NamespaceConfig ),
211
- }
212
-
213
211
ac := AccessController {
214
212
RPCRouter : NewMapClientRouter (),
215
- Hashring : NewConsistentHashring (nil ),
216
- PeerNamespaceConfigStore : peerConfigs ,
213
+ Hashring : hashring . NewConsistentHashring (nil ),
214
+ PeerNamespaceConfigStore : namespacemgr . NewInMemoryPeerNamespaceConfigStore () ,
217
215
shutdown : make (chan struct {}),
218
216
}
219
217
@@ -234,8 +232,8 @@ func NewAccessController(opts ...AccessControllerOption) (*AccessController, err
234
232
}
235
233
236
234
switch entry .Operation {
237
- case AddNamespace , UpdateNamespace :
238
- err = peerConfigs .SetNamespaceConfigSnapshot (ac .ServerID , entry .Namespace , entry .Config , entry .Timestamp )
235
+ case namespacemgr . AddNamespace , namespacemgr . UpdateNamespace :
236
+ err = ac .SetNamespaceConfigSnapshot (ac .ServerID , entry .Namespace , entry .Config , entry .Timestamp )
239
237
if err != nil {
240
238
return nil , err
241
239
}
@@ -461,7 +459,7 @@ func (a *AccessController) checkRewrite(ctx context.Context, rule *aclpb.Rewrite
461
459
462
460
func (a * AccessController ) check (ctx context.Context , namespace , object , relation , subject string ) (bool , error ) {
463
461
464
- if peerChecksum , ok := ChecksumFromContext (ctx ); ok {
462
+ if peerChecksum , ok := hashring . ChecksumFromContext (ctx ); ok {
465
463
// The hash ring checksum of the peer should always be present if the
466
464
// request is proxied from another access-controller. If the request
467
465
// is made externally it won't be present.
@@ -476,14 +474,14 @@ func (a *AccessController) check(ctx context.Context, namespace, object, relatio
476
474
// The namespace config timestamp from the peer should always be present if
477
475
// the request is proxied from another access-controller. If the request is
478
476
// made externally, we select a namespace config timestamp and forward it on.
479
- peerNamespaceCfgTs , ok := NamespaceConfigTimestampFromContext (ctx , namespace )
477
+ peerNamespaceCfgTs , ok := namespacemgr . NamespaceConfigTimestampFromContext (ctx , namespace )
480
478
if ! ok {
481
479
snapshot , err := a .chooseNamespaceConfigSnapshot (namespace )
482
480
if err != nil {
483
- if err == ErrNoLocalNamespacesDefined {
484
- return false , NamespaceConfigError {
481
+ if err == namespacemgr . ErrNoLocalNamespacesDefined {
482
+ return false , namespacemgr. NamespaceConfigError {
485
483
Message : fmt .Sprintf ("'%s' namespace is undefined. If you recently added it, it may take a couple minutes to propagate" , namespace ),
486
- Type : NamespaceDoesntExist ,
484
+ Type : namespacemgr . NamespaceDoesntExist ,
487
485
}.ToStatus ().Err ()
488
486
}
489
487
@@ -492,7 +490,7 @@ func (a *AccessController) check(ctx context.Context, namespace, object, relatio
492
490
493
491
snapshotTimestamp = snapshot .Timestamp
494
492
495
- ctx = NewContextWithNamespaceConfigTimestamp (ctx , namespace , snapshotTimestamp )
493
+ ctx = namespacemgr . NewContextWithNamespaceConfigTimestamp (ctx , namespace , snapshotTimestamp )
496
494
} else {
497
495
snapshotTimestamp = peerNamespaceCfgTs
498
496
}
@@ -503,9 +501,9 @@ func (a *AccessController) check(ctx context.Context, namespace, object, relatio
503
501
}
504
502
505
503
if cfg == nil {
506
- return false , NamespaceConfigError {
504
+ return false , namespacemgr. NamespaceConfigError {
507
505
Message : fmt .Sprintf ("'%s' namespace is undefined. If you recently added it, it may take a couple minutes to propagate" , namespace ),
508
- Type : NamespaceDoesntExist ,
506
+ Type : namespacemgr . NamespaceDoesntExist ,
509
507
}.ToStatus ().Err ()
510
508
}
511
509
@@ -531,7 +529,7 @@ func (a *AccessController) check(ctx context.Context, namespace, object, relatio
531
529
panic ("unexpected rpc client type encountered" )
532
530
}
533
531
534
- ctx = NewContextWithChecksum (ctx , a .Hashring .Checksum ())
532
+ ctx = hashring . NewContextWithChecksum (ctx , a .Hashring .Checksum ())
535
533
536
534
subject := SubjectID {ID : subject }
537
535
@@ -709,10 +707,10 @@ func (a *AccessController) expand(ctx context.Context, namespace, object, relati
709
707
710
708
configSnapshot , err := a .chooseNamespaceConfigSnapshot (namespace )
711
709
if err != nil {
712
- if err == ErrNoLocalNamespacesDefined {
713
- return nil , NamespaceConfigError {
710
+ if err == namespacemgr . ErrNoLocalNamespacesDefined {
711
+ return nil , namespacemgr. NamespaceConfigError {
714
712
Message : fmt .Sprintf ("'%s' namespace is undefined. If you recently added it, it may take a couple minutes to propagate" , namespace ),
715
- Type : NamespaceDoesntExist ,
713
+ Type : namespacemgr . NamespaceDoesntExist ,
716
714
}.ToStatus ().Err ()
717
715
}
718
716
@@ -732,7 +730,7 @@ func (a *AccessController) expand(ctx context.Context, namespace, object, relati
732
730
return nil , nil
733
731
}
734
732
735
- ctx = NewContextWithNamespaceConfigTimestamp (ctx , namespace , configSnapshot .Timestamp )
733
+ ctx = namespacemgr . NewContextWithNamespaceConfigTimestamp (ctx , namespace , configSnapshot .Timestamp )
736
734
737
735
return a .expandWithRewrite (ctx , rewrite , tree , namespace , object , relation , depth )
738
736
}
@@ -804,9 +802,9 @@ func (a *AccessController) WriteRelationTuplesTxn(ctx context.Context, req *aclp
804
802
805
803
configSnapshot , err := a .chooseNamespaceConfigSnapshot (namespace )
806
804
if err != nil {
807
- return nil , NamespaceConfigError {
805
+ return nil , namespacemgr. NamespaceConfigError {
808
806
Message : fmt .Sprintf ("'%s' namespace is undefined. If you recently added it, it may take a couple minutes to propagate" , namespace ),
809
- Type : NamespaceDoesntExist ,
807
+ Type : namespacemgr . NamespaceDoesntExist ,
810
808
}.ToStatus ().Err ()
811
809
}
812
810
@@ -822,9 +820,9 @@ func (a *AccessController) WriteRelationTuplesTxn(ctx context.Context, req *aclp
822
820
823
821
rewrite := rewriteFromNamespaceConfig (relation , configSnapshot .Config )
824
822
if rewrite == nil {
825
- return nil , NamespaceConfigError {
823
+ return nil , namespacemgr. NamespaceConfigError {
826
824
Message : fmt .Sprintf ("'%s' relation is undefined in namespace '%s' at snapshot config timestamp '%s'. If this relation was recently added, please try again in a couple minutes" , relation , namespace , configSnapshot .Timestamp ),
827
- Type : NamespaceRelationUndefined ,
825
+ Type : namespacemgr . NamespaceRelationUndefined ,
828
826
}.ToStatus ().Err ()
829
827
}
830
828
@@ -835,10 +833,10 @@ func (a *AccessController) WriteRelationTuplesTxn(ctx context.Context, req *aclp
835
833
836
834
configSnapshot , err := a .chooseNamespaceConfigSnapshot (n )
837
835
if err != nil {
838
- if err == ErrNoLocalNamespacesDefined {
839
- return nil , NamespaceConfigError {
836
+ if err == namespacemgr . ErrNoLocalNamespacesDefined {
837
+ return nil , namespacemgr. NamespaceConfigError {
840
838
Message : fmt .Sprintf ("SubjectSet '%s' references the '%s' namespace which is undefined. If this namespace was recently added, please try again in a couple minutes" , subject .String (), n ),
841
- Type : NamespaceDoesntExist ,
839
+ Type : namespacemgr . NamespaceDoesntExist ,
842
840
}.ToStatus ().Err ()
843
841
}
844
842
@@ -848,9 +846,9 @@ func (a *AccessController) WriteRelationTuplesTxn(ctx context.Context, req *aclp
848
846
if r != "..." {
849
847
rewrite := rewriteFromNamespaceConfig (r , configSnapshot .Config )
850
848
if rewrite == nil {
851
- return nil , NamespaceConfigError {
849
+ return nil , namespacemgr. NamespaceConfigError {
852
850
Message : fmt .Sprintf ("SubjectSet '%s' references relation '%s' which is undefined in the namespace '%s' at snapshot config timestamp '%s'. If this relation was recently added to the config, please try again in a couple minutes" , subject .String (), r , n , configSnapshot .Timestamp ),
853
- Type : NamespaceRelationUndefined ,
851
+ Type : namespacemgr . NamespaceRelationUndefined ,
854
852
}.ToStatus ().Err ()
855
853
}
856
854
}
@@ -892,9 +890,9 @@ func (a *AccessController) ListRelationTuples(ctx context.Context, req *aclpb.Li
892
890
893
891
_ , err := a .chooseNamespaceConfigSnapshot (namespace )
894
892
if err != nil {
895
- return nil , NamespaceConfigError {
893
+ return nil , namespacemgr. NamespaceConfigError {
896
894
Message : fmt .Sprintf ("'%s' namespace is undefined. If you recently added it, it may take a couple minutes to propagate" , namespace ),
897
- Type : NamespaceDoesntExist ,
895
+ Type : namespacemgr . NamespaceDoesntExist ,
898
896
}.ToStatus ().Err ()
899
897
}
900
898
@@ -990,7 +988,7 @@ func (a *AccessController) WriteConfig(ctx context.Context, req *aclpb.WriteConf
990
988
err := a .NamespaceManager .WrapTransaction (ctx , func (txnCtx context.Context ) error {
991
989
currentConfig , err := a .NamespaceManager .GetConfig (txnCtx , namespace )
992
990
if err != nil {
993
- if err == ErrNamespaceDoesntExist {
991
+ if err == namespacemgr . ErrNamespaceDoesntExist {
994
992
return a .NamespaceManager .UpsertConfig (txnCtx , config )
995
993
}
996
994
@@ -1022,17 +1020,17 @@ func (a *AccessController) WriteConfig(ctx context.Context, req *aclpb.WriteConf
1022
1020
relations = append (relations , relation )
1023
1021
}
1024
1022
1025
- return NamespaceConfigError {
1023
+ return namespacemgr. NamespaceConfigError {
1026
1024
Message : fmt .Sprintf ("Relation(s) [%v] cannot be removed while one or more relation tuples reference them. Please migrate all relation tuples before removing a relation." , strings .Join (relations , "," )),
1027
- Type : NamespaceUpdateFailedPrecondition ,
1025
+ Type : namespacemgr . NamespaceUpdateFailedPrecondition ,
1028
1026
}
1029
1027
}
1030
1028
}
1031
1029
1032
1030
return a .NamespaceManager .UpsertConfig (txnCtx , config )
1033
1031
})
1034
1032
if err != nil {
1035
- err , ok := err .(NamespaceConfigError )
1033
+ err , ok := err .(namespacemgr. NamespaceConfigError )
1036
1034
if ok {
1037
1035
return nil , err .ToStatus ().Err ()
1038
1036
}
@@ -1054,7 +1052,7 @@ func (a *AccessController) ReadConfig(ctx context.Context, req *aclpb.ReadConfig
1054
1052
1055
1053
config , err := a .NamespaceManager .GetConfig (ctx , namespace )
1056
1054
if err != nil {
1057
- if errors .Is (err , ErrNamespaceDoesntExist ) {
1055
+ if errors .Is (err , namespacemgr . ErrNamespaceDoesntExist ) {
1058
1056
return nil , status .Errorf (codes .NotFound , "The namespace '%s' does not exist. If it was recently added, please try again in a couple of minutes" , namespace )
1059
1057
}
1060
1058
return nil , internalErrorStatus
0 commit comments