@cap-js/audit-logging 0.8.3 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -4,8 +4,23 @@ All notable changes to this project will be documented in this file.
4
4
  This project adheres to [Semantic Versioning](http://semver.org/).
5
5
  The format is based on [Keep a Changelog](http://keepachangelog.com/).
6
6
 
7
+ ## Version 1.0.0 - 2025-07-11
8
+
9
+ ### Added
10
+
11
+ - Beta support for next generation SAP Audit Log Service
12
+ - Use explicit kind `audit-log-to-alsng` or alpha auto-detect kind `audit-log-to-als`
13
+
14
+ ## Version 0.9.0 - 2025-06-05
15
+
16
+ ### Added
17
+
18
+ - Support for `@sap/cds^9`
19
+
7
20
  ## Version 0.8.3 - 2025-04-09
8
21
 
22
+ ### Fixed
23
+
9
24
  - Preperation for `@sap/cds^9`
10
25
 
11
26
  ## Version 0.8.2 - 2024-11-27
package/lib/utils.js CHANGED
@@ -123,7 +123,8 @@ const _buildSubSelect = (model, { entity, relative, element, next }, row, previo
123
123
  const targetAlias = _alias(element._target)
124
124
  const relativeAlias = _alias(relative)
125
125
 
126
- if (!('_relations' in relative)) {
126
+ // REVISIT: there seems to be a caching issue in cds^9 when elemets are renamed
127
+ if (!('_relations' in relative) || !relative._relations[element.name]) {
127
128
  const newRelation = Relation.to(relative)
128
129
  relative._relations = new Proxy(exposeRelation(newRelation), relationHandler(newRelation))
129
130
  }
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@cap-js/audit-logging",
3
- "version": "0.8.3",
3
+ "version": "1.0.0",
4
4
  "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.",
5
5
  "repository": "cap-js/audit-logging",
6
6
  "author": "SAP SE (https://www.sap.com)",
7
7
  "homepage": "https://cap.cloud.sap/",
8
- "license": "SEE LICENSE IN LICENSE",
8
+ "license": "Apache-2.0",
9
9
  "main": "cds-plugin.js",
10
10
  "files": [
11
11
  "lib",
@@ -14,21 +14,19 @@
14
14
  ],
15
15
  "scripts": {
16
16
  "lint": "npx eslint .",
17
- "test": "npm run test-new-db && npm run test-old-db",
18
- "test-new-db": "CDS_ENV='better-sqlite' npx jest --silent",
19
- "test-old-db": "CDS_ENV='legacy-sqlite' npx jest --silent"
17
+ "test": "npx jest --silent"
20
18
  },
21
19
  "peerDependencies": {
22
- "@sap/cds": ">=7"
20
+ "@sap/cds": ">=8"
23
21
  },
24
22
  "devDependencies": {
25
23
  "@cap-js/audit-logging": "file:.",
26
- "@cap-js/sqlite": "^1",
24
+ "@cap-js/cds-test": ">=0",
25
+ "@cap-js/sqlite": ">=1",
27
26
  "axios": "^1",
28
27
  "eslint": "^9",
29
28
  "express": "^4",
30
- "jest": "^29",
31
- "sqlite3": "^5.1.6"
29
+ "jest": "^29"
32
30
  },
33
31
  "cds": {
34
32
  "requires": {
@@ -55,11 +53,17 @@
55
53
  "audit-log-to-console": {
56
54
  "impl": "@cap-js/audit-logging/srv/log2console"
57
55
  },
56
+ "audit-log-to-als": {
57
+ "impl": "@cap-js/audit-logging/srv/log2als"
58
+ },
58
59
  "audit-log-to-restv2": {
59
60
  "impl": "@cap-js/audit-logging/srv/log2restv2",
60
61
  "vcap": {
61
62
  "label": "auditlog"
62
63
  }
64
+ },
65
+ "audit-log-to-alsng": {
66
+ "impl": "@cap-js/audit-logging/srv/log2alsng"
63
67
  }
64
68
  }
65
69
  }
package/srv/log2als.js ADDED
@@ -0,0 +1,4 @@
1
+ const credentials = JSON.parse(process.env.VCAP_SERVICES) || {}
2
+ const isV3 = credentials['user-provided']?.some(obj => obj.tags.includes('auditlog-ng'))
3
+
4
+ module.exports = isV3 ? require('./log2alsng') : require('./log2restv2')
@@ -0,0 +1,178 @@
1
+ const cds = require('@sap/cds')
2
+
3
+ const LOG = cds.log('audit-log')
4
+
5
+ const https = require('https')
6
+
7
+ const AuditLogService = require('./service')
8
+
9
+ module.exports = class AuditLog2ALSNG extends AuditLogService {
10
+ constructor() {
11
+ super()
12
+ this._vcap = JSON.parse(process.env.VCAP_SERVICES || '{}')
13
+ this._userProvided = this._vcap['user-provided']?.find(obj => obj.tags.includes('auditlog-ng')) || {}
14
+ if (!this._userProvided.credentials) throw new Error('No credentials found for SAP Audit Log Service NG')
15
+ this._vcapApplication = this._vcap['VCAP_APPLICATION'] || {}
16
+ }
17
+
18
+ async init() {
19
+ this.on('*', function (req) {
20
+ const { event, data } = req
21
+ return this.eventMapper(event, data)
22
+ })
23
+ await super.init()
24
+ }
25
+
26
+ eventMapper(event, data) {
27
+ return {
28
+ PersonalDataModified: () => this.logEvent('dppDataModification', data),
29
+ SensitiveDataRead: () => this.logEvent('dppDataAccess', data),
30
+ ConfigurationModified: () => this.logEvent('configurationChange', data),
31
+ SecurityEvent: () => this.logEvent('legacySecurityWrapper', data)
32
+ }[event]()
33
+ }
34
+
35
+ eventDataPayload(event, data) {
36
+ const object = data['object'] || { type: 'not provided', id: { ID: 'not provided' } }
37
+ const channel = data['channel'] || { type: 'not specified', id: 'not specified' }
38
+ const subject = data['data_subject'] || { type: 'not provided', id: { ID: 'not provided' } }
39
+ const attributes = data['attributes'] || [{ name: 'not provided', old: 'not provided', new: 'not provided' }]
40
+ const objectId = object['id']?.['ID'] || 'not provided'
41
+ const oldValue = attributes[0]['old'] ?? ''
42
+ const newValue = attributes[0]['new'] ?? ''
43
+ const dataSubjectId = subject['id']?.['ID'] || 'not provided'
44
+ return {
45
+ dppDataModification: {
46
+ objectType: object['type'],
47
+ objectId: objectId,
48
+ attribute: attributes[0]['name'],
49
+ oldValue: oldValue,
50
+ newValue: newValue,
51
+ dataSubjectType: subject['type'],
52
+ dataSubjectId: dataSubjectId
53
+ },
54
+ dppDataAccess: {
55
+ channelType: channel['type'],
56
+ channelId: channel['id'],
57
+ dataSubjectType: subject['type'],
58
+ dataSubjectId: dataSubjectId,
59
+ objectType: object['type'],
60
+ objectId: objectId,
61
+ attribute: attributes[0]['name']
62
+ },
63
+ configurationChange: {
64
+ propertyName: attributes[0]['name'],
65
+ oldValue: oldValue,
66
+ newValue: newValue,
67
+ objectType: object['type'],
68
+ objectId: objectId
69
+ },
70
+ legacySecurityWrapper: {
71
+ origEvent: JSON.stringify({
72
+ ...data,
73
+ data:
74
+ typeof data.data === 'object' && data.data !== null && !Array.isArray(data.data)
75
+ ? JSON.stringify(data.data)
76
+ : data.data
77
+ })
78
+ }
79
+ }[event]
80
+ }
81
+
82
+ eventPayload(event, data) {
83
+ const tenant = cds.context?.tenant || null
84
+ const timestamp = new Date().toISOString()
85
+
86
+ const eventData = {
87
+ id: cds.utils.uuid(),
88
+ specversion: 1,
89
+ source: `/${this._userProvided.credentials?.region}/${this._userProvided.credentials?.namespace}/${tenant}`,
90
+ type: event,
91
+ time: timestamp,
92
+ data: {
93
+ metadata: {
94
+ ts: timestamp,
95
+ appId: this._vcapApplication.application_id || 'default app',
96
+ infrastructure: {
97
+ other: {
98
+ runtimeType: 'Node.js'
99
+ }
100
+ },
101
+ platform: {
102
+ other: {
103
+ platformName: 'CAP'
104
+ }
105
+ }
106
+ },
107
+ data: {
108
+ [event]: this.eventDataPayload(event, data)
109
+ }
110
+ }
111
+ }
112
+
113
+ return eventData
114
+ }
115
+
116
+ logEvent(event, data) {
117
+ const passphrase = this._userProvided.credentials?.keyPassphrase
118
+ const url = new URL(`${this._userProvided.credentials?.url}/ingestion/v1/events`)
119
+ let eventData = []
120
+
121
+ if (event === 'legacySecurityWrapper') {
122
+ eventData = JSON.stringify([this.eventPayload(event, data)])
123
+ } else {
124
+ eventData = data['attributes'].map(attr => {
125
+ return this.eventPayload(event, {
126
+ ...data,
127
+ attributes: [attr]
128
+ })
129
+ })
130
+ eventData = JSON.stringify(eventData)
131
+ }
132
+
133
+ const options = {
134
+ method: 'POST',
135
+ headers: {
136
+ 'Content-Type': 'application/json',
137
+ 'Content-Length': Buffer.byteLength(eventData)
138
+ },
139
+ key: this._userProvided.credentials?.key,
140
+ cert: this._userProvided.credentials?.cert,
141
+ ...(passphrase !== undefined && { passphrase })
142
+ }
143
+
144
+ return new Promise((resolve, reject) => {
145
+ const req = https.request(url, options, res => {
146
+ LOG.trace('🛰️ Status Code:', res.statusCode)
147
+
148
+ const chunks = []
149
+ res.on('data', chunk => chunks.push(chunk))
150
+
151
+ res.on('end', () => {
152
+ const { statusCode, statusMessage } = res
153
+ let body = Buffer.concat(chunks).toString()
154
+ if (res.headers['content-type']?.match(/json/)) body = JSON.parse(body)
155
+ if (res.statusCode >= 400) {
156
+ // prettier-ignore
157
+ const err = new Error(`Request failed with${statusMessage ? `: ${statusCode} - ${statusMessage}` : ` status ${statusCode}`}`)
158
+ err.request = { method: options.method, url, headers: options.headers, body: data }
159
+ if (err.request.headers.authorization)
160
+ err.request.headers.authorization = err.request.headers.authorization.split(' ')[0] + ' ***'
161
+ err.response = { statusCode, statusMessage, headers: res.headers, body }
162
+ reject(err)
163
+ } else {
164
+ resolve(body)
165
+ }
166
+ })
167
+ })
168
+
169
+ req.on('error', e => {
170
+ reject(e.message)
171
+ LOG.trace(`Problem with request: ${e.message}`)
172
+ })
173
+
174
+ req.write(eventData)
175
+ req.end()
176
+ })
177
+ }
178
+ }
package/srv/service.js CHANGED
@@ -1,8 +1,6 @@
1
1
  const cds = require('@sap/cds')
2
2
 
3
- const Base = cds.outboxed ? cds.Service : require('@sap/cds/libx/_runtime/messaging/Outbox')
4
-
5
- module.exports = class AuditLogService extends Base {
3
+ module.exports = class AuditLogService extends cds.Service {
6
4
  async init() {
7
5
  // add common audit log entry fields
8
6
  this.before('*', req => {