-
Notifications
You must be signed in to change notification settings - Fork 0
/
quota_guard.go
44 lines (40 loc) · 1.08 KB
/
quota_guard.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
package handlers
import (
"fmt"
"policy-server/models"
"policy-server/uaa_client"
)
type QuotaGuard struct {
Store store
MaxPolicies int
}
func (g *QuotaGuard) CheckAccess(policies []models.Policy, userToken uaa_client.CheckTokenResponse) (bool, error) {
for _, scope := range userToken.Scope {
if scope == "network.admin" {
return true, nil
}
}
appGuids := uniqueAppGUIDs(policies)
toAddSourceCounts := sourceCounts(policies, appGuids)
sourcePolicies, err := g.Store.ByGuids(appGuids, []string{})
if err != nil {
return false, fmt.Errorf("getting policies: %s", err)
}
currentAppCounts := sourceCounts(sourcePolicies, appGuids)
for _, appGuid := range appGuids {
if currentAppCounts[appGuid]+toAddSourceCounts[appGuid] > g.MaxPolicies {
return false, nil
}
}
return true, nil
}
func sourceCounts(policies []models.Policy, knownAppGuids []string) map[string]int {
var set = make(map[string]int)
for _, appGuid := range knownAppGuids {
set[appGuid] = 0
}
for _, policy := range policies {
set[policy.Source.ID] = set[policy.Source.ID] + 1
}
return set
}