@cap-js/audit-logging 0.9.0 → 1.0.1

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,6 +4,19 @@ 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.1 - 2025-08-05
8
+
9
+ ### Fixed
10
+
11
+ - `audit-log-to-alsng`: EventDataPayload to Support Multi-Key Object and DataSubject IDs
12
+
13
+ ## Version 1.0.0 - 2025-07-11
14
+
15
+ ### Added
16
+
17
+ - Beta support for next generation SAP Audit Log Service
18
+ - Use explicit kind `audit-log-to-alsng` or alpha auto-detect kind `audit-log-to-als`
19
+
7
20
  ## Version 0.9.0 - 2025-06-05
8
21
 
9
22
  ### Added
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cap-js/audit-logging",
3
- "version": "0.9.0",
3
+ "version": "1.0.1",
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)",
@@ -53,11 +53,17 @@
53
53
  "audit-log-to-console": {
54
54
  "impl": "@cap-js/audit-logging/srv/log2console"
55
55
  },
56
+ "audit-log-to-als": {
57
+ "impl": "@cap-js/audit-logging/srv/log2als"
58
+ },
56
59
  "audit-log-to-restv2": {
57
60
  "impl": "@cap-js/audit-logging/srv/log2restv2",
58
61
  "vcap": {
59
62
  "label": "auditlog"
60
63
  }
64
+ },
65
+ "audit-log-to-alsng": {
66
+ "impl": "@cap-js/audit-logging/srv/log2alsng"
61
67
  }
62
68
  }
63
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,189 @@
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
+ flattenAndSortIdObject(id) {
36
+ if (!id || !Object.keys(id).length) return 'not provided'
37
+
38
+ let s = ''
39
+ for (const k of Object.keys(id).sort()) s += `${k}:${id[k]} `
40
+ return s.trim()
41
+ }
42
+
43
+ eventDataPayload(event, data) {
44
+ const object = data['object'] || { type: 'not provided', id: { ID: 'not provided' } }
45
+ const channel = data['channel'] || { type: 'not specified', id: 'not specified' }
46
+ const subject = data['data_subject'] || { type: 'not provided', id: { ID: 'not provided' } }
47
+ const attributes = data['attributes'] || [{ name: 'not provided', old: 'not provided', new: 'not provided' }]
48
+ const objectId = this.flattenAndSortIdObject(object['id'])
49
+ const oldValue = attributes[0]['old'] ?? ''
50
+ const newValue = attributes[0]['new'] ?? ''
51
+ const dataSubjectId = this.flattenAndSortIdObject(subject['id'])
52
+ return {
53
+ dppDataModification: {
54
+ objectType: object['type'],
55
+ objectId: objectId,
56
+ attribute: attributes[0]['name'],
57
+ oldValue: oldValue,
58
+ newValue: newValue,
59
+ dataSubjectType: subject['type'],
60
+ dataSubjectId: dataSubjectId
61
+ },
62
+ dppDataAccess: {
63
+ channelType: channel['type'],
64
+ channelId: channel['id'],
65
+ dataSubjectType: subject['type'],
66
+ dataSubjectId: dataSubjectId,
67
+ objectType: object['type'],
68
+ objectId: objectId,
69
+ attribute: attributes[0]['name']
70
+ },
71
+ configurationChange: {
72
+ propertyName: attributes[0]['name'],
73
+ oldValue: oldValue,
74
+ newValue: newValue,
75
+ objectType: object['type'],
76
+ objectId: objectId
77
+ },
78
+ legacySecurityWrapper: {
79
+ origEvent: JSON.stringify({
80
+ ...data,
81
+ data:
82
+ typeof data.data === 'object' && data.data !== null && !Array.isArray(data.data)
83
+ ? JSON.stringify(data.data)
84
+ : data.data
85
+ })
86
+ }
87
+ }[event]
88
+ }
89
+
90
+ eventPayload(event, data) {
91
+ const tenant = cds.context?.tenant || null
92
+ const timestamp = new Date().toISOString()
93
+
94
+ const eventData = {
95
+ id: cds.utils.uuid(),
96
+ specversion: 1,
97
+ source: `/${this._userProvided.credentials?.region}/${this._userProvided.credentials?.namespace}/${tenant}`,
98
+ type: event,
99
+ time: timestamp,
100
+ data: {
101
+ metadata: {
102
+ ts: timestamp,
103
+ appId: this._vcapApplication.application_id || 'default app',
104
+ infrastructure: {
105
+ other: {
106
+ runtimeType: 'Node.js'
107
+ }
108
+ },
109
+ platform: {
110
+ other: {
111
+ platformName: 'CAP'
112
+ }
113
+ }
114
+ },
115
+ data: {
116
+ [event]: this.eventDataPayload(event, data)
117
+ }
118
+ }
119
+ }
120
+
121
+ return eventData
122
+ }
123
+
124
+ formatEventData(event, data) {
125
+ if (event === 'legacySecurityWrapper') {
126
+ return JSON.stringify([this.eventPayload(event, data)])
127
+ }
128
+
129
+ const eventData = data['attributes']?.map(attr => {
130
+ return this.eventPayload(event, {
131
+ ...data,
132
+ attributes: [attr]
133
+ })
134
+ })
135
+
136
+ return JSON.stringify(eventData || [])
137
+ }
138
+
139
+ logEvent(event, data) {
140
+ const passphrase = this._userProvided.credentials?.keyPassphrase
141
+ const url = new URL(`${this._userProvided.credentials?.url}/ingestion/v1/events`)
142
+ const eventData = this.formatEventData(event, data)
143
+
144
+ const options = {
145
+ method: 'POST',
146
+ headers: {
147
+ 'Content-Type': 'application/json',
148
+ 'Content-Length': Buffer.byteLength(eventData)
149
+ },
150
+ key: this._userProvided.credentials?.key,
151
+ cert: this._userProvided.credentials?.cert,
152
+ ...(passphrase !== undefined && { passphrase })
153
+ }
154
+
155
+ return new Promise((resolve, reject) => {
156
+ const req = https.request(url, options, res => {
157
+ LOG.trace('🛰️ Status Code:', res.statusCode)
158
+
159
+ const chunks = []
160
+ res.on('data', chunk => chunks.push(chunk))
161
+
162
+ res.on('end', () => {
163
+ const { statusCode, statusMessage } = res
164
+ let body = Buffer.concat(chunks).toString()
165
+ if (res.headers['content-type']?.match(/json/)) body = JSON.parse(body)
166
+ if (res.statusCode >= 400) {
167
+ // prettier-ignore
168
+ const err = new Error(`Request failed with${statusMessage ? `: ${statusCode} - ${statusMessage}` : ` status ${statusCode}`}`)
169
+ err.request = { method: options.method, url, headers: options.headers, body: data }
170
+ if (err.request.headers.authorization)
171
+ err.request.headers.authorization = err.request.headers.authorization.split(' ')[0] + ' ***'
172
+ err.response = { statusCode, statusMessage, headers: res.headers, body }
173
+ reject(err)
174
+ } else {
175
+ resolve(body)
176
+ }
177
+ })
178
+ })
179
+
180
+ req.on('error', e => {
181
+ reject(e.message)
182
+ LOG.trace(`Problem with request: ${e.message}`)
183
+ })
184
+
185
+ req.write(eventData)
186
+ req.end()
187
+ })
188
+ }
189
+ }