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

feat: allow to pass falsy tenant in oder to log to provider account in multi-tenant scenarios #96

Merged
merged 3 commits into from
May 23, 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
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).
The format is based on [Keep a Changelog](http://keepachangelog.com/).

## Version 0.7.0 - tbd
## Version 0.8.0 - TBD

### Added

- Allow to specify undefined tenant in order to log to provider account in multi-tenant scenarios

## Version 0.7.0 - 2024-05-15

### Added

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cap-js/audit-logging",
"version": "0.7.0",
"version": "0.8.0",
"description": "CDS plugin providing integration to the SAP Audit Log service as well as out-of-the-box personal data-related audit logging based on annotations.",
"repository": "cap-js/audit-logging",
"author": "SAP SE (https://www.sap.com)",
Expand Down
1 change: 1 addition & 0 deletions srv/log2restv2.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ module.exports = class AuditLog2RESTv2 extends AuditLogService {
} else {
url = this.options.credentials.url + PATHS.OAUTH2[path]
data.tenant ??= this._provider //> if request has no tenant, stay in provider account
if (data.tenant === '$PROVIDER') data.tenant = this._provider
headers.authorization = 'Bearer ' + (await this._getToken(data.tenant))
data.tenant = data.tenant === this._provider ? '$PROVIDER' : '$SUBSCRIBER'
}
Expand Down
3 changes: 2 additions & 1 deletion srv/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ module.exports = class AuditLogService extends Base {
this.before('*', req => {
const { tenant, user, timestamp } = cds.context
req.data.uuid ??= cds.utils.uuid()
req.data.tenant ??= tenant
// allows to specify undefined tenant in order to log to provider in multi-tenant scenarios
if (!('tenant' in req.data)) req.data.tenant = tenant
req.data.user ??= user.id
req.data.time ??= timestamp
})
Expand Down
12 changes: 12 additions & 0 deletions test/api/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,17 @@ describe('AuditLogService API', () => {
time
})
})

test('tenant can be undefined', async () => {
await cds.tx({ tenant: 'bar' }, async () => {
const audit = await cds.connect.to('audit-log')
await audit.log('foo', { uuid: 'baz', tenant: undefined, user: 'baz' })
})
expect(_logs).toContainMatchObject({
uuid: 'baz',
tenant: undefined,
user: 'baz'
})
})
})
})
8 changes: 8 additions & 0 deletions test/integration/oauth2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,12 @@ describe('Log to Audit Log Service with oauth2 plan', () => {
expect(res).toMatchObject({ status: 204 })
expect(log.output.match(/\$PROVIDER/)).toBeTruthy()
})

// NOTE: unoffcial feature
test('tenant $PROVIDER is handled correctly', async () => {
const data = JSON.stringify({ data: { foo: 'bar' }, tenant: '$PROVIDER' })
const res = await POST('/integration/passthrough', { event: 'SecurityEvent', data })
expect(res).toMatchObject({ status: 204 })
expect(log.output.match(/\$PROVIDER/)).toBeTruthy()
})
})
8 changes: 8 additions & 0 deletions test/integration/premium.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,12 @@ describe('Log to Audit Log Service with premium plan', () => {
expect(res).toMatchObject({ status: 204 })
expect(log.output.match(/\$PROVIDER/)).toBeTruthy()
})

// NOTE: unoffcial feature
test('tenant $PROVIDER is handled correctly', async () => {
const data = JSON.stringify({ data: { foo: 'bar' }, tenant: '$PROVIDER' })
const res = await POST('/integration/passthrough', { event: 'SecurityEvent', data })
expect(res).toMatchObject({ status: 204 })
expect(log.output.match(/\$PROVIDER/)).toBeTruthy()
})
})
Loading