Skip to content

Commit

Permalink
fix: reload policy from database
Browse files Browse the repository at this point in the history
Signed-off-by: Zixuan Liu <[email protected]>
  • Loading branch information
nodece committed Apr 12, 2021
1 parent 5cf5dff commit 9df953d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
7 changes: 6 additions & 1 deletion store/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ func NewPolicyOperator(path string, e casbin.IDistributedEnforcer) (*PolicyOpera
return nil, errors.Wrapf(err, "failed to open bolt file")
}

err := p.LoadPolicy()
if err != nil {
return nil, errors.Wrapf(err, "failed to load policy from bolt")
}

return p, nil
}

Expand Down Expand Up @@ -173,7 +178,7 @@ func (p *PolicyOperator) LoadPolicy() error {
return err
})
if err != nil {
p.logger.Error("failed to persist to database", zap.Error(err))
p.logger.Error("failed to load policy from database", zap.Error(err))
}

return err
Expand Down
6 changes: 6 additions & 0 deletions store/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func TestPolicyOperator_AddPolicies(t *testing.T) {
assert.NoError(t, err)
defer os.RemoveAll(dir)

e.EXPECT().ClearPolicySelf(nil)
p, err := NewPolicyOperator(dir, e)
assert.NoError(t, err)

Expand All @@ -42,6 +43,7 @@ func TestPolicyOperator_RemovePolicies(t *testing.T) {
assert.NoError(t, err)
defer os.RemoveAll(dir)

e.EXPECT().ClearPolicySelf(nil)
p, err := NewPolicyOperator(dir, e)
assert.NoError(t, err)

Expand All @@ -60,6 +62,7 @@ func TestPolicyOperator_RemoveFilteredPolicy(t *testing.T) {
assert.NoError(t, err)
defer os.RemoveAll(dir)

e.EXPECT().ClearPolicySelf(nil)
p, err := NewPolicyOperator(dir, e)
assert.NoError(t, err)

Expand All @@ -78,6 +81,7 @@ func TestPolicyOperator_UpdatePolicy(t *testing.T) {
assert.NoError(t, err)
defer os.RemoveAll(dir)

e.EXPECT().ClearPolicySelf(nil)
p, err := NewPolicyOperator(dir, e)
assert.NoError(t, err)

Expand All @@ -96,6 +100,7 @@ func TestPolicyOperator_LoadPolicy(t *testing.T) {
assert.NoError(t, err)
defer os.RemoveAll(dir)

e.EXPECT().ClearPolicySelf(nil)
p, err := NewPolicyOperator(dir, e)
assert.NoError(t, err)

Expand All @@ -120,6 +125,7 @@ func TestPolicyOperator_Backup_Restore(t *testing.T) {
assert.NoError(t, err)
defer os.RemoveAll(dir)

e.EXPECT().ClearPolicySelf(nil)
p, err := NewPolicyOperator(dir, e)
assert.NoError(t, err)

Expand Down
3 changes: 3 additions & 0 deletions store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ func TestStore_SingleNode(t *testing.T) {

enforcer := mocks.NewMockIDistributedEnforcer(ctl)
raftID := "node-leader"
enforcer.EXPECT().ClearPolicySelf(nil)

raftAddress := GetLocalIP() + ":6790"

Expand Down Expand Up @@ -253,11 +254,13 @@ func TestStore_MultipleNode(t *testing.T) {
leaderCtl := gomock.NewController(t)
defer leaderCtl.Finish()
leaderEnforcer := mocks.NewMockIDistributedEnforcer(leaderCtl)
leaderEnforcer.EXPECT().ClearPolicySelf(nil)

// mock follower
followerCtl := gomock.NewController(t)
defer followerCtl.Finish()
followerEnforcer := mocks.NewMockIDistributedEnforcer(followerCtl)
followerEnforcer.EXPECT().ClearPolicySelf(nil)

localIP := GetLocalIP()
leaderAddress := localIP + ":6790"
Expand Down

0 comments on commit 9df953d

Please sign in to comment.