- Notifications
You must be signed in to change notification settings - Fork913
chore: ensure proper rbac permissions on 'Acquire' file in the cache#18348
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
496a096
b909207
bcfd754
f4f849b
12f91e2
3ddc7a9
2015837
4256a6c
c9cf780
9a29e58
bc25afa
038bb57
3b73371
fb4b02e
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -234,6 +234,10 @@ func (r *RecordingAuthorizer) AssertOutOfOrder(t *testing.T, actor rbac.Subject, | ||
// AssertActor asserts in order. If the order of authz calls does not match, | ||
// this will fail. | ||
func (r *RecordingAuthorizer) AssertActor(t *testing.T, actor rbac.Subject, did ...ActionObjectPair) { | ||
r.AssertActorID(t, actor.ID, did...) | ||
} | ||
aslilac marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
func (r *RecordingAuthorizer) AssertActorID(t *testing.T, id string, did ...ActionObjectPair) { | ||
r.Lock() | ||
defer r.Unlock() | ||
ptr := 0 | ||
@@ -242,7 +246,7 @@ func (r *RecordingAuthorizer) AssertActor(t *testing.T, actor rbac.Subject, did | ||
// Finished all assertions | ||
return | ||
} | ||
if call.Actor.ID ==id { | ||
action, object := did[ptr].Action, did[ptr].Object | ||
assert.Equalf(t, action, call.Action, "assert action %d", ptr) | ||
assert.Equalf(t, object, call.Object, "assert object %d", ptr) | ||
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -13,33 +13,41 @@ import ( | ||||||||||
archivefs "github.com/coder/coder/v2/archive/fs" | ||||||||||
"github.com/coder/coder/v2/coderd/database" | ||||||||||
"github.com/coder/coder/v2/coderd/database/dbauthz" | ||||||||||
"github.com/coder/coder/v2/coderd/rbac" | ||||||||||
"github.com/coder/coder/v2/coderd/rbac/policy" | ||||||||||
"github.com/coder/coder/v2/coderd/util/lazy" | ||||||||||
) | ||||||||||
// NewFromStore returns a file cache that will fetch files from the provided | ||||||||||
// database. | ||||||||||
func NewFromStore(store database.Store, registerer prometheus.Registerer, authz rbac.Authorizer) *Cache { | ||||||||||
fetch := func(ctx context.Context, fileID uuid.UUID) (CacheEntryValue, error) { | ||||||||||
// Make sure the read does not fail due to authorization issues. | ||||||||||
// Authz is checked on the Acquire call, so this is safe. | ||||||||||
//nolint:gocritic | ||||||||||
file, err := store.GetFileByID(dbauthz.AsFileReader(ctx), fileID) | ||||||||||
if err != nil { | ||||||||||
returnCacheEntryValue{}, xerrors.Errorf("failed to read file from database: %w", err) | ||||||||||
} | ||||||||||
content := bytes.NewBuffer(file.Data) | ||||||||||
return CacheEntryValue{ | ||||||||||
Object: file.RBACObject(), | ||||||||||
FS: archivefs.FromTarReader(content), | ||||||||||
Size: int64(content.Len()), | ||||||||||
}, nil | ||||||||||
} | ||||||||||
return New(fetch, registerer, authz) | ||||||||||
} | ||||||||||
func New(fetch fetcher, registerer prometheus.Registerer, authz rbac.Authorizer) *Cache { | ||||||||||
return (&Cache{ | ||||||||||
lock: sync.Mutex{}, | ||||||||||
data: make(map[uuid.UUID]*cacheEntry), | ||||||||||
fetcher: fetch, | ||||||||||
authz: authz, | ||||||||||
}).registerMetrics(registerer) | ||||||||||
} | ||||||||||
@@ -101,6 +109,7 @@ type Cache struct { | ||||||||||
lock sync.Mutex | ||||||||||
data map[uuid.UUID]*cacheEntry | ||||||||||
fetcher | ||||||||||
authz rbac.Authorizer | ||||||||||
// metrics | ||||||||||
cacheMetrics | ||||||||||
@@ -117,18 +126,19 @@ type cacheMetrics struct { | ||||||||||
totalCacheSize prometheus.Counter | ||||||||||
} | ||||||||||
type CacheEntryValue struct { | ||||||||||
Object rbac.Object | ||||||||||
fs.FS | ||||||||||
Comment on lines +130 to 131 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Suggested change
nit | ||||||||||
Size int64 | ||||||||||
} | ||||||||||
type cacheEntry struct { | ||||||||||
// refCount must only be accessed while the Cache lock is held. | ||||||||||
refCount int | ||||||||||
value *lazy.ValueWithError[CacheEntryValue] | ||||||||||
} | ||||||||||
type fetcher func(context.Context, uuid.UUID) (CacheEntryValue, error) | ||||||||||
// Acquire will load the fs.FS for the given file. It guarantees that parallel | ||||||||||
// calls for the same fileID will only result in one fetch, and that parallel | ||||||||||
@@ -146,22 +156,33 @@ func (c *Cache) Acquire(ctx context.Context, fileID uuid.UUID) (fs.FS, error) { | ||||||||||
c.Release(fileID) | ||||||||||
return nil, err | ||||||||||
} | ||||||||||
subject, ok := dbauthz.ActorFromContext(ctx) | ||||||||||
if !ok { | ||||||||||
return nil, dbauthz.ErrNoActor | ||||||||||
} | ||||||||||
// Always check the caller can actually read the file. | ||||||||||
if err := c.authz.Authorize(ctx, subject, policy.ActionRead, it.Object); err != nil { | ||||||||||
c.Release(fileID) | ||||||||||
return nil, err | ||||||||||
} | ||||||||||
return it.FS, err | ||||||||||
} | ||||||||||
func (c *Cache) prepare(ctx context.Context, fileID uuid.UUID) *lazy.ValueWithError[CacheEntryValue] { | ||||||||||
c.lock.Lock() | ||||||||||
defer c.lock.Unlock() | ||||||||||
entry, ok := c.data[fileID] | ||||||||||
if !ok { | ||||||||||
value := lazy.NewWithError(func() (CacheEntryValue, error) { | ||||||||||
val, err := c.fetcher(ctx, fileID) | ||||||||||
// Always add to the cache size the bytes of the file loaded. | ||||||||||
if err == nil { | ||||||||||
c.currentCacheSize.Add(float64(val.Size)) | ||||||||||
c.totalCacheSize.Add(float64(val.Size)) | ||||||||||
} | ||||||||||
return val, err | ||||||||||
@@ -206,7 +227,7 @@ func (c *Cache) Release(fileID uuid.UUID) { | ||||||||||
ev, err := entry.value.Load() | ||||||||||
if err == nil { | ||||||||||
c.currentCacheSize.Add(-1 * float64(ev.Size)) | ||||||||||
} | ||||||||||
delete(c.data, fileID) | ||||||||||
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.