8 "git.curoverse.com/arvados.git/sdk/go/arvados"
12 maxPermCacheAge = time.Hour
13 minPermCacheAge = 5 * time.Minute
16 type permChecker interface {
17 SetToken(token string)
18 Check(uuid string) (bool, error)
21 func newPermChecker(ac arvados.Client) permChecker {
23 return &cachingPermChecker{
25 cache: make(map[string]cacheEnt),
30 type cacheEnt struct {
35 type cachingPermChecker struct {
37 cache map[string]cacheEnt
45 func (pc *cachingPermChecker) SetToken(token string) {
46 if pc.Client.AuthToken == token {
49 pc.Client.AuthToken = token
50 pc.cache = make(map[string]cacheEnt)
53 func (pc *cachingPermChecker) Check(uuid string) (bool, error) {
55 logger := logger(nil).
56 WithField("token", pc.Client.AuthToken).
57 WithField("uuid", uuid)
60 if perm, ok := pc.cache[uuid]; ok && now.Sub(perm.Time) < maxPermCacheAge {
61 logger.WithField("allowed", perm.allowed).Debug("cache hit")
62 return perm.allowed, nil
64 var buf map[string]interface{}
65 path, err := pc.PathForUUID("get", uuid)
72 err = pc.RequestAndDecode(&buf, "GET", path, nil, url.Values{
73 "select": {`["uuid"]`},
79 } else if txErr, ok := err.(*arvados.TransactionError); ok && pc.isNotAllowed(txErr.StatusCode) {
82 logger.WithError(err).Error("lookup error")
85 logger.WithField("allowed", allowed).Debug("cache miss")
86 pc.cache[uuid] = cacheEnt{Time: now, allowed: allowed}
90 func (pc *cachingPermChecker) isNotAllowed(status int) bool {
92 case http.StatusForbidden, http.StatusUnauthorized, http.StatusNotFound:
99 func (pc *cachingPermChecker) tidy() {
100 if len(pc.cache) <= pc.maxCurrent*2 {
103 tooOld := time.Now().Add(-minPermCacheAge)
104 for uuid, t := range pc.cache {
105 if t.Before(tooOld) {
106 delete(pc.cache, uuid)
109 pc.maxCurrent = len(pc.cache)