Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: rls check on move #400

Merged
merged 1 commit into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions src/storage/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,16 +340,8 @@ export class ObjectStorage {
return Promise.all([
db.findObject(this.bucketId, sourceObjectName, 'id'),
db.updateObject(this.bucketId, sourceObjectName, {
name: sourceObjectName,
version: '1',
owner,
}),
// We also check if we can create the destination object
// before starting the move
db.asSuperUser().createObject({
name: destinationObjectName,
version: newVersion,
bucket_id: this.bucketId,
owner,
}),
])
Expand All @@ -371,15 +363,15 @@ export class ObjectStorage {
const metadata = await this.backend.headObject(globalS3Bucket, s3DestinationKey, newVersion)

await this.db.asSuperUser().withTransaction(async (db) => {
await db.createObject({
await db.findObject(this.bucketId, sourceObjectName, 'id', { forUpdate: true })

await db.updateObject(this.bucketId, sourceObjectName, {
name: destinationObjectName,
version: newVersion,
bucket_id: this.bucketId,
owner: sourceObj.owner,
metadata,
})

await db.deleteObject(this.bucketId, sourceObjectName, sourceObj.version)
await ObjectAdminDelete.send({
name: sourceObjectName,
bucketId: this.bucketId,
Expand Down
71 changes: 39 additions & 32 deletions src/test/rls.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,44 +223,51 @@ describe('RLS policies', () => {
`Running operation ${assert.operation} with role ${assert.role || 'authenticated'}`
)

const response = await runOperation(assert.operation, {
bucket: bucketName,
objectName: objectName,
jwt: assert.role === 'service' ? serviceKey : jwt,
})

console.log(
`Operation ${assert.operation} with role ${assert.role || 'authenticated'} returned ${
response.statusCode
}`
)

await Promise.all([
...localPolicies.map((policies) => {
return Promise.all(
policies.map(async (policy) => {
await db.raw(`DROP POLICY IF EXISTS "${policy.name}" ON ${policy.table};`)
})
)
}),
])

bucketName = originalBucketName

try {
expect(response.statusCode).toBe(assert.status)
} catch (e) {
console.log(`Operation ${assert.operation} failed`)
throw e
}
const response = await runOperation(assert.operation, {
bucket: bucketName,
objectName: objectName,
jwt: assert.role === 'service' ? serviceKey : jwt,
})

if (assert.error) {
const body = await response.json()
console.log(
`Operation ${assert.operation} with role ${assert.role || 'authenticated'} returned ${
response.statusCode
}`
)

expect(body.message).toBe(assert.error)
bucketName = originalBucketName

try {
expect(response.statusCode).toBe(assert.status)
} catch (e) {
console.log(`Operation ${assert.operation} failed`, response.body)
throw e
}

if (assert.error) {
const body = await response.json()

expect(body.message).toBe(assert.error)
}
} finally {
console.log('deleting local policies')
await Promise.all([
...localPolicies.map((policies) => {
return Promise.all(
policies.map(async (policy) => {
console.log(
`RUNNING QUERY DROP POLICY IF EXISTS "${policy.name}" ON ${policy.table};`
)
await db.raw(`DROP POLICY "${policy.name}" ON ${policy.table};`)
})
)
}),
])
}
}
} catch (e) {
console.error('error', e)
throw e
} finally {
await sleep(2000)
Expand Down
Loading