ory / ladon

A SDK for access control policies: authorization for the microservice and IoT age. Inspired by AWS IAM policies. Written for Go.

Home Page:https://www.ory.sh/?utm_source=github&utm_medium=banner&utm_campaign=ladon

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

MemoryManager.GetAll() return random policies

datastream opened this issue · comments

If I call memory.GetAll(10,0), every time it return different data.

func (m *MemoryManager) GetAll(limit, offset int64) (Policies, error) {

so I think this function should be sort first.

        var keys []string
        m. RLock()  
        start, end := pagination.Index(int(limit), int(offset), len(m.Policies))
	for key, p := range m.Policies {
                keys = append(keys, key)
	}
        sort.Ints(keys)
        var rst []Policies
         for _, key := range keys[start:end] {
                  rst = append(rst, m.Policies[key])
        }
        m. RUnlock()
	return rst, nil

It would probably be better to change the internal storage of the policies from a map to a slice, which would save us some time sorting and appending here. As a side note, var keys []string is not initialized so append will allocate by power of 2 which takes a lot of time in pressure environments. You can save a lot of unnecessary allocation with keys := make([]string, len(m.Policies))

Feel free to PR according to my feedback!