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 97c66ed
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
34 changes: 18 additions & 16 deletions store/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type PolicyOperator struct {
db *bolt.DB
l *sync.Mutex
logger *zap.Logger
path string
}

// NewPolicyOperator returns a PolicyOperator.
Expand All @@ -40,13 +41,23 @@ func NewPolicyOperator(path string, e casbin.IDistributedEnforcer) (*PolicyOpera
enforcer: e,
l: &sync.Mutex{},
logger: zap.NewExample(),
path: path,
}
dbPath := filepath.Join(path, databaseFilename)
err := p.init()
return p, err
}

func (p *PolicyOperator) init() error {
dbPath := filepath.Join(p.path, databaseFilename)
if err := p.openDBFile(dbPath); err != nil {
return nil, errors.Wrapf(err, "failed to open bolt file")
return errors.Wrapf(err, "failed to open bolt file")
}

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

// openDBFile opens the bolt file.
Expand Down Expand Up @@ -101,13 +112,7 @@ func (p *PolicyOperator) Restore(rc io.ReadCloser) error {
return err
}

err = p.openDBFile(dbPath)
if err != nil {
p.logger.Error("failed to open the database file", zap.Error(err))
return err
}

return nil
return p.init()
}

// Backup writes the database to bytes with gzip.
Expand Down Expand Up @@ -147,11 +152,8 @@ func (p *PolicyOperator) createBucket(name []byte) error {
})
}

// LoadPolicy clears the policies held by enforcer, and loads policy from database.
func (p *PolicyOperator) LoadPolicy() error {
p.l.Lock()
defer p.l.Unlock()

// loadPolicy clears the policies held by enforcer, and loads policy from database.
func (p *PolicyOperator) loadPolicy() error {
err := p.enforcer.ClearPolicySelf(nil)
if err != nil {
p.logger.Error("failed to call loadPolicy", zap.Error(err))
Expand All @@ -173,7 +175,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
13 changes: 8 additions & 5 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 @@ -106,7 +111,7 @@ func TestPolicyOperator_LoadPolicy(t *testing.T) {
e.EXPECT().ClearPolicySelf(nil)
e.EXPECT().AddPoliciesSelf(nil, "p", "p", [][]string{{"role:admin", "/", "*"}})
e.EXPECT().AddPoliciesSelf(nil, "p", "p", [][]string{{"role:user", "/", "GET"}})
err = p.LoadPolicy()
err = p.loadPolicy()
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 All @@ -131,12 +137,9 @@ func TestPolicyOperator_Backup_Restore(t *testing.T) {
err = ioutil.WriteFile(path.Join(dir, "backup.db"), b, 0666)
assert.NoError(t, err)

err = p.Restore(ioutil.NopCloser(bytes.NewBuffer(b)))
assert.NoError(t, err)

e.EXPECT().ClearPolicySelf(nil)
e.EXPECT().AddPoliciesSelf(nil, "p", "p", [][]string{{"role:admin", "/", "*"}})
e.EXPECT().AddPoliciesSelf(nil, "p", "p", [][]string{{"role:user", "/", "GET"}})
err = p.LoadPolicy()
err = p.Restore(ioutil.NopCloser(bytes.NewBuffer(b)))
assert.NoError(t, err)
}
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 97c66ed

Please sign in to comment.